#NEWLINE# def run( self , name , image , entrypoint , command ) : #NEWLINE# #INDENT# 'Run a one-off command.' #NEWLINE# self . _create_container ( name , image , command , copy . deepcopy ( RUN_TEMPLATE ) , entrypoint = entrypoint ) #NEWLINE# self . _put_unit ( name , { #NEWLINE# #INDENT# 'desiredState' : 'launched' , #NEWLINE# #UNINDENT# } ) #NEWLINE# state = self . _wait_for_container_state ( name ) #NEWLINE# try : #NEWLINE# #INDENT# machineID = state . get ( 'machineID' ) #NEWLINE# machines = self . _get_machines ( ) #NEWLINE# if ( not machines ) : #NEWLINE# #INDENT# raise RuntimeError ( 'no available hosts to run command' ) #NEWLINE# #UNINDENT# primaryIP = None #NEWLINE# for m in machines . get ( 'machines' , [ ] ) : #NEWLINE# #INDENT# if ( m [ 'id' ] == machineID ) : #NEWLINE# #INDENT# primaryIP = m [ 'primaryIP' ] #NEWLINE# #UNINDENT# #UNINDENT# if ( not primaryIP ) : #NEWLINE# #INDENT# raise RuntimeError ( 'could not find host' ) #NEWLINE# #UNINDENT# file_obj = cStringIO . StringIO ( base64 . b64decode ( self . pkey ) ) #NEWLINE# pkey = paramiko . RSAKey ( file_obj = file_obj ) #NEWLINE# ssh = paramiko . SSHClient ( ) #NEWLINE# ssh . set_missing_host_key_policy ( paramiko . AutoAddPolicy ( ) ) #NEWLINE# ssh . connect ( primaryIP , username = 'core' , pkey = pkey ) #NEWLINE# tran = ssh . get_transport ( ) #NEWLINE# def _do_ssh( cmd ) : #NEWLINE# #INDENT# with tran . open_session ( ) as chan : #NEWLINE# #INDENT# chan . exec_command ( cmd ) #NEWLINE# while ( not chan . exit_status_ready ( ) ) : #NEWLINE# #INDENT# time . sleep ( 1 ) #NEWLINE# #UNINDENT# out = chan . makefile ( ) #NEWLINE# output = out . read ( ) #NEWLINE# rc = chan . recv_exit_status ( ) #NEWLINE# return ( rc , output ) #NEWLINE# #UNINDENT# #UNINDENT# while True : #NEWLINE# #INDENT# ( rc , _ ) = _do_ssh ( 'docker inspect {name}' . format ( ** locals ( ) ) ) #NEWLINE# if ( rc == 0 ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# time . sleep ( 1 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise RuntimeError ( 'failed to create container' ) #NEWLINE# #UNINDENT# for _ in xrange ( 2 ) : #NEWLINE# #INDENT# ( _rc , _output ) = _do_ssh ( 'docker inspect {name}' . format ( ** locals ( ) ) ) #NEWLINE# if ( _rc != 0 ) : #NEWLINE# #INDENT# raise RuntimeError ( 'failed to inspect container' ) #NEWLINE# #UNINDENT# _container = json . loads ( _output ) #NEWLINE# started_at = _container [ 0 ] [ 'State' ] [ 'StartedAt' ] #NEWLINE# if ( not started_at . startswith ( '0001' ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# time . sleep ( 1 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise RuntimeError ( 'container failed to start' ) #NEWLINE# #UNINDENT# for _ in xrange ( 1200 ) : #NEWLINE# #INDENT# ( _rc , _output ) = _do_ssh ( 'docker inspect {name}' . format ( ** locals ( ) ) ) #NEWLINE# if ( _rc != 0 ) : #NEWLINE# #INDENT# raise RuntimeError ( 'failed to inspect container' ) #NEWLINE# #UNINDENT# _container = json . loads ( _output ) #NEWLINE# finished_at = _container [ 0 ] [ 'State' ] [ 'FinishedAt' ] #NEWLINE# if ( not finished_at . startswith ( '0001' ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# time . sleep ( 1 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise RuntimeError ( 'container timed out' ) #NEWLINE# #UNINDENT# ( _rc , output ) = _do_ssh ( 'docker logs {name}' . format ( ** locals ( ) ) ) #NEWLINE# if ( primaryIP != 0 ) : #NEWLINE# #INDENT# raise RuntimeError ( 'could not attach to container' ) #NEWLINE# #UNINDENT# ( _rc , _output ) = _do_ssh ( 'docker inspect {name}' . format ( ** locals ( ) ) ) #NEWLINE# if ( _rc != 0 ) : #NEWLINE# #INDENT# raise RuntimeError ( 'could not determine exit code' ) #NEWLINE# #UNINDENT# container = json . loads ( _output ) #NEWLINE# rc = container [ 0 ] [ 'State' ] [ 'ExitCode' ] #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# self . _destroy_container ( name ) #NEWLINE# self . _wait_for_destroy ( name ) #NEWLINE# #UNINDENT# return ( rc , output )