github.com/containers/podman/v5@v5.1.0-rc1/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 self.anchor_directory = tempfile.mkdtemp(prefix="podman_restapi_") 24 self.cmd.append("--root=" + os.path.join(self.anchor_directory, "crio")) 25 self.cmd.append("--runroot=" + os.path.join(self.anchor_directory, "crio-run")) 26 27 os.environ["CONTAINERS_REGISTRIES_CONF"] = os.path.join( 28 self.anchor_directory, "registry.conf" 29 ) 30 p = configparser.ConfigParser() 31 p.read_dict( 32 { 33 "registries.search": {"registries": "['quay.io']"}, 34 "registries.insecure": {"registries": "[]"}, 35 "registries.block": {"registries": "[]"}, 36 } 37 ) 38 with open(os.environ["CONTAINERS_REGISTRIES_CONF"], "w") as w: 39 p.write(w) 40 41 os.environ["CNI_CONFIG_PATH"] = os.path.join(self.anchor_directory, "cni", "net.d") 42 os.makedirs(os.environ["CNI_CONFIG_PATH"], exist_ok=True) 43 self.cmd.append("--network-config-dir=" + os.environ["CNI_CONFIG_PATH"]) 44 cni_cfg = os.path.join(os.environ["CNI_CONFIG_PATH"], "87-podman-bridge.conflist") 45 # json decoded and encoded to ensure legal json 46 buf = json.loads( 47 """ 48 { 49 "cniVersion": "0.3.0", 50 "name": "podman", 51 "plugins": [{ 52 "type": "bridge", 53 "bridge": "cni0", 54 "isGateway": true, 55 "ipMasq": true, 56 "ipam": { 57 "type": "host-local", 58 "subnet": "10.88.0.0/16", 59 "routes": [{ 60 "dst": "0.0.0.0/0" 61 }] 62 } 63 }, 64 { 65 "type": "portmap", 66 "capabilities": { 67 "portMappings": true 68 } 69 } 70 ] 71 } 72 """ 73 ) 74 with open(cni_cfg, "w") as w: 75 json.dump(buf, w) 76 77 def open(self, command, *args, **kwargs): 78 """Podman initialized instance to run a given command 79 80 :param self: Podman instance 81 :param command: podman sub-command to run 82 :param args: arguments and options for command 83 :param kwargs: See subprocess.Popen() for shell keyword 84 :return: subprocess.Popen() instance configured to run podman instance 85 """ 86 cmd = self.cmd.copy() 87 cmd.append(command) 88 cmd.extend(args) 89 90 shell = kwargs.get("shell", False) 91 92 return subprocess.Popen( 93 cmd, 94 shell=shell, 95 stdin=subprocess.DEVNULL, 96 stdout=subprocess.DEVNULL, 97 stderr=subprocess.DEVNULL, 98 ) 99 100 def run(self, command, *args, **kwargs): 101 """Run given podman command 102 103 :param self: Podman instance 104 :param command: podman sub-command to run 105 :param args: arguments and options for command 106 :param kwargs: See subprocess.Popen() for shell and check keywords 107 :return: subprocess.Popen() instance configured to run podman instance 108 """ 109 cmd = self.cmd.copy() 110 cmd.append(command) 111 cmd.extend(args) 112 113 check = kwargs.get("check", False) 114 shell = kwargs.get("shell", False) 115 116 try: 117 return subprocess.run( 118 cmd, 119 shell=shell, 120 check=check, 121 stdout=subprocess.PIPE, 122 stderr=subprocess.PIPE, 123 ) 124 except subprocess.CalledProcessError as e: 125 if e.stdout: 126 sys.stdout.write("\nRun Stdout:\n" + e.stdout.decode("utf-8")) 127 if e.stderr: 128 sys.stderr.write("\nRun Stderr:\n" + e.stderr.decode("utf-8")) 129 raise 130 131 def tear_down(self): 132 shutil.rmtree(self.anchor_directory, ignore_errors=True)