소스 검색

Launch podman machine on windows if not running

jherve 1 개월 전
부모
커밋
b75731bc84
1개의 변경된 파일33개의 추가작업 그리고 0개의 파일을 삭제
  1. 33 0
      pc_backup/podman.py

+ 33 - 0
pc_backup/podman.py

@@ -88,3 +88,36 @@ class Podman:
         print(f"Executing `{" ".join(args)}`")
         out = subprocess.check_output(args, **kwargs)
         print(out.decode())
+
+    @classmethod
+    def machine_is_running(cls) -> bool:
+        try:
+            subprocess.run(["podman", "ps"], capture_output=True, check=True)
+        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)}`")
+
+        def run_and_output():
+            out = subprocess.check_output(args, **kwargs)
+            print(out.decode())
+
+        try:
+            run_and_output()
+        except subprocess.CalledProcessError as e:
+            if not cls.machine_is_running():
+                print("podman not running, starting it")
+                cls.machine_start()
+                run_and_output()
+            else:
+                raise e