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