Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added resource exclusion filter handling.

[Issue(s) #24]
  • Loading branch information...
commit c0c1e322973842db429443a8a0cc3b685473eb75 1 parent 7ddbbdf
@sopel authored
View
15 botocross/__init__.py
@@ -47,6 +47,9 @@ def create_arn(iam, service, region, resource):
return 'arn:aws:' + service + ':' + region + ':' + account.id + ':' + resource
# TODO: refactor to argparse custom action for inline usage!
+def build_filter(filter_args, exclude_args):
+ return {'filters': build_filter_params(filter_args), 'excludes': build_filter_params(exclude_args)}
+
def build_filter_params(filter_args):
from collections import defaultdict
@@ -76,10 +79,12 @@ def build_region_parser():
parser.add_argument("-r", "--region", help="A region substring selector (e.g. 'us-west')")
return parser
-def build_filter_parser(resource_name):
+def build_filter_parser(resource_name, add_ids=True):
parser = argparse.ArgumentParser(add_help=False)
- parser.add_argument("-f", "--filter", action="append", help="An EC2 instance filter. [can be used multiple times]")
- parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="An EC2 instance id. [can be used multiple times]")
+ parser.add_argument("-f", "--filter", action="append", help="A {0} filter. [can be used multiple times]".format(resource_name))
+ parser.add_argument("-x", "--exclude", action="append", help="A {0} filter (matching ones are excluded). [can be used multiple times]".format(resource_name))
+ if add_ids:
+ parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="A {0} id. [can be used multiple times]".format(resource_name))
return parser
def parse_credentials(args):
@@ -88,6 +93,10 @@ def parse_credentials(args):
def is_region_selected(region, name):
return True if region.name.find(name) != -1 else False
+def filter_list_by_attribute(includes, excludes, attribute):
+ excluded_ids = set([getattr(exclude, attribute) for exclude in excludes])
+ return [include for include in includes if getattr(include, attribute) not in excluded_ids]
+
def filter_regions(regions, region):
if region:
botocross_log.info("... (filtered by region '" + region + "')")
View
11 scripts/backup-instances.py
@@ -30,9 +30,7 @@
# configure command line argument parsing
parser = argparse.ArgumentParser(description='Backup EC2 instances in all/some available EC2 regions',
- parents=[bc.build_region_parser(), bc.build_common_parser()])
-parser.add_argument("-f", "--filter", action="append", help="An EC2 instance filter. [can be used multiple times]")
-parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="An EC2 instance id. [can be used multiple times]")
+ parents=[bc.build_region_parser(), bc.build_filter_parser('EC2 instance'), bc.build_common_parser()])
parser.add_argument("-d", "--description", help="A description for the EC2 image [default: <provided>]")
parser.add_argument("-br", "--backup_retention", type=int, default=1, help="The number of backups to retain (correlated via backup_set). [default: 1]")
parser.add_argument("-bs", "--backup_set", default=DEFAULT_BACKUP_SET, help="A backup set name (determines retention correlation). [default: 'default]'")
@@ -45,7 +43,7 @@
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
-filters = bc.build_filter_params(args.filter)
+filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)
# execute business logic
@@ -57,7 +55,10 @@
for region in regions:
try:
ec2 = boto.connect_ec2(region=region, **credentials)
- reservations = ec2.get_all_instances(instance_ids=args.resource_ids, filters=filters)
+ reservations = ec2.get_all_instances(instance_ids=args.resource_ids, filters=filter['filters'])
+ if filter['excludes']:
+ exclusions = ec2.get_all_instances(filters=filter['excludes'])
+ reservations = bc.filter_list_by_attribute(reservations, exclusions, 'id')
instances = [instance for reservation in reservations for instance in reservation.instances]
print region.name + ": " + str(len(instances)) + " instances"
images = create_images(ec2, instances, backup_set, args.description, no_reboot=args.no_reboot)
View
11 scripts/backup-volumes.py
@@ -30,9 +30,7 @@
# configure command line argument parsing
parser = argparse.ArgumentParser(description='Backup EBS volumes in all/some available EC2 regions',
- parents=[bc.build_region_parser(), bc.build_common_parser()])
-parser.add_argument("-f", "--filter", action="append", help="An EBS volume filter. [can be used multiple times]")
-parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="An EBS volume id. [can be used multiple times]")
+ parents=[bc.build_region_parser(), bc.build_filter_parser('EBS volume'), bc.build_common_parser()])
parser.add_argument("-d", "--description", help="A description for the EBS snapshot [default: <provided>]")
parser.add_argument("-br", "--backup_retention", type=int, default=1, help="The number of backups to retain (correlated via backup_set). [default: 1]")
parser.add_argument("-bs", "--backup_set", default=DEFAULT_BACKUP_SET, help="A backup set name (determines retention correlation). [default: 'default']")
@@ -44,7 +42,7 @@
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
-filters = bc.build_filter_params(args.filter)
+filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)
# execute business logic
@@ -56,7 +54,10 @@
for region in regions:
try:
ec2 = boto.connect_ec2(region=region, **credentials)
- volumes = ec2.get_all_volumes(volume_ids=args.resource_ids, filters=filters)
+ volumes = ec2.get_all_volumes(volume_ids=args.resource_ids, filters=filter['filters'])
+ if filter['excludes']:
+ exclusions = ec2.get_all_volumes(filters=filter['excludes'])
+ volumes = bc.filter_list_by_attribute(volumes, exclusions, 'id')
print region.name + ": " + str(len(volumes)) + " volumes"
snapshots = create_snapshots(ec2, volumes, backup_set, args.description)
# TODO: add support for 'awaiting' the smapshot creation result, once available.
View
11 scripts/create-images.py
@@ -30,9 +30,7 @@
# configure command line argument parsing
parser = argparse.ArgumentParser(description='Create images of EC2 instances in all/some available EC2 regions',
- parents=[bc.build_region_parser(), bc.build_common_parser()])
-parser.add_argument("-f", "--filter", action="append", help="An EC2 instance filter. [can be used multiple times]")
-parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="An EC2 instance id. [can be used multiple times]")
+ parents=[bc.build_region_parser(), bc.build_filter_parser('EC2 instance'), bc.build_common_parser()])
parser.add_argument("-d", "--description", help="A description for the EC2 image [default: <provided>]")
parser.add_argument("-nr", "--no_reboot", action="store_true", help="Prevent shut down of instance before creating the image. [default: False]")
parser.add_argument("-bs", "--backup_set", default=DEFAULT_BACKUP_SET, help="A backup set name (determines retention correlation). [default: 'default'")
@@ -43,7 +41,7 @@
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
-filters = bc.build_filter_params(args.filter)
+filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)
# execute business logic
@@ -55,7 +53,10 @@
for region in regions:
try:
ec2 = boto.connect_ec2(region=region, **credentials)
- reservations = ec2.get_all_instances(instance_ids=args.resource_ids, filters=filters)
+ reservations = ec2.get_all_instances(instance_ids=args.resource_ids, filters=filter['filters'])
+ if filter['excludes']:
+ exclusions = ec2.get_all_instances(filters=filter['excludes'])
+ reservations = bc.filter_list_by_attribute(reservations, exclusions, 'id')
instances = [instance for reservation in reservations for instance in reservation.instances]
print region.name + ": " + str(len(instances)) + " instances"
create_images(ec2, instances, backup_set, args.description, no_reboot=args.no_reboot)
View
11 scripts/create-snapshots.py
@@ -30,9 +30,7 @@
# configure command line argument parsing
parser = argparse.ArgumentParser(description='Create snapshots of EBS volumes in all/some available EC2 regions',
- parents=[bc.build_region_parser(), bc.build_common_parser()])
-parser.add_argument("-f", "--filter", action="append", help="An EBS volume filter. [can be used multiple times]")
-parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="An EBS volume id. [can be used multiple times]")
+ parents=[bc.build_region_parser(), bc.build_filter_parser('EBS volume'), bc.build_common_parser()])
parser.add_argument("-d", "--description", help="A description for the EBS snapshot [default: <provided>]")
parser.add_argument("-bs", "--backup_set", default=DEFAULT_BACKUP_SET, help="A backup set name (determines retention correlation). [default: 'default'")
args = parser.parse_args()
@@ -42,7 +40,7 @@
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
-filters = bc.build_filter_params(args.filter)
+filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)
# execute business logic
@@ -54,7 +52,10 @@
for region in regions:
try:
ec2 = boto.connect_ec2(region=region, **credentials)
- volumes = ec2.get_all_volumes(volume_ids=args.resource_ids, filters=filters)
+ volumes = ec2.get_all_volumes(volume_ids=args.resource_ids, filters=filter['filters'])
+ if filter['excludes']:
+ exclusions = ec2.get_all_volumes(filters=filter['excludes'])
+ volumes = bc.filter_list_by_attribute(volumes, exclusions, 'id')
print region.name + ": " + str(len(volumes)) + " volumes"
create_snapshots(ec2, volumes, backup_set, args.description)
except boto.exception.BotoServerError, e:
View
19 scripts/delete-snapshots.py
@@ -29,9 +29,7 @@
# configure command line argument parsing
parser = argparse.ArgumentParser(description='Delete EBS snapshots in all/some available EC2 regions',
- parents=[bc.build_region_parser(), bc.build_common_parser()])
-parser.add_argument("-f", "--filter", action="append", help="An EBS snapshot filter. [can be used multiple times]")
-parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="An EBS snapshot id. [can be used multiple times]")
+ parents=[bc.build_region_parser(), bc.build_filter_parser('EBS snapshot'), bc.build_common_parser()])
args = parser.parse_args()
# process common command line arguments
@@ -39,7 +37,7 @@
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
-filters = bc.build_filter_params(args.filter)
+filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)
# execute business logic
@@ -48,12 +46,15 @@
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)) + " EBS snapshots"
- for resource in resources:
+ 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 resource.id
+ print snapshot.id
else:
- ec2.delete_snapshot(resource.id)
+ ec2.delete_snapshot(snapshot.id)
except boto.exception.BotoServerError, e:
log.error(e.error_message)
View
19 scripts/deregister-images.py
@@ -29,9 +29,7 @@
# configure command line argument parsing
parser = argparse.ArgumentParser(description='Deregister EC2 images in all/some available EC2 regions',
- parents=[bc.build_region_parser(), bc.build_common_parser()])
-parser.add_argument("-f", "--filter", action="append", help="An EC2 image filter. [can be used multiple times]")
-parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="An EC2 image id. [can be used multiple times]")
+ parents=[bc.build_region_parser(), bc.build_filter_parser('EC2 image'), bc.build_common_parser()])
args = parser.parse_args()
# process common command line arguments
@@ -39,7 +37,7 @@
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
-filters = bc.build_filter_params(args.filter)
+filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)
# execute business logic
@@ -48,12 +46,15 @@
for region in regions:
try:
ec2 = boto.connect_ec2(region=region, **credentials)
- resources = ec2.get_all_images(image_ids=args.resource_ids, owners=['self'], filters=filters)
- print region.name + ": " + str(len(resources)) + " EC2 images"
- for resource in resources:
+ images = ec2.get_all_images(image_ids=args.resource_ids, owners=['self'], filters=filter['filters'])
+ if filter['excludes']:
+ exclusions = ec2.get_all_images(owners=['self'], filters=filter['excludes'])
+ images = bc.filter_list_by_attribute(images, exclusions, 'id')
+ print region.name + ": " + str(len(images)) + " EC2 images"
+ for image in images:
if args.verbose:
- print resource.id
+ print image.id
else:
- ec2.deregister_image(resource.id, delete_snapshot=True)
+ ec2.deregister_image(image.id, delete_snapshot=True)
except boto.exception.BotoServerError, e:
log.error(e.error_message)
View
19 scripts/describe-images.py
@@ -29,9 +29,7 @@
# configure command line argument parsing
parser = argparse.ArgumentParser(description='Describe EC2 images in all/some available EC2 regions',
- parents=[bc.build_region_parser(), bc.build_common_parser()])
-parser.add_argument("-f", "--filter", action="append", help="An EC2 image filter. [can be used multiple times]")
-parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="An EC2 image id. [can be used multiple times]")
+ parents=[bc.build_region_parser(), bc.build_filter_parser('EC2 image'), bc.build_common_parser()])
args = parser.parse_args()
# process common command line arguments
@@ -39,7 +37,7 @@
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
-filters = bc.build_filter_params(args.filter)
+filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)
# execute business logic
@@ -48,12 +46,15 @@
for region in regions:
try:
ec2 = boto.connect_ec2(region=region, **credentials)
- resources = ec2.get_all_images(image_ids=args.resource_ids, owners=['self'], filters=filters)
- print region.name + ": " + str(len(resources)) + " images"
- for resource in resources:
+ images = ec2.get_all_images(image_ids=args.resource_ids, owners=['self'], filters=filter['filters'])
+ if filter['excludes']:
+ exclusions = ec2.get_all_images(owners=['self'], filters=filter['excludes'])
+ images = bc.filter_list_by_attribute(images, exclusions, 'id')
+ print region.name + ": " + str(len(images)) + " images"
+ for image in images:
if args.verbose:
- pprint(vars(resource))
+ pprint(vars(image))
else:
- print resource.id
+ print image.id
except boto.exception.BotoServerError, e:
log.error(e.error_message)
View
11 scripts/describe-instances.py
@@ -29,9 +29,7 @@
# configure command line argument parsing
parser = argparse.ArgumentParser(description='Describe EC2 instances in all/some available EC2 regions',
- parents=[bc.build_region_parser(), bc.build_common_parser()])
-parser.add_argument("-f", "--filter", action="append", help="An EC2 instance filter. [can be used multiple times]")
-parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="An EBS instance id. [can be used multiple times]")
+ parents=[bc.build_region_parser(), bc.build_filter_parser('EC2 instance'), bc.build_common_parser()])
args = parser.parse_args()
# process common command line arguments
@@ -39,7 +37,7 @@
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
-filters = bc.build_filter_params(args.filter)
+filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)
# execute business logic
@@ -48,7 +46,10 @@
for region in regions:
try:
ec2 = boto.connect_ec2(region=region, **credentials)
- reservations = ec2.get_all_instances(instance_ids=args.resource_ids, filters=filters)
+ reservations = ec2.get_all_instances(instance_ids=args.resource_ids, filters=filter['filters'])
+ if filter['excludes']:
+ exclusions = ec2.get_all_instances(filters=filter['excludes'])
+ reservations = bc.filter_list_by_attribute(reservations, exclusions, 'id')
print region.name + ": " + str(len(reservations)) + " instances"
instances = [instance for reservation in reservations for instance in reservation.instances]
for instance in instances:
View
17 scripts/describe-securitygroups.py
@@ -29,8 +29,7 @@
# configure command line argument parsing
parser = argparse.ArgumentParser(description='Describe EC2 security groups in all/some available EC2 regions',
- parents=[bc.build_region_parser(), bc.build_common_parser()])
-parser.add_argument("-f", "--filter", action="append", help="A (key,value) pair for a filter to limit the results returned. [can be used multiple times]")
+ parents=[bc.build_region_parser(), bc.build_filter_parser('EC2 security group', False), bc.build_common_parser()])
parser.add_argument("-li", "--instances", action="store_true", help="List all instances currently running within this security group")
parser.add_argument("-lr", "--rules", action="store_true", help="List all rules currently active in this security group")
args = parser.parse_args()
@@ -40,7 +39,7 @@
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
-filters = bc.build_filter_params(args.filter)
+filter = bc.build_filter(args.filter, args.exclude)
# execute business logic
log.info("Describing EC2 security groups:")
@@ -52,10 +51,16 @@
pprint(region.name, indent=2)
try:
ec2 = boto.connect_ec2(region=region, **credentials)
- groups = ec2.get_all_security_groups(filters=filters)
+ groups = ec2.get_all_security_groups(filters=filter['filters'])
+ if filter['excludes']:
+ exclusions = ec2.get_all_security_groups(filters=filter['excludes'])
+ groups = bc.filter_list_by_attribute(groups, exclusions, 'id')
for group in groups:
- vpc_id = "|" + group.vpc_id if group.vpc_id else ""
- print("\n" + group.name + " (" + group.id + vpc_id + " - " + group.description + "):")
+ if args.verbose:
+ pprint(vars(group))
+ else:
+ vpc_id = "|" + group.vpc_id if group.vpc_id else ""
+ print("\n" + group.name + " (" + group.id + vpc_id + " - " + group.description + "):")
if args.rules:
print("... rules ...")
for rule in group.rules:
View
19 scripts/describe-snapshots.py
@@ -29,9 +29,7 @@
# configure command line argument parsing
parser = argparse.ArgumentParser(description='Describe EBS snapshots in all/some available EC2 regions',
- parents=[bc.build_region_parser(), bc.build_common_parser()])
-parser.add_argument("-f", "--filter", action="append", help="An EBS snapshot filter. [can be used multiple times]")
-parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="An EBS snapshot id. [can be used multiple times]")
+ parents=[bc.build_region_parser(), bc.build_filter_parser('EBS snapshots'), bc.build_common_parser()])
args = parser.parse_args()
# process common command line arguments
@@ -39,7 +37,7 @@
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
-filters = bc.build_filter_params(args.filter)
+filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)
# execute business logic
@@ -48,12 +46,15 @@
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:
+ 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(resource))
+ pprint(vars(snapshot))
else:
- print resource.id
+ print snapshot.id
except boto.exception.BotoServerError, e:
log.error(e.error_message)
View
6 scripts/describe-tags.py
@@ -20,6 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
+from pprint import pprint
import argparse
import boto
import boto.ec2
@@ -49,6 +50,9 @@
resources = ec2.get_all_tags(filters=filters)
print region.name + ": " + str(len(resources)) + " resources with tags"
for resource in resources:
- print "type: " + resource.res_type + ", id: " + resource.res_id + ", key: " + resource.name + ", value: " + resource.value
+ if args.verbose:
+ pprint(vars(resource))
+ else:
+ print "type: " + resource.res_type + ", id: " + resource.res_id + ", key: " + resource.name + ", value: " + resource.value
except boto.exception.BotoServerError, e:
log.error(e.error_message)
View
19 scripts/describe-volumes.py
@@ -29,9 +29,7 @@
# configure command line argument parsing
parser = argparse.ArgumentParser(description='Describe EBS volumes in all/some available EC2 regions',
- parents=[bc.build_region_parser(), bc.build_common_parser()])
-parser.add_argument("-f", "--filter", action="append", help="An EBS volume filter. [can be used multiple times]")
-parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="An EBS volume id. [can be used multiple times]")
+ parents=[bc.build_region_parser(), bc.build_filter_parser('EBS volume'), bc.build_common_parser()])
args = parser.parse_args()
# process common command line arguments
@@ -39,7 +37,7 @@
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
-filters = bc.build_filter_params(args.filter)
+filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)
# execute business logic
@@ -48,12 +46,15 @@
for region in regions:
try:
ec2 = boto.connect_ec2(region=region, **credentials)
- resources = ec2.get_all_volumes(volume_ids=args.resource_ids, filters=filters)
- print region.name + ": " + str(len(resources)) + " volumes"
- for resource in resources:
+ volumes = ec2.get_all_volumes(volume_ids=args.resource_ids, filters=filter['filters'])
+ if filter['excludes']:
+ exclusions = ec2.get_all_volumes(filters=filter['excludes'])
+ volumes = bc.filter_list_by_attribute(volumes, exclusions, 'id')
+ print region.name + ": " + str(len(volumes)) + " volumes"
+ for volume in volumes:
if args.verbose:
- pprint(vars(resource))
+ pprint(vars(volume))
else:
- print resource.id
+ print volume.id
except boto.exception.BotoServerError, e:
log.error(e.error_message)
View
11 scripts/expire-images.py
@@ -30,9 +30,7 @@
# configure command line argument parsing
parser = argparse.ArgumentParser(description='Expire images of EC2 instances in all/some available EC2 regions',
- parents=[bc.build_region_parser(), bc.build_common_parser()])
-parser.add_argument("-f", "--filter", action="append", help="An EC2 instance filter. [can be used multiple times]")
-parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="An EC2 instance id (existing or terminated). [can be used multiple times]")
+ parents=[bc.build_region_parser(), bc.build_filter_parser('EC2 instance'), bc.build_common_parser()])
parser.add_argument("-br", "--backup_retention", type=int, default=1, help="The number of backups to retain (correlated via backup_set). [default: 1]")
parser.add_argument("-bs", "--backup_set", default=DEFAULT_BACKUP_SET, help="A backup set name (determines retention correlation). [default: 'default]'")
parser.add_argument("-ns", "--no_origin_safeguard", action="store_true", help="Allow deletion of images originating from other tools. [default: False]")
@@ -43,7 +41,7 @@
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
-filters = bc.build_filter_params(args.filter)
+filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)
# execute business logic
@@ -56,7 +54,10 @@
try:
ec2 = boto.connect_ec2(region=region, **credentials)
# NOTE: Not filtering by id allows expiring images of terminated instances as well.
- reservations = ec2.get_all_instances(filters=filters)
+ reservations = ec2.get_all_instances(filters=filter['filters'])
+ if filter['excludes']:
+ exclusions = ec2.get_all_instances(filters=filter['excludes'])
+ reservations = bc.filter_list_by_attribute(reservations, exclusions, 'id')
instance_ids = [instance.id for reservation in reservations for instance in reservation.instances]
num_instances = len(instance_ids)
if args.resource_ids:
View
11 scripts/expire-snapshots.py
@@ -30,9 +30,7 @@
# configure command line argument parsing
parser = argparse.ArgumentParser(description='Expire snapshots of EBS volumes in all/some available EC2 regions',
- parents=[bc.build_region_parser(), bc.build_common_parser()])
-parser.add_argument("-f", "--filter", action="append", help="An EBS volume filter. [can be used multiple times]")
-parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="An EBS volume id (existing or deregistered). [can be used multiple times]")
+ parents=[bc.build_region_parser(), bc.build_filter_parser('EBS volume'), bc.build_common_parser()])
parser.add_argument("-br", "--backup_retention", type=int, default=1, help="The number of backups to retain (correlated via backup_set). [default: 1]")
parser.add_argument("-bs", "--backup_set", default=DEFAULT_BACKUP_SET, help="A backup set name (determines retention correlation). [default: 'default']")
parser.add_argument("-ns", "--no_origin_safeguard", action="store_true", help="Allow deletion of snapshots originating from other tools. [default: False]")
@@ -43,7 +41,7 @@
bc.configure_logging(log, args.log_level)
credentials = bc.parse_credentials(args)
regions = bc.filter_regions(boto.ec2.regions(), args.region)
-filters = bc.build_filter_params(args.filter)
+filter = bc.build_filter(args.filter, args.exclude)
log.info(args.resource_ids)
# execute business logic
@@ -56,7 +54,10 @@
try:
ec2 = boto.connect_ec2(region=region, **credentials)
# NOTE: Not filtering by id allows expiring snapshots of deregistered volumes as well.
- volumes = ec2.get_all_volumes(filters=filters)
+ volumes = ec2.get_all_volumes(filters=filter['filters'])
+ if filter['excludes']:
+ exclusions = ec2.get_all_volumes(filters=filter['excludes'])
+ volumes = bc.filter_list_by_attribute(volumes, exclusions, 'id')
volume_ids = [volume.id for volume in volumes]
if args.resource_ids:
volume_ids.extend(args.resource_ids)
Please sign in to comment.
Something went wrong with that request. Please try again.