labpack/platforms/aws/ec2.py
def cleanup(self):

        '''
            a method for removing instances and images in unusual states

        :return: True
        '''

    # find non-running instances
        self.iam.printer('Cleaning up AWS region %s.' % self.iam.region_name)
        response = self.connection.describe_instances()
        instance_list = response['Reservations']
        for instance in instance_list:
            instance_info = instance['Instances'][0]
            if instance_info['State']['Name'] == 'pending':
                self.check_instance_state(instance_info['InstanceId'])
                response = self.connection.describe_instances(
                    InstanceIds=[ instance_info['InstanceId'] ]
                )
                instance_info = response['Reservations'][0]['Instances'][0]
            if instance_info['State']['Name'] != 'running':

    # try to remove tags associated with non-running instance
                try:
                    response = self.connection.describe_tags(
                        Filters=[ { 'Name': 'resource-id', 'Values': [ instance_info['InstanceId'] ] } ]
                    )
                    tag_list = []
                    import re
                    aws_tag_pattern = re.compile('aws:')
                    for i in range(0, len(response['Tags'])):
                        if not aws_tag_pattern.findall(response['Tags'][i]['Key']):
                            tag = {}
                            tag['Key'] = response['Tags'][i]['Key']
                            tag['Value'] = response['Tags'][i]['Value']
                            tag_list.append(tag)
                    self.connection.delete_tags(
                        Resources=[ instance_info['InstanceId'] ],
                        Tags=tag_list
                    )
                    self.iam.printer('Tags have been deleted from instance %s.' % instance_info['InstanceId'])
                except:
                    pass

    # try stopping non-running instance
                try:
                    self.connection.stop_instances(
                        InstanceIds=[ instance_info['InstanceId'] ]
                    )
                    self.iam.printer('Instance %s is stopping.' % instance_info['InstanceId'])
                except:
                    pass

    # try terminating non-running instance
                try:
                    self.connection.terminate_instances(
                        InstanceIds=[ instance_info['InstanceId'] ]
                    )
                    self.iam.printer('Instance %s is terminating.' % instance_info['InstanceId'])
                except:
                    pass

    # find non-available images
        response = self.connection.describe_images(
            Owners=[ self.iam.owner_id ]
        )
        image_list = response['Images']
        for image in image_list:
            image_info = image.copy()
            if image_info['State'] == 'pending':
                self.check_image_state(image_info['ImageId'])
                response = self.connection.describe_images(
                    ImageIds=[ image_info['ImageId'] ]
                )
                image_info = response['Images'][0]
            if image_info['State'] != 'available':

    # try removing tags from non-available images
                try:
                    response = self.connection.describe_images(
                        ImageIds=[ image_info['ImageId'] ]
                    )
                    image_tags = response['Images'][0]['Tags']
                    tag_list = []
                    for i in range(0, len(image_tags)):
                        tag = {}
                        tag['Key'] = image_tags[i]['Key']
                        tag['Value'] = image_tags[i]['Value']
                        tag_list.append(tag)
                    self.connection.delete_tags(
                        Resources=[ image_info['ImageId'] ],
                        Tags=tag_list
                    )
                    self.iam.printer('Tags have been deleted from image %s.' % image_info['ImageId'])
                except:
                    pass

    # try deregistering non-available image
                try:
                    self.connection.deregister_image(
                        ImageId=image_info['ImageId']
                    )
                    self.iam.printer('Image %s has been deregistered.' % image_info['ImageId'])
                except:
                    pass

    # try deleting snapshot associated with non-available image
                try:
                    self.connection.delete_snapshot(
                        SnapshotId=image_info['BlockDeviceMappings'][0]['Ebs']['SnapshotId']
                    )
                    snap_id = image_info['BlockDeviceMappings'][0]['Ebs']['SnapshotId']
                    image_id = image_info['ImageId']
                    self.iam.printer('Snapshot %s associated with image %s has been deleted.' % (snap_id, image_id))
                except:
                    pass

    # find snapshots with errors
        response = self.connection.describe_snapshots(
            OwnerIds=[ self.iam.owner_id ]
        )
        snapshot_list = response['Snapshots']
        for snapshot in snapshot_list:
            snapshot_info = snapshot.copy()
            if snapshot_info['State'] == 'error':

    # try deleting snapshot with errors
                try:
                    self.connection.delete_snapshot(
                        SnapshotId=snapshot_info['SnapshotId']
                    )
                    self.iam.printer('Snapshot %s has been deleted.' % snapshot_info['SnapshotId'])
                except:
                    pass

        return True

            a method for removing instances and images in unusual states

        :return: True
        
a method for removing instances and images in unusual states
a method for removing instances and images in unusual states
def cleanup(self):
        self.iam.printer('Cleaning up AWS region %s.' % self.iam.region_name)
        response = self.connection.describe_instances()
        instance_list = response['Reservations']
        for instance in instance_list:
            instance_info = instance['Instances'][0]
            if instance_info['State']['Name'] == 'pending':
                self.check_instance_state(instance_info['InstanceId'])
                response = self.connection.describe_instances(
                    InstanceIds=[ instance_info['InstanceId'] ]
                )
                instance_info = response['Reservations'][0]['Instances'][0]
            if instance_info['State']['Name'] != 'running':
                try:
                    response = self.connection.describe_tags(
                        Filters=[ { 'Name': 'resource-id', 'Values': [ instance_info['InstanceId'] ] } ]
                    )
                    tag_list = []
                    import re
                    aws_tag_pattern = re.compile('aws:')
                    for i in range(0, len(response['Tags'])):
                        if not aws_tag_pattern.findall(response['Tags'][i]['Key']):
                            tag = {}
                            tag['Key'] = response['Tags'][i]['Key']
                            tag['Value'] = response['Tags'][i]['Value']
                            tag_list.append(tag)
                    self.connection.delete_tags(
                        Resources=[ instance_info['InstanceId'] ],
                        Tags=tag_list
                    )
                    self.iam.printer('Tags have been deleted from instance %s.' % instance_info['InstanceId'])
                except:
                    pass
                try:
                    self.connection.stop_instances(
                        InstanceIds=[ instance_info['InstanceId'] ]
                    )
                    self.iam.printer('Instance %s is stopping.' % instance_info['InstanceId'])
                except:
                    pass
                try:
                    self.connection.terminate_instances(
                        InstanceIds=[ instance_info['InstanceId'] ]
                    )
                    self.iam.printer('Instance %s is terminating.' % instance_info['InstanceId'])
                except:
                    pass
        response = self.connection.describe_images(
            Owners=[ self.iam.owner_id ]
        )
        image_list = response['Images']
        for image in image_list:
            image_info = image.copy()
            if image_info['State'] == 'pending':
                self.check_image_state(image_info['ImageId'])
                response = self.connection.describe_images(
                    ImageIds=[ image_info['ImageId'] ]
                )
                image_info = response['Images'][0]
            if image_info['State'] != 'available':
                try:
                    response = self.connection.describe_images(
                        ImageIds=[ image_info['ImageId'] ]
                    )
                    image_tags = response['Images'][0]['Tags']
                    tag_list = []
                    for i in range(0, len(image_tags)):
                        tag = {}
                        tag['Key'] = image_tags[i]['Key']
                        tag['Value'] = image_tags[i]['Value']
                        tag_list.append(tag)
                    self.connection.delete_tags(
                        Resources=[ image_info['ImageId'] ],
                        Tags=tag_list
                    )
                    self.iam.printer('Tags have been deleted from image %s.' % image_info['ImageId'])
                except:
                    pass
                try:
                    self.connection.deregister_image(
                        ImageId=image_info['ImageId']
                    )
                    self.iam.printer('Image %s has been deregistered.' % image_info['ImageId'])
                except:
                    pass
                try:
                    self.connection.delete_snapshot(
                        SnapshotId=image_info['BlockDeviceMappings'][0]['Ebs']['SnapshotId']
                    )
                    snap_id = image_info['BlockDeviceMappings'][0]['Ebs']['SnapshotId']
                    image_id = image_info['ImageId']
                    self.iam.printer('Snapshot %s associated with image %s has been deleted.' % (snap_id, image_id))
                except:
                    pass
        response = self.connection.describe_snapshots(
            OwnerIds=[ self.iam.owner_id ]
        )
        snapshot_list = response['Snapshots']
        for snapshot in snapshot_list:
            snapshot_info = snapshot.copy()
            if snapshot_info['State'] == 'error':
                try:
                    self.connection.delete_snapshot(
                        SnapshotId=snapshot_info['SnapshotId']
                    )
                    self.iam.printer('Snapshot %s has been deleted.' % snapshot_info['SnapshotId'])
                except:
                    pass
        return True
def cleanup ( self ) : self . iam . printer ( 'Cleaning up AWS region %s.' % self . iam . region_name ) response = self . connection . describe_instances ( ) instance_list = response [ 'Reservations' ] for instance in instance_list : instance_info = instance [ 'Instances' ] [ 0 ] if instance_info [ 'State' ] [ 'Name' ] == 'pending' : self . check_instance_state ( instance_info [ 'InstanceId' ] ) response = self . connection . describe_instances ( InstanceIds = [ instance_info [ 'InstanceId' ] ] ) instance_info = response [ 'Reservations' ] [ 0 ] [ 'Instances' ] [ 0 ] if instance_info [ 'State' ] [ 'Name' ] != 'running' : try : response = self . connection . describe_tags ( Filters = [ { 'Name' : 'resource-id' , 'Values' : [ instance_info [ 'InstanceId' ] ] } ] ) tag_list = [ ] import re aws_tag_pattern = re . compile ( 'aws:' ) for i in range ( 0 , len ( response [ 'Tags' ] ) ) : if not aws_tag_pattern . findall ( response [ 'Tags' ] [ i ] [ 'Key' ] ) : tag = { } tag [ 'Key' ] = response [ 'Tags' ] [ i ] [ 'Key' ] tag [ 'Value' ] = response [ 'Tags' ] [ i ] [ 'Value' ] tag_list . append ( tag ) self . connection . delete_tags ( Resources = [ instance_info [ 'InstanceId' ] ] , Tags = tag_list ) self . iam . printer ( 'Tags have been deleted from instance %s.' % instance_info [ 'InstanceId' ] ) except : pass try : self . connection . stop_instances ( InstanceIds = [ instance_info [ 'InstanceId' ] ] ) self . iam . printer ( 'Instance %s is stopping.' % instance_info [ 'InstanceId' ] ) except : pass try : self . connection . terminate_instances ( InstanceIds = [ instance_info [ 'InstanceId' ] ] ) self . iam . printer ( 'Instance %s is terminating.' % instance_info [ 'InstanceId' ] ) except : pass response = self . connection . describe_images ( Owners = [ self . iam . owner_id ] ) image_list = response [ 'Images' ] for image in image_list : image_info = image . copy ( ) if image_info [ 'State' ] == 'pending' : self . check_image_state ( image_info [ 'ImageId' ] ) response = self . connection . describe_images ( ImageIds = [ image_info [ 'ImageId' ] ] ) image_info = response [ 'Images' ] [ 0 ] if image_info [ 'State' ] != 'available' : try : response = self . connection . describe_images ( ImageIds = [ image_info [ 'ImageId' ] ] ) image_tags = response [ 'Images' ] [ 0 ] [ 'Tags' ] tag_list = [ ] for i in range ( 0 , len ( image_tags ) ) : tag = { } tag [ 'Key' ] = image_tags [ i ] [ 'Key' ] tag [ 'Value' ] = image_tags [ i ] [ 'Value' ] tag_list . append ( tag ) self . connection . delete_tags ( Resources = [ image_info [ 'ImageId' ] ] , Tags = tag_list ) self . iam . printer ( 'Tags have been deleted from image %s.' % image_info [ 'ImageId' ] ) except : pass try : self . connection . deregister_image ( ImageId = image_info [ 'ImageId' ] ) self . iam . printer ( 'Image %s has been deregistered.' % image_info [ 'ImageId' ] ) except : pass try : self . connection . delete_snapshot ( SnapshotId = image_info [ 'BlockDeviceMappings' ] [ 0 ] [ 'Ebs' ] [ 'SnapshotId' ] ) snap_id = image_info [ 'BlockDeviceMappings' ] [ 0 ] [ 'Ebs' ] [ 'SnapshotId' ] image_id = image_info [ 'ImageId' ] self . iam . printer ( 'Snapshot %s associated with image %s has been deleted.' % ( snap_id , image_id ) ) except : pass response = self . connection . describe_snapshots ( OwnerIds = [ self . iam . owner_id ] ) snapshot_list = response [ 'Snapshots' ] for snapshot in snapshot_list : snapshot_info = snapshot . copy ( ) if snapshot_info [ 'State' ] == 'error' : try : self . connection . delete_snapshot ( SnapshotId = snapshot_info [ 'SnapshotId' ] ) self . iam . printer ( 'Snapshot %s has been deleted.' % snapshot_info [ 'SnapshotId' ] ) except : pass return True
a method for removing instances and images in unusual states
a method for removing instances and images in unusual states
'''
            a method for removing instances and images in unusual states

        :return: True
        ''' # find non-running instances # try to remove tags associated with non-running instance # try stopping non-running instance # try terminating non-running instance # find non-available images # try removing tags from non-available images # try deregistering non-available image # try deleting snapshot associated with non-available image # find snapshots with errors # try deleting snapshot with errors
None
self