<s> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> from libcloud . container . base import ( ContainerDriver , Container , <EOL> ContainerCluster , ContainerImage ) <EOL> from libcloud . container . types import ContainerState <EOL> from libcloud . container . utils . docker import RegistryClient <EOL> from libcloud . common . aws import SignedAWSConnection , AWSJsonResponse <EOL> __all__ = [ <EOL> '<STR_LIT>' <EOL> ] <EOL> ECS_VERSION = '<STR_LIT>' <EOL> ECR_VERSION = '<STR_LIT>' <EOL> ECS_HOST = '<STR_LIT>' <EOL> ECR_HOST = '<STR_LIT>' <EOL> ROOT = '<STR_LIT:/>' <EOL> ECS_TARGET_BASE = '<STR_LIT>' % ( ECS_VERSION . replace ( '<STR_LIT:->' , '<STR_LIT>' ) ) <EOL> ECR_TARGET_BASE = '<STR_LIT>' % ( ECR_VERSION . replace ( '<STR_LIT:->' , '<STR_LIT>' ) ) <EOL> class ECSJsonConnection ( SignedAWSConnection ) : <EOL> version = ECS_VERSION <EOL> host = ECS_HOST <EOL> responseCls = AWSJsonResponse <EOL> service_name = '<STR_LIT>' <EOL> class ECRJsonConnection ( SignedAWSConnection ) : <EOL> version = ECR_VERSION <EOL> host = ECR_HOST <EOL> responseCls = AWSJsonResponse <EOL> service_name = '<STR_LIT>' <EOL> class ElasticContainerDriver ( ContainerDriver ) : <EOL> name = '<STR_LIT>' <EOL> website = '<STR_LIT>' <EOL> ecr_repository_host = '<STR_LIT>' <EOL> connectionCls = ECSJsonConnection <EOL> ecrConnectionClass = ECRJsonConnection <EOL> supports_clusters = False <EOL> status_map = { <EOL> '<STR_LIT>' : ContainerState . RUNNING <EOL> } <EOL> def __init__ ( self , access_id , secret , region ) : <EOL> super ( ElasticContainerDriver , self ) . __init__ ( access_id , secret ) <EOL> self . region = region <EOL> self . region_name = region <EOL> self . connection . host = ECS_HOST % ( region ) <EOL> conn_kwargs = self . _ex_connection_class_kwargs ( ) <EOL> self . ecr_connection = self . ecrConnectionClass ( <EOL> access_id , secret , ** conn_kwargs ) <EOL> self . ecr_connection . host = ECR_HOST % ( region ) <EOL> self . ecr_connection . driver = self <EOL> self . ecr_connection . connect ( ) <EOL> def _ex_connection_class_kwargs ( self ) : <EOL> return { '<STR_LIT>' : '<STR_LIT:4>' } <EOL> def list_images ( self , ex_repository_name ) : <EOL> """<STR_LIT>""" <EOL> request = { } <EOL> request [ '<STR_LIT>' ] = ex_repository_name <EOL> list_response = self . ecr_connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( request ) , <EOL> headers = self . _get_ecr_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> repository_id = self . ex_get_repository_id ( ex_repository_name ) <EOL> host = self . _get_ecr_host ( repository_id ) <EOL> return self . _to_images ( list_response [ '<STR_LIT>' ] , <EOL> host , <EOL> ex_repository_name ) <EOL> def list_clusters ( self ) : <EOL> """<STR_LIT>""" <EOL> listdata = self . connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( { } ) , <EOL> headers = self . _get_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> request = { '<STR_LIT>' : listdata [ '<STR_LIT>' ] } <EOL> data = self . connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( request ) , <EOL> headers = self . _get_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> return self . _to_clusters ( data ) <EOL> def create_cluster ( self , name , location = None ) : <EOL> """<STR_LIT>""" <EOL> request = { '<STR_LIT>' : name } <EOL> response = self . connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( request ) , <EOL> headers = self . _get_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> return self . _to_cluster ( response [ '<STR_LIT>' ] ) <EOL> def destroy_cluster ( self , cluster ) : <EOL> """<STR_LIT>""" <EOL> request = { '<STR_LIT>' : cluster . id } <EOL> data = self . connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( request ) , <EOL> headers = self . _get_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> return data [ '<STR_LIT>' ] [ '<STR_LIT:status>' ] == '<STR_LIT>' <EOL> def list_containers ( self , image = None , cluster = None ) : <EOL> """<STR_LIT>""" <EOL> request = { '<STR_LIT>' : '<STR_LIT:default>' } <EOL> if cluster is not None : <EOL> request [ '<STR_LIT>' ] = cluster . id <EOL> if image is not None : <EOL> request [ '<STR_LIT>' ] = image . name <EOL> list_response = self . connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( request ) , <EOL> headers = self . _get_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> if len ( list_response [ '<STR_LIT>' ] ) == <NUM_LIT:0> : <EOL> return [ ] <EOL> containers = self . ex_list_containers_for_task ( <EOL> list_response [ '<STR_LIT>' ] ) <EOL> return containers <EOL> def deploy_container ( self , name , image , cluster = None , <EOL> parameters = None , start = True , ex_cpu = <NUM_LIT:10> , ex_memory = <NUM_LIT> , <EOL> ex_container_port = None , ex_host_port = None ) : <EOL> """<STR_LIT>""" <EOL> data = { } <EOL> if ex_container_port is None and ex_host_port is None : <EOL> port_maps = [ ] <EOL> else : <EOL> port_maps = [ <EOL> { <EOL> "<STR_LIT>" : ex_container_port , <EOL> "<STR_LIT>" : ex_host_port <EOL> } <EOL> ] <EOL> data [ '<STR_LIT>' ] = [ <EOL> { <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT:name>" : name , <EOL> "<STR_LIT:image>" : image . name , <EOL> "<STR_LIT>" : ex_cpu , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : ex_memory , <EOL> "<STR_LIT>" : port_maps , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : [ ] <EOL> } <EOL> ] <EOL> data [ '<STR_LIT>' ] = name <EOL> response = self . connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( data ) , <EOL> headers = self . _get_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> if start : <EOL> return self . ex_start_task ( <EOL> response [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> else : <EOL> return Container ( <EOL> id = None , <EOL> name = name , <EOL> image = image , <EOL> state = ContainerState . RUNNING , <EOL> ip_addresses = [ ] , <EOL> extra = { <EOL> '<STR_LIT>' : <EOL> response [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> } , <EOL> driver = self . connection . driver <EOL> ) <EOL> def get_container ( self , id ) : <EOL> """<STR_LIT>""" <EOL> containers = self . ex_list_containers_for_task ( [ id ] ) <EOL> return containers [ <NUM_LIT:0> ] <EOL> def start_container ( self , container , count = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . ex_start_task ( container . extra [ '<STR_LIT>' ] , count ) <EOL> def stop_container ( self , container ) : <EOL> """<STR_LIT>""" <EOL> request = { '<STR_LIT>' : container . extra [ '<STR_LIT>' ] } <EOL> response = self . connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( request ) , <EOL> headers = self . _get_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> containers = [ ] <EOL> containers . extend ( self . _to_containers ( <EOL> response [ '<STR_LIT>' ] , <EOL> container . extra [ '<STR_LIT>' ] ) ) <EOL> return containers <EOL> def restart_container ( self , container ) : <EOL> """<STR_LIT>""" <EOL> self . stop_container ( container ) <EOL> return self . start_container ( container ) <EOL> def destroy_container ( self , container ) : <EOL> """<STR_LIT>""" <EOL> return self . stop_container ( container ) <EOL> def ex_start_task ( self , task_arn , count = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> request = None <EOL> request = { '<STR_LIT:count>' : count , <EOL> '<STR_LIT>' : task_arn } <EOL> response = self . connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( request ) , <EOL> headers = self . _get_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> containers = [ ] <EOL> for task in response [ '<STR_LIT>' ] : <EOL> containers . extend ( self . _to_containers ( task , task_arn ) ) <EOL> return containers <EOL> def ex_list_containers_for_task ( self , task_arns ) : <EOL> """<STR_LIT>""" <EOL> describe_request = { '<STR_LIT>' : task_arns } <EOL> descripe_response = self . connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( describe_request ) , <EOL> headers = self . _get_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> containers = [ ] <EOL> for task in descripe_response [ '<STR_LIT>' ] : <EOL> containers . extend ( self . _to_containers ( <EOL> task , task [ '<STR_LIT>' ] ) ) <EOL> return containers <EOL> def ex_create_service ( self , name , cluster , <EOL> task_definition , desired_count = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> request = { <EOL> '<STR_LIT>' : name , <EOL> '<STR_LIT>' : task_definition , <EOL> '<STR_LIT>' : desired_count , <EOL> '<STR_LIT>' : cluster . id } <EOL> response = self . connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( request ) , <EOL> headers = self . _get_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> return response [ '<STR_LIT>' ] <EOL> def ex_list_service_arns ( self , cluster = None ) : <EOL> """<STR_LIT>""" <EOL> request = { } <EOL> if cluster is not None : <EOL> request [ '<STR_LIT>' ] = cluster . id <EOL> response = self . connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( request ) , <EOL> headers = self . _get_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> return response [ '<STR_LIT>' ] <EOL> def ex_describe_service ( self , service_arn ) : <EOL> """<STR_LIT>""" <EOL> request = { '<STR_LIT>' : [ service_arn ] } <EOL> response = self . connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( request ) , <EOL> headers = self . _get_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> return response [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> def ex_destroy_service ( self , service_arn ) : <EOL> """<STR_LIT>""" <EOL> request = { <EOL> '<STR_LIT>' : service_arn } <EOL> response = self . connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( request ) , <EOL> headers = self . _get_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> return response [ '<STR_LIT>' ] <EOL> def ex_get_registry_client ( self , repository_name ) : <EOL> """<STR_LIT>""" <EOL> repository_id = self . ex_get_repository_id ( repository_name ) <EOL> token = self . ex_get_repository_token ( repository_id ) <EOL> host = self . _get_ecr_host ( repository_id ) <EOL> return RegistryClient ( <EOL> host = host , <EOL> username = '<STR_LIT>' , <EOL> password = token <EOL> ) <EOL> def ex_get_repository_token ( self , repository_id ) : <EOL> """<STR_LIT>""" <EOL> request = { '<STR_LIT>' : [ repository_id ] } <EOL> response = self . ecr_connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( request ) , <EOL> headers = self . _get_ecr_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> return response [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> def ex_get_repository_id ( self , repository_name ) : <EOL> """<STR_LIT>""" <EOL> request = { '<STR_LIT>' : [ repository_name ] } <EOL> list_response = self . ecr_connection . request ( <EOL> ROOT , <EOL> method = '<STR_LIT:POST>' , <EOL> data = json . dumps ( request ) , <EOL> headers = self . _get_ecr_headers ( '<STR_LIT>' ) <EOL> ) . object <EOL> repository_id = list_response [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> return repository_id <EOL> def _get_ecr_host ( self , repository_id ) : <EOL> return self . ecr_repository_host % ( <EOL> repository_id , <EOL> self . region ) <EOL> def _get_headers ( self , action ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT>' : '<STR_LIT>' % <EOL> ( ECS_TARGET_BASE , action ) , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' <EOL> } <EOL> def _get_ecr_headers ( self , action ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT>' : '<STR_LIT>' % <EOL> ( ECR_TARGET_BASE , action ) , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' <EOL> } <EOL> def _to_clusters ( self , data ) : <EOL> clusters = [ ] <EOL> for cluster in data [ '<STR_LIT>' ] : <EOL> clusters . append ( self . _to_cluster ( cluster ) ) <EOL> return clusters <EOL> def _to_cluster ( self , data ) : <EOL> return ContainerCluster ( <EOL> id = data [ '<STR_LIT>' ] , <EOL> name = data [ '<STR_LIT>' ] , <EOL> driver = self . connection . driver <EOL> ) <EOL> def _to_containers ( self , data , task_definition_arn ) : <EOL> clusters = [ ] <EOL> for cluster in data [ '<STR_LIT>' ] : <EOL> clusters . append ( self . _to_container ( cluster , task_definition_arn ) ) <EOL> return clusters <EOL> def _to_container ( self , data , task_definition_arn ) : <EOL> return Container ( <EOL> id = data [ '<STR_LIT>' ] , <EOL> name = data [ '<STR_LIT:name>' ] , <EOL> image = ContainerImage ( <EOL> id = None , <EOL> name = data [ '<STR_LIT:name>' ] , <EOL> path = None , <EOL> version = None , <EOL> driver = self . connection . driver <EOL> ) , <EOL> ip_addresses = None , <EOL> state = self . status_map . get ( data [ '<STR_LIT>' ] , None ) , <EOL> extra = { <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : task_definition_arn <EOL> } , <EOL> driver = self . connection . driver <EOL> ) <EOL> def _to_images ( self , data , host , repository_name ) : <EOL> images = [ ] <EOL> for image in data : <EOL> images . append ( self . _to_image ( image , host , repository_name ) ) <EOL> return images <EOL> def _to_image ( self , data , host , repository_name ) : <EOL> path = '<STR_LIT>' % ( <EOL> host , <EOL> repository_name , <EOL> data [ '<STR_LIT>' ] <EOL> ) <EOL> return ContainerImage ( <EOL> id = None , <EOL> name = path , <EOL> path = path , <EOL> version = data [ '<STR_LIT>' ] , <EOL> driver = self . connection . driver <EOL> ) </s>
