forked from d2iq-archive/mesos-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chronos_docker
executable file
·305 lines (244 loc) · 8.73 KB
/
chronos_docker
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
#!/usr/bin/env python
import itertools
import json
import os
import re
import signal
import subprocess
import sys
import threading
import time
import traceback
import shlex
import mesos
import mesos_pb2
import socket
# Syslog host information
SYSLOG_HOST = "localhost"
SYSLOG_PORT = 1122
SYSLOG_APPNAME = "Chronos_docker"
class MySysLogger():
def __init__(self, app_name=SYSLOG_APPNAME, host=SYSLOG_HOST, port=SYSLOG_PORT):
self.app_name = app_name
self.host = host
self.port = port
self.hostname = socket.gethostname()
def __getattr__(self, facility):
def wrapper_func(message):
localtime = time.localtime()
timestamp = time.strftime('%b %d %H:%M:%S', localtime)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
data = "%s %s %s %s %s" % (str(timestamp), self.hostname, self.app_name, facility.upper(), message)
sock.sendto(data, (self.host, self.port))
sock.close()
return wrapper_func
def main():
def handler(signum, _):
log.info('Exiting due to signal: ' + str(signum))
exit(-signum)
signal.signal(signal.SIGINT, handler)
signal.signal(signal.SIGTERM, handler)
signal.signal(signal.SIGABRT, handler)
signal.signal(signal.SIGPIPE, handler)
signal.signal(signal.SIGSEGV, handler)
driver = mesos.MesosExecutorDriver(DockerExecutor(sys.argv[1:]))
log.info('Ready to serve!')
log.info('Args in handler function: %s' % sys.argv)
exit(0 if driver.run() == mesos_pb2.DRIVER_STOPPED else 1)
# This script wraps one and only one container.
cid = None
cidfile = None
def read_cid():
global cid
if cidfile is not None:
try:
with open(cidfile) as f:
cid = f.read().strip()
except IOError:
pass
log = MySysLogger(SYSLOG_APPNAME)
class DockerExecutor(mesos.Executor):
def __init__(self, args):
self.args = args
self.task = None
self.driver = None
self.runner_thread = None
self.shutdown_thread = None
self.data = {}
self.env = {}
log.info('Args Provided __INIT__: %s' % args)
def run(self):
global proc
exitcode = 2
finalstate = mesos_pb2.TASK_FAILED
log.info("Task from Chronos: %s" % self.task_json)
try:
img = self.task_json['container']
args = self.task_json['cmd']
params = self.task_json['params']
cmd = run_with_settings(img, args, params)
self.send_state(mesos_pb2.TASK_RUNNING)
proc = subprocess.call(cmd)
log.info('Container exited with code: %d' % proc)
if proc == 0:
finalstate = mesos_pb2.TASK_FINISHED
else:
if self.shutdown_thread:
finalstate = mesos_pb2.TASK_KILLED
else:
finalstate = mesos_pb2.TASK_FAILED
except Exception, e:
log_exc()
finally:
self.send_state(finalstate)
exit(exitcode)
def send_state(self, state):
try:
update = mesos_pb2.TaskStatus()
update.task_id.value = self.task.task_id.value
update.state = state
self.driver.sendStatusUpdate(update)
except Exception, e:
log_exc()
#### Mesos Executor API methods ####
def registered(self, driver, executorInfo, frameworkInfo, slaveInfo):
log.info("Registered with Mesos slave")
def reregistered(driver, slaveInfo):
log.info("Reregistered with Mesos slave. More info: %s" % slaveInfo)
def disconnected(driver):
log.warning('Disconnected from Mesos slave')
def launchTask(self, driver, task):
if self.task is not None:
log.error('Executor was reused but this executor is not reuseable. Old Task: %s' % self.task)
exit(2)
self.task = task
self.driver = driver
self.task_data = self.task.data.replace("|", "")
try:
self.task_json = json.loads(self.task_data)
except Exception, e:
log.info("JSON Task Data: %s" % self.task_data)
log.info("JSON Exception: %s" % e)
log.info('Task is: %s' % task.task_id.value)
try:
self.run_thread = threading.Thread(target=self.run)
self.run_thread.daemon = True
self.run_thread.start()
except Exception, e:
log_exc()
self.send_state(mesos_pb2.TASK_FAILED)
exit(2)
def killTask(self, driver, task_id):
if self.task.task_id.value == task_id.value:
log.info('Asked to shutdown managed task %s' % task_id.value)
self.cleanup()
else:
log.info('Asked to shutdown unknown task %s' % task_id.value)
def cleanup(self):
if self.shutdown_thread is None:
self.shutdown_thread = threading.Thread(target=cleanup_container)
self.shutdown_thread.daemon = True
self.shutdown_thread.start()
def allocated_ports(self):
range_resources = [_.ranges.range for _ in self.task.resources
if _.name == 'ports']
ranges = itertools.chain(*range_resources)
# NB: Casting long() to int() so there's no trailing 'L' in later
# stringifications. Ports should only ever be shorts, anyways.
ports = [range(int(_.begin), int(_.end) + 1) for _ in ranges]
return list(itertools.chain(*ports))
def shutdown(self, driver):
self.cleanup()
# Handles signals, passed as negative numbers, and ensures worker process is
# cleaned up if it exists.
#
# This function shows up in many places but because it's final statement is a
# call to os._exit() we can be sure it is only ever called one time.
def exit(returncode):
try:
cleanup_container()
except Exception, e:
log_exc()
finally:
os._exit(((-returncode) + 128) if returncode < 0 else returncode)
def log_exc():
for line in traceback.format_exc().splitlines():
log.error(line)
def json_pp(thing):
s = json.dumps(thing, indent=2, separators=(',', ': '), sort_keys=True)
data_lines = s.splitlines()[1:-1]
return "{ " + '\n'.join([data_lines[0][2:]] + data_lines[1:]) + " }"
def ensure_image(f):
def f_(image, *args, **kwargs):
pull_once(image)
return f(image, *args, **kwargs)
return f_
def try_cid(f):
def f_(*args, **kwargs):
if cid is None:
read_cid()
return f(*args, **kwargs)
return f_
cleaning_up_already = False # Hackish lock.
@try_cid
def cleanup_container():
global cid
global cidfile
global cleaning_up_already
if cid is not None and not cleaning_up_already:
cleaning_up_already = True
log.info('Cleaning up container %s' % cid)
subprocess.check_call(['docker', 'stop', '-t=2', cid])
subprocess.check_call(['rm', '-f', cidfile])
cid = None
cidfile = None
@ensure_image
def run_with_settings(image, args, params):
global cidfile
cidfile = '/tmp/docker_cid.' + os.urandom(8).encode('hex')
cmd = ['run', '-cidfile', cidfile]
argv = ['docker'] + cmd + [params] + [image] + [args]
new_cmd = ' '.join(str(arg) for arg in argv)
new_args = shlex.split(new_cmd)
log.info("New Args: %s" % new_args)
log.info('Docker Command: ' + ' '.join(str(arg) for arg in argv))
return new_args
@ensure_image
def inner_ports(image):
text = subprocess.check_output(['docker', 'inspect', image])
parsed = json.loads(text)[0]
config = None
if 'Config' in parsed:
config = parsed['Config']
if 'config' in parsed and config is None:
config = parsed['config']
if config:
exposed = config.get('ExposedPorts', {})
if exposed and isinstance(exposed, dict):
return [int(k.split('/')[0]) for k in exposed.keys()]
specs = config.get('PortSpecs', [])
if specs and isinstance(specs, list):
return [int(v.split(':')[-1]) for v in specs]
return [] # If all else fails...
def pull(image):
subprocess.check_call(['docker', 'pull', image])
refresh_docker_image_info(image)
def pull_once(image):
if not image_info(image):
pull(image)
def image_info(image):
if image in images:
return images[image]
else:
return refresh_docker_image_info(image)
images = {}
def refresh_docker_image_info(image):
delim = re.compile(' +')
text = subprocess.check_output(['docker', 'images', image])
records = [delim.split(line) for line in text.splitlines()]
for record in records:
if record[0] == image:
images[image] = image
return record
if __name__ == '__main__':
main()