import base64 import datetime import hmac import json import pytz import os import time import boto3 import requests instanceIDs = [] def dig_instances(): for region in ["us-west-2", "us-east-1", "ap-northeast-1", "ap-northeast-2", "ap-southeast-1", "eu-central-1"]: ec2Client = boto3.client('ec2', region_name=region) response = ec2Client.describe_instances() for reservation in response['Reservations']: for instance in reservation['Instances']: instanceType = instance['InstanceType'] instanceId = instance['InstanceId'] launchTime = instance['LaunchTime'] instanceName = None component = '' tenant = '' if 'Tags' not in instance: continue tags = instance['Tags'] for tag in tags: if tag['Key'] == 'Name': instanceName = tag['Value'] if tag['Key'] == 'component': component = tag['Value'] if tag['Key'] == 'tenant': tenant = tag['Value'] if instanceName is None: continue if datetime.datetime.utcnow().replace(tzinfo=pytz.UTC) > launchTime + datetime.timedelta(hours=8): if instanceType == 'c5.9xlarge' and 'shoot' in instanceName and component not in ("tikv", "cdc"): instanceIDs.append( { "region": region, "instanceId": instanceId, 'component': component, 'tenant': tenant } ) return instanceIDs