From a2ddee756d3f80885014aef13eec4907be21631d Mon Sep 17 00:00:00 2001 From: Chetan Goswami Date: Wed, 25 Jun 2014 14:25:52 +0200 Subject: [PATCH 1/6] pep8 improvs --- makesnapshots.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/makesnapshots.py b/makesnapshots.py index c9a2885..9b0989f 100755 --- a/makesnapshots.py +++ b/makesnapshots.py @@ -121,16 +121,18 @@ else: sns = boto.sns.connect_to_region(ec2_region_name) + def get_resource_tags(resource_id): resource_tags = {} if resource_id: - tags = conn.get_all_tags({ 'resource-id': resource_id }) + tags = conn.get_all_tags({'resource-id': resource_id}) for tag in tags: # Tags starting with 'aws:' are reserved for internal use if not tag.name.startswith('aws:'): resource_tags[tag.name] = tag.value return resource_tags + def set_resource_tags(resource, tags): for tag_key, tag_value in tags.iteritems(): if tag_key not in resource.tags or resource.tags[tag_key] != tag_value: @@ -143,7 +145,7 @@ def set_resource_tags(resource, tags): # Get all the volumes that match the tag criteria print 'Finding volumes that match the requested tag ({ "tag:%(tag_name)s": "%(tag_value)s" })' % config -vols = conn.get_all_volumes(filters={ 'tag:' + config['tag_name']: config['tag_value'] }) +vols = conn.get_all_volumes(filters={'tag:' + config['tag_name']: config['tag_value']}) for vol in vols: try: From 7a179bda94dc7ff6947df0e2b40786a27d6cbfdf Mon Sep 17 00:00:00 2001 From: Chetan Goswami Date: Wed, 25 Jun 2014 16:41:39 +0200 Subject: [PATCH 2/6] Improved snapshot description with corresponding ec2 name and id --- makesnapshots.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/makesnapshots.py b/makesnapshots.py index 9b0989f..92df5aa 100755 --- a/makesnapshots.py +++ b/makesnapshots.py @@ -20,6 +20,9 @@ # version 3.2: Tags of the volume are placed on the new snapshot # version 3.3: Merged IAM role addidtion from Github +# Adding ec2 instance name and device name in the description of the snapshots +# this helps in recognising the volumes easily when restore is required + from boto.ec2.connection import EC2Connection from boto.ec2.regioninfo import RegionInfo import boto.sns @@ -99,6 +102,7 @@ # non proxy: # using roles if aws_access_key: + conn = EC2Connection(aws_access_key, aws_secret_key, region=region) else: conn = EC2Connection(region=region) @@ -122,6 +126,14 @@ sns = boto.sns.connect_to_region(ec2_region_name) +## adding routine to get extra info like instance name per volume +def get_ec2_instance_names(id_of_ec2): + if id_of_ec2: + ec2_test = conn.get_all_instances(filters={'instance-id': id_of_ec2}) + new_res = ec2_test[0].instances[0].tags['Name'] + return new_res + + def get_resource_tags(resource_id): resource_tags = {} if resource_id: @@ -148,6 +160,11 @@ def set_resource_tags(resource, tags): vols = conn.get_all_volumes(filters={'tag:' + config['tag_name']: config['tag_value']}) for vol in vols: + machine_name = get_ec2_instance_names(vol.attach_data.instance_id) + machine_id = vol.attach_data.instance_id + device_info = vol.attach_data.device + extra_info = machine_name + '\t' + machine_id + '\t' + device_info + #print extra_info try: count_total += 1 logging.info(vol) @@ -157,9 +174,15 @@ def set_resource_tags(resource, tags): 'vol_id': vol.id, 'date_suffix': date_suffix, 'date': datetime.today().strftime('%d-%m-%Y %H:%M:%S') + } + + extra_description = extra_info + '\t' + description + + print extra_description + try: - current_snap = vol.create_snapshot(description) + current_snap = vol.create_snapshot(extra_description) set_resource_tags(current_snap, tags_volume) suc_message = 'Snapshot created with description: %s and tags: %s' % (description, str(tags_volume)) print ' ' + suc_message @@ -237,4 +260,3 @@ def date_compare(snap1, snap2): sns.publish(sns_arn, message, 'Finished AWS snapshotting') logging.info(result) - From 77b0566cbb1ed7d1b6a52692b2c5ebe0c4c36211 Mon Sep 17 00:00:00 2001 From: Chetan Goswami Date: Wed, 25 Jun 2014 16:43:44 +0200 Subject: [PATCH 3/6] comments in script --- makesnapshots.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/makesnapshots.py b/makesnapshots.py index 92df5aa..36d0981 100755 --- a/makesnapshots.py +++ b/makesnapshots.py @@ -21,7 +21,8 @@ # version 3.3: Merged IAM role addidtion from Github # Adding ec2 instance name and device name in the description of the snapshots -# this helps in recognising the volumes easily when restore is required +# This helps in recognising the volumes easily when restore is required +# Each corresponding ec2 instance must have Tag with Key "Name" and Value assigned as its name/hostname from boto.ec2.connection import EC2Connection from boto.ec2.regioninfo import RegionInfo From d97a035a04b0774ce2614a810bd9fc9b60f7b47f Mon Sep 17 00:00:00 2001 From: Chetan Goswami Date: Wed, 25 Jun 2014 16:49:40 +0200 Subject: [PATCH 4/6] comments --- makesnapshots.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makesnapshots.py b/makesnapshots.py index 36d0981..4eaa898 100755 --- a/makesnapshots.py +++ b/makesnapshots.py @@ -180,7 +180,7 @@ def set_resource_tags(resource, tags): extra_description = extra_info + '\t' + description - print extra_description + #print extra_description try: current_snap = vol.create_snapshot(extra_description) From 5eb5aa71a79d6d51c3069cde416e5bc2b314b9e8 Mon Sep 17 00:00:00 2001 From: Chetan Goswami Date: Tue, 1 Jul 2014 09:43:48 +0200 Subject: [PATCH 5/6] improved description and corrected double tag error --- makesnapshots.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/makesnapshots.py b/makesnapshots.py index 4eaa898..25d7039 100755 --- a/makesnapshots.py +++ b/makesnapshots.py @@ -158,9 +158,12 @@ def set_resource_tags(resource, tags): # Get all the volumes that match the tag criteria print 'Finding volumes that match the requested tag ({ "tag:%(tag_name)s": "%(tag_value)s" })' % config -vols = conn.get_all_volumes(filters={'tag:' + config['tag_name']: config['tag_value']}) +#vols = conn.get_all_volumes(filters={'tag:' + config['tag_name']: config['tag_value']}) + +vols = conn.get_all_volumes(filters={config['tag_name']: config['tag_value']}) for vol in vols: +# print vol machine_name = get_ec2_instance_names(vol.attach_data.instance_id) machine_id = vol.attach_data.instance_id device_info = vol.attach_data.device @@ -185,7 +188,7 @@ def set_resource_tags(resource, tags): try: current_snap = vol.create_snapshot(extra_description) set_resource_tags(current_snap, tags_volume) - suc_message = 'Snapshot created with description: %s and tags: %s' % (description, str(tags_volume)) + suc_message = 'Snapshot created with description: %s and tags: %s' % (extra_description, str(tags_volume)) print ' ' + suc_message logging.info(suc_message) total_creates += 1 @@ -260,4 +263,4 @@ def date_compare(snap1, snap2): sns.publish(sns_arn, 'Error in processing volumes: ' + errmsg, 'Error with AWS Snapshot') sns.publish(sns_arn, message, 'Finished AWS snapshotting') -logging.info(result) +logging.info(result) \ No newline at end of file From b1b2fd00fc945a6c9401e3f261dd0086b95806a2 Mon Sep 17 00:00:00 2001 From: Chetan Goswami Date: Fri, 4 Jul 2014 11:29:12 +0200 Subject: [PATCH 6/6] correction for delete snaps --- makesnapshots.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/makesnapshots.py b/makesnapshots.py index 25d7039..81b6af1 100755 --- a/makesnapshots.py +++ b/makesnapshots.py @@ -181,7 +181,7 @@ def set_resource_tags(resource, tags): } - extra_description = extra_info + '\t' + description + extra_description = description + '\t' + extra_info #print extra_description @@ -263,4 +263,4 @@ def date_compare(snap1, snap2): sns.publish(sns_arn, 'Error in processing volumes: ' + errmsg, 'Error with AWS Snapshot') sns.publish(sns_arn, message, 'Finished AWS snapshotting') -logging.info(result) \ No newline at end of file +logging.info(result)