From c59d14262eecaf66b873924b0bff58548d537c4e Mon Sep 17 00:00:00 2001 From: PsyanticY Date: Wed, 27 Feb 2019 15:45:28 +0100 Subject: [PATCH] some styling changes --- nixops/backends/ec2.py | 96 +++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/nixops/backends/ec2.py b/nixops/backends/ec2.py index 0d5aa16b2..9fce381f9 100644 --- a/nixops/backends/ec2.py +++ b/nixops/backends/ec2.py @@ -705,6 +705,52 @@ def _wait_for_spot_request_fulfillment(self, request_id): def create_instance(self, defn, zone, user_data, ebs_optimized, args): + IamInstanceProfile = {} + if defn.instance_profile.startswith("arn:") : + IamInstanceProfile["Arn"] = defn.instance_profile + else: + IamInstanceProfile["Name"] = defn.instance_profile + + if defn.subnet_id != "": + if defn.security_groups != [] and defn.security_groups != ["default"]: + raise Exception("‘deployment.ec2.securityGroups’ is incompatible with ‘deployment.ec2.subnetId’") + + args['NetworkInterfaces'] = [dict( + AssociatePublicIpAddress=defn.associate_public_ip_address, + SubnetId=defn.subnet_id, + DeviceIndex=0, + Groups=self.security_groups_to_ids(defn.subnet_id, defn.security_group_ids) + )] + else: + args['SecurityGroups'] = defn.security_groups + + if defn.spot_instance_price: + args["InstanceMarketOptions"] = dict( + MarketType="spot", + SpotOptions=dict( + MaxPrice=str(defn.spot_instance_price/100.0), + SpotInstanceType=defn.spot_instance_request_type, + InstanceInterruptionBehavior=defn.spot_instance_interruption_behavior + ) + ) + if defn.spot_instance_timeout and defn.spot_instance_request_type != "one-time": + args["InstanceMarketOptions"]["SpotOptions"]["ValidUntil"]=(datetime.datetime.utcnow() + + datetime.timedelta(0, defn.spot_instance_timeout)).isoformat() + + placement = dict( + AvailabilityZone=zone or "" + ) + + args['InstanceType'] = defn.instance_type + args['ImageId'] = defn.ami + args['IamInstanceProfile'] = IamInstanceProfile + args['KeyName'] = defn.key_pair + args['Placement'] = placement + args['UserData'] = user_data + args['EbsOptimized'] = ebs_optimized + args['MaxCount'] = 1 # We always want to deploy one instance. + args['MinCount'] = 1 + # Use a client token to ensure that instance creation is # idempotent; i.e., if we get interrupted before recording # the instance ID, we'll get the same instance ID on the @@ -888,54 +934,16 @@ def create(self, defn, check, allow_reboot, allow_recreate): ami = self._conn_boto3.describe_images(ImageIds=[defn.ami])['Images'][0] self.root_device_type = ami['RootDeviceType'] - IamInstanceProfile = {} - if defn.instance_profile.startswith("arn:") : - IamInstanceProfile["Arn"] = defn.instance_profile - else: - IamInstanceProfile["Name"] = defn.instance_profile - - args = dict( - InstanceType=defn.instance_type, - ImageId=defn.ami, - IamInstanceProfile=IamInstanceProfile, - KeyName=defn.key_pair, - MaxCount=1, # We always want to deploy one instance. - MinCount=1 - ) - - if defn.subnet_id != "": - if defn.security_groups != [] and defn.security_groups != ["default"]: - raise Exception("‘deployment.ec2.securityGroups’ is incompatible with ‘deployment.ec2.subnetId’") - - args['NetworkInterfaces'] = [dict( - AssociatePublicIpAddress=defn.associate_public_ip_address, - SubnetId=defn.subnet_id, - DeviceIndex=0, - Groups=self.security_groups_to_ids(defn.subnet_id, defn.security_group_ids) - )] - else: - args['SecurityGroups'] = defn.security_groups - if defn.spot_instance_price: - args["InstanceMarketOptions"] = dict( - MarketType="spot", - SpotOptions=dict( - MaxPrice=str(defn.spot_instance_price/100.0), - SpotInstanceType=defn.spot_instance_request_type, - InstanceInterruptionBehavior=defn.spot_instance_interruption_behavior - ) - ) - if defn.spot_instance_timeout: - args["InstanceMarketOptions"]["SpotOptions"]["ValidUntil"]=(datetime.datetime.utcnow() + - datetime.timedelta(0, defn.spot_instance_timeout)).isoformat() - # Check if we need to resize the root disk resize_root = defn.root_disk_size != 0 and ami['RootDeviceType'] == 'ebs' + args = dict() + # Set the initial block device mapping to the ephemeral # devices defined in the spec. These cannot be changed # later. - args['BlockDeviceMappings'] = [] + for device_stored, v in defn.block_device_mapping.iteritems(): device_real = device_name_stored_to_real(device_stored) # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html @@ -981,10 +989,6 @@ def create(self, defn, check, allow_reboot, allow_recreate): raise Exception("unable to start EC2 instance ‘{0}’ in zone ‘{1}’ because volume ‘{2}’ is in zone ‘{3}’" .format(self.name, zone, v['disk'], volume.zone)) - placement = dict( - AvailabilityZone=zone or "" - ) - args['Placement'] = placement # Do we want an EBS-optimized instance? prefer_ebs_optimized = False for device_stored, v in defn.block_device_mapping.iteritems(): @@ -993,7 +997,6 @@ def create(self, defn, check, allow_reboot, allow_recreate): # if we have PIOPS volume and instance type supports EBS Optimized flags, then use ebs_optimized ebs_optimized = prefer_ebs_optimized and defn.ebs_optimized - args['EbsOptimized'] = ebs_optimized # Generate a public/private host key. if not self.public_host_key: (private, public) = nixops.util.create_key_pair(type=defn.host_key_type()) @@ -1005,7 +1008,6 @@ def create(self, defn, check, allow_reboot, allow_recreate): self.public_host_key, self.private_host_key.replace("\n", "|"), defn.host_key_type().upper()) - args['UserData'] = user_data instance = self.create_instance(defn, zone, user_data, ebs_optimized, args) update_instance_profile = False