github.com/AbhinandanKurakure/podman/v3@v3.4.10/test/apiv2/python/rest_api/fixtures/podman.py (about) 1 import configparser 2 import json 3 import os 4 import shutil 5 import subprocess 6 import sys 7 import tempfile 8 9 10 class Podman: 11 """ 12 Instances hold the configuration and setup for running podman commands 13 """ 14 15 def __init__(self): 16 """Initialize a Podman instance with global options""" 17 binary = os.getenv("PODMAN", "bin/podman") 18 self.cmd = [binary, "--storage-driver=vfs"] 19 20 cgroupfs = os.getenv("CGROUP_MANAGER", "systemd") 21 self.cmd.append(f"--cgroup-manager={cgroupfs}") 22 23 if os.getenv("DEBUG"): 24 self.cmd.append("--log-level=debug") 25 self.cmd.append("--syslog=true") 26 27 self.anchor_directory = tempfile.mkdtemp(prefix="podman_restapi_") 28 self.cmd.append("--root=" + os.path.join(self.anchor_directory, "crio")) 29 self.cmd.append("--runroot=" + os.path.join(self.anchor_directory, "crio-run")) 30 31 os.environ["CONTAINERS_REGISTRIES_CONF"] = os.path.join( 32 self.anchor_directory, "registry.conf" 33 ) 34 p = configparser.ConfigParser() 35 p.read_dict( 36 { 37 "registries.search": {"registries": "['quay.io']"}, 38 "registries.insecure": {"registries": "[]"}, 39 "registries.block": {"registries": "[]"}, 40 } 41 ) 42 with open(os.environ["CONTAINERS_REGISTRIES_CONF"], "w") as w: 43 p.write(w) 44 45 os.environ["CNI_CONFIG_PATH"] = os.path.join(self.anchor_directory, "cni", "net.d") 46 os.makedirs(os.environ["CNI_CONFIG_PATH"], exist_ok=True) 47 self.cmd.append("--cni-config-dir=" + os.environ["CNI_CONFIG_PATH"]) 48 cni_cfg = os.path.join(os.environ["CNI_CONFIG_PATH"], "87-podman-bridge.conflist") 49 # json decoded and encoded to ensure legal json 50 buf = json.loads( 51 """ 52 { 53 "cniVersion": "0.3.0", 54 "name": "podman", 55 "plugins": [{ 56 "type": "bridge", 57 "bridge": "cni0", 58 "isGateway": true, 59 "ipMasq": true, 60 "ipam": { 61 "type": "host-local", 62 "subnet": "10.88.0.0/16", 63 "routes": [{ 64 "dst": "0.0.0.0/0" 65 }] 66 } 67 }, 68 { 69 "type": "portmap", 70 "capabilities": { 71 "portMappings": true 72 } 73 } 74 ] 75 } 76 """ 77 ) 78 with open(cni_cfg, "w") as w: 79 json.dump(buf, w) 80 81 def open(self, command, *args, **kwargs): 82 """Podman initialized instance to run a given command 83 84 :param self: Podman instance 85 :param command: podman sub-command to run 86 :param args: arguments and options for command 87 :param kwargs: See subprocess.Popen() for shell keyword 88 :return: subprocess.Popen() instance configured to run podman instance 89 """ 90 cmd = self.cmd.copy() 91 cmd.append(command) 92 cmd.extend(args) 93 94 shell = kwargs.get("shell", False) 95 96 return subprocess.Popen( 97 cmd, 98 shell=shell, 99 stdin=subprocess.DEVNULL, 100 stdout=subprocess.DEVNULL, 101 stderr=subprocess.DEVNULL, 102 ) 103 104 def run(self, command, *args, **kwargs): 105 """Run given podman command 106 107 :param self: Podman instance 108 :param command: podman sub-command to run 109 :param args: arguments and options for command 110 :param kwargs: See subprocess.Popen() for shell and check keywords 111 :return: subprocess.Popen() instance configured to run podman instance 112 """ 113 cmd = self.cmd.copy() 114 cmd.append(command) 115 cmd.extend(args) 116 117 check = kwargs.get("check", False) 118 shell = kwargs.get("shell", False) 119 120 try: 121 return subprocess.run( 122 cmd, 123 shell=shell, 124 check=check, 125 stdout=subprocess.PIPE, 126 stderr=subprocess.PIPE, 127 ) 128 except subprocess.CalledProcessError as e: 129 if e.stdout: 130 sys.stdout.write("\nRun Stdout:\n" + e.stdout.decode("utf-8")) 131 if e.stderr: 132 sys.stderr.write("\nRun Stderr:\n" + e.stderr.decode("utf-8")) 133 raise 134 135 def tear_down(self): 136 shutil.rmtree(self.anchor_directory, ignore_errors=True)