From 50ff0c3bc83b9e45e4f4aeca90af0f6c5f9e1196 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Apr 07 2020 08:21:23 +0000
Subject: [PATCH 1/2] Add a script to clean up potential left over side-tags


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>

---

diff --git a/clean_up_side_tags.py b/clean_up_side_tags.py
new file mode 100644
index 0000000..607ba4c
--- /dev/null
+++ b/clean_up_side_tags.py
@@ -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)
diff --git a/runner.cfg b/runner.cfg
index 0d660f6..1be6a44 100644
--- a/runner.cfg
+++ b/runner.cfg
@@ -18,3 +18,5 @@ workflow_multi_gating_args = "--conf monitor_gating_stg.cfg"
 # project.
 # kb_principal = "packagerbot/os-master01.phx2.fedoraproject.org@FEDORAPROJECT.ORG"
 # kb_keytab_file = "/etc/keytabs/monitor-gating-keytab"
+
+fedpkg = "fedpkg"

From 6d2792a6de92e81422064a037b500361a01b3bc6 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Apr 07 2020 08:21:33 +0000
Subject: [PATCH 2/2] Always try to clean up the side-tag at the end of the run


This should fail if the tests all passed and will prevent dangling
side-tags which are only consuming resources if the tests failed.

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

---

diff --git a/monitor_gating_multi_builds.py b/monitor_gating_multi_builds.py
index cef5dcd..c4b88ae 100644
--- a/monitor_gating_multi_builds.py
+++ b/monitor_gating_multi_builds.py
@@ -218,6 +218,15 @@ def main(args):
                 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
 

