def instances(self) -> None: for region in self.regions: ec2 = boto3.resource('ec2', region_name=region) instances = [ instance for instance in ec2.instances.filter(Filters=self.filters) ] if len(instances): print(yel(region)) for i in instances: subnet = i.subnet if self.params.terminate and not self.have_tag_owner( i.tags) and i.state['Code'] != 48: i.terminate() try: state_name = self.state_name(i) print( f"{i.id} {i.instance_type} {state_name}{'':5} {alert(self.have_tag_owner(i.tags)):25} {i.launch_time:%Y-%m-%d} {subnet.vpc_id} {subnet.id}" ) except Exception as e: print( f"{i.id} {i.instance_type} {state_name} {'':3}{alert(self.have_tag_owner(i.tags)):25}" )
def autoscaling(self) -> None: """Print all autoscaling group in all regions :returns: list """ for region in self.regions: client = boto3.client('autoscaling', region_name=region) groups = client.describe_auto_scaling_groups() autoscaling = groups['AutoScalingGroups'] if autoscaling: print(yel(region)) for group in autoscaling: groupname = group['AutoScalingGroupName'] print( f"{group['MinSize']}/{red(group['MaxSize'], group['MaxSize'])} {gre(groupname)} {group['LoadBalancerNames']} {group['AvailabilityZones']} {group['Tags']}" ) stop = self.have_tag(group['Tags'], 'stop') if self.params.delete and not self.have_tag( group['Tags'], 'owner'): log.info(f"Terminate {groupname}") response = client.delete_auto_scaling_group( AutoScalingGroupName=groupname, ForceDelete=True) print(response) if not stop: pass
def summary(i, region_name: str = ''): try: print() print("Id: {0}\tRegion: {1}\tState: {2}\tLaunched: {3}".format( cyan(i.id), yel(region_name), gre(i.state['Name']), cyan(i.launch_time), )) print("\tArch: {0}\tHypervisor: {1}\tRoot Device Name: {2}".format( cyan(i.architecture), cyan(i.hypervisor), cyan(i.root_device_name))) print("\tPriv. IP: {0}\tPub. IP: {1}".format(red(i.private_ip_address), gre(i.public_ip_address))) print("\tPriv. DNS: {0}\tPub. DNS: {1}".format(red(i.private_dns_name), gre(i.public_dns_name))) print("\tSubnet: {0}\tSubnet Id: {1}".format(cyan(i.subnet), cyan(i.subnet_id))) print("\tKernel: {0}\tInstance Type: {1}".format( cyan(i.kernel_id), cyan(i.instance_type))) print( "\tRAM Disk Id: {0}\tAMI Id: {1}\tPlatform: {2}\t EBS Optimized: {3}" .format(cyan(i.ramdisk_id), cyan(i.image_id), cyan(i.platform), cyan(i.ebs_optimized))) print("\tBlock Device Mappings:") for idx, dev in enumerate(i.block_device_mappings, start=1): print( "\t- [{0}] Device Name: {1}\tVol Id: {2}\tStatus: {3}\tDeleteOnTermination: {4}\tAttachTime: {5}" .format(idx, cyan(dev['DeviceName']), cyan(dev['Ebs']['VolumeId']), cyan(dev['Ebs']['Status']), cyan(dev['Ebs']['DeleteOnTermination']), cyan(dev['Ebs']['AttachTime']))) print("\tTags:") for tag in i.tags: print("\t- {0}: {1}".format(cyan(tag['Key']), cyan(tag['Value']))) print("\tProduct codes:") for idx, details in enumerate(i.product_codes, start=1): print("\t - [{0}] Id: {1}\tType: {2}".format( idx, cyan(details['ProductCodeId']), cyan(details['ProductCodeType']))) print("Console Output:") # Commented out because this creates a lot of clutter.. # print(i.console_output()['Output']) except Exception as e: print(e) print()
def state_name(self, i): state = i.state['Name'] if i.state['Code'] == 16: # not terminated state = yel(state) if i.state['Code'] != 48: # not terminated state += str(difference(i.launch_time)) return state
def ebs(self) -> None: for region in self.regions: ec2 = boto3.resource('ec2', region_name=region) volumes = [v for v in ec2.volumes.filter(Filters=self.filters) ] # if you want to list out only attached volumes if volumes: print(yel(region)) for v in volumes: if self.params.delete and not self.have_tag_owner(v.tags): v.delete() print( f"{v.id} {v.size} {v.state} {alert(self.have_tag_owner(v.tags))}" )
def summary(response, region_name='eu-west-1'): code = response['Code'] r = response['ResponseMetadata'] c = response['Configuration'] try: print("\nFunctionName: {0}\n \tRegoin: {1}\tDesc: {2}".format( yel(c['FunctionName']), yel(region_name), c['Description'], )) print("\tRuntime: {0}\tRole: {1}".format(gre(c['Runtime']), cyan(c['Role']))) print("\tLastModified: {0}".format(yel(c['LastModified']))) print( "\tHandler: {0}\t \tCodeSize: {1}\tTimeout: {2}\t MemorySize: {3}\n\tFunctionArn: {4}" .format(cyan(c['Handler']), gre(c['CodeSize']), red(c['Timeout']), red(c['MemorySize']), cyan(c['FunctionArn']))) except Exception as e: print(e) try: print("\tTags:") tags = response['Tags'] for i in tags: print("\t\t{0}: {1}".format( cyan(i), gre(tags[i]), )) except KeyError as e: print(e) print("\tLastModified: {0}\t \tVersion: {1}\t CodeSha256: {2}".format( cyan(c['LastModified']), gre(c['Version']), cyan(c['CodeSha256']), )) print( "\tResponseMetadata:\n \t\tHTTPStatusCode: {0}\tRetryAttempts: {1}\tRequestId: {2}" .format( red(r['HTTPStatusCode']), cyan(r['RetryAttempts']), cyan(r['RequestId']), )) print( "\t\tHTTPHeaders:\n \t\t\tdate: {0}\n\t\t\tcontent-type: {1}\tcontent-length: {2}\tconnection: {3}\n\t\t\tx-amz-requestid: {4}" .format( cyan(r['HTTPHeaders']['date']), cyan(r['HTTPHeaders']['content-type']), cyan(r['HTTPHeaders']['content-length']), cyan(r['HTTPHeaders']['connection']), cyan(r['HTTPHeaders']['x-amzn-requestid']), )) try: print("\tVPC:\n \t\tSubnetIds: {0}\tSecurityGroudIds: {1}\tVpcId: {2}". format( cyan(c['VpcConfig']['SubnetIds']), cyan(c['VpcConfig']['SecurityGroupIds']), cyan(c['VpcConfig']['VpcId']), )) except Exception as e: pass try: print("\tEnvironment Variables: {0}".format( cyan(c['Environment']['Variables']), )) print("\tTracingConfig: {0}\t RevisionId: {1}".format( cyan(c['TracingConfig']), cyan(c['RevisionId']), )) except Exception as e: pass try: print("\tRepositoryType: {0}\n\tLocation: {1}".format( cyan(code['RepositoryType']), grey(code['Location']))) except Exception as e: pass