/
org.osbuild.container-deploy
executable file
·65 lines (57 loc) · 2.12 KB
/
org.osbuild.container-deploy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python3
import contextlib
import os
import random
import string
import subprocess
import sys
import osbuild.api
from osbuild.util import containers
@contextlib.contextmanager
def mount_container(image_tag):
result = subprocess.run(
["podman", "image", "mount", image_tag],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
check=False,
)
if result.returncode != 0:
code = result.returncode
msg = result.stderr.strip()
raise RuntimeError(f"Failed to mount image ({code}): {msg}")
try:
yield result.stdout.strip()
finally:
subprocess.run(
["podman", "image", "umount", image_tag],
check=True,
)
def main(inputs, tree, options):
images = containers.parse_containers_input(inputs)
assert len(images) == 1
image = list(images.values())[0]
# skopeo needs /var/tmp but the bwrap env is minimal and may not have it
os.makedirs("/var/tmp", mode=0o1777, exist_ok=True)
# We cannot use a tmpdir as storage here because of
# https://github.com/containers/storage/issues/1779 so instead
# just pick a random suffix. This runs inside bwrap which gives a
# tmp /var so it does not really matter much.
image_tag = "tmp-container-deploy-" + "".join(random.choices(string.digits, k=14))
with contextlib.ExitStack() as cm:
cm.callback(subprocess.run, ["podman", "rmi", image_tag], check=True)
with containers.container_source(image) as (_, source):
subprocess.run(
["skopeo", "copy", source,
f"containers-storage:{image_tag}"],
check=True,
)
with mount_container(image_tag) as img:
subprocess.run(["cp", "-a", f"{img}/.", f"{tree}/"], check=True)
# postprocess the tree, would be nicer to filter before already
for exclude in options.get("exclude", []):
subprocess.run(["rm", "-rf", f"{tree}/{exclude}"], check=True)
if __name__ == "__main__":
args = osbuild.api.arguments()
r = main(args["inputs"], args["tree"], args["options"])
sys.exit(r)