<s> import docker <EOL> import os , sys , yaml , copy , StringIO <EOL> import dockermix <EOL> from requests . exceptions import HTTPError <EOL> from . container import Container <EOL> import utils <EOL> class ContainerError ( Exception ) : <EOL> pass <EOL> class ContainerMix : <EOL> def __init__ ( self , conf_file = None , environment = None ) : <EOL> self . log = utils . _setupLogging ( ) <EOL> self . containers = { } <EOL> if environment : <EOL> self . load ( environment ) <EOL> else : <EOL> if not conf_file . startswith ( '<STR_LIT:/>' ) : <EOL> conf_file = os . path . join ( os . path . dirname ( sys . argv [ <NUM_LIT:0> ] ) , conf_file ) <EOL> data = open ( conf_file , '<STR_LIT:r>' ) <EOL> self . config = yaml . load ( data ) <EOL> self . start_order = utils . order ( self . config [ '<STR_LIT>' ] ) <EOL> def get ( self , container ) : <EOL> return self . containers [ container ] <EOL> def build ( self , wait_time = <NUM_LIT> ) : <EOL> for container in self . start_order : <EOL> if not self . config [ '<STR_LIT>' ] [ container ] : <EOL> sys . stderr . write ( "<STR_LIT>" + container + "<STR_LIT:\n>" ) <EOL> exit ( <NUM_LIT:1> ) <EOL> config = self . config [ '<STR_LIT>' ] [ container ] <EOL> if '<STR_LIT>' not in config : <EOL> sys . stderr . write ( "<STR_LIT>" + container ) <EOL> exit ( <NUM_LIT:1> ) <EOL> base = config [ '<STR_LIT>' ] <EOL> self . _handleRequire ( container , wait_time ) <EOL> count = tag_name = <NUM_LIT:1> <EOL> if '<STR_LIT:count>' in config : <EOL> count = tag_name = config [ '<STR_LIT:count>' ] <EOL> while count > <NUM_LIT:0> : <EOL> name = container <EOL> if tag_name > <NUM_LIT:1> : <EOL> name = name + "<STR_LIT>" + str ( count ) <EOL> self . log . info ( '<STR_LIT>' , name , base ) <EOL> build = Container ( name , copy . deepcopy ( config ) ) <EOL> dockerfile = None <EOL> if '<STR_LIT>' in config : <EOL> if '<STR_LIT>' in config [ '<STR_LIT>' ] : <EOL> dockerfile = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if '<STR_LIT:url>' in config [ '<STR_LIT>' ] : <EOL> dockerfile_url = config [ '<STR_LIT>' ] [ '<STR_LIT:url>' ] <EOL> build . build ( dockerfile ) <EOL> self . containers [ name ] = build <EOL> count = count - <NUM_LIT:1> <EOL> def destroy ( self , timeout = None ) : <EOL> for container in self . containers : <EOL> self . log . info ( '<STR_LIT>' , container ) <EOL> self . containers [ container ] . destroy ( timeout ) <EOL> def start ( self , container = None , wait_time = <NUM_LIT> ) : <EOL> if container : <EOL> self . log . info ( '<STR_LIT>' , container ) <EOL> self . containers [ container ] . start ( ) <EOL> else : <EOL> for container in self . start_order : <EOL> self . log . info ( '<STR_LIT>' , container ) <EOL> self . _handleRequire ( container , wait_time ) <EOL> self . containers [ container ] . start ( ) <EOL> def stop ( self , container = None , timeout = None ) : <EOL> if container : <EOL> self . log . info ( '<STR_LIT>' , container ) <EOL> self . containers [ container ] . stop ( timeout ) <EOL> else : <EOL> for container in self . containers : <EOL> self . log . info ( '<STR_LIT>' , container ) <EOL> self . containers [ container ] . stop ( timeout ) <EOL> def load ( self , filename = '<STR_LIT>' ) : <EOL> self . log . info ( '<STR_LIT>' , filename ) <EOL> with open ( filename , '<STR_LIT:r>' ) as input_file : <EOL> self . config = yaml . load ( input_file ) <EOL> for container in self . config [ '<STR_LIT>' ] : <EOL> self . containers [ container ] = Container ( container , self . config [ '<STR_LIT>' ] [ container ] ) <EOL> def save ( self , filename = '<STR_LIT>' ) : <EOL> self . log . info ( '<STR_LIT>' , filename ) <EOL> with open ( filename , '<STR_LIT:w>' ) as output_file : <EOL> output_file . write ( self . dump ( ) ) <EOL> def status ( self ) : <EOL> columns = "<STR_LIT>" <EOL> result = columns . format ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> for container in self . containers : <EOL> container_id = self . containers [ container ] . desc [ '<STR_LIT>' ] <EOL> node_name = ( container [ : <NUM_LIT:15> ] + '<STR_LIT:..>' ) if len ( container ) > <NUM_LIT> else container <EOL> command = '<STR_LIT>' <EOL> status = '<STR_LIT>' <EOL> try : <EOL> state = docker . Client ( ) . inspect_container ( container_id ) <EOL> command = "<STR_LIT>" . join ( [ state [ '<STR_LIT>' ] ] + state [ '<STR_LIT>' ] + [ "<STR_LIT:U+0020>" ] ) <EOL> command = ( command [ : <NUM_LIT:30> ] + '<STR_LIT:..>' ) if len ( command ) > <NUM_LIT:32> else command <EOL> if state [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> status = '<STR_LIT>' <EOL> except HTTPError : <EOL> status = '<STR_LIT>' <EOL> result += columns . format ( container_id , node_name , command , status ) <EOL> return result . rstrip ( '<STR_LIT:\n>' ) <EOL> def dump ( self ) : <EOL> result = { } <EOL> result [ '<STR_LIT>' ] = { } <EOL> for container in self . containers : <EOL> output = result [ '<STR_LIT>' ] [ container ] = self . containers [ container ] . desc <EOL> return yaml . dump ( result , Dumper = yaml . SafeDumper ) <EOL> def _pollService ( self , container , service , port , wait_time ) : <EOL> service_ip = self . containers [ service ] . get_ip_address ( ) <EOL> self . log . info ( '<STR_LIT>' , container , service , service_ip , port ) <EOL> result = utils . waitForService ( service_ip , int ( port ) , wait_time ) <EOL> if result < <NUM_LIT:0> : <EOL> self . log . error ( '<STR_LIT>' , service , port ) <EOL> raise ContainerError ( "<STR_LIT>" ) <EOL> return service_ip + "<STR_LIT::>" + str ( port ) <EOL> def _handleRequire ( self , container , wait_time ) : <EOL> env = [ ] <EOL> config = self . config [ '<STR_LIT>' ] [ container ] <EOL> if '<STR_LIT>' in config : <EOL> try : <EOL> for service in config [ '<STR_LIT>' ] : <EOL> service_env = [ ] <EOL> port = config [ '<STR_LIT>' ] [ service ] [ '<STR_LIT:port>' ] <EOL> if port : <EOL> count = config [ '<STR_LIT>' ] [ service ] . get ( '<STR_LIT:count>' , <NUM_LIT:1> ) <EOL> if count > <NUM_LIT:1> : <EOL> while count > <NUM_LIT:0> : <EOL> name = service + "<STR_LIT>" + str ( count ) <EOL> service_env . append ( self . _pollService ( container , name , port , wait_time ) ) <EOL> count = count - <NUM_LIT:1> <EOL> else : <EOL> service_env . append ( self . _pollService ( container , service , port , wait_time ) ) <EOL> env . append ( service . upper ( ) + "<STR_LIT:=>" + "<STR_LIT:U+0020>" . join ( service_env ) ) <EOL> except : <EOL> self . log . error ( '<STR_LIT>' ) <EOL> self . destroy ( ) <EOL> raise <EOL> if '<STR_LIT>' in config [ '<STR_LIT>' ] : <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT>' ] += env <EOL> else : <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT>' ] = env </s>
