2022-12-05 15:10:50 +01:00
|
|
|
import subprocess
|
|
|
|
|
2022-12-09 14:26:35 +01:00
|
|
|
from steps.common import debug
|
|
|
|
|
2022-12-05 15:10:50 +01:00
|
|
|
|
|
|
|
class Podman:
|
2022-12-09 14:26:35 +01:00
|
|
|
def __init__(self, context):
|
|
|
|
self.context = context
|
|
|
|
debug(context, "Podman.__init__()")
|
2022-12-05 15:10:50 +01:00
|
|
|
self.container_id = None
|
2022-12-09 15:09:44 +01:00
|
|
|
self.start()
|
2022-12-05 15:10:50 +01:00
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
try:
|
|
|
|
self.kill()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _run(self, args, check=True):
|
|
|
|
cmd = ["podman"] + args
|
2022-12-09 14:26:35 +01:00
|
|
|
debug(self.context, "Running command:", cmd)
|
2022-12-05 15:10:50 +01:00
|
|
|
proc = subprocess.run(
|
|
|
|
cmd,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
encoding="utf-8",
|
|
|
|
check=check,
|
|
|
|
)
|
2022-12-09 14:26:35 +01:00
|
|
|
debug(self.context, "> return code:", proc.returncode)
|
|
|
|
debug(self.context, "> stdout:", proc.stdout)
|
|
|
|
debug(self.context, "> stderr:", proc.stderr)
|
2022-12-05 15:10:50 +01:00
|
|
|
return proc
|
|
|
|
|
2022-12-09 15:09:44 +01:00
|
|
|
def start(self):
|
|
|
|
debug(self.context, "Podman.start()")
|
2022-12-05 15:10:50 +01:00
|
|
|
args = [
|
|
|
|
"run",
|
|
|
|
"--name", "obs-server-behave",
|
|
|
|
"--hostname", "obs-server-behave",
|
|
|
|
"--replace",
|
|
|
|
"--rm",
|
|
|
|
"--detach",
|
|
|
|
"--interactive",
|
|
|
|
"--tty",
|
|
|
|
"-p", "443",
|
|
|
|
"obs-server"
|
|
|
|
]
|
|
|
|
proc = self._run(args)
|
|
|
|
lines = proc.stdout.strip().splitlines()
|
|
|
|
self.container_id = lines[-1]
|
2022-12-09 15:09:44 +01:00
|
|
|
self.wait_on_systemd()
|
|
|
|
self.port = self.get_port()
|
2022-12-05 15:10:50 +01:00
|
|
|
|
|
|
|
def kill(self):
|
|
|
|
if not self.container_id:
|
|
|
|
return
|
2022-12-09 14:26:35 +01:00
|
|
|
debug(self.context, "Podman.kill()")
|
2022-12-05 15:10:50 +01:00
|
|
|
args = ["kill", self.container_id]
|
|
|
|
self._run(args)
|
|
|
|
self.container_id = None
|
|
|
|
|
2022-12-09 15:09:44 +01:00
|
|
|
def restart(self):
|
|
|
|
debug(self.context, "Podman.restart()")
|
|
|
|
self.kill()
|
|
|
|
self.start()
|
|
|
|
|
2022-12-05 15:10:50 +01:00
|
|
|
def wait_on_systemd(self):
|
|
|
|
args = [
|
|
|
|
"exec",
|
|
|
|
self.container_id,
|
|
|
|
"/usr/bin/systemctl", "is-system-running", "--wait"
|
|
|
|
]
|
|
|
|
self._run(args, check=False)
|
|
|
|
|
|
|
|
def get_port(self):
|
|
|
|
args = ["port", self.container_id]
|
|
|
|
proc = self._run(args)
|
|
|
|
lines = proc.stdout.strip().splitlines()
|
|
|
|
for line in lines:
|
|
|
|
if line.startswith("443/tcp"):
|
|
|
|
# return <port> from: "443/tcp -> 0.0.0.0:<port>"
|
|
|
|
return line.split(":")[-1]
|
2022-12-09 14:26:35 +01:00
|
|
|
raise RuntimeError(f"Could not determine port of container {self.container_id}")
|