<s> """<STR_LIT>""" <EOL> import json <EOL> import logging <EOL> import threading <EOL> import uuid <EOL> from perfkitbenchmarker import context <EOL> from perfkitbenchmarker import errors <EOL> from perfkitbenchmarker import flags <EOL> from perfkitbenchmarker import network <EOL> from perfkitbenchmarker import resource <EOL> from perfkitbenchmarker import vm_util <EOL> from perfkitbenchmarker . providers . aws import util <EOL> from perfkitbenchmarker import providers <EOL> FLAGS = flags . FLAGS <EOL> REGION = '<STR_LIT>' <EOL> ZONE = '<STR_LIT>' <EOL> class AwsFirewall ( network . BaseFirewall ) : <EOL> """<STR_LIT>""" <EOL> CLOUD = providers . AWS <EOL> def __init__ ( self ) : <EOL> self . firewall_set = set ( ) <EOL> self . _lock = threading . Lock ( ) <EOL> def AllowPort ( self , vm , port ) : <EOL> """<STR_LIT>""" <EOL> if vm . is_static : <EOL> return <EOL> entry = ( port , vm . group_id ) <EOL> if entry in self . firewall_set : <EOL> return <EOL> with self . _lock : <EOL> if entry in self . firewall_set : <EOL> return <EOL> authorize_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % vm . region , <EOL> '<STR_LIT>' % vm . group_id , <EOL> '<STR_LIT>' % port , <EOL> '<STR_LIT>' ] <EOL> util . IssueRetryableCommand ( <EOL> authorize_cmd + [ '<STR_LIT>' ] ) <EOL> util . IssueRetryableCommand ( <EOL> authorize_cmd + [ '<STR_LIT>' ] ) <EOL> self . firewall_set . add ( entry ) <EOL> def DisallowAllPorts ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class AwsVpc ( resource . BaseResource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , region ) : <EOL> super ( AwsVpc , self ) . __init__ ( ) <EOL> self . region = region <EOL> self . id = None <EOL> self . _subnet_index = <NUM_LIT:0> <EOL> self . _subnet_index_lock = threading . Lock ( ) <EOL> self . default_security_group_id = None <EOL> def _Create ( self ) : <EOL> """<STR_LIT>""" <EOL> create_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' ] <EOL> stdout , _ , _ = vm_util . IssueCommand ( create_cmd ) <EOL> response = json . loads ( stdout ) <EOL> self . id = response [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . _EnableDnsHostnames ( ) <EOL> util . AddDefaultTags ( self . id , self . region ) <EOL> def _PostCreate ( self ) : <EOL> """<STR_LIT>""" <EOL> cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , self . region , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' + self . id ] <EOL> stdout , _ , _ = vm_util . IssueCommand ( cmd ) <EOL> response = json . loads ( stdout ) <EOL> groups = response [ '<STR_LIT>' ] <EOL> if len ( groups ) != <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' . format ( <EOL> len ( groups ) , response ) ) <EOL> self . default_security_group_id = groups [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> logging . info ( '<STR_LIT>' , <EOL> self . default_security_group_id ) <EOL> def _Exists ( self ) : <EOL> """<STR_LIT>""" <EOL> describe_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' % self . id ] <EOL> stdout , _ = util . IssueRetryableCommand ( describe_cmd ) <EOL> response = json . loads ( stdout ) <EOL> vpcs = response [ '<STR_LIT>' ] <EOL> assert len ( vpcs ) < <NUM_LIT:2> , '<STR_LIT>' <EOL> return len ( vpcs ) > <NUM_LIT:0> <EOL> def _EnableDnsHostnames ( self ) : <EOL> """<STR_LIT>""" <EOL> enable_hostnames_command = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' , self . id , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> util . IssueRetryableCommand ( enable_hostnames_command ) <EOL> def _Delete ( self ) : <EOL> """<STR_LIT>""" <EOL> delete_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' % self . id ] <EOL> vm_util . IssueCommand ( delete_cmd ) <EOL> def NextSubnetCidrBlock ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . _subnet_index_lock : <EOL> if self . _subnet_index >= ( <NUM_LIT:1> << <NUM_LIT:8> ) - <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' . format ( <EOL> self . _subnet_index ) ) <EOL> cidr = '<STR_LIT>' . format ( self . _subnet_index ) <EOL> self . _subnet_index += <NUM_LIT:1> <EOL> return cidr <EOL> class AwsSubnet ( resource . BaseResource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , zone , vpc_id , cidr_block = '<STR_LIT>' ) : <EOL> super ( AwsSubnet , self ) . __init__ ( ) <EOL> self . zone = zone <EOL> self . region = util . GetRegionFromZone ( zone ) <EOL> self . vpc_id = vpc_id <EOL> self . id = None <EOL> self . cidr_block = cidr_block <EOL> def _Create ( self ) : <EOL> """<STR_LIT>""" <EOL> create_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' % self . vpc_id , <EOL> '<STR_LIT>' % self . cidr_block ] <EOL> if not util . IsRegion ( self . zone ) : <EOL> create_cmd . append ( '<STR_LIT>' % self . zone ) <EOL> stdout , _ , _ = vm_util . IssueCommand ( create_cmd ) <EOL> response = json . loads ( stdout ) <EOL> self . id = response [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> util . AddDefaultTags ( self . id , self . region ) <EOL> def _Delete ( self ) : <EOL> """<STR_LIT>""" <EOL> logging . info ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> self . id ) <EOL> delete_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' % self . id ] <EOL> vm_util . IssueCommand ( delete_cmd ) <EOL> def _Exists ( self ) : <EOL> """<STR_LIT>""" <EOL> describe_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' % self . id ] <EOL> stdout , _ = util . IssueRetryableCommand ( describe_cmd ) <EOL> response = json . loads ( stdout ) <EOL> subnets = response [ '<STR_LIT>' ] <EOL> assert len ( subnets ) < <NUM_LIT:2> , '<STR_LIT>' <EOL> return len ( subnets ) > <NUM_LIT:0> <EOL> class AwsInternetGateway ( resource . BaseResource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , region ) : <EOL> super ( AwsInternetGateway , self ) . __init__ ( ) <EOL> self . region = region <EOL> self . vpc_id = None <EOL> self . id = None <EOL> self . attached = False <EOL> def _Create ( self ) : <EOL> """<STR_LIT>""" <EOL> create_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region ] <EOL> stdout , _ , _ = vm_util . IssueCommand ( create_cmd ) <EOL> response = json . loads ( stdout ) <EOL> self . id = response [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> util . AddDefaultTags ( self . id , self . region ) <EOL> def _Delete ( self ) : <EOL> """<STR_LIT>""" <EOL> delete_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' % self . id ] <EOL> vm_util . IssueCommand ( delete_cmd ) <EOL> def _Exists ( self ) : <EOL> """<STR_LIT>""" <EOL> describe_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' % self . id ] <EOL> stdout , _ = util . IssueRetryableCommand ( describe_cmd ) <EOL> response = json . loads ( stdout ) <EOL> internet_gateways = response [ '<STR_LIT>' ] <EOL> assert len ( internet_gateways ) < <NUM_LIT:2> , '<STR_LIT>' <EOL> return len ( internet_gateways ) > <NUM_LIT:0> <EOL> def Attach ( self , vpc_id ) : <EOL> """<STR_LIT>""" <EOL> if not self . attached : <EOL> self . vpc_id = vpc_id <EOL> attach_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' % self . id , <EOL> '<STR_LIT>' % self . vpc_id ] <EOL> util . IssueRetryableCommand ( attach_cmd ) <EOL> self . attached = True <EOL> def Detach ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . attached : <EOL> detach_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' % self . id , <EOL> '<STR_LIT>' % self . vpc_id ] <EOL> util . IssueRetryableCommand ( detach_cmd ) <EOL> self . attached = False <EOL> class AwsRouteTable ( resource . BaseResource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , region , vpc_id ) : <EOL> super ( AwsRouteTable , self ) . __init__ ( ) <EOL> self . region = region <EOL> self . vpc_id = vpc_id <EOL> def _Create ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _Delete ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ vm_util . Retry ( ) <EOL> def _PostCreate ( self ) : <EOL> """<STR_LIT>""" <EOL> describe_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' % self . vpc_id ] <EOL> stdout , _ = util . IssueRetryableCommand ( describe_cmd ) <EOL> response = json . loads ( stdout ) <EOL> self . id = response [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> def CreateRoute ( self , internet_gateway_id ) : <EOL> """<STR_LIT>""" <EOL> create_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' % self . id , <EOL> '<STR_LIT>' % internet_gateway_id , <EOL> '<STR_LIT>' ] <EOL> util . IssueRetryableCommand ( create_cmd ) <EOL> class AwsPlacementGroup ( resource . BaseResource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , region ) : <EOL> """<STR_LIT>""" <EOL> super ( AwsPlacementGroup , self ) . __init__ ( ) <EOL> self . name = ( <EOL> '<STR_LIT>' % ( FLAGS . run_uri , str ( uuid . uuid4 ( ) ) [ - <NUM_LIT:12> : ] ) ) <EOL> self . region = region <EOL> def _Create ( self ) : <EOL> """<STR_LIT>""" <EOL> create_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' % self . name , <EOL> '<STR_LIT>' ] <EOL> vm_util . IssueCommand ( create_cmd ) <EOL> def _Delete ( self ) : <EOL> """<STR_LIT>""" <EOL> delete_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' % self . name ] <EOL> vm_util . IssueCommand ( delete_cmd ) <EOL> def _Exists ( self ) : <EOL> """<STR_LIT>""" <EOL> describe_cmd = util . AWS_PREFIX + [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % self . region , <EOL> '<STR_LIT>' % self . name ] <EOL> stdout , _ = util . IssueRetryableCommand ( describe_cmd ) <EOL> response = json . loads ( stdout ) <EOL> placement_groups = response [ '<STR_LIT>' ] <EOL> assert len ( placement_groups ) < <NUM_LIT:2> , '<STR_LIT>' <EOL> return len ( placement_groups ) > <NUM_LIT:0> <EOL> class _AwsRegionalNetwork ( network . BaseNetwork ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , region ) : <EOL> self . region = region <EOL> self . vpc = AwsVpc ( self . region ) <EOL> self . internet_gateway = AwsInternetGateway ( region ) <EOL> self . route_table = None <EOL> self . created = False <EOL> self . _create_lock = threading . Lock ( ) <EOL> self . _reference_count = <NUM_LIT:0> <EOL> self . _reference_count_lock = threading . Lock ( ) <EOL> @ classmethod <EOL> def GetForRegion ( cls , region ) : <EOL> """<STR_LIT>""" <EOL> benchmark_spec = context . GetThreadBenchmarkSpec ( ) <EOL> if benchmark_spec is None : <EOL> raise errors . Error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> key = cls . CLOUD , REGION , region <EOL> if key not in benchmark_spec . networks : <EOL> benchmark_spec . networks [ key ] = cls ( region ) <EOL> return benchmark_spec . networks [ key ] <EOL> def Create ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . _reference_count_lock : <EOL> assert self . _reference_count >= <NUM_LIT:0> , self . _reference_count <EOL> self . _reference_count += <NUM_LIT:1> <EOL> with self . _create_lock : <EOL> if self . created : <EOL> return <EOL> self . vpc . Create ( ) <EOL> self . internet_gateway . Create ( ) <EOL> self . internet_gateway . Attach ( self . vpc . id ) <EOL> if self . route_table is None : <EOL> self . route_table = AwsRouteTable ( self . region , self . vpc . id ) <EOL> self . route_table . Create ( ) <EOL> self . route_table . CreateRoute ( self . internet_gateway . id ) <EOL> self . created = True <EOL> def Delete ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . _reference_count_lock : <EOL> assert self . _reference_count >= <NUM_LIT:1> , self . _reference_count <EOL> self . _reference_count -= <NUM_LIT:1> <EOL> if self . _reference_count : <EOL> return <EOL> self . internet_gateway . Detach ( ) <EOL> self . internet_gateway . Delete ( ) <EOL> self . vpc . Delete ( ) <EOL> class AwsNetwork ( network . BaseNetwork ) : <EOL> """<STR_LIT>""" <EOL> CLOUD = providers . AWS <EOL> def __init__ ( self , spec ) : <EOL> """<STR_LIT>""" <EOL> super ( AwsNetwork , self ) . __init__ ( spec ) <EOL> self . region = util . GetRegionFromZone ( spec . zone ) <EOL> self . regional_network = _AwsRegionalNetwork . GetForRegion ( self . region ) <EOL> self . subnet = None <EOL> self . placement_group = AwsPlacementGroup ( self . region ) <EOL> def Create ( self ) : <EOL> """<STR_LIT>""" <EOL> self . regional_network . Create ( ) <EOL> if self . subnet is None : <EOL> cidr = self . regional_network . vpc . NextSubnetCidrBlock ( ) <EOL> self . subnet = AwsSubnet ( self . zone , self . regional_network . vpc . id , <EOL> cidr_block = cidr ) <EOL> self . subnet . Create ( ) <EOL> self . placement_group . Create ( ) <EOL> def Delete ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . subnet : <EOL> self . subnet . Delete ( ) <EOL> self . placement_group . Delete ( ) <EOL> self . regional_network . Delete ( ) <EOL> @ classmethod <EOL> def _GetKeyFromNetworkSpec ( cls , spec ) : <EOL> """<STR_LIT>""" <EOL> return ( cls . CLOUD , ZONE , spec . zone ) </s>
