1- import cStringIO
21import base64
32import copy
3+ import cStringIO
44import httplib
55import json
66import paramiko
7- import socket
87import re
8+ import socket
99import time
1010
1111from django .conf import settings
1212
13+ from . import AbstractSchedulerClient
1314from .states import JobState
1415
1516
@@ -32,13 +33,10 @@ def connect(self):
3233 self .sock = sock
3334
3435
35- class FleetHTTPClient (object ):
36+ class FleetHTTPClient (AbstractSchedulerClient ):
3637
3738 def __init__ (self , target , auth , options , pkey ):
38- self .target = target
39- self .auth = auth
40- self .options = options
41- self .pkey = pkey
39+ super (FleetHTTPClient , self ).__init__ (target , auth , options , pkey )
4240 # single global connection
4341 self .conn = UHTTPConnection (self .target )
4442
@@ -119,7 +117,7 @@ def _get_machines(self):
119117 # container api
120118
121119 def create (self , name , image , command = '' , template = None , ** kwargs ):
122- """Create a container"""
120+ """Create a container. """
123121 self ._create_container (name , image , command ,
124122 template or copy .deepcopy (CONTAINER_TEMPLATE ), ** kwargs )
125123
@@ -173,7 +171,7 @@ def _get_hostname(self, application_name):
173171 raise RuntimeError ('Unsupported hostname: ' + hostname )
174172
175173 def start (self , name ):
176- """Start a container"""
174+ """Start a container. """
177175 self ._put_unit (name , {'desiredState' : 'launched' })
178176 self ._wait_for_container_running (name )
179177
@@ -212,12 +210,12 @@ def _wait_for_destroy(self, name):
212210 raise RuntimeError ('timeout on container destroy' )
213211
214212 def stop (self , name ):
215- """Stop a container"""
213+ """Stop a container. """
216214 self ._put_unit (name , {"desiredState" : "loaded" })
217215 self ._wait_for_job_state (name , JobState .created )
218216
219217 def destroy (self , name ):
220- """Destroy a container"""
218+ """Destroy a container. """
221219 # call all destroy functions, ignoring any errors
222220 try :
223221 self ._destroy_container (name )
@@ -235,7 +233,7 @@ def _destroy_container(self, name):
235233 raise
236234
237235 def run (self , name , image , entrypoint , command ): # noqa
238- """Run a one-off command"""
236+ """Run a one-off command. """
239237 self ._create_container (name , image , command , copy .deepcopy (RUN_TEMPLATE ),
240238 entrypoint = entrypoint )
241239 # launch the container
@@ -337,13 +335,14 @@ def _do_ssh(cmd):
337335 return rc , output
338336
339337 def state (self , name ):
338+ """Display the given job's running state."""
340339 systemdActiveStateMap = {
341- " active" : "up" ,
342- " reloading" : " down" ,
343- " inactive" : " created" ,
344- " failed" : " crashed" ,
345- " activating" : " down" ,
346- " deactivating" : " down" ,
340+ ' active' : 'up' ,
341+ ' reloading' : ' down' ,
342+ ' inactive' : ' created' ,
343+ ' failed' : ' crashed' ,
344+ ' activating' : ' down' ,
345+ ' deactivating' : ' down' ,
347346 }
348347 try :
349348 # NOTE (bacongobbler): this call to ._get_unit() acts as a pre-emptive check to
@@ -354,9 +353,8 @@ def state(self, name):
354353 # FIXME (bacongobbler): when fleet loads a job, sometimes it'll automatically start and
355354 # stop the container, which in our case will return as 'failed', even though
356355 # the container is perfectly fine.
357- if activeState == 'failed' :
358- if state ['systemdLoadState' ] == 'loaded' :
359- return JobState .created
356+ if activeState == 'failed' and state ['systemdLoadState' ] == 'loaded' :
357+ return JobState .created
360358 return getattr (JobState , systemdActiveStateMap [activeState ])
361359 except KeyError :
362360 # failed retrieving a proper response from the fleet API
@@ -366,12 +364,6 @@ def state(self, name):
366364 # which means it does not exist
367365 return JobState .destroyed
368366
369- def attach (self , name ):
370- """
371- Attach to a job's stdin, stdout and stderr
372- """
373- raise NotImplementedError
374-
375367SchedulerClient = FleetHTTPClient
376368
377369
0 commit comments