diff --git a/contrib/mbs-build b/contrib/mbs-build index 14e55c3f..bffecf44 100755 --- a/contrib/mbs-build +++ b/contrib/mbs-build @@ -119,6 +119,7 @@ def watch_build(server, build_id): if not done: time.sleep(30) + # Ideally we would use oidc.send_request here, but it doesn't support # custom HTTP verbs/methods like "PATCH". It sends just "POST"... # TODO: Remove this method once python-openidc-client with verb support @@ -168,6 +169,7 @@ def _send_oidc_request(oidc, verb, *args, **kwargs): else: return resp + def send_authorized_request(verb, server, id_provider, url, body, **kwargs): """ Sends authorized request to server. @@ -193,6 +195,7 @@ def send_authorized_request(verb, server, id_provider, url, body, **kwargs): scopes=scopes, **kwargs) return resp + def get_scm_url(scm_url, pyrpkg, local=False): """ If `scm_url` it not set, returns the scm_url based on git repository @@ -203,7 +206,7 @@ def get_scm_url(scm_url, pyrpkg, local=False): return scm_url logging.info("You have not provided SCM URL or branch. Trying to get " - "it from current working directory") + "it from current working directory") if local: # Just get the local URL from the current working directory. @@ -212,16 +215,17 @@ def get_scm_url(scm_url, pyrpkg, local=False): else: # Get the url using pyrpkg implementation. process = subprocess.Popen([pyrpkg, 'giturl'], stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + stderr=subprocess.PIPE) out, err = process.communicate() if process.returncode != 0 and len(err) != 0: logging.error("Cannot get the giturl from current " - "working directory using the %s", pyrpkg) + "working directory using the %s", pyrpkg) logging.error(err) return None - scm_url = out[:-1] # remove new-line + scm_url = out[:-1] # remove new-line return scm_url + def get_scm_branch(branch): """ If `branch` it not set, returns the branch name based on git repository @@ -231,39 +235,46 @@ def get_scm_branch(branch): return branch process = subprocess.Popen(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], - stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = process.communicate() if process.returncode != 0 and len(err) != 0: logging.error("Cannot get the branch name from current " - "working directory.") + "working directory.") logging.error(err) return None - branch = out[:-1] # remove new-line + branch = out[:-1] # remove new-line return branch + def submit_module_build(scm_url, branch, server, id_provider, pyrpkg, verify=True, optional=None): """ Submits the module defined by `scm_url` to MBS instance defined - by `server`. Returns build_id or negative error code. + by `server`. Returns tuple: build_id or negative error code, error message. """ scm_url = get_scm_url(scm_url, pyrpkg) branch = get_scm_branch(branch) if not scm_url or not branch: - return -2 - + return -2, None logging.info("Submitting module build %s", scm_url) body = {'scmurl': scm_url, 'branch': branch} - body.update({x.split("=")[0]: x.split("=")[1] for x in optional}) + try: + optional_dict = {y[0]: y[1] for y in [x.split("=", 1) for x in optional]} + except IndexError: + return -5, "Optional arguments are not in a proper arg=value format." + body.update(optional_dict) resp = send_authorized_request( "POST", server, id_provider, "/module-build-service/1/module-builds/", body, verify=verify) logging.info(resp.text) data = resp.json() - if 'id' in data: - return data['id'] - return -3 + if 'error' in data: + return -4, "%s %s: %s" % (data['status'], data['error'], data['message']) + elif 'id' in data: + return data['id'], None + return -3, None + def do_local_build(scm_url, branch, skiptests, log_flag=None): """ @@ -276,7 +287,6 @@ def do_local_build(scm_url, branch, skiptests, log_flag=None): if not scm_url or not branch: return None - logging.info("Starting local build of %s, branch %s", scm_url, branch) command = ['mbs-manager'] if log_flag: @@ -291,6 +301,7 @@ def do_local_build(scm_url, branch, skiptests, log_flag=None): process.communicate() return process.returncode + def cancel_module_build(server, id_provider, build_id, verify=True): """ Cancels the module build. @@ -302,6 +313,7 @@ def cancel_module_build(server, id_provider, build_id, verify=True): {'state': 'failed'}, verify=verify) logging.info(resp.text) + def show_overview(server, finished, limit=30): if not server: server = DEFAULT_MBS_SERVER @@ -371,6 +383,7 @@ def show_overview(server, finished, limit=30): print(tabulate(table, headers=headers)) + def main(): # Parse command line arguments parser = argparse.ArgumentParser(description="Submits and manages module builds.") @@ -433,7 +446,7 @@ def main(): parser_local.add_argument("scm_url", nargs='?') parser_local.add_argument("branch", nargs='?') parser_local.add_argument('--skiptests', dest='skiptests', action='store_true', - help="add macro for skipping check section/phase") + help="add macro for skipping check section/phase") parser_overview = subparsers.add_parser( 'overview', help="show overview of module builds", @@ -464,15 +477,17 @@ def main(): if args.cmd_name == "submit": # Submit the module build. - build_id = submit_module_build(args.scm_url, args.branch, args.server, - args.idprovider, args.pyrpkg_client, args.verify, args.optional) + build_id, errmsg = submit_module_build(args.scm_url, args.branch, args.server, + args.idprovider, args.pyrpkg_client, args.verify, args.optional) if build_id < 0: + if errmsg: + logging.critical(errmsg) sys.exit(build_id) if args.watch: watch_build(args.server, build_id) else: - logging.info("Submitted module build %r" % build_id) + print("Submitted module build %r" % build_id) elif args.cmd_name == "local": sys.exit(do_local_build(args.scm_url, args.branch, args.skiptests, log_flag))