← Back to team overview

canonical-ubuntu-qa team mailing list archive

[Merge] ~andersson123/autopkgtest-cloud:cleanup_old_ppa_containers into autopkgtest-cloud:master

 

Tim Andersson has proposed merging ~andersson123/autopkgtest-cloud:cleanup_old_ppa_containers 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/448353
-- 
Your team Canonical's Ubuntu QA is requested to review the proposed merge of ~andersson123/autopkgtest-cloud:cleanup_old_ppa_containers 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
new file mode 100644
index 0000000..53018f8
--- /dev/null
+++ b/charms/focal/autopkgtest-cloud-worker/autopkgtest-cloud/tools/cleanup_ppa_containers
@@ -0,0 +1,44 @@
+#!/usr/bin/python3
+
+import swiftclient
+import json
+import os
+import datetime
+import logging
+
+
+logging.basicConfig(encoding='utf-8', level=logging.DEBUG)
+
+# 180 days in seconds
+MAX_SECONDS = 15552000
+
+
+def connect_swift():
+    return swiftclient.Connection(
+        authurl=os.environ["OS_AUTH_URL"],
+        user=os.environ["OS_USERNAME"],
+        key=os.environ["OS_PASSWORD"],
+        tenant_name=os.environ["OS_TENANT_NAME"],
+        os_options={"region_name": os.environ["OS_REGION_NAME"]},
+        auth_version="2.0",
+        retries=10,
+        starting_backoff=10,
+    )
+
+
+if __name__ == "__main__":
+    con = connect_swift()
+    _, container_list = con.get_account()
+    for container in container_list:
+        if "ppa" in container["name"]:
+            for data in con.get_container(container["name"]):
+                if isinstance(data, dict):
+                    container_time = float(data["x-timestamp"])
+                    current_time = datetime.datetime.now().timestamp()
+                    if (current_time - container_time) > MAX_SECONDS:
+                        logging.info("%s is older than 180 days, deleting." % container["name"])
+                        hdrs, objects = con.get_container(container["name"])
+                        for obj in objects:
+                            con.delete_object(container["name"], obj["name"])
+                        con.delete_container(container["name"])
+                        logging.info("All objects in container %s and container itself also deleted." % container["name"])
diff --git a/charms/focal/autopkgtest-cloud-worker/reactive/autopkgtest_cloud_worker.py b/charms/focal/autopkgtest-cloud-worker/reactive/autopkgtest_cloud_worker.py
index 2095e42..f447982 100644
--- a/charms/focal/autopkgtest-cloud-worker/reactive/autopkgtest_cloud_worker.py
+++ b/charms/focal/autopkgtest-cloud-worker/reactive/autopkgtest_cloud_worker.py
@@ -125,6 +125,7 @@ def clone_autopkgtest():
     "autopkgtest.influx-creds-written",
 )
 def set_up_systemd_units():
+    lxdremotes = config().get("lxd-remotes") or ""
     for unit in glob.glob(os.path.join(charm_dir(), "units", "*")):
         base = os.path.basename(unit)
         dest = os.path.join(os.path.sep, "etc", "systemd", "system", base)
@@ -133,7 +134,10 @@ def set_up_systemd_units():
             # pylint: disable=cell-var-from-loop
             os.symlink(unit, dest)
             if "@" not in base:
-                subprocess.check_call(["systemctl", "enable", base])
+                if "cleanup_ppa_containers" in base and lxdremotes != "":
+                    subprocess.check_call(["systemctl", "enable", base])
+                else:
+                    subprocess.check_call(["systemctl", "enable", base])
 
         try:
             link_and_enable()
diff --git a/charms/focal/autopkgtest-cloud-worker/units/swift_cleanup_ppa_containers.service b/charms/focal/autopkgtest-cloud-worker/units/swift_cleanup_ppa_containers.service
new file mode 100644
index 0000000..00441ea
--- /dev/null
+++ b/charms/focal/autopkgtest-cloud-worker/units/swift_cleanup_ppa_containers.service
@@ -0,0 +1,9 @@
+[Unit]
+Description=Deletes ppa containers from swift database that are older than 180 days
+
+[Service]
+User=ubuntu
+Group=Ubuntu
+Type=oneshot
+EnvironmentFile=/home/ubuntu/.novarc
+ExecStart=/home/ubuntu/autopkgtest-cloud/charms/focal/autopkgtest-cloud-worker/tools/cleanup_ppa_containers
diff --git a/charms/focal/autopkgtest-cloud-worker/units/swift_cleanup_ppa_containers.timer b/charms/focal/autopkgtest-cloud-worker/units/swift_cleanup_ppa_containers.timer
new file mode 100644
index 0000000..4d80b59
--- /dev/null
+++ b/charms/focal/autopkgtest-cloud-worker/units/swift_cleanup_ppa_containers.timer
@@ -0,0 +1,9 @@
+[Unit]
+Description=Deletes ppa containers from swift database that are older than 180 days (timer)
+
+[Timer]
+OnBootSec=15min
+OnUnitActiveSec=1d
+
+[Install]
+WantedBy=autopkgtest.target