def main(args, evpc=None): """ For every AWS profile + region, dump every VPC to STDOUT. :param args: The parsed arguments and flags from the CLI. :returns: None """ sessions = get_all_sessions() # We assume all sessions have the same list of regions. # This might not always be true. This is an optimization. regions = get_region_names(sessions[0]) vpcs = {} for session in sessions: boto3.setup_default_session(profile_name=session.profile) vpcs[session.profile] = {} for region_name in regions: if region_name not in vpcs[session.profile]: vpcs[session.profile][region_name] = {} ec2 = boto3.resource('ec2', region_name=region_name) for vpc in ec2.vpcs.all(): vpc_tags = make_tag_dict(vpc) vpc_name = vpc_tags.get('Name', vpc.id) vpcs[session.profile][region_name][vpc_name] = vpc.id print(output_formatter(vpcs, args.output_format))
def test_make_tag_dict(): class TestSubject(object): tags = [ {'Key':'Name', 'Value':'myapp01-web01'}, {'Key':'role', 'Value':'web'}, ] obj = TestSubject tags = make_tag_dict(obj) assert('Name' in tags) assert('role' in tags) assert(tags['Name'] == 'myapp01-web01') assert(tags['role'] == 'web')
def main(args, evpc=None): """ List all VPC names to STDOUT for a given AWS profile + region. :param args: The parsed arguments and flags from the CLI. :returns: None """ vpc_names = [] bconn = BotoConnections(args.region, args.profile) for vpc in bconn.ec2.vpcs.all(): vpc_tags = make_tag_dict(vpc) vpc_name = vpc_tags.get("Name", vpc.id) vpc_names.append(vpc_name) print(output_formatter(vpc_names, args.output_format))
def test_make_tag_dict(): class TestSubject(object): tags = [ { "Key": "Name", "Value": "myapp01-web01" }, { "Key": "role", "Value": "web" }, ] obj = TestSubject tags = make_tag_dict(obj) assert "Name" in tags assert "role" in tags assert tags["Name"] == "myapp01-web01" assert tags["role"] == "web"
def test_make_tag_dict(): class TestSubject(object): tags = [ { 'Key': 'Name', 'Value': 'myapp01-web01' }, { 'Key': 'role', 'Value': 'web' }, ] obj = TestSubject tags = make_tag_dict(obj) assert ('Name' in tags) assert ('role' in tags) assert (tags['Name'] == 'myapp01-web01') assert (tags['role'] == 'web')
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 tag_dict(self): return make_tag_dict(self.vpc)
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 tag_dict(self): """Return dictionary of tags.""" return make_tag_dict(self.ec2_object)
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 tag_dict(self): """Return dictionary of tags.""" return make_tag_dict(self.instance)
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)