#19 Add a script to clean up potential left over side-tags
Merged 4 years ago by asaleh. Opened 4 years ago by pingou.
fedora-ci/ pingou/monitor-gating clean_up_script  into  master

@@ -0,0 +1,79 @@ 

+ #!/usr/bin/python

+ 

+ import argparse

+ import logging

+ import subprocess

+ import sys

+ 

+ import toml

+ 

+ 

+ _log = logging.getLogger(__name__)

+ 

+ 

+ def get_cli_args(args):

+ 

+     parser = argparse.ArgumentParser(

+         prog="clean up side-tags", formatter_class=argparse.ArgumentDefaultsHelpFormatter

+     )

+ 

+     parser.add_argument(

+         "conf",

+         help="Configuration file used by the runner",

+     )

+ 

+     return parser.parse_args(args)

+ 

+ 

+ def run_command(command) -> bytes:

+     """ Run the specified command in a specific working directory if one

+     is specified.

+     """

+     output = None

+     try:

+         output = subprocess.check_output(command, stderr=subprocess.PIPE)

+     except subprocess.CalledProcessError as e:

+         _log.error("Command `{}` return code: `{}`".format(" ".join(command), e.returncode))

+         _log.error("stdout:\n-------\n{}".format(e.stdout))

+         _log.error("stderr:\n-------\n{}".format(e.stderr))

+         pass

+ 

+     return output

+ 

+ 

+ def main(args):

+ 

+     conf = toml.load(args.conf)

+     if conf.get("kb_principal") and conf.get("kb_keytab_file"):

+         print(f"Logging as {conf['kb_principal']} into kerberos using: {conf['kb_keytab_file']}")

+         cmd = ["kinit", conf["kb_principal"], "-kt", conf["kb_keytab_file"]]

+         run_command(cmd)

+ 

+     # list side-tags:

+     cmd = [

+         conf["fedpkg"], "list-side-tags", "--user", conf["kb_principal"].rsplit('@', 1)[0]

+     ]

+     output = run_command(cmd)

+ 

+     if not output:

+         print("No side-tags to clean found.")

+         return

+ 

+     output = output.decode("UTF-8").strip().split("\n")

+     # Remove all the side-tags but the last one (which is the latest one), which

+     # we keep in case there is a run ongoing

+     for line in output[:-1]:

+         side_tag = line.split('\t')[0]

+         print("Removing side-tag: %s" % side_tag)

+         cmd = [

+             conf["fedpkg"], "remove-side-tag", side_tag

+         ]

+         output = run_command(cmd)

+ 

+ 

+ 

+ if __name__ == "__main__":

+     """ Main method. """

+ 

+     args = get_cli_args(sys.argv[1:])

+     main(args)

@@ -218,6 +218,15 @@ 

                  conf.get("koji_hub"), nevr, expected_ends=conf["koji_end_tag"],

              )

  

+         try:

+             print("  Removing side-tag: %s" % side_tag_name)

+             cmd = [

+                 conf["fedpkg"], "remove-side-tag", side_tag_name

+             ]

+             output = run_command(cmd)

+         except Exception:

+             pass

+ 

      utils.finalize(start)

      return utils.logs

  

file modified
+2
@@ -18,3 +18,5 @@ 

  # project.

  # kb_principal = "packagerbot/os-master01.phx2.fedoraproject.org@FEDORAPROJECT.ORG"

  # kb_keytab_file = "/etc/keytabs/monitor-gating-keytab"

+ 

+ fedpkg = "fedpkg"

If a run of monitor-gating fails, there can be some side-tags left
over. While we need to fix monitor-gating to avoid them, this script
can also become handy to just clean up when needed.

Relates to #18

Signed-off-by: Pierre-Yves Chibon pingou@pingoured.fr

rebased onto 29dc98128c02cbc99bac8c9af798c2bec5a7e3b3

4 years ago

rebased onto 50ff0c3

4 years ago

Tried, read, looks good, merging.

Pull-Request has been merged by asaleh

4 years ago