def _Create(self): """Creates the peering object. Documentation on creating a vpc object: https://docs.aws.amazon.com/vpc/latest/peering/vpc-pg.pdf """ # Creates Peering Connection create_cmd = util.AWS_PREFIX + [ 'ec2', 'create-vpc-peering-connection', '--region=%s' % self.network_a.region, '--peer-region=%s' % self.network_b.region, '--vpc-id=%s' % self.network_a.vpc.id, '--peer-vpc-id=%s' % self.network_b.vpc.id] stdout, _ = vm_util.IssueRetryableCommand(create_cmd) response = json.loads(stdout) self.id = response['VpcPeeringConnection'][ 'VpcPeeringConnectionId'] # Accepts Peering Connection accept_cmd = util.AWS_PREFIX + [ 'ec2', 'accept-vpc-peering-connection', '--region=%s' % self.network_b.region, '--vpc-peering-connection-id=%s' % self.id] vm_util.IssueRetryableCommand(accept_cmd) util.AddDefaultTags(self.id, self.network_a.region) logging.info('Creating VPC peering between %s and %s', self.network_a.vpc.cidr, self.network_b.vpc.cidr) # Adds VPC peering to both networks' route tables self.network_a.route_table.CreateVpcPeeringRoute(self.id, self.network_b.vpc.cidr) self.network_b.route_table.CreateVpcPeeringRoute(self.id, self.network_a.vpc.cidr) # Updates security group to allow inbound traffic from peering networks self.network_a.vpc.AllowVpcPeerInBound(self.network_b.vpc) self.network_b.vpc.AllowVpcPeerInBound(self.network_a.vpc)
def _PostCreate(self): """Get the instance's data and tag it.""" describe_cmd = util.AWS_PREFIX + [ 'ec2', 'describe-instances', '--region=%s' % self.region, '--instance-ids=%s' % self.id] logging.info('Getting instance %s public IP. This will fail until ' 'a public IP is available, but will be retried.', self.id) stdout, _ = util.IssueRetryableCommand(describe_cmd) response = json.loads(stdout) instance = response['Reservations'][0]['Instances'][0] self.ip_address = instance['PublicIpAddress'] self.internal_ip = instance['PrivateIpAddress'] if util.IsRegion(self.zone): self.zone = str(instance['Placement']['AvailabilityZone']) util.AddDefaultTags(self.id, self.region) assert self.group_id == instance['SecurityGroups'][0]['GroupId'], ( self.group_id, instance['SecurityGroups'][0]['GroupId'])
def _Create(self): """Creates the disk.""" create_cmd = util.AWS_PREFIX + [ 'ec2', 'create-volume', '--region=%s' % self.region, '--size=%s' % self.disk_size, '--volume-type=%s' % self.disk_type ] if not util.IsRegion(self.zone): create_cmd.append('--availability-zone=%s' % self.zone) if self.disk_type in [IO1, IO2]: create_cmd.append('--iops=%s' % self.iops) if self.disk_type == GP3 and self.iops: create_cmd.append('--iops=%s' % self.iops) if self.disk_type == GP3 and self.throughput: create_cmd.append('--throughput=%s' % self.throughput) stdout, _, _ = vm_util.IssueCommand(create_cmd) response = json.loads(stdout) self.id = response['VolumeId'] util.AddDefaultTags(self.id, self.region)
def _PostCreate(self): """See base class.""" util.AddDefaultTags(self.id, self.region)
def AddMetadata(self, **kwargs): """Adds metadata to the VM.""" util.AddTags(self.id, self.region, **kwargs) if self.use_spot_instance: util.AddDefaultTags(self.spot_instance_request_id, self.region)
def _CreateSpot(self): """Create a Spot VM instance.""" placement = OrderedDict() if not util.IsRegion(self.zone): placement['AvailabilityZone'] = self.zone if self.use_dedicated_host: raise errors.Resource.CreationError( 'Tenancy=host is not supported for Spot Instances') elif IsPlacementGroupCompatible(self.machine_type): placement['GroupName'] = self.network.placement_group.name block_device_map = GetBlockDeviceMap(self.machine_type, self.boot_disk_size, self.image, self.region) network_interface = [ OrderedDict([('DeviceIndex', 0), ('AssociatePublicIpAddress', True), ('SubnetId', self.network.subnet.id)]) ] launch_specification = OrderedDict([ ('ImageId', self.image), ('InstanceType', self.machine_type), ('KeyName', 'perfkit-key-%s' % FLAGS.run_uri), ('Placement', placement) ]) if block_device_map: launch_specification['BlockDeviceMappings'] = json.loads( block_device_map, object_pairs_hook=collections.OrderedDict) launch_specification['NetworkInterfaces'] = network_interface create_cmd = util.AWS_PREFIX + [ 'ec2', 'request-spot-instances', '--region=%s' % self.region, '--spot-price=%s' % self.spot_price, '--client-token=%s' % self.client_token, '--launch-specification=%s' % json.dumps(launch_specification, separators=(',', ':')) ] stdout, _, _ = vm_util.IssueCommand(create_cmd) create_response = json.loads(stdout) self.spot_instance_request_id = ( create_response['SpotInstanceRequests'][0]['SpotInstanceRequestId'] ) util.AddDefaultTags(self.spot_instance_request_id, self.region) while True: describe_sir_cmd = util.AWS_PREFIX + [ '--region=%s' % self.region, 'ec2', 'describe-spot-instance-requests', '--spot-instance-request-ids=%s' % self.spot_instance_request_id ] stdout, _, _ = vm_util.IssueCommand(describe_sir_cmd) sir_response = json.loads(stdout)['SpotInstanceRequests'] assert len( sir_response) == 1, 'Expected exactly 1 SpotInstanceRequest' status_code = sir_response[0]['Status']['Code'] if (status_code in SPOT_INSTANCE_REQUEST_HOLDING_STATUSES or status_code in SPOT_INSTANCE_REQUEST_TERMINAL_STATUSES): message = sir_response[0]['Status']['Message'] raise errors.Resource.CreationError(message) elif status_code == 'fulfilled': self.id = sir_response[0]['InstanceId'] break time.sleep(2)