-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd-iaso-docker.py
53 lines (42 loc) · 1.48 KB
/
cmd-iaso-docker.py
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
import os
import subprocess
import sys
from pathlib import Path
from subprocess import PIPE
def run_in_subprocess(cmd, *args, **kwargs):
return subprocess.run(
[arg for arg in cmd.split(" ") if len(arg) > 0], *args, **kwargs
)
def main():
devnull = open(os.devnull, "w")
# Check if the cmd-iaso Docker image has been built already
if (
run_in_subprocess(
"docker image inspect cmd-iaso", stdout=devnull, stderr=devnull
).returncode
!= 0
):
print("Building docker image cmd-iaso ...")
run_in_subprocess(f"docker build --tag cmd-iaso {Path()}")
# Run cmd-iaso with the --docker option to generate the docker run command whilst binding
# stdin and stdout (stderr is used to return the command)
cmd = run_in_subprocess(
f"docker run -i --net=host --rm --init cmd-iaso --docker {Path().resolve()} {' '.join(sys.argv[1:])}",
stdin=sys.stdin,
stdout=sys.stdout,
stderr=subprocess.PIPE,
text=True,
)
if cmd.returncode != 0 or not cmd.stderr.startswith("> docker run"):
# There has been an error - forward it to stderr
print(cmd.stderr, file=sys.stderr, end="")
else:
# Run the returned docker run command whilst binding stdin, stdout and stderr
run_in_subprocess(
cmd.stderr[2:],
stdin=sys.stdin,
stdout=sys.stdout,
stderr=sys.stderr,
)
if __name__ == "__main__":
main()