← Back to team overview

canonical-ubuntu-qa team mailing list archive

[Merge] ~andersson123/autopkgtest-cloud:cleanup-ppa-containers-user-input into autopkgtest-cloud:master

 

Tim Andersson has proposed merging ~andersson123/autopkgtest-cloud:cleanup-ppa-containers-user-input into autopkgtest-cloud:master.

Requested reviews:
  Canonical's Ubuntu QA (canonical-ubuntu-qa)

For more details, see:
https://code.launchpad.net/~andersson123/autopkgtest-cloud/+git/autopkgtest-cloud/+merge/476329

Adds a basic TUI to cleanup-ppa-containers, as well as some minor idiot-proofing, and a bit more documentation in the script.
-- 
Your team Canonical's Ubuntu QA is requested to review the proposed merge of ~andersson123/autopkgtest-cloud:cleanup-ppa-containers-user-input into autopkgtest-cloud:master.
diff --git a/charms/focal/autopkgtest-cloud-worker/autopkgtest-cloud/tools/cleanup-ppa-containers b/charms/focal/autopkgtest-cloud-worker/autopkgtest-cloud/tools/cleanup-ppa-containers
index 16af81f..a66614d 100755
--- a/charms/focal/autopkgtest-cloud-worker/autopkgtest-cloud/tools/cleanup-ppa-containers
+++ b/charms/focal/autopkgtest-cloud-worker/autopkgtest-cloud/tools/cleanup-ppa-containers
@@ -40,9 +40,19 @@ def main():
     _, container_list = swift_con.get_account()
     current_time = datetime.datetime.now()
     upstream_regex = re.compile("^autopkgtest-[a-z]*-upstream-", re.DOTALL)
+
+    containers_to_empty = []
+
     for container in container_list:
-        if container in SKIP_THESE_CONTAINERS or upstream_regex.search(
-            container["name"]
+        likely_old_release_container = (
+            container["name"].split("-")[0] == "autopkgtest"
+            and len(container["name"].split("-")) == 2
+        )
+        if (
+            container["name"] in SKIP_THESE_CONTAINERS
+            or upstream_regex.search(container["name"])
+            or likely_old_release_container
+            or container["name"].startswith("private-")
         ):
             logging.info("skipping container %s", container["name"])
             continue
@@ -54,25 +64,116 @@ def main():
                 if (current_time - container_time) > datetime.timedelta(
                     weeks=PPA_RESULTS_RETENTION_TIME
                 ):
-                    logging.info(
-                        "%s is older than %i weeks, deleting.",
-                        container["name"],
-                        PPA_RESULTS_RETENTION_TIME,
-                    )
-                    _, objects = swift_con.get_container(container["name"])
-                    for obj in objects:
-                        swift_con.delete_object(container["name"], obj["name"])
-                    swift_con.delete_container(container["name"])
-                    logging.info(
-                        "All objects in container %s and container itself also deleted.",
-                        container["name"],
-                    )
-            else:
-                logging.info(
-                    "Container %s not older than %i weeks.",
-                    container["name"],
-                    PPA_RESULTS_RETENTION_TIME,
-                )
+                    containers_to_empty.append(container["name"])
+
+    user_input = None
+    while user_input != "done":
+        os.system("clear")
+        idx = 0
+        for cont in containers_to_empty:
+            print(f"{idx}: {cont}")
+            idx += 1
+        print(
+            "The above containers have been marked as potentially suitable to delete."
+        )
+        print(
+            "Please go through the list, and if there are any you'd like to leave untouched,"
+        )
+        print("type the index of that entry, and press enter.")
+        print("You can also do this in a comma separated manner, e.g.: 2,6,7")
+        print(
+            "Alternatively, you can remove all containers containing a string"
+        )
+        print("by typing 'string: $string'")
+        print("When you're done pruning the list, type 'done' and press enter")
+        print("to remove all remaining containers.")
+        user_input = input("What do you want to do:")
+        if user_input.lower() == "done":
+            os.system("clear")
+            idx = 0
+            for cont in containers_to_empty:
+                print(f"{idx}: {cont}")
+                idx += 1
+            print("Okay, let's just double check everything...")
+            user_input = input(
+                "Are you happy with the list of containers above being deleted? (y/n)"
+            )
+            if user_input.lower() == "y":
+                user_input = "done"
+            continue
+        indexes = []
+        if user_input.startswith("string: "):
+            del_word = user_input.replace("string: ", "")
+            idx = 0
+            for cont in containers_to_empty:
+                if del_word in cont:
+                    indexes.append(idx)
+                idx += 1
+        else:
+            user_input.replace(" ", "")
+            indexes = [int(x) for x in user_input.split("," "")]
+        indexes.sort()
+        aggregator = 0
+        for del_idx in indexes:
+            del containers_to_empty[del_idx - aggregator]
+            aggregator += 1
+
+    for delete_container in containers_to_empty:
+        logging.info("Deleting container %s...", delete_container)
+        _, objects = swift_con.get_container(delete_container)
+        for obj in objects:
+            swift_con.delete_object(delete_container, obj["name"])
+        swift_con.delete_container(delete_container)
+        logging.info(
+            "All objects in container %s and container itself also deleted.",
+            delete_container,
+        )
+
+    #########################################################
+    # below is OLD!
+    # for container in container_list:
+    #     if container["name"] in SKIP_THESE_CONTAINERS or upstream_regex.search(
+    #         container["name"]
+    #     ):
+    #         logging.info("skipping container %s", container["name"])
+    #         continue
+    #     for data in swift_con.get_container(container["name"]):
+    #         if isinstance(data, dict):
+    #             container_time = datetime.datetime.fromtimestamp(
+    #                 float(data["x-timestamp"])
+    #             )
+    #             if (current_time - container_time) > datetime.timedelta(
+    #                 weeks=PPA_RESULTS_RETENTION_TIME
+    #             ):
+    #                 user_input = input(
+    #                     f"Do you want to attempt cleanup of container {container['name']}? (y/n)"
+    #                 )
+    #                 if user_input.lower() == "y":
+    #                     logging.info(
+    #                         "%s is older than %i weeks, deleting.",
+    #                         container["name"],
+    #                         PPA_RESULTS_RETENTION_TIME,
+    #                     )
+    #                     _, objects = swift_con.get_container(container["name"])
+    #                     for obj in objects:
+    #                         swift_con.delete_object(
+    #                             container["name"], obj["name"]
+    #                         )
+    #                     swift_con.delete_container(container["name"])
+    #                     logging.info(
+    #                         "All objects in container %s and container itself also deleted.",
+    #                         container["name"],
+    #                     )
+    #                 else:
+    #                     logging.info(
+    #                         f"Skipping container {container['name']} based on user input."
+    #                     )
+    #         else:
+    #             logging.info(
+    #                 "Container %s not older than %i weeks.",
+    #                 container["name"],
+    #                 PPA_RESULTS_RETENTION_TIME,
+    #             )
 
 
 if __name__ == "__main__":