| 1 | #!/usr/bin/python3 |
| 2 | # Copyright (C) 2019 - Jonathan Rajotte Julien <jonathan.rajotte-julien@efficios.com> |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | import argparse |
| 18 | import os |
| 19 | import sys |
| 20 | import time |
| 21 | import xmlrpc.client |
| 22 | |
| 23 | from jinja2 import Environment, FileSystemLoader |
| 24 | |
| 25 | USERNAME = "lava-jenkins" |
| 26 | HOSTNAME = "lava-master-03.internal.efficios.com" |
| 27 | DEFAULT_KERNEL_COMMIT = "a227f8436f2b21146fc024d84e6875907475ace2" |
| 28 | |
| 29 | |
| 30 | def wait_on(server, jobid): |
| 31 | """ |
| 32 | Wait for the completion of the job. |
| 33 | Do not care for result. This is mostly to prevent flooding of lava with |
| 34 | multiple jobs for the same commit hash. Jenkins is responsible for |
| 35 | running only one job for job submissions. |
| 36 | """ |
| 37 | # Check the status of the job every 30 seconds |
| 38 | jobstatus = server.scheduler.job_state(jobid)["job_state"] |
| 39 | running = False |
| 40 | while jobstatus in ["Submitted", "Scheduling", "Scheduled", "Running"]: |
| 41 | if not running and jobstatus == "Running": |
| 42 | print("Job started running", flush=True) |
| 43 | running = True |
| 44 | time.sleep(30) |
| 45 | try: |
| 46 | jobstatus = server.scheduler.job_state(jobid)["job_state"] |
| 47 | except xmlrpc.client.ProtocolError: |
| 48 | print("Protocol error, retrying", flush=True) |
| 49 | continue |
| 50 | print("Job ended with {} status.".format(jobstatus), flush=True) |
| 51 | |
| 52 | |
| 53 | def submit( |
| 54 | commit, debug=False, kernel_commit=DEFAULT_KERNEL_COMMIT, wait_for_completion=True |
| 55 | ): |
| 56 | nfsrootfs = "https://obj.internal.efficios.com/lava/rootfs/rootfs_amd64_xenial_2018-12-05.tar.gz" |
| 57 | nfsrootfs_sha256 = "0df15933ed18eb73ed5f0e7b1eca8d032ee88d92e5dbfc0f56dcc68c821048a8" |
| 58 | kernel_url = "https://obj.internal.efficios.com/lava/kernel/{}.baremetal.bzImage".format( |
| 59 | kernel_commit |
| 60 | ) |
| 61 | modules_url = "https://obj.internal.efficios.com/lava/modules/linux/{}.baremetal.linux.modules.tar.gz".format( |
| 62 | kernel_commit |
| 63 | ) |
| 64 | |
| 65 | lava_api_key = None |
| 66 | if not debug: |
| 67 | try: |
| 68 | lava_api_key = os.environ["LAVA2_JENKINS_TOKEN"] |
| 69 | except Exception as error: |
| 70 | print( |
| 71 | "LAVA2_JENKINS_TOKEN not found in the environment variable. Exiting...", |
| 72 | error, |
| 73 | ) |
| 74 | return -1 |
| 75 | |
| 76 | jinja_loader = FileSystemLoader(os.path.dirname(os.path.realpath(__file__))) |
| 77 | jinja_env = Environment(loader=jinja_loader, trim_blocks=True, lstrip_blocks=True) |
| 78 | jinja_template = jinja_env.get_template("template_lava_job_bt_benchmark.jinja2") |
| 79 | |
| 80 | context = dict() |
| 81 | context["kernel_url"] = kernel_url |
| 82 | context["nfsrootfs_url"] = nfsrootfs |
| 83 | context["nfsrootfs_sha256"] = nfsrootfs_sha256 |
| 84 | context["commit_hash"] = commit |
| 85 | |
| 86 | render = jinja_template.render(context) |
| 87 | |
| 88 | print("Job to be submitted:", flush=True) |
| 89 | |
| 90 | print(render, flush=True) |
| 91 | |
| 92 | if debug: |
| 93 | return 0 |
| 94 | |
| 95 | server = xmlrpc.client.ServerProxy( |
| 96 | "https://%s:%s@%s/RPC2" % (USERNAME, lava_api_key, HOSTNAME) |
| 97 | ) |
| 98 | |
| 99 | for attempt in range(10): |
| 100 | try: |
| 101 | jobid = server.scheduler.submit_job(render) |
| 102 | except xmlrpc.client.ProtocolError as error: |
| 103 | print( |
| 104 | "Protocol error on submit, sleeping and retrying. Attempt #{}".format( |
| 105 | attempt |
| 106 | ), |
| 107 | flush=True, |
| 108 | ) |
| 109 | time.sleep(5) |
| 110 | continue |
| 111 | else: |
| 112 | break |
| 113 | |
| 114 | print("Lava jobid:{}".format(jobid), flush=True) |
| 115 | print( |
| 116 | "Lava job URL: https://{}/scheduler/job/{}".format( |
| 117 | HOSTNAME, jobid |
| 118 | ), |
| 119 | flush=True, |
| 120 | ) |
| 121 | |
| 122 | if not wait_for_completion: |
| 123 | return 0 |
| 124 | |
| 125 | wait_on(server, jobid) |
| 126 | |
| 127 | |
| 128 | if __name__ == "__main__": |
| 129 | parser = argparse.ArgumentParser( |
| 130 | description="Launch baremetal babeltrace test using Lava" |
| 131 | ) |
| 132 | parser.add_argument("-c", "--commit", required=True) |
| 133 | parser.add_argument( |
| 134 | "-k", "--kernel-commit", required=False, default=DEFAULT_KERNEL_COMMIT |
| 135 | ) |
| 136 | parser.add_argument("-d", "--debug", required=False, action="store_true") |
| 137 | args = parser.parse_args() |
| 138 | sys.exit(submit(args.kernel_commit, args.commit, args.debug)) |