Przeglądaj źródła

iterate on current patch

jherve 1 miesiąc temu
rodzic
commit
83f36fe938
1 zmienionych plików z 25 dodań i 10 usunięć
  1. 25 10
      pc_backup/podman.py

+ 25 - 10
pc_backup/podman.py

@@ -93,20 +93,35 @@ class Podman:
     def ps(cls):
         out = subprocess.run(["podman", "ps"], capture_output=True, check=True)
 
+    @classmethod
+    def machine_is_running(cls) -> bool:
+        try:
+            cls.ps()
+        except subprocess.CalledProcessError as e:
+            if "Cannot connect to Podman" in e.stderr.decode():
+                return False
+
+        return True
+
+    @classmethod
+    def machine_start(cls):
+        cls._call(["machine", "start"])
+
     @classmethod
     def _call(cls, args: list, **kwargs):
         args = ["podman"] + args
         print(f"Executing `{" ".join(args)}`")
-        try:
+
+        def run_and_output():
             out = subprocess.check_output(args, **kwargs)
             print(out.decode())
+
+        try:
+            run_and_output()
         except subprocess.CalledProcessError as e:
-            print(e)
-            try:
-                cls.ps()
-            except subprocess.CalledProcessError as e2:
-                if "Cannot connect to Podman" in e2.stderr.decode():
-                    print("podman not running, starting it")
-                    subprocess.check_output(["podman", "machine", "start"])
-                    out = subprocess.check_output(args, **kwargs)
-                    print(out.decode())
+            if not cls.machine_is_running:
+                print("podman not running, starting it")
+                cls.machine_start()
+                run_and_output()
+            else:
+                raise e