|
| 1 | +import re |
| 2 | +import time |
| 3 | +from django.conf import settings |
| 4 | +from marathon import MarathonClient |
| 5 | +from marathon.models import MarathonApp |
| 6 | +from .states import JobState |
| 7 | +from docker import Client |
| 8 | +from .fleet import FleetHTTPClient |
| 9 | + |
| 10 | +# turn down standard marathon logging |
| 11 | + |
| 12 | +MATCH = re.compile( |
| 13 | + '(?P<app>[a-z0-9-]+)_?(?P<version>v[0-9]+)?\.?(?P<c_type>[a-z-_]+)?.(?P<c_num>[0-9]+)') |
| 14 | +RETRIES = 3 |
| 15 | + |
| 16 | + |
| 17 | +class MarathonHTTPClient(object): |
| 18 | + |
| 19 | + def __init__(self, target, auth, options, pkey): |
| 20 | + self.target = settings.MARATHON_HOST |
| 21 | + self.auth = auth |
| 22 | + self.options = options |
| 23 | + self.pkey = pkey |
| 24 | + self.registry = settings.REGISTRY_HOST + ':' + settings.REGISTRY_PORT |
| 25 | + self.client = MarathonClient('http://'+self.target+':8180') |
| 26 | + self.fleet = FleetHTTPClient('/var/run/fleet.sock', auth, options, pkey) |
| 27 | + |
| 28 | + # helpers |
| 29 | + def _app_id(self, name): |
| 30 | + return name.replace('_', '.') |
| 31 | + |
| 32 | + # container api |
| 33 | + def create(self, name, image, command='', **kwargs): |
| 34 | + """Create a container""" |
| 35 | + app_id = self._app_id(name) |
| 36 | + l = locals().copy() |
| 37 | + l.update(re.match(MATCH, name).groupdict()) |
| 38 | + image = self.registry + '/' + image |
| 39 | + mems = kwargs.get('memory', {}).get(l['c_type']) |
| 40 | + m = 0 |
| 41 | + if mems: |
| 42 | + mems = mems.lower() |
| 43 | + if mems[-2:-1].isalpha() and mems[-1].isalpha(): |
| 44 | + mems = mems[:-1] |
| 45 | + m = int(mems[:-1]) |
| 46 | + c = 0.5 |
| 47 | + cpu = kwargs.get('cpu', {}).get(l['c_type']) |
| 48 | + if cpu: |
| 49 | + c = cpu |
| 50 | + cpu = kwargs.get('cpu', {}).get(l['c_type']) |
| 51 | + self.client.create_app(app_id, |
| 52 | + MarathonApp(cmd="docker run --name "+name+" -P "+image+" "+command, |
| 53 | + mem=m, cpus=c)) |
| 54 | + self.client.scale_app(app_id, 0, force=True) |
| 55 | + for _ in xrange(30): |
| 56 | + if self.client.get_app(self._app_id(name)).tasks_running == 0: |
| 57 | + return |
| 58 | + time.sleep(1) |
| 59 | + |
| 60 | + def start(self, name): |
| 61 | + """Start a container""" |
| 62 | + self.client.scale_app(self._app_id(name), 1, force=True) |
| 63 | + for _ in xrange(30): |
| 64 | + if self.client.get_app(self._app_id(name)).tasks_running == 1: |
| 65 | + return |
| 66 | + time.sleep(1) |
| 67 | + raise RuntimeError("App Not Started") |
| 68 | + |
| 69 | + def stop(self, name): |
| 70 | + """Stop a container""" |
| 71 | + raise NotImplementedError |
| 72 | + |
| 73 | + def destroy(self, name): |
| 74 | + """Destroy a container""" |
| 75 | + try: |
| 76 | + host = self.client.get_app(self._app_id(name)).tasks[0].host |
| 77 | + self.client.delete_app(self._app_id(name), force=True) |
| 78 | + self._delete_container(host, name) |
| 79 | + except: |
| 80 | + self.client.delete_app(self._app_id(name), force=True) |
| 81 | + |
| 82 | + def _delete_container(self, host, name): |
| 83 | + docker_cli = Client("tcp://{}:2375".format(host), timeout=1200, version='1.17') |
| 84 | + try: |
| 85 | + if docker_cli.inspect_container(name)['State']: |
| 86 | + docker_cli.remove_container(name, force=True) |
| 87 | + except: |
| 88 | + pass |
| 89 | + |
| 90 | + def run(self, name, image, entrypoint, command): # noqa |
| 91 | + """Run a one-off command""" |
| 92 | + return self.fleet.run(name, image, entrypoint, command) |
| 93 | + |
| 94 | + def state(self, name): |
| 95 | + try: |
| 96 | + for _ in xrange(30): |
| 97 | + if self.client.get_app(self._app_id(name)).tasks_running == 1: |
| 98 | + return JobState.up |
| 99 | + elif self.client.get_app(self._app_id(name)).tasks_running == 0: |
| 100 | + return JobState.created |
| 101 | + time.sleep(1) |
| 102 | + except: |
| 103 | + return JobState.destroyed |
| 104 | + |
| 105 | + def attach(self, name): |
| 106 | + """ |
| 107 | + Attach to a job's stdin, stdout and stderr |
| 108 | + """ |
| 109 | + raise NotImplementedError |
| 110 | + |
| 111 | +SchedulerClient = MarathonHTTPClient |
0 commit comments