11import re
22import time
3+
34from django .conf import settings
45from marathon import MarathonClient
56from marathon .models import MarathonApp
6- from .states import JobState
77from docker import Client
8+
9+ from .states import JobState
810from .fleet import FleetHTTPClient
911
1012# turn down standard marathon logging
1113
1214MATCH = re .compile (
1315 '(?P<app>[a-z0-9-]+)_?(?P<version>v[0-9]+)?\.?(?P<c_type>[a-z-_]+)?.(?P<c_num>[0-9]+)' )
1416RETRIES = 3
17+ POLL_ATTEMPTS = 30
18+ POLL_WAIT = 100
1519
1620
1721class MarathonHTTPClient (object ):
@@ -47,24 +51,23 @@ def create(self, name, image, command='', **kwargs):
4751 cpu = kwargs .get ('cpu' , {}).get (l ['c_type' ])
4852 if cpu :
4953 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+ cmd = "docker run --name {name} -P {image} {command}" .format (** locals ())
55+ self .client .create_app (app_id , MarathonApp (cmd = cmd , mem = m , cpus = c ))
5456 self .client .scale_app (app_id , 0 , force = True )
55- for _ in xrange (30 ):
57+ for _ in xrange (POLL_ATTEMPTS ):
5658 if self .client .get_app (self ._app_id (name )).tasks_running == 0 :
5759 return
5860 time .sleep (1 )
5961
6062 def start (self , name ):
6163 """Start a container"""
6264 self .client .scale_app (self ._app_id (name ), 1 , force = True )
63- for _ in xrange (30 ):
65+ for _ in xrange (POLL_ATTEMPTS ):
6466 if self .client .get_app (self ._app_id (name )).tasks_running == 1 :
65- return
67+ break
6668 time .sleep (1 )
67- raise RuntimeError ("App Not Started" )
69+ host = self .client .get_app (self ._app_id (name )).tasks [0 ].host
70+ self ._waitforcontainer (host , name )
6871
6972 def stop (self , name ):
7073 """Stop a container"""
@@ -79,21 +82,33 @@ def destroy(self, name):
7982 except :
8083 self .client .delete_app (self ._app_id (name ), force = True )
8184
82- def _delete_container (self , host , name ):
85+ def _get_container_state (self , host , name ):
8386 docker_cli = Client ("tcp://{}:2375" .format (host ), timeout = 1200 , version = '1.17' )
8487 try :
85- if docker_cli .inspect_container (name )['State' ]:
86- docker_cli . remove_container ( name , force = True )
88+ if docker_cli .inspect_container (name )['State' ][ 'Running' ] :
89+ return JobState . up
8790 except :
88- pass
91+ return JobState .destroyed
92+
93+ def _waitforcontainer (self , host , name ):
94+ for _ in xrange (POLL_WAIT ):
95+ if self ._get_container_state (host , name ) == JobState .up :
96+ return
97+ time .sleep (1 )
98+ raise RuntimeError ("App container Not Started" )
99+
100+ def _delete_container (self , host , name ):
101+ docker_cli = Client ("tcp://{}:2375" .format (host ), timeout = 1200 , version = '1.17' )
102+ if docker_cli .inspect_container (name )['State' ]:
103+ docker_cli .remove_container (name , force = True )
89104
90105 def run (self , name , image , entrypoint , command ): # noqa
91106 """Run a one-off command"""
92107 return self .fleet .run (name , image , entrypoint , command )
93108
94109 def state (self , name ):
95110 try :
96- for _ in xrange (30 ):
111+ for _ in xrange (POLL_ATTEMPTS ):
97112 if self .client .get_app (self ._app_id (name )).tasks_running == 1 :
98113 return JobState .up
99114 elif self .client .get_app (self ._app_id (name )).tasks_running == 0 :
0 commit comments