def check_modification_state(instance_id):
    client = boto3.client('ec2')
    volume = client.describe_instances(
        InstanceIds=[
            instance_id
        ]
    )
    volume_id = volume['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['VolumeId']
    response = client.describe_volumes_modifications(
        VolumeIds=[
            volume_id
        ]
    )
    while True:
        time.sleep(45)
        logger.info(f'Volume state...\n{json.dumps(response["VolumesModifications"][0]["ModificationState"], default=json_datetime_serializer, indent=4)}')
        response = client.describe_volumes_modifications(
            VolumeIds=[
                volume_id
            ]
        )
        volume_modification_state = response['VolumesModifications'][0]['ModificationState']
        
        if volume_modification_state == 'optimizing':
            logger.info('Volume is being optimized...')
        elif volume_modification_state == 'completed':
            logger.info('Volume has been optimized...')
            break
        else:
            logger.info('Volume is being modified...')
示例#2
0
def describe_volume(instance_id):
    client = boto3.client('ec2')
    volume_id = client.describe_instances(InstanceIds=[
        instance_id,
    ])
    response = client.describe_volumes(VolumeIds=[
        volume_id['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]
        ['Ebs']['VolumeId']
    ])
    return response['Volumes'][0]
def get_instance_by_eni(eni):
    client = boto3.client('ec2')
    reservations = client.describe_instances(Filters=[{
        'Name': 'network-interface.network-interface-id',
        'Values': [eni]
    }])
    del client
    for reservation in reservations['Reservations']:
        for instance in reservation['Instances']:
            return instance
def modify_volumes(instance_id):
    client = boto3.client('ec2')
    volume = client.describe_instances(
        InstanceIds=[
            instance_id
        ]
    )
    volume_id = volume['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['VolumeId']
    response = client.modify_volume(
        VolumeId=volume_id,
        Size=40
    )
    logger.info(f'Volume description...\n{json.dumps(response["VolumeModification"], default=json_datetime_serializer, indent=4)}')
    return instance_id
示例#5
0
def get_modification_state(instance_id):
    client = boto3.client('ec2')
    volume = client.describe_instances(
        InstanceIds=[
            instance_id,
        ]
    )
    volume_id = volume['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['VolumeId']
    response = boto3.client('ec2').describe_volumes_modifications(
        VolumeIds=[
            volume_id
        ]
    )
    return response['VolumesModifications'][0]['ModificationState']
示例#6
0
def describe_volume(instance_id):
    client = boto3.client('ec2')
    volume_id = client.describe_instances(
        InstanceIds=[
            instance_id,
        ]
    )
    response = client.describe_volumes(
        VolumeIds=[
            volume_id['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['VolumeId']
        ]
    )
    logger.info(f'Volume description...\n{json.dumps(response["Volumes"][0], default=json_datetime_serializer, indent=4)}')
    return response['Volumes'][0]
def instance_health(instanceId):
    client = boto3.client('ec2')
    response = client.describe_instances(InstanceIds=[instanceId])
    print("checking health for instance {}".format(instanceId))
    try:
        conn = http.client.HTTPConnection(
            response['Reservations'][0]['Instances'][0]['PrivateIpAddress'],
            8080)
        conn.request("GET", "/v1/info")
        r1 = conn.getresponse()
        print(r1.status, r1.reason)
        data = json.loads(r1.read().decode('utf-8').replace("'", '"'))
    except Exception as e:
        print("AN EXCEPTION OCCURED", str(e))
        data = {"starting": True}
    return data
def attach_eni(eni_id):
    client = boto3.client('ec2')
    response = client.describe_instances(
        Filters=[{
            'Name': 'tag:presto:opensource:identification:role',
            'Values': ['presto:coordinator']
        }, {
            'Name': 'tag:aws:cloudformation:stack-name',
            'Values': [os.environ['STACK_NAME']]
        }])
    for j in range(len(response['Reservations'])):
        for i in range(len(response['Reservations'][j]['Instances'])):
            if response['Reservations'][j]['Instances'][i]['State'][
                    'Name'] != 'running':
                continue
            privateIpAddress = response['Reservations'][j]['Instances'][i][
                'PrivateIpAddress']
            instanceId = response['Reservations'][j]['Instances'][i][
                'InstanceId']
            print("Found instance to attach {}, {}".format(
                privateIpAddress, instanceId))
            try:
                conn = http.client.HTTPConnection(privateIpAddress, 8080)
                conn.request("GET", "/v1/info")
                r1 = conn.getresponse()
                print(privateIpAddress, r1.status, r1.reason)
                data = json.loads(r1.read().decode('utf-8').replace("'", '"'))
            except Exception as e:
                print("AN EXCEPTION OCCURED", str(e))
                data = {"starting": True}
            if not data['starting']:
                print("Instance {} is healthy | Attaching ENI to Instance".
                      format(instanceId))
                print(attach_eni_instance(instanceId, eni_id))
                break
            else:
                print(instanceId + "Instance is unhealthy ...")
def get_instances_info(id_list):
    client = boto3.client('ec2')
    instances = client.describe_instances(InstanceIds=id_list)
    del client
    return instances
示例#10
0
    volume = client.describe_instances(
        InstanceIds=[
            instance_id,
        ]
    )
    volume_id = volume['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['VolumeId']
    response = boto3.client('ec2').describe_volumes_modifications(
        VolumeIds=[
            volume_id
        ]
    )
    return response['VolumesModifications'][0]['ModificationState']

volume = client.describe_instances(
        InstanceIds=[
            instance_id,
        ]
    )
volume_id = volume['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['VolumeId']
describe_volume(instance_id)
modify_volume_response = client.modify_volume(
    VolumeId=volume_id,
    Size=55
)

while True:
    state = get_modification_state(volume_id)
    if state == 'completed' or state == None:
        describe_volume(instance_id)
        break
    elif state == 'failed':