-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.py
More file actions
executable file
·145 lines (107 loc) · 3.84 KB
/
dev.py
File metadata and controls
executable file
·145 lines (107 loc) · 3.84 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/python3
from argparse import ArgumentParser, Namespace
import subprocess
"""
Highly recommend reading the wiki:
- https://github.com/mviereck/x11docker/wiki
"""
OPTS = [
["-x", "--x11", "add X11 from host"],
["-w", "--wayland", "add Wayland from host"],
["-g", "--gpu", "enable GPU"],
["-a", "--audio", "enable audio via Pipewire"],
]
def get_parser() -> ArgumentParser:
parser = ArgumentParser()
subparsers = parser.add_subparsers(dest="command", required=True)
create_parser = subparsers.add_parser("create", help="create a container")
for opt, full_opt, desc in OPTS:
create_parser.add_argument(opt, full_opt, help=desc, action="store_true")
create_parser.add_argument("-p", "--ports", help="publish ports to localhost")
create_parser.add_argument(
"-v", "--volume", help="mount volume", nargs="?", const=""
)
create_parser.add_argument("-i", "--image", help="specify image", nargs=1)
create_parser.add_argument("name", help="container name")
enter_parser = subparsers.add_parser("enter", help="enter a container")
enter_parser.add_argument("name", help="container name")
return parser
def get_args() -> Namespace:
return get_parser().parse_args()
def deduplicate(extra_args: str) -> str:
unique_args = {arg.strip() for arg in extra_args.splitlines()}
return " ".join(unique_args)
def process_create(args: Namespace) -> str:
assert args.command == "create"
extra_args = ""
if args.x11:
extra_args += """
--security-opt label=type:container_runtime_t
--userns keep-id:uid=1000,gid=1000
-v /tmp/.X11-unix:/tmp/.X11-unix:ro
-e DISPLAY
"""
if args.wayland:
extra_args += """
--security-opt label=type:container_runtime_t
--userns keep-id:uid=1000,gid=1000
-e WAYLAND_DISPLAY
-v "${XDG_RUNTIME_DIR}/${WAYLAND_DISPLAY}:/tmp/${WAYLAND_DISPLAY}:ro"
"""
if args.gpu:
extra_args += """
--security-opt label=type:container_runtime_t
--group-add video
--group-add render
--device /dev/dri
"""
if args.audio:
extra_args += """
--security-opt label=type:container_runtime_t
-v ${XDG_RUNTIME_DIR}/pipewire-0:/tmp/pipewire-0:ro
"""
if args.ports:
try:
ports = list(map(int, args.ports.split(",")))
except:
print("dev: error: ports should be comma seperated integers")
exit(1)
extra_args += "".join(f" -p 127.0.0.1:{port}:{port}" for port in ports)
if args.volume is not None:
volume_name = args.volume or args.name
extra_args += f" -v {volume_name}:/home/user/data"
image_name = args.image[0] if isinstance(args.image, list) else "base"
extra_args = deduplicate(extra_args)
command = f"""
podman run -itd --stop-timeout=1
--name {args.name}
-e XDG_RUNTIME_DIR=/tmp
{extra_args}
{image_name} bash
"""
# Removes duplicate whitespace
return " ".join(command.split())
def process_enter(args: Namespace) -> str:
assert args.command == "enter"
return f"podman exec -it {args.name} bash"
def process_args(args: Namespace) -> str:
match args.command:
case "create":
return process_create(args)
case "enter":
return process_enter(args)
# This should be unreachable
print(args)
exit(1)
if __name__ == "__main__":
args = get_args()
command = process_args(args)
print()
print(command)
print()
if args.command == "create":
if input("Continue? [y/N]").lower() != "y":
print("Aborted.")
exit()
# Probably insecure but allows using environment vars
subprocess.run(command, shell=True)