forked from Yelp/paasta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_wrapper.py
executable file
·333 lines (266 loc) · 9.95 KB
/
docker_wrapper.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env python
""" Meant to be used by mesos-slave instead of the /usr/bin/docker executable
directly This will parse the CLI arguments intended for docker, extract
environment variable settings related to the actual node hostname and mesos
task ID, and use those as an additional --hostname argument when calling the
underlying docker command.
If the environment variables are unspecified, or if --hostname is already
specified, this does not change any arguments and just directly calls docker
as-is.
Additionally this wrapper will look for the environment variable
PIN_TO_NUMA_NODE which contains the physical CPU and memory to restrict the
container to. If the system is NUMA enabled, docker will be called with the
arguments cpuset-cpus and cpuset-mems.
"""
import logging
import os
import re
import socket
import sys
from paasta_tools.firewall import DEFAULT_SYNAPSE_SERVICE_DIR
from paasta_tools.firewall import firewall_flock
from paasta_tools.firewall import prepare_new_container
from paasta_tools.mac_address import reserve_unique_mac_address
from paasta_tools.utils import DEFAULT_SOA_DIR
LOCK_DIRECTORY = '/var/lib/paasta/mac-address'
ENV_MATCH_RE = re.compile('^(-\w*e\w*|--env(?P<file>-file)?)(=(?P<arg>\S.*))?$')
MAX_HOSTNAME_LENGTH = 63
def parse_env_args(args):
result = dict(os.environ.items())
in_env = False
in_file = False
for arg in args:
if not in_env:
match = ENV_MATCH_RE.match(arg)
if not match:
continue
arg = match.group('arg') or ''
in_file = bool(match.group('file'))
if not arg:
in_env = True
continue
in_env = False
if in_file:
result.update(read_env_file(arg))
in_file = False
continue
try:
k, v = arg.split('=', 1)
except ValueError:
continue
result[k] = v
return result
def read_env_file(filename):
# Parse a file where each line is KEY=VALUE
# return contents in dictionary form
result = {}
with open(filename) as f:
for line in f:
try:
k, v = line.split('=', 1)
except ValueError:
continue
result[k] = v.strip()
return result
def can_add_hostname(args):
# return False if --hostname is already specified or if --network=host
if is_network_host(args):
return False
for index, arg in enumerate(args):
# Check for --hostname and variants
if arg == '-h':
return False
if arg.startswith('--hostname'):
return False
if len(arg) > 1 and arg[0] == '-' and arg[1] != '-':
# several short args
arg = arg.partition('=')[0]
if 'h' in arg:
return False
return True
def is_network_host(args):
for index, arg in enumerate(args):
# Check for --network=host and variants
if arg in ('--net=host', '--network=host'):
return True
try:
if arg in ('--net', '--network') and args[index + 1] == 'host':
return True
except IndexError:
pass
return False
def is_run(args):
try:
list(args).index('run')
return True
except ValueError:
return False
def can_add_mac_address(args):
# return False if --mac-address is already specified or if --network=host
if is_network_host(args) or not is_run(args):
return False
for index, arg in enumerate(args):
# Check for --mac-address
if arg.startswith('--mac-address'):
return False
return True
def generate_hostname(fqdn, mesos_task_id):
host_hostname = fqdn.partition('.')[0]
task_id = mesos_task_id.rpartition('.')[2]
hostname = host_hostname + '-' + task_id
# hostnames can only contain alphanumerics and dashes and must be no more
# than 63 characters
hostname = re.sub('[^a-zA-Z0-9-]+', '-', hostname)[:MAX_HOSTNAME_LENGTH]
# hostnames can also not end with dashes as per RFC952
hostname = hostname.rstrip('-')
return hostname
def add_argument(args, argument):
# Add an argument immediately after 'run' command if it exists
args = list(args)
try:
run_index = args.index('run')
except ValueError:
pass
else:
args.insert(run_index + 1, argument)
return args
def get_cpumap():
# Return a dict containing the core numbers per physical CPU
core = 0
cpumap = {}
try:
with open('/proc/cpuinfo', 'r') as f:
for line in f:
m = re.match('physical\sid.*(\d)', line)
if m:
cpuid = int(m.group(1))
if cpuid not in cpumap:
cpumap[cpuid] = []
cpumap[cpuid].append(core)
core += 1
except IOError:
logging.warning('Error while trying to read cpuinfo')
pass
return cpumap
def get_numa_memsize(nb_nodes):
# Return memory size in mB per NUMA node assuming memory is split evenly
# TODO: calculate and return real memory map
try:
with open('/proc/meminfo', 'r') as f:
for line in f:
m = re.match('MemTotal:\s*(\d+)\skB', line)
if m:
return int(m.group(1)) / 1024 / int(nb_nodes)
except IOError:
logging.warning('Error while trying to read meminfo')
pass
return 0
def arg_collision(new_args, current_args):
# Returns True if one of the new arguments is already in the
# current argument list.
cur_arg_keys = []
for c in current_args:
cur_arg_keys.append(c.split('=')[0])
return bool(set(new_args).intersection(set(cur_arg_keys)))
def is_numa_enabled():
if os.path.exists('/proc/1/numa_maps'):
return True
else:
logging.warning('The system does not support NUMA')
return False
def get_cpu_requierements(env_args):
# Ensure we return a float. If no requierements we return 0.0
try:
return float(env_args.get('MARATHON_APP_RESOURCE_CPUS'))
except (ValueError, TypeError):
logging.warning('Could not read {} as a float'.format(env_args.get('MARATHON_APP_RESOURCE_CPUS')))
return 0.0
def get_mem_requierements(env_args):
# Ensure we return a float. If no requierements we return 0.0
try:
return float(env_args.get('MARATHON_APP_RESOURCE_MEM'))
except (ValueError, TypeError):
logging.warning('Could not read {} as a float'.format(env_args.get('MARATHON_APP_RESOURCE_MEM')))
return 0.0
def append_cpuset_args(argv, env_args):
# Enable log messages to stderr
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
try:
pinned_numa_node = int(env_args.get('PIN_TO_NUMA_NODE'))
except (ValueError, TypeError):
logging.error('Could not read PIN_TO_NUMA_NODE value as an int: {}'.format(
env_args.get('PIN_TO_NUMA_NODE'),
))
return argv
cpumap = get_cpumap()
if len(cpumap) < 1:
logging.error('Less than 2 physical CPU detected')
return argv
if pinned_numa_node not in cpumap:
logging.error('Specified NUMA node: {} does not exist on this system'.format(
pinned_numa_node,
))
return argv
if arg_collision(['--cpuset-cpus', '--cpuset-mems'], argv):
logging.error('--cpuset options are already set. Not overriding')
return argv
if not is_numa_enabled():
logging.error('Could not detect NUMA on the system')
return argv
if len(cpumap[pinned_numa_node]) < get_cpu_requierements(env_args):
logging.error('The NUMA node has less cores than requested')
return argv
if get_numa_memsize(len(cpumap)) <= get_mem_requierements(env_args):
logging.error('Requested memory:{} MB does not fit in one NUMA node: {} MB'.format(
get_mem_requierements(env_args), get_numa_memsize(len(cpumap)),
))
return argv
logging.info(f'Binding container to NUMA node {pinned_numa_node}')
argv = add_argument(
argv, ('--cpuset-cpus=' + ','.join(
str(c) for c in cpumap[pinned_numa_node]
)),
)
argv = add_argument(argv, ('--cpuset-mems=' + str(pinned_numa_node)))
return argv
def add_firewall(argv, service, instance):
output = ''
try:
mac_address, lockfile = reserve_unique_mac_address(LOCK_DIRECTORY)
except Exception as e:
output = f'Unable to add mac address: {e}'
else:
argv = add_argument(argv, f'--mac-address={mac_address}')
try:
with firewall_flock():
prepare_new_container(
DEFAULT_SOA_DIR,
DEFAULT_SYNAPSE_SERVICE_DIR,
service,
instance,
mac_address,
)
except Exception as e:
output = f'Unable to add firewall rules: {e}'
if output:
print(output, file=sys.stderr)
return argv
def main(argv=None):
argv = argv if argv is not None else sys.argv
env_args = parse_env_args(argv)
if env_args.get('PIN_TO_NUMA_NODE'):
argv = append_cpuset_args(argv, env_args)
# Marathon sets MESOS_TASK_ID whereas Chronos sets mesos_task_id
mesos_task_id = env_args.get('MESOS_TASK_ID') or env_args.get('mesos_task_id')
if mesos_task_id and can_add_hostname(argv):
hostname = generate_hostname(socket.getfqdn(), mesos_task_id)
argv = add_argument(argv, f'--hostname={hostname}')
paasta_firewall = env_args.get('PAASTA_FIREWALL')
service = env_args.get('PAASTA_SERVICE')
instance = env_args.get('PAASTA_INSTANCE')
if paasta_firewall and service and instance and can_add_mac_address(argv):
try:
argv = add_firewall(argv, service, instance)
except Exception as e:
print(f'Unhandled exception in add_firewall: {e}', file=sys.stderr)
os.execlp('docker', 'docker', *argv[1:])