Пример #1
0
 def has_snapshot(self, snapshot_id):
     """
     Checks to see if the given snapshot is available in the account
     """
     ec2 = self.connect_ec2()
     try:
         ec2.get_all_snapshots(snapshot_id)
         return True, ''
     except boto.exception.EC2ResponseError:
         return False, 'The snapshot id \'{0}\' does not exist in this ' \
                       'account.'.format(snapshot_id)
Пример #2
0
 def has_snapshot(self, snapshot_id):
     """
     Checks to see if the given snapshot is available in the account
     """
     ec2 = self.connect_ec2()
     try:
         ec2.get_all_snapshots(snapshot_id)
         return True, ''
     except boto.exception.EC2ResponseError:
         return False, 'The snapshot id \'{0}\' does not exist in this ' \
                       'account.'.format(snapshot_id)
Пример #3
0
def create_block_device(module, ec2, volume):
    # Not aware of a way to determine this programatically
    # http://aws.amazon.com/about-aws/whats-new/2013/10/09/ebs-provisioned-iops-maximum-iops-gb-ratio-increased-to-30-1/
    MAX_IOPS_TO_SIZE_RATIO = 30
    if 'snapshot' not in volume and 'ephemeral' not in volume:
        if 'volume_size' not in volume:
            module.fail_json(msg = 'Size must be specified when creating a new volume or modifying the root volume')
    if 'snapshot' in volume:
        if 'device_type' in volume and volume.get('device_type') == 'io1' and 'iops' not in volume:
            module.fail_json(msg = 'io1 volumes must have an iops value set')
        if 'iops' in volume:
            snapshot = ec2.get_all_snapshots(snapshot_ids=[volume['snapshot']])[0]
            size = volume.get('volume_size', snapshot.volume_size)
            if int(volume['iops']) > MAX_IOPS_TO_SIZE_RATIO * size:
                module.fail_json(msg = 'IOPS must be at most %d times greater than size' % MAX_IOPS_TO_SIZE_RATIO)
        if 'encrypted' in volume:
            module.fail_json(msg = 'You can not set encyrption when creating a volume from a snapshot')
    if 'ephemeral' in volume:
        if 'snapshot' in volume:
            module.fail_json(msg = 'Cannot set both ephemeral and snapshot')
    return BlockDeviceType(snapshot_id=volume.get('snapshot'),
                           ephemeral_name=volume.get('ephemeral'),
                           size=volume.get('volume_size'),
                           volume_type=volume.get('device_type'),
                           delete_on_termination=volume.get('delete_on_termination', False),
                           iops=volume.get('iops'),
                           encrypted=volume.get('encrypted', None))
Пример #4
0
def create_block_device(module, ec2, volume):
    # Not aware of a way to determine this programatically
    # http://aws.amazon.com/about-aws/whats-new/2013/10/09/ebs-provisioned-iops-maximum-iops-gb-ratio-increased-to-30-1/
    MAX_IOPS_TO_SIZE_RATIO = 30
    if "snapshot" not in volume and "ephemeral" not in volume:
        if "volume_size" not in volume:
            module.fail_json(msg="Size must be specified when creating a new volume or modifying the root volume")
    if "snapshot" in volume:
        if "device_type" in volume and volume.get("device_type") == "io1" and "iops" not in volume:
            module.fail_json(msg="io1 volumes must have an iops value set")
        if "iops" in volume:
            snapshot = ec2.get_all_snapshots(snapshot_ids=[volume["snapshot"]])[0]
            size = volume.get("volume_size", snapshot.volume_size)
            if int(volume["iops"]) > MAX_IOPS_TO_SIZE_RATIO * size:
                module.fail_json(msg="IOPS must be at most %d times greater than size" % MAX_IOPS_TO_SIZE_RATIO)
        if "encrypted" in volume:
            module.fail_json(msg="You can not set encyrption when creating a volume from a snapshot")
    if "ephemeral" in volume:
        if "snapshot" in volume:
            module.fail_json(msg="Cannot set both ephemeral and snapshot")
    return BlockDeviceType(
        snapshot_id=volume.get("snapshot"),
        ephemeral_name=volume.get("ephemeral"),
        size=volume.get("volume_size"),
        volume_type=volume.get("device_type"),
        delete_on_termination=volume.get("delete_on_termination", False),
        iops=volume.get("iops"),
        encrypted=volume.get("encrypted", None),
    )
Пример #5
0
def create_block_device(module, ec2, volume):
    # Not aware of a way to determine this programatically
    # http://aws.amazon.com/about-aws/whats-new/2013/10/09/ebs-provisioned-iops-maximum-iops-gb-ratio-increased-to-30-1/
    MAX_IOPS_TO_SIZE_RATIO = 30
    if 'snapshot' not in volume and 'ephemeral' not in volume:
        if 'volume_size' not in volume:
            module.fail_json(msg = 'Size must be specified when creating a new volume or modifying the root volume')
    if 'snapshot' in volume:
        if 'device_type' in volume and volume.get('device_type') == 'io1' and 'iops' not in volume:
            module.fail_json(msg = 'io1 volumes must have an iops value set')
        if 'iops' in volume:
            snapshot = ec2.get_all_snapshots(snapshot_ids=[volume['snapshot']])[0]
            size = volume.get('volume_size', snapshot.volume_size)
            if int(volume['iops']) > MAX_IOPS_TO_SIZE_RATIO * size:
                module.fail_json(msg = 'IOPS must be at most %d times greater than size' % MAX_IOPS_TO_SIZE_RATIO)
        if 'encrypted' in volume:
            module.fail_json(msg = 'You can not set encyrption when creating a volume from a snapshot')
    if 'ephemeral' in volume:
        if 'snapshot' in volume:
            module.fail_json(msg = 'Cannot set both ephemeral and snapshot')
    return BlockDeviceType(snapshot_id=volume.get('snapshot'),
                           ephemeral_name=volume.get('ephemeral'),
                           size=volume.get('volume_size'),
                           volume_type=volume.get('device_type'),
                           delete_on_termination=volume.get('delete_on_termination', False),
                           iops=volume.get('iops'),
                           encrypted=volume.get('encrypted', None))
Пример #6
0
def get_snapshots():
    snaps = ec2.get_all_snapshots(owner='self')
    snap_costs['count'] = len(snaps)
    for snap in snaps:
        snap_cost = round(
            snap.volume_size * EBS_PRICING[region]['snapshot_gb'], 2)
        snap_costs['total'] += snap_cost
Пример #7
0
def process_region(region):
    print('Processing region %s' % region.name)

    ec2 = region.connect()

    print('Scanning instances...\n')

    instances = ec2.get_only_instances()
    for inst in instances:
        inst_accessor = Instance(inst)
        process_instance(inst_accessor)

    print('\nScanning images...\n')

    images = ec2.get_all_images(None, ('self'))
    for image in images:
        image_accessor = Image(image)
        process_image(image_accessor)

    print('\nScanning volumes...\n')

    volumes = ec2.get_all_volumes()
    for volume in volumes:
        volume_accessor = Volume(volume)
        process_volume(volume_accessor)

    print('\nScanning snapshots...\n')

    snapshots = ec2.get_all_snapshots(owner='self')
    for snapshot in snapshots:
        process_snapshot(Snapshot(snapshot))
Пример #8
0
def process_region(region):
    print "Processing region %s" % region.name

    ec2 = region.connect()

    print "Scanning instances...\n"

    instances = ec2.get_only_instances()
    for inst in instances:
        inst_accessor = Instance(inst)
        process_instance(inst_accessor)

    print "\nScanning images...\n"

    images = ec2.get_all_images(None, ("self"))
    for image in images:
        image_accessor = Image(image)
        process_image(image_accessor)

    print "\nScanning volumes...\n"

    volumes = ec2.get_all_volumes()
    for volume in volumes:
        volume_accessor = Volume(volume)
        process_volume(volume_accessor)

    print "\nScanning snapshots...\n"

    snapshots = ec2.get_all_snapshots(owner="self")
    for snapshot in snapshots:
        process_snapshot(Snapshot(snapshot))
Пример #9
0
def check_backups(max_age, environment, cluster, role):
    import dateutil.parser
    import pytz

    ec2 = get_ec2_conn()
    now = datetime.utcnow().replace(tzinfo=pytz.utc)

    tag_filters = {
        'tag:environment': environment,
        'tag:cluster': cluster,
        'tag:role': role,
        'status': 'in-use',
    }

    vols = ec2.get_all_volumes(filters=tag_filters)

    ids = []
    mountpoints = defaultdict(list)

    for vol in vols:
        ids.append(vol.id)
        mountpoints[vol.tags['mount_point']].append(vol.id)

    # filter snapshots with 1 day resolution
    limit = (datetime.now() - timedelta(days=(max_age - 1) // (24 * 3600)))

    snaps = ec2.get_all_snapshots(
        filters={
            'volume_id': ids,
            'start-time': "{}*".format(limit.date().isoformat())
        }
    )
    dones = {}
    for snap in snaps:
        mp = snap.tags['mount_point']
        start_time = dateutil.parser.parse(snap.start_time)

        # do a finer grain age check in python
        if (now - start_time) > timedelta(seconds=max_age):
            continue

        logger.info("Checking mountpoint {}".format(mp))

        # pop any accounted for mountpoints
        if snap.volume_id in mountpoints[mp]:
            dones[mp] = mountpoints.pop(mp)
        else:
            if mp in dones:
                mountpoints.pop(mp)

    if len(mountpoints.keys()) > 0:
        logger.warning("Some volumes are missing a recent snapshot \
            (cluster={}, env={}, role={}):".format(cluster, environment, role))

        for mp in mountpoints:
            logger.warning("\t* {} on volume(s) {}".format(
                mp, ", ".join(mountpoints[mp])))

    return len(mountpoints.keys())
Пример #10
0
def check_backups(max_age, environment, cluster, role):
    import dateutil.parser
    import pytz

    ec2 = get_ec2_conn()
    now = datetime.utcnow().replace(tzinfo=pytz.utc)

    tag_filters = {
        'tag:environment': environment,
        'tag:cluster': cluster,
        'tag:role': role,
        'status': 'in-use',
    }

    vols = ec2.get_all_volumes(filters=tag_filters)

    ids = []
    mountpoints = defaultdict(list)

    for vol in vols:
        ids.append(vol.id)
        mountpoints[vol.tags['mount_point']].append(vol.id)

    # filter snapshots with 1 day resolution
    limit = (datetime.now() - timedelta(days=(max_age - 1) // (24 * 3600)))

    snaps = ec2.get_all_snapshots(
        filters={
            'volume_id': ids,
            'start-time': "{}*".format(limit.date().isoformat())
        })
    dones = {}
    for snap in snaps:
        mp = snap.tags['mount_point']
        start_time = dateutil.parser.parse(snap.start_time)

        # do a finer grain age check in python
        if (now - start_time) > timedelta(seconds=max_age):
            continue

        logger.info("Checking mountpoint {}".format(mp))

        # pop any accounted for mountpoints
        if snap.volume_id in mountpoints[mp]:
            dones[mp] = mountpoints.pop(mp)
        else:
            if mp in dones:
                mountpoints.pop(mp)

    if len(mountpoints.keys()) > 0:
        logger.warning("Some volumes are missing a recent snapshot \
            (cluster={}, env={}, role={}):".format(cluster, environment, role))

        for mp in mountpoints:
            logger.warning("\t* {} on volume(s) {}".format(
                mp, ", ".join(mountpoints[mp])))

    return len(mountpoints.keys())
Пример #11
0
def snapCleanup():
    delete_time = datetime.utcnow() - timedelta(days=7)
    snap_list = ec2.get_all_snapshots(filters = filters)

    for i in snap_list:
	snap = Snap(i.id)
	start_time = datetime.strptime(
	    snap.snapDate(),
	    '%Y-%m-%dT%H:%M:%S.000Z'
	)
        if start_time < delete_time:
            snap.deleteSnap()
            logger.info('Deleted snapshot: %s for Volume: %s - %s - %s' % (i.id, snap.snapVolume(), snap.snapInstanceName(), snap.snapDeviceName()))
def create_block_device(module, ec2, volume):
    # Not aware of a way to determine this programatically
    # http://aws.amazon.com/about-aws/whats-new/2013/10/09/ebs-provisioned-iops-maximum-iops-gb-ratio-increased-to-30-1/
    MAX_IOPS_TO_SIZE_RATIO = 30

    # device_type has been used historically to represent volume_type,
    # however ec2_vol uses volume_type, as does the BlockDeviceType, so
    # we add handling for either/or but not both
    if all(key in volume for key in ['device_type', 'volume_type']):
        module.fail_json(
            msg=
            'device_type is a deprecated name for volume_type. Do not use both device_type and volume_type'
        )

    # get whichever one is set, or NoneType if neither are set
    volume_type = volume.get('device_type') or volume.get('volume_type')

    if 'snapshot' not in volume and 'ephemeral' not in volume:
        if 'volume_size' not in volume:
            module.fail_json(
                msg=
                'Size must be specified when creating a new volume or modifying the root volume'
            )
    if 'snapshot' in volume:
        if volume_type == 'io1' and 'iops' not in volume:
            module.fail_json(msg='io1 volumes must have an iops value set')
        if 'iops' in volume:
            snapshot = ec2.get_all_snapshots(
                snapshot_ids=[volume['snapshot']])[0]
            size = volume.get('volume_size', snapshot.volume_size)
            if int(volume['iops']) > MAX_IOPS_TO_SIZE_RATIO * size:
                module.fail_json(
                    msg='IOPS must be at most %d times greater than size' %
                    MAX_IOPS_TO_SIZE_RATIO)
        if 'encrypted' in volume:
            module.fail_json(
                msg=
                'You can not set encryption when creating a volume from a snapshot'
            )
    if 'ephemeral' in volume:
        if 'snapshot' in volume:
            module.fail_json(msg='Cannot set both ephemeral and snapshot')
    return BlockDeviceType(snapshot_id=volume.get('snapshot'),
                           ephemeral_name=volume.get('ephemeral'),
                           size=volume.get('volume_size'),
                           volume_type=volume_type,
                           delete_on_termination=volume.get(
                               'delete_on_termination', False),
                           iops=volume.get('iops'),
                           encrypted=volume.get('encrypted', None))
Пример #13
0
 def get_snapshots(self):
     """
     Returns a list of all completed snapshots for this volume ID.
     """
     ec2 = self.get_ec2_connection()
     rs = ec2.get_all_snapshots()
     all_vols = [self.volume_id] + self.past_volume_ids
     snaps = []
     for snapshot in rs:
         if snapshot.volume_id in all_vols:
             if snapshot.progress == '100%':
                 snapshot.date = dateutil.parser.parse(snapshot.start_time)
                 snapshot.keep = True
                 snaps.append(snapshot)
     snaps.sort(cmp=lambda x,y: cmp(x.date, y.date))
     return snaps
Пример #14
0
 def get_snapshots(self):
     """
     Returns a list of all completed snapshots for this volume ID.
     """
     ec2 = self.get_ec2_connection()
     rs = ec2.get_all_snapshots()
     all_vols = [self.volume_id] + self.past_volume_ids
     snaps = []
     for snapshot in rs:
         if snapshot.volume_id in all_vols:
             if snapshot.progress == '100%':
                 snapshot.date = dateutil.parser.parse(snapshot.start_time)
                 snapshot.keep = True
                 snaps.append(snapshot)
     snaps.sort(cmp=lambda x, y: cmp(x.date, y.date))
     return snaps
Пример #15
0
def create_volume(snapshot_id, mount_point):
    for snapshot in ec2.get_all_snapshots(snapshot_ids=[snapshot_id]):
        volume = snapshot.create_volume(availability_zone, volume_type='gp2')
        while volume.volume_state() != 'available':
            time.sleep(1)
            volume.update()
        device_name = _get_available_device_name()
        ec2.attach_volume(volume.id, instance_id, device_name)
        while volume.attachment_state() != 'attached':
            time.sleep(1)
            volume.update()
        try:
            os.makedirs(mount_point)
        except:
            pass
        status = subprocess.call(['mount', device_name, mount_point])
        return volume
Пример #16
0
def snapshots(aws_region, days, ss_delete):
    try:
        ec2 = boto.ec2.connect_to_region(aws_region)
        ebs_snapshots = ec2.get_all_snapshots(owner=['self'])
        for ebs_ss in ebs_snapshots:
            try:
                date_now = str(datetime.datetime.now().strftime('%Y-%m-%d'))
                snapshot_time = str(ebs_ss.start_time).split("T")
                date_delta = date_compare(snapshot_time[0], date_now)
                if date_delta >= days:
                    print ebs_ss.id
                    if ss_delete:
       			try:
                            ec2.delete_snapshot(ebs_ss.id) 
			    print "deleting.\n"
            		except Exception, e:
                            print e.response
            except Exception, e:
                print e
    except KeyboardInterrupt:
        sys.exit(0)
    except Exception, e:
        print e
Пример #17
0
def create_block_device(module, ec2, volume):
    # Not aware of a way to determine this programatically
    # http://aws.amazon.com/about-aws/whats-new/2013/10/09/ebs-provisioned-iops-maximum-iops-gb-ratio-increased-to-30-1/
    MAX_IOPS_TO_SIZE_RATIO = 30

    # device_type has been used historically to represent volume_type, 
    # however ec2_vol uses volume_type, as does the BlockDeviceType, so 
    # we add handling for either/or but not both
    if all(key in volume for key in ['device_type','volume_type']):
        module.fail_json(msg = 'device_type is a deprecated name for volume_type. Do not use both device_type and volume_type')

    # get whichever one is set, or NoneType if neither are set
    volume_type = volume.get('device_type') or volume.get('volume_type')

    if 'snapshot' not in volume and 'ephemeral' not in volume:
        if 'volume_size' not in volume:
            module.fail_json(msg = 'Size must be specified when creating a new volume or modifying the root volume')
    if 'snapshot' in volume:
        if volume_type == 'io1' and 'iops' not in volume:
            module.fail_json(msg = 'io1 volumes must have an iops value set')
        if 'iops' in volume:
            snapshot = ec2.get_all_snapshots(snapshot_ids=[volume['snapshot']])[0]
            size = volume.get('volume_size', snapshot.volume_size)
            if int(volume['iops']) > MAX_IOPS_TO_SIZE_RATIO * size:
                module.fail_json(msg = 'IOPS must be at most %d times greater than size' % MAX_IOPS_TO_SIZE_RATIO)
        if 'encrypted' in volume:
            module.fail_json(msg = 'You can not set encyrption when creating a volume from a snapshot')
    if 'ephemeral' in volume:
        if 'snapshot' in volume:
            module.fail_json(msg = 'Cannot set both ephemeral and snapshot')
    return BlockDeviceType(snapshot_id=volume.get('snapshot'),
                           ephemeral_name=volume.get('ephemeral'),
                           size=volume.get('volume_size'),
                           volume_type=volume_type,
                           delete_on_termination=volume.get('delete_on_termination', False),
                           iops=volume.get('iops'),
                           encrypted=volume.get('encrypted', None))
Пример #18
0
def create_snapshot(module, ec2, state=None, description=None, wait=None,
                    wait_timeout=None, volume_id=None, instance_id=None,
                    snapshot_id=None, device_name=None, snapshot_tags=None,
                    last_snapshot_min_age=None):
    snapshot = None
    changed = False

    required = [volume_id, snapshot_id, instance_id]
    if required.count(None) != len(required) - 1: # only 1 must be set
        module.fail_json(msg='One and only one of volume_id or instance_id or snapshot_id must be specified')
    if instance_id and not device_name or device_name and not instance_id:
        module.fail_json(msg='Instance ID and device name must both be specified')

    if instance_id:
        try:
            volumes = ec2.get_all_volumes(filters={'attachment.instance-id': instance_id, 'attachment.device': device_name})
        except boto.exception.BotoServerError as e:
            module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))

        if not volumes:
            module.fail_json(msg="Could not find volume with name %s attached to instance %s" % (device_name, instance_id))

        volume_id = volumes[0].id

    if state == 'absent':
        if not snapshot_id:
            module.fail_json(msg = 'snapshot_id must be set when state is absent')
        try:
            ec2.delete_snapshot(snapshot_id)
        except boto.exception.BotoServerError as e:
            # exception is raised if snapshot does not exist
            if e.error_code == 'InvalidSnapshot.NotFound':
                module.exit_json(changed=False)
            else:
                module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))

        # successful delete
        module.exit_json(changed=True)

    if last_snapshot_min_age > 0:
        try:
            current_snapshots = ec2.get_all_snapshots(filters={'volume_id': volume_id})
        except boto.exception.BotoServerError as e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

        last_snapshot_min_age = last_snapshot_min_age * 60 # Convert to seconds
        snapshot = _get_most_recent_snapshot(current_snapshots,
                                             max_snapshot_age_secs=last_snapshot_min_age)
    try:
        # Create a new snapshot if we didn't find an existing one to use
        if snapshot is None:
            snapshot = ec2.create_snapshot(volume_id, description=description)
            changed = True
        if wait:
            if not _create_with_wait(snapshot, wait_timeout):
                module.fail_json(msg='Timed out while creating snapshot.')
        if snapshot_tags:
            for k, v in snapshot_tags.items():
                snapshot.add_tag(k, v)
    except boto.exception.BotoServerError as e:
        module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

    module.exit_json(changed=changed,
                     snapshot_id=snapshot.id,
                     volume_id=snapshot.volume_id,
                     volume_size=snapshot.volume_size,
                     tags=snapshot.tags.copy())
Пример #19
0
    parents=[bc.build_region_parser(), bc.build_filter_parser("EBS snapshots"), bc.build_common_parser()],
)
args = parser.parse_args()

# process common command line arguments
log = logging.getLogger("botocross")
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)

# execute business logic
log.info("Describing EBS snapshots:")

for region in regions:
    try:
        ec2 = boto.connect_ec2(region=region, **credentials)
        snapshots = ec2.get_all_snapshots(snapshot_ids=args.resource_ids, owner="self", filters=filter["filters"])
        if filter["excludes"]:
            exclusions = ec2.get_all_snapshots(owner="self", filters=filter["excludes"])
            snapshots = bc.filter_list_by_attribute(snapshots, exclusions, "id")
        print region.name + ": " + str(len(snapshots)) + " snapshots"
        for snapshot in snapshots:
            if args.verbose:
                pprint(vars(snapshot))
            else:
                print snapshot.id
    except boto.exception.BotoServerError, e:
        log.error(e.error_message)
Пример #20
0
            module.fail_json(msg = 'snapshot_id must be set when state is absent')
        try:
            ec2.delete_snapshot(snapshot_id)
        except boto.exception.BotoServerError, e:
            # exception is raised if snapshot does not exist
            if e.error_code == 'InvalidSnapshot.NotFound':
                module.exit_json(changed=False)
            else:
                module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))

        # successful delete
        module.exit_json(changed=True)

    if last_snapshot_min_age > 0:
        try:
            current_snapshots = ec2.get_all_snapshots(filters={'volume_id': volume_id})
        except boto.exception.BotoServerError, e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

        last_snapshot_min_age = last_snapshot_min_age * 60 # Convert to seconds
        snapshot = _get_most_recent_snapshot(current_snapshots,
                                             max_snapshot_age_secs=last_snapshot_min_age)
    try:
        # Create a new snapshot if we didn't find an existing one to use
        if snapshot is None:
            snapshot = ec2.create_snapshot(volume_id, description=description)
            changed = True
        if wait:
            if not _create_with_wait(snapshot, wait_timeout):
                module.fail_json(msg='Timed out while creating snapshot.')
        if snapshot_tags:
Пример #21
0
    def __init__(self, snap):
        self.snap = snap
	self.snapshot = ec2.get_all_snapshots(snapshot_ids=self.snap)[0]
Пример #22
0
    ec2 = ec2_connect(module)

    if instance_id:
        try:
            volumes = ec2.get_all_volumes(filters={'attachment.instance-id': instance_id, 'attachment.device': device_name})
            if not volumes:
                module.fail_json(msg="Could not find volume with name %s attached to instance %s" % (device_name, instance_id))
            volume_id = volumes[0].id
        except boto.exception.BotoServerError, e:
            module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))

    if state == 'absent':
        if not snapshot_id:
            module.fail_json(msg = 'snapshot_id must be set when state is absent')
        try:
            snapshots = ec2.get_all_snapshots([snapshot_id])
            ec2.delete_snapshot(snapshot_id)
            module.exit_json(changed=True)
        except boto.exception.BotoServerError, e:
            # exception is raised if snapshot does not exist
            if e.error_code == 'InvalidSnapshot.NotFound':
                module.exit_json(changed=False)
            else:
                module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))

    try:
        snapshot = ec2.create_snapshot(volume_id, description=description)
        time_waited = 0
        if wait:
            snapshot.update()
            while snapshot.status != 'completed':
Пример #23
0
# TODO: cause snapshots to inherit the cost allocation tags from their source EBS

region = DEFAULT_REGION

# Positional arguments
# 1. AWS region
if sys.argv[1:]:
    region = sys.argv.pop(1)

snaps = list(sys.argv[1:])

ec2 = ec2.connect_to_region(region)

if snaps:
    snapshots = ec2.get_all_snapshots(snapshot_ids=snaps, owner='self')
else:
    snapshots = ec2.get_all_snapshots(owner='self')

force_tagging = False

for snapshot in snapshots:

    snap_tags = ec2.get_all_tags(filters={
        'resource-id': snapshot.id,
        'resource-type': 'snapshot'
    })

    is_named = False
    snap_name = "unnamed"
    is_tagged = False
Пример #24
0
try:
    days = int(sys.argv[1])
except IndexError:
    days = 30

delete_time = datetime.utcnow() - timedelta(days=days)

filters = {
    'owner_id': '<INSERT_AWS_ID>'
}

print 'Deleting any snapshots older than {days} days'.format(days=days)

ec2 = boto.ec2.connect_to_region('us-west-2')
snapshots = ec2.get_all_snapshots(filters=filters)

deletion_counter = 0
size_counter = 0

for snapshot in snapshots:
    start_time = datetime.strptime(
        snapshot.start_time,
        '%Y-%m-%dT%H:%M:%S.000Z'
    )

    if start_time < delete_time:
        print 'Deleting {id}'.format(id=snapshot.id)
        deletion_counter = deletion_counter + 1
        size_counter = size_counter + snapshot.volume_size
        try:
Пример #25
0
parser = argparse.ArgumentParser(description='Delete EBS snapshots in all/some available EC2 regions',
                                 parents=[bc.build_region_parser(), bc.build_filter_parser('EBS snapshot'), bc.build_common_parser()])
args = parser.parse_args()

# process common command line arguments
log = logging.getLogger('botocross')
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)

# execute business logic
log.info("Deleting EBS snapshots:")

for region in regions:
    try:
        ec2 = boto.connect_ec2(region=region, **credentials)
        snapshots = ec2.get_all_snapshots(snapshot_ids=args.resource_ids, owner='self', filters=filter['filters'])
        if filter['excludes']:
            exclusions = ec2.get_all_snapshots(owner='self', filters=filter['excludes'])
            snapshots = bc.filter_list_by_attribute(snapshots, exclusions, 'id')
        print region.name + ": " + str(len(snapshots)) + " EBS snapshots"
        for snapshot in snapshots:
            if args.verbose:
                print snapshot.id
            else:
                ec2.delete_snapshot(snapshot.id)
    except boto.exception.BotoServerError, e:
        log.error(e.error_message)
Пример #26
0
def create_snapshot(module,
                    ec2,
                    state=None,
                    description=None,
                    wait=None,
                    wait_timeout=None,
                    volume_id=None,
                    instance_id=None,
                    snapshot_id=None,
                    device_name=None,
                    snapshot_tags=None,
                    last_snapshot_min_age=None,
                    create_volume_permissions=None):
    snapshot = None
    changed = False

    if instance_id:
        try:
            volumes = ec2.get_all_volumes(
                filters={
                    'attachment.instance-id': instance_id,
                    'attachment.device': device_name
                })
        except boto.exception.BotoServerError as e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

        if not volumes:
            module.fail_json(
                msg="Could not find volume with name %s attached to instance %s"
                % (device_name, instance_id))

        volume_id = volumes[0].id

    if state == 'absent':
        try:
            ec2.delete_snapshot(snapshot_id)
        except boto.exception.BotoServerError as e:
            # exception is raised if snapshot does not exist
            if e.error_code == 'InvalidSnapshot.NotFound':
                module.exit_json(changed=False)
            else:
                module.fail_json(msg="%s: %s" %
                                 (e.error_code, e.error_message))

        # successful delete
        module.exit_json(changed=True)

    if last_snapshot_min_age > 0:
        try:
            current_snapshots = ec2.get_all_snapshots(
                filters={'volume_id': volume_id})
        except boto.exception.BotoServerError as e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

        last_snapshot_min_age = last_snapshot_min_age * 60  # Convert to seconds
        snapshot = _get_most_recent_snapshot(
            current_snapshots, max_snapshot_age_secs=last_snapshot_min_age)
    try:
        if snapshot_id:
            snapshot = ec2.get_all_snapshots(snapshot_ids=[snapshot_id])[0]
        # Create a new snapshot if we didn't find an existing one to use
        if snapshot is None:
            snapshot = ec2.create_snapshot(volume_id, description=description)
            changed = True
        if wait:
            if not _create_with_wait(snapshot, wait_timeout):
                module.fail_json(msg='Timed out while creating snapshot.')
        if snapshot_tags:
            tags_to_add, tags_to_remove = existing_to_desired(
                snapshot.tags.items(), snapshot_tags.items())
            for (k, v) in tags_to_add:
                snapshot.add_tag(k, v)
                changed = True
            for (k, v) in tags_to_remove:
                snapshot.remove_tag(k, value=v)
                changed = True

        permissions = ec2.get_snapshot_attribute(snapshot.id)

        users_to_add, users_to_remove = existing_to_desired(
            permissions.attrs.get('user_ids', []), [
                str(user_id)
                for user_id in create_volume_permissions.get('user_ids', [])
            ])
        groups_to_add, groups_to_remove = existing_to_desired(
            permissions.attrs.get('groups', []),
            create_volume_permissions.get('groups', []))

        if users_to_add or groups_to_add:
            ec2.modify_snapshot_attribute(snapshot.id,
                                          user_ids=users_to_add,
                                          groups=groups_to_add)
            permissions = ec2.get_snapshot_attribute(snapshot.id)
            changed = True
        if users_to_remove or groups_to_remove:
            ec2.modify_snapshot_attribute(snapshot.id,
                                          operation='remove',
                                          user_ids=users_to_remove,
                                          groups=groups_to_remove)
            permissions = ec2.get_snapshot_attribute(snapshot.id)
            changed = True
    except boto.exception.BotoServerError as e:
        module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

    module.exit_json(changed=changed,
                     snapshot_id=snapshot.id,
                     volume_id=snapshot.volume_id,
                     volume_size=snapshot.volume_size,
                     tags=snapshot.tags.copy(),
                     permissions=permissions.attrs)
Пример #27
0
# process common command line arguments
log = logging.getLogger('botocross')
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)

# execute business logic
log.info("Deleting EBS snapshots:")

for region in regions:
    try:
        ec2 = boto.connect_ec2(region=region, **credentials)
        snapshots = ec2.get_all_snapshots(snapshot_ids=args.resource_ids,
                                          owner='self',
                                          filters=filter['filters'])
        if filter['excludes']:
            exclusions = ec2.get_all_snapshots(owner='self',
                                               filters=filter['excludes'])
            snapshots = bc.filter_list_by_attribute(snapshots, exclusions,
                                                    'id')
        print region.name + ": " + str(len(snapshots)) + " EBS snapshots"
        for snapshot in snapshots:
            if args.verbose:
                print snapshot.id
            else:
                ec2.delete_snapshot(snapshot.id)
    except boto.exception.BotoServerError, e:
        log.error(e.error_message)
Пример #28
0
import boto.ec2.image
import boto.exception
import os
import re
import sys

ec2 = boto.ec2.connect_to_region(
    'ap-northeast-1',
    aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
    aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'))

snapshot_to_ami = {}
for ami in ec2.get_all_images(owners=['self']):
    for (device, volume) in ami.block_device_mapping.iteritems():
        if volume.snapshot_id:
            snapshot_to_ami[volume.snapshot_id] = ami

snapshots = dict([(snapshot.id, snapshot)
                  for snapshot in ec2.get_all_snapshots(owner='self')])

for (snapshot_id, snapshot) in snapshots.iteritems():
    if not snapshot_to_ami.has_key(snapshot_id):
        if snapshot.description:  # confirmation
            matched = re.search(r'ami-[0-9A-Fa-f]+', snapshot.description)
            if matched:
                ami = ec2.get_image(matched.group())
                if ami:
                    continue
        print "orphaned: %s # %s" % (snapshot_id, snapshot.description)

# vim:set ft=python :
    return True if region.name.find(args.region) != -1 else False

# execute business logic    
credentials = {'aws_access_key_id': args.aws_access_key_id, 'aws_secret_access_key': args.aws_secret_access_key}
heading = "Describing EBS snapshots"
regions = boto.ec2.regions()
if args.region:
    heading += " (filtered by region '" + args.region + "')"
    regions = filter(isSelected, regions)

filters = None
if args.filter:
    filters = dict([filter.split('=') for filter in args.filter])
log.info(args.filter)
log.debug(filters)
log.info(args.resource_ids)

print heading + ":"
for region in regions:
    try:
        ec2 = boto.connect_ec2(region=region, **credentials)
        resources = ec2.get_all_snapshots(snapshot_ids=args.resource_ids, owner='self', filters=filters)
        print region.name + ": " + str(len(resources)) + " snapshots"
        for resource in resources:
            if args.verbose:
                pprint(vars(resource))
            else:
                print resource.id
    except boto.exception.BotoServerError, e:
        log.error(e.error_message)
Пример #30
0
def create_snapshot(module,
                    ec2,
                    state=None,
                    description=None,
                    wait=None,
                    wait_timeout=None,
                    volume_id=None,
                    instance_id=None,
                    snapshot_id=None,
                    device_name=None,
                    snapshot_tags=None,
                    last_snapshot_min_age=None):
    snapshot = None
    changed = False

    required = [volume_id, snapshot_id, instance_id]
    if required.count(None) != len(required) - 1:  # only 1 must be set
        module.fail_json(
            msg=
            'One and only one of volume_id or instance_id or snapshot_id must be specified'
        )
    if instance_id and not device_name or device_name and not instance_id:
        module.fail_json(
            msg='Instance ID and device name must both be specified')

    if instance_id:
        try:
            volumes = ec2.get_all_volumes(
                filters={
                    'attachment.instance-id': instance_id,
                    'attachment.device': device_name
                })
        except boto.exception.BotoServerError as e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

        if not volumes:
            module.fail_json(
                msg="Could not find volume with name %s attached to instance %s"
                % (device_name, instance_id))

        volume_id = volumes[0].id

    if state == 'absent':
        if not snapshot_id:
            module.fail_json(
                msg='snapshot_id must be set when state is absent')
        try:
            ec2.delete_snapshot(snapshot_id)
        except boto.exception.BotoServerError as e:
            # exception is raised if snapshot does not exist
            if e.error_code == 'InvalidSnapshot.NotFound':
                module.exit_json(changed=False)
            else:
                module.fail_json(msg="%s: %s" %
                                 (e.error_code, e.error_message))

        # successful delete
        module.exit_json(changed=True)

    if last_snapshot_min_age > 0:
        try:
            current_snapshots = ec2.get_all_snapshots(
                filters={'volume_id': volume_id})
        except boto.exception.BotoServerError as e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

        last_snapshot_min_age = last_snapshot_min_age * 60  # Convert to seconds
        snapshot = _get_most_recent_snapshot(
            current_snapshots, max_snapshot_age_secs=last_snapshot_min_age)
    try:
        # Create a new snapshot if we didn't find an existing one to use
        if snapshot is None:
            snapshot = ec2.create_snapshot(volume_id, description=description)
            changed = True
        if wait:
            if not _create_with_wait(snapshot, wait_timeout):
                module.fail_json(msg='Timed out while creating snapshot.')
        if snapshot_tags:
            for k, v in snapshot_tags.items():
                snapshot.add_tag(k, v)
    except boto.exception.BotoServerError as e:
        module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

    module.exit_json(changed=changed,
                     snapshot_id=snapshot.id,
                     volume_id=snapshot.volume_id,
                     volume_size=snapshot.volume_size,
                     tags=snapshot.tags.copy())
Пример #31
0
            module.fail_json(msg = 'snapshot_id must be set when state is absent')
        try:
            ec2.delete_snapshot(snapshot_id)
        except boto.exception.BotoServerError, e:
            # exception is raised if snapshot does not exist
            if e.error_code == 'InvalidSnapshot.NotFound':
                module.exit_json(changed=False)
            else:
                module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))

        # successful delete
        module.exit_json(changed=True)

    if last_snapshot_min_age > 0:
        try:
            current_snapshots = ec2.get_all_snapshots(filters={'volume_id': volume_id})
        except boto.exception.BotoServerError, e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

        last_snapshot_min_age = last_snapshot_min_age * 60 # Convert to seconds
        snapshot = _get_most_recent_snapshot(current_snapshots,
                                             max_snapshot_age_secs=last_snapshot_min_age)
    try:
        # Create a new snapshot if we didn't find an existing one to use
        if snapshot is None:
            snapshot = ec2.create_snapshot(volume_id, description=description)
            changed = True
        if wait:
            if not _create_with_wait(snapshot, wait_timeout):
                module.fail_json(msg='Timed out while creating snapshot.')
        if snapshot_tags:
Пример #32
0
vols = list(sys.argv[1:])

ec2 = ec2.connect_to_region(region)

do_archive = True       # do archival snapshots
available_only = True   # snapshot only those volumes which 
                        #     are available (unattached)


if vols:
  volumes = ec2.get_all_volumes(volume_ids=vols)
else:
  volumes = ec2.get_all_volumes()

snapshots = ec2.get_all_snapshots(owner='self')

for volume in volumes:
    if available_only:
        if  volume.attach_data.status:
            print "SKIPPING: Attached volume", volume.id
            continue

    vol_tags = ec2.get_all_tags(filters={'resource-id': volume.id, 'resource-type': 'volume'})

    is_archived = False
    is_named = False
    vol_name = 'unnamed'
    for tag in vol_tags:
        if tag.name == 'Name':
            vol_name = tag.value
#!//usr/bin/python

import boto.ec2

print '*** Apagar todos os Snapshots ***'

ec2 = boto.ec2.connect_to_region('sa-east-1')

snapshots = ec2.get_all_snapshots()

for snapshot in snapshots:
	if snapshot.description == "backup script":
		print "Deletado : "+ snapshot.id
		snapshot.delete()


print '*** Snapshots foram limpos com sucesso ***'