def instance_role(self, role_name, role_data, desired_count): self.log.emit('creating role: {}'.format(role_name)) ami = self.amis[role_data['ami']][self.evpc.region_name] key_pair = self.evpc.key_pair.get_key_pair( role_data.get('key_pair', 'default') ) security_groups = map( self.evpc.get_security_group, role_data.get('security_groups', []) ) subnets = map( self.evpc.get_subnet, role_data.get('subnets', []) ) if len(subnets) == 0: self.log.emit( 'no subnets found for role: {}'.format(role_name), 'warning' ) # exit early. return None # sort by subnets by amount of instances, smallest first. subnets = sorted( subnets, key = lambda sn : collection_len(sn.instances), ) # determine the count of this role's existing instances. # Note: we look for role in all subnets, not just the listed subnets. existing_count = len(self.evpc.get_role(role_name)) if existing_count >= desired_count: # for now we exit early, maybe terminate extras... self.log.emit(existing_count + ' ' + desired_count, 'debug') return None # determine count of additional instances needed to reach desired_count. needed_count = desired_count - existing_count needed_per_subnet = needed_count / len(subnets) needed_remainder = needed_count % len(subnets) role_instances = [] for subnet in subnets: # ensure Run_Instance_Idempotency.html#client-tokens client_token = str(uuid4()) # figure out how many instances this subnet needs to create ... existing_in_subnet = len(self.evpc.get_role(role_name, subnet.instances.all())) count = needed_per_subnet - existing_in_subnet if needed_remainder != 0: needed_remainder -= 1 count += 1 if count == 0: # skip this subnet, it doesn't need to launch any instances. continue subnet_name = make_tag_dict(subnet)['Name'] msg = '{} instances of role {} launching into {}' self.log.emit(msg.format(count, role_name, subnet_name)) # create a batch of instances in subnet! instances = subnet.create_instances( ImageId = ami, InstanceType = role_data.get('instance_type'), MinCount = count, MaxCount = count, KeyName = key_pair.name, SecurityGroupIds = get_ids(security_groups), ClientToken = client_token, ) # accumulate all new instances into a single list. role_instances += instances # cast role Instance objets to EnrichedInstance objects. role_instances = self.evpc.get_instances(role_instances) self.tag_instances(role_name, role_instances) return role_instances
def instance_role(self, role_name, role_data, desired_count): if role_data.get('autoscaling', False) == True: # exit early if this instance_role is autoscaling. return None self.log.emit('creating role: {}'.format(role_name)) ami = self.amis[role_data['ami']][self.evpc.region_name] key_pair = self.evpc.key_pair.get_key_pair( role_data.get('key_pair', 'default')) security_groups = map_filter_false( self.evpc.get_security_group, role_data.get('security_groups', [])) subnets = map_filter_false(self.evpc.get_subnet, role_data.get('subnets', [])) if len(subnets) == 0: self.log.emit('no subnets found for role: {}'.format(role_name), 'warning') # exit early. return None # sort by subnets by amount of instances, smallest first. subnets = sorted( subnets, key=lambda sn: collection_len(sn.instances), ) # determine the count of this role's existing instances. # Note: we look for role in all subnets, not just the listed subnets. existing_count = len(self.evpc.get_role(role_name)) if existing_count >= desired_count: # for now we exit early, maybe terminate extras... msg = 'skipping role: {} (existing_count {} is greater than or equal to {})' self.log.emit(msg.format(role_name, existing_count, desired_count), 'debug') return None # determine count of additional instances needed to reach desired_count. needed_count = desired_count - existing_count needed_per_subnet = desired_count / len(subnets) needed_remainder = desired_count % len(subnets) block_device_map = get_block_device_map_from_role_config(role_data) role_instances = [] kwargs = { 'ImageId': ami, 'InstanceType': role_data.get('instance_type'), 'KeyName': key_pair.name, 'SecurityGroupIds': get_ids(security_groups), 'BlockDeviceMappings': block_device_map, 'UserData': role_data.get('userdata', ''), 'IamInstanceProfile': {}, } profile_name = role_data.get('instance_profile_name', None) if profile_name: kwargs['IamInstanceProfile'] = {'Name': profile_name} for subnet in subnets: # ensure Run_Instance_Idempotency.html#client-tokens kwargs['ClientToken'] = str(uuid4()) # figure out how many instances this subnet needs to create ... existing_in_subnet = len( self.evpc.get_role(role_name, subnet.instances.all())) count = needed_per_subnet - existing_in_subnet if needed_remainder != 0: needed_remainder -= 1 count += 1 if count == 0: # skip this subnet, it doesn't need to launch any instances. continue subnet_name = make_tag_dict(subnet)['Name'] msg = '{} instances of role {} launching into {} subnet' self.log.emit(msg.format(count, role_name, subnet_name)) # create a batch of instances in subnet! kwargs['MinCount'] = kwargs['MaxCount'] = count instances = self._create_instances(subnet, **kwargs) # accumulate all new instances into a single list. role_instances += instances # add role tag to each instance. for instance in role_instances: update_tags(instance, role=role_name)
def instance_role(self, role_name, role_data, desired_count): if role_data.get("autoscaling", False) == True: # exit early if this instance_role is autoscaling. return None self.log.emit("creating role: {}".format(role_name)) ami = self.amis[role_data["ami"]][self.evpc.region_name] key_pair = self.evpc.key_pair.get_key_pair(role_data.get("key_pair", "default")) security_groups = map_filter_false( self.evpc.get_security_group, role_data.get("security_groups", []) ) subnets = map_filter_false(self.evpc.get_subnet, role_data.get("subnets", [])) if len(subnets) == 0: self.log.emit("no subnets found for role: {}".format(role_name), "warning") # exit early. return None # sort by subnets by amount of instances, smallest first. subnets = sorted(subnets, key=lambda sn: collection_len(sn.instances)) # determine the count of this role's existing instances. # Note: we look for role in all subnets, not just the listed subnets. existing_count = len(self.evpc.get_role(role_name)) if existing_count >= desired_count: # for now we exit early, maybe terminate extras... msg = "skipping role: {} (existing_count {} is greater than or equal to {})" self.log.emit(msg.format(role_name, existing_count, desired_count), "debug") return None # determine count of additional instances needed to reach desired_count. needed_count = desired_count - existing_count needed_per_subnet = desired_count / len(subnets) needed_remainder = desired_count % len(subnets) block_device_map = get_block_device_map_from_role_config(role_data) role_instances = [] kwargs = { "ImageId": ami, "InstanceType": role_data.get("instance_type"), "KeyName": key_pair.name, "SecurityGroupIds": get_ids(security_groups), "BlockDeviceMappings": block_device_map, "UserData": role_data.get("userdata", ""), "IamInstanceProfile": {}, } profile_name = role_data.get("instance_profile_name", None) if profile_name: kwargs["IamInstanceProfile"] = {"Name": profile_name} private_ip_address = role_data.get("private_ip_address", None) if private_ip_address: kwargs["PrivateIpAddress"] = private_ip_address for subnet in subnets: # ensure Run_Instance_Idempotency.html#client-tokens kwargs["ClientToken"] = str(uuid4()) # figure out how many instances this subnet needs to create ... existing_in_subnet = len( self.evpc.get_role(role_name, subnet.instances.all()) ) count = needed_per_subnet - existing_in_subnet if needed_remainder != 0: needed_remainder -= 1 count += 1 if count == 0: # skip this subnet, it doesn't need to launch any instances. continue subnet_name = make_tag_dict(subnet)["Name"] msg = "{} instances of role {} launching into {} subnet" self.log.emit(msg.format(count, role_name, subnet_name)) # create a batch of instances in subnet! kwargs["MinCount"] = kwargs["MaxCount"] = count instances = self._create_instances(subnet, **kwargs) # accumulate all new instances into a single list. role_instances += instances # add role tag to each instance. for instance in role_instances: update_tags(instance, role=role_name)
def instance_role(self, role_name, role_data, desired_count): if role_data.get('autoscaling', False) == True: # exit early if this instance_role is autoscaling. return [] self.log.emit('creating role: {}'.format(role_name)) ami = self.amis[role_data['ami']][self.evpc.region_name] key_pair = self.evpc.key_pair.get_key_pair( role_data.get('key_pair', 'default') ) security_groups = map( self.evpc.get_security_group, role_data.get('security_groups', []) ) subnets = map( self.evpc.get_subnet, role_data.get('subnets', []) ) if len(subnets) == 0: self.log.emit( 'no subnets found for role: {}'.format(role_name), 'warning' ) # exit early. return None # sort by subnets by amount of instances, smallest first. subnets = sorted( subnets, key = lambda sn : collection_len(sn.instances), ) # determine the count of this role's existing instances. # Note: we look for role in all subnets, not just the listed subnets. existing_count = len(self.evpc.get_role(role_name)) if existing_count >= desired_count: # for now we exit early, maybe terminate extras... self.log.emit('{} {}'.format(existing_count, desired_count), 'debug') return None # determine count of additional instances needed to reach desired_count. needed_count = desired_count - existing_count needed_per_subnet = desired_count / len(subnets) needed_remainder = desired_count % len(subnets) block_device_map = get_block_device_map_from_role_config(role_data) role_instances = [] kwargs = { 'ImageId' : ami, 'InstanceType' : role_data.get('instance_type'), 'KeyName' : key_pair.name, 'SecurityGroupIds' : get_ids(security_groups), 'BlockDeviceMappings' : block_device_map, 'UserData' : role_data.get('userdata', ''), 'IamInstanceProfile' : {}, } profile_name = role_data.get('instance_profile_name', None) if profile_name: kwargs['IamInstanceProfile'] = { 'Name' : profile_name } for subnet in subnets: # ensure Run_Instance_Idempotency.html#client-tokens kwargs['ClientToken'] = str(uuid4()) # figure out how many instances this subnet needs to create ... existing_in_subnet = len(self.evpc.get_role(role_name, subnet.instances.all())) count = needed_per_subnet - existing_in_subnet if needed_remainder != 0: needed_remainder -= 1 count += 1 if count == 0: # skip this subnet, it doesn't need to launch any instances. continue subnet_name = make_tag_dict(subnet)['Name'] msg = '{} instances of role {} launching into {} subnet' self.log.emit(msg.format(count, role_name, subnet_name)) # create a batch of instances in subnet! kwargs['MinCount'] = kwargs['MaxCount'] = count instances = self._create_instances(subnet, **kwargs) # accumulate all new instances into a single list. role_instances += instances # add role tag to each instance. for instance in role_instances: update_tags(instance, role = role_name)