Skip to content

Commit

Permalink
some styling changes
Browse files Browse the repository at this point in the history
  • Loading branch information
PsyanticY committed Feb 27, 2019
1 parent 5d447d4 commit d89e469
Showing 1 changed file with 51 additions and 47 deletions.
98 changes: 51 additions & 47 deletions nixops/backends/ec2.py
Expand Up @@ -705,6 +705,54 @@ 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

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()

args['UserData'] = user_data
args['EbsOptimized'] = ebs_optimized

placement = dict(
AvailabilityZone=zone or ""
)
args['Placement'] = placement

# 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
Expand Down Expand Up @@ -888,54 +936,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
Expand Down Expand Up @@ -981,10 +991,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():
Expand All @@ -993,7 +999,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())
Expand All @@ -1005,7 +1010,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

Expand Down

0 comments on commit d89e469

Please sign in to comment.