From fceabb4fcfcca16b97db57c217eaca6619422dec Mon Sep 17 00:00:00 2001 From: Ralph Bean Date: Aug 04 2017 18:17:12 +0000 Subject: Add script to create new branches in PDC (with SLA/EOL info). Signed-off-by: Ralph Bean --- diff --git a/.gitignore b/.gitignore index f89c758..c658a4a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ docs/build/ pylint-log .vagrant tests/pylint/.pylint.d/ +*.pyc diff --git a/scripts/pdc/create-branch.py b/scripts/pdc/create-branch.py new file mode 100644 index 0000000..72b7eb7 --- /dev/null +++ b/scripts/pdc/create-branch.py @@ -0,0 +1,49 @@ +""" insert-sla.py - Add a new branch SLA to PDC. + +https://pdc.fedoraproject.org/rest_api/v1/component-sla-types/ + +Branches in dist-git may have arbitrary EOLs and SLAs, but the SLAs must be +chosen from a set specified by release-engineering. + +You can run this on pdc-backend01, the token is in /etc/pdc.d/ +You can also find the token in the private git repo. +""" + +import argparse + +try: + import utilities +except ImportError: + print("Try setting PYTHONPATH to find the utilities.py file.") + raise + +import pdc_client + +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument('servername', help='PDC server name. See /etc/pdc.d/') +parser.add_argument('token', help='PDC token for authentication.') +parser.add_argument('package', help='Name of the package') +parser.add_argument('type', help='Type of the package (rpm, module, container, ..)') +parser.add_argument('branch', help='Name of the branch (f26, or 1.12, or master)') +parser.add_argument('slas', help='Comma-separated list of SLAs.') +parser.add_argument('eol', help='End of life date for the SLAs, ' + 'in the format of "2020-01-01"') +parser.add_argument('-y', '--yes', dest='yes', action='store_true', + default=False, + help='Force "yes" to every question.') + +args = parser.parse_args() + + +if __name__ == '__main__': + print("Connecting to PDC args.server %r with token %r" % (args.servername, args.token)) + pdc = pdc_client.PDCClient(args.servername, token=args.token) + + slas = args.slas.split(',') + + utilities.verify_slas( + pdc, slas) + utilities.ensure_global_component( + pdc, args.package, args.yes) + utilities.ensure_component_branches( + pdc, args.package, slas, args.eol, args.branch, args.type, args.yes) diff --git a/scripts/pdc/utilities.py b/scripts/pdc/utilities.py new file mode 100644 index 0000000..af4298d --- /dev/null +++ b/scripts/pdc/utilities.py @@ -0,0 +1,62 @@ +""" Common PDC utility functions, shared by scripts. """ + +import sys + +def prompt(message, force): + return force or raw_input(message + " [y/N]: ").lower() in ('y', 'yes') + + +def die(message): + print("! " + message) + sys.exit(3) + + +def ensure_global_component(pdc, package, force): + """ Ensure that the given global component exists. """ + pdc['global-components']._(name=package) + results = list(pdc.get_paged(pdc['global-components'], name=package)) + if results: + print "Found global-components/%s" % package + return + message = "global-component %s does not exist. Create it?" % package + if not prompt(message, force): + print "Not creating global-component", package + sys.exit(1) + payload = dict(name=package) + pdc['global-components']._(payload) + + +def verify_slas(pdc, slas): + """ Verify that the given SLAs exist in PDC. """ + for sla in slas: + endpoint = pdc['component-sla-types']._ + results = list(pdc.get_paged(endpoint, name=sla)) + if not results: + die("%r is not a valid SLA in PDC. See %s" % (sla, endpoint)) + + +def ensure_component_branches(pdc, package, slas, eol, branch, type, force): + endpoint = pdc['component-branch-slas'] + # A base query + base = dict(branch=branch, global_component=package, branch_type=type) + for sla in slas: + # See if the sla already exists on the branch: + results = list(pdc.get_paged(endpoint, sla=sla, **base)) + if results: + print("sla %r already exists for %r" % (sla, base)) + continue + message = "Apply sla %r to %r" % (sla, base) + if not prompt(message, force): + print("Not applying sla %r to %r" % (sla, base)) + continue + print("Applying sla %r to %r" % (sla, base)) + payload = dict( + sla=sla, + eol=eol, + branch=dict( + name=branch, + global_component=package, + type=type, + ) + ) + endpoint._(payload)