-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd-run
executable file
·137 lines (104 loc) · 2.81 KB
/
d-run
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
#!/bin/env python3
import os
import json
import argparse
def make_volume(path, dir):
if ':' in path:
hostpath, path = path.split(':', 1)
else:
hostpath = os.path.join(dir, 'volumes', path.lstrip('/'))
os.makedirs(hostpath, exist_ok=True)
if ':' in path:
path, mode = path.rsplit(':', 1)
else:
mode = 'rw'
if mode not in ['ro', 'rw']:
raise ValueError(f'Invalid volume mode: {mode}')
op = '--ro-bind' if mode == 'ro' else '--bind'
return [op, hostpath, path]
def get_id(file, name):
with open(file) as fh:
for line in fh:
parts = line.split(':')
if parts[0] == name:
return parts[2]
raise KeyError(name)
def parse_user(user, root):
uid = user
gid = None
if ':' in user:
uid, gid = uid.split(':', 1)
if not gid.isdigit():
gid = get_id(os.path.join(root, 'etc/group'), gid)
if not uid.isdigit():
uid = get_id(os.path.join(root, 'etc/passwd'), uid)
return uid, gid
def build_cmd(dir, config):
cmd = [
'bwrap',
'--bind', os.path.join(dir, 'rootfs'), '/',
'--tmpfs', '/tmp',
'--dev', '/dev',
'--proc', '/proc',
'--clearenv',
'--unshare-all',
'--die-with-parent',
]
if config.get('Hostname'):
cmd += ['--hostname', config['Hostname']]
if config.get('WorkingDir'):
cmd += ['--chdir', config['WorkingDir']]
for entry in config['Env']:
key, value = entry.split('=', 1)
cmd += ['--setenv', key, value]
if config.get('Volumes'):
for volume in config['Volumes']:
cmd += make_volume(volume, dir)
if config.get('net'):
cmd += [
'--ro-bind', '/etc/resolv.conf', '/etc/resolv.conf',
'--share-net',
]
if not config.get('rw'):
cmd += ['--remount-ro', '/']
if config.get('User'):
uid, gid = parse_user(config['User'], os.path.join(dir, 'rootfs'))
cmd += ['--uid', uid]
if gid is not None:
cmd += ['--gid', gid]
cmd.append('--')
if config.get('Entrypoint'):
cmd += config['Entrypoint']
cmd += config['Cmd']
return cmd
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('dir')
parser.add_argument('-v', '--volume', action='append')
parser.add_argument('-u', '--user')
parser.add_argument('-n', '--net', action='store_true')
parser.add_argument('-w', '--rw', action='store_true')
parser.add_argument('--debug', action='store_true')
parser.add_argument('cmd', nargs='...')
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
with open(os.path.join(args.dir, 'config.json')) as fh:
config = json.load(fh)
if args.cmd:
config['Cmd'] = args.cmd
if args.net:
config['net'] = True
if args.rw:
config['rw'] = True
if args.user:
config['User'] = args.user
if args.volume:
if not config.get('Volumes'):
config['Volumes'] = {}
for volume in args.volume:
config['Volumes'][volume] = {}
cmd = build_cmd(args.dir, config)
if args.debug:
print(' '.join(cmd))
os.execvp(cmd[0], cmd)