Tim Andersson has proposed merging 
~andersson123/autopkgtest-cloud:hacks-to-deploy-storage 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/476792

Sometimes (lol) our /tmp storage is not mounted on our cloud/lxd worker units. 
This MP implements a workaround to this bug.
-- 
Your team Canonical's Ubuntu QA is requested to review the proposed merge of 
~andersson123/autopkgtest-cloud:hacks-to-deploy-storage into 
autopkgtest-cloud:master.
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 47fe98f..a172fbd 100644
--- a/charms/focal/autopkgtest-cloud-worker/reactive/autopkgtest_cloud_worker.py
+++ b/charms/focal/autopkgtest-cloud-worker/reactive/autopkgtest_cloud_worker.py
@@ -731,7 +731,7 @@ def reload_systemd_units():
 
 @hook("tmp-storage-attached")
 def fix_tmp_permissions():
-    status.maintenance("Fixing tmp permissions")
+    status.maintenance("Checking to see if storage is mounted...")
     storageids = storage_list("tmp")
     if not storageids:
         status.blocked("Cannot locate attached storage")
@@ -739,7 +739,70 @@ def fix_tmp_permissions():
     storageid = storageids[0]
 
     mount = storage_get("location", storageid)
+    agent_dirs = Path("/var/lib/juju/agents").glob("*")
+    storage_min_size = None
+    for agent in agent_dirs:
+        dir_name = str(agent).rsplit("/", maxsplit=1)[-1]
+        if dir_name.startswith("unit-autopkgtest"):
+            with open(agent / "charm" / "metadata.yaml") as file:
+                metadata_yaml = yaml.safe_load(file)
+                storage_min_size = int(
+                    metadata_yaml["storage"][mount.replace("/", "")][
+                        "minimum-size"
+                    ].replace("G", "")
+                )
+    block_device = None
+    try:
+        subprocess.check_call(["findmnt", "-T", mount], shell=True)
+        filesystems = json.loads(
+            subprocess.check_output(
+                f"findmnt -T {mount} --json", shell=True
+            ).decode("utf-8")
+        )["filesystems"]
+        assert len(filesystems) == 1
+        assert filesystems[0]["target"] == mount
+        status.maintenance(f"{mount} storage already mounted!")
+    except (subprocess.CalledProcessError, KeyError, AssertionError):
+        status.maintenance(
+            "tmp storage not mounted! Finding appropriate disk to mount..."
+        )
+        block_devices = json.loads(
+            subprocess.check_output("lsblk --json", shell=True).decode("utf-8")
+        )["blockdevices"]
+        for device in block_devices:
+            try:
+                for child in device["children"]:
+                    if (
+                        child["mountpoint"] is None
+                        and child["size"].endswith("G")
+                        and int(child["size"].replace("G", ""))
+                        >= storage_min_size
+                    ):
+                        block_device = f"/dev/{child['name']}"
+                        status.maintenance(
+                            f"Looks like {block_device} is unmounted, attempting to mount..."
+                        )
+                        try:
+                            subprocess.check_call(
+                                ["mount", block_device, mount]
+                            )
+                            status.maintenance(
+                                f"Mounted {block_device} to {mount}"
+                            )
+                        except subprocess.CalledProcessError as e:
+                            status.maintenance(
+                                f"Mounting storage failed with: {e}"
+                            )
+            except KeyError:
+                continue
+        fstab = Path("/etc/fstab")
+        fstab_entry = f"{block_device} {mount} ext4 nofail,relatime,rw 0 0"
+        status.maintenance(
+            f"Writing the following line to {str(fstab.absolute())}:\n{fstab_entry}"
+        )
+        fstab.write_text(f"{fstab.read_text()}\n{fstab_entry}\n")
 
+    status.maintenance("Fixing tmp permissions")
     os.chmod(mount, 0o777)
     status.maintenance("Done fixing tmp permissions")
 
-- 
Mailing list: https://launchpad.net/~canonical-ubuntu-qa
Post to     : canonical-ubuntu-qa@lists.launchpad.net
Unsubscribe : https://launchpad.net/~canonical-ubuntu-qa
More help   : https://help.launchpad.net/ListHelp

Reply via email to