<s> import logging <EOL> from socket import create_connection <EOL> from time import sleep <EOL> from urlparse import urlparse <EOL> from docker_client import DockerClientError <EOL> log = logging . getLogger ( __name__ ) <EOL> def running ( client , container_id ) : <EOL> container_info = client . inspect_container ( container_id ) <EOL> return container_info and container_info [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def successfully_destroyed ( client , container_id ) : <EOL> try : <EOL> client . destroy ( container_id ) <EOL> return True <EOL> except DockerClientError , e : <EOL> log . debug ( '<STR_LIT>' , e ) <EOL> sleep ( <NUM_LIT:0.5> ) <EOL> return False <EOL> def connectable ( client , container_id , hostname , ports ) : <EOL> infos = client . inspect_container ( container_id ) <EOL> info = infos and infos [ <NUM_LIT:0> ] <EOL> settings = info and info . get ( '<STR_LIT>' ) <EOL> port_settings = settings and settings . get ( '<STR_LIT>' ) <EOL> for port in ports : <EOL> mappings = port_settings and port_settings . get ( '<STR_LIT>' % ( port , ) ) <EOL> if not mappings : <EOL> return False <EOL> for mapping in mappings : <EOL> external = int ( mapping [ '<STR_LIT>' ] ) <EOL> try : <EOL> address = ( hostname , external ) <EOL> log . debug ( '<STR_LIT>' , * address ) <EOL> conn = create_connection ( address , <NUM_LIT:5> ) <EOL> log . debug ( '<STR_LIT>' , * address ) <EOL> except IOError , e : <EOL> log . debug ( '<STR_LIT>' , e ) <EOL> return False <EOL> else : <EOL> conn . close ( ) <EOL> return True <EOL> def destroy_containers ( client , nametag ) : <EOL> while True : <EOL> sleep ( <NUM_LIT:1> ) <EOL> containers = client . list_containers ( needle = nametag ) <EOL> if not containers : <EOL> break <EOL> for container in containers : <EOL> try : <EOL> client . kill ( container ) <EOL> except Exception , e : <EOL> log . debug ( '<STR_LIT>' , e ) <EOL> for container in containers : <EOL> try : <EOL> client . destroy ( container ) <EOL> except Exception , e : <EOL> log . debug ( '<STR_LIT>' , e ) <EOL> def endpoint_address ( endpoint ) : <EOL> if '<STR_LIT>' not in endpoint : <EOL> endpoint = '<STR_LIT>' + endpoint <EOL> return urlparse ( endpoint ) . hostname or '<STR_LIT:localhost>' </s>
