← Back to team overview

canonical-ubuntu-qa team mailing list archive

[Merge] ~andersson123/autopkgtest-cloud:backup-worker-logs into autopkgtest-cloud:master

 

Tim Andersson has proposed merging ~andersson123/autopkgtest-cloud:backup-worker-logs 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/456538
-- 
Your team Canonical's Ubuntu QA is requested to review the proposed merge of ~andersson123/autopkgtest-cloud:backup-worker-logs into autopkgtest-cloud:master.
diff --git a/charms/focal/autopkgtest-cloud-worker/autopkgtest-cloud/tools/store-worker-logs b/charms/focal/autopkgtest-cloud-worker/autopkgtest-cloud/tools/store-worker-logs
new file mode 100755
index 0000000..581cb0b
--- /dev/null
+++ b/charms/focal/autopkgtest-cloud-worker/autopkgtest-cloud/tools/store-worker-logs
@@ -0,0 +1,116 @@
+#!/usr/bin/python3
+
+
+import datetime
+import os
+import subprocess
+import tempfile
+
+import swiftclient
+
+JOURNAL_CMD = ["journalctl", "--since", "6 hours ago", "--no-pager"]
+CONTAINER_NAME = "worker-logs"
+LOGFILE_LIFESPAN = 2678400 * 3  # 3 months
+
+
+# must have openstack-creds as environment file - similar to worker
+# EnvironmentFile=/home/ubuntu/swift-password.cred as env file
+# twice a day
+# journalctl --since "6 hours ago" > pipe to file with correct timestamp etc
+# write to swift container - worker-logs
+# use object file path to make it easy to find, by date
+# e.g.
+# 2023/11/24/worker-mm-HH-YYYY-MM-DD.log.gz
+# make sure to zip up the file and set the content encoding
+# upload object to container
+# done
+# maybe add a secondary functionality which cleans up old logs?
+
+# container called what? "worker-logs"?
+
+
+def acquire_journal(jrnl_opts):
+    return (
+        subprocess.Popen(JOURNAL_CMD.extend(jrnl_opts), stdout=subprocess.PIPE)
+        .stdout.read()
+        .decode("utf-8")
+    )
+
+
+swift_creds = {
+    "authurl": os.environ["SWIFT_AUTH_URL"],
+    "user": os.environ["SWIFT_USERNAME"],
+    "key": os.environ["SWIFT_PASSWORD"],
+    "os_options": {
+        "region_name": os.environ["SWIFT_REGION"],
+        "project_domain_name": os.environ["SWIFT_PROJECT_DOMAIN_NAME"],
+        "project_name": os.environ["SWIFT_PROJECT_NAME"],
+        "user_domain_name": os.environ["SWIFT_USER_DOMAIN_NAME"],
+    },
+    "auth_version": "3.0",
+}
+
+swift_conn = swiftclient.Connection(**swift_creds)
+
+try:
+    swift_conn.put_container(CONTAINER_NAME)
+except Exception as _:
+    print("Container maybe already exists?")
+
+rc_files = os.listdir("/home/ubuntu/cloudrcs/")
+
+res = {}
+
+temp_dir = tempfile.TemporaryDirectory()
+
+for file in rc_files:
+    loc_arch = file.replace(".rc", "")
+    jrnl_args = "-u autopkgtest@" + loc_arch + "*"
+    journal = acquire_journal(jrnl_args.split(" "))
+    fp = os.path.join([temp_dir.name, loc_arch + ".log"])
+    with open(fp, "w") as logfile:
+        logfile.write(journal)
+    res[loc_arch] = fp
+
+curr_time = datetime.datetime.now()
+for loc_arch, log_fp in res.items():
+    # file path like:
+    # YYYYMMDD/loc/arch/HHMMSS/log.gz
+    object_path = os.path.join(
+        [
+            curr_time.strftime("%Y/%m/%d"),
+            loc_arch.replace("-", "/"),
+            curr_time.strftime("%H-%M"),
+            "log.gz",
+        ]
+    )
+    print(object_path)
+    _ = subprocess.check_call(["gzip", "-9", log_fp])
+    with open(res[loc_arch]) as fd:
+        content_type = "text/plain; charset=UTF-8"
+        headers = {"Content-Encoding": "gzip"}
+        swift_conn.put_object(
+            container=CONTAINER_NAME,
+            obj=object_path,
+            contents=fd,
+            content_type=content_type,
+            headers=headers,
+            content_length=os.path.getsize(res[loc_arch]),
+        )
+
+# remove old log files?
+# use X-Timestamp from this again:
+# swift stat autopkgtest-noble noble/amd64/d/debhelper/20231127_163649_9412e@/result.tar
+# ..............................................................
+#            X-Timestamp: 1701103011.40143
+# ..............................................................
+# but be SMART about it
+
+
+temp_dir.cleanup()
+
+swift_conn.close()
+
+
+# walk the cloudrcs directory
+# journal = acquire_journal()

References