Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional description for the snapshots #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions makesnapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
# 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
# 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
import boto.sns
Expand Down Expand Up @@ -99,6 +103,7 @@
# non proxy:
# using roles
if aws_access_key:

conn = EC2Connection(aws_access_key, aws_secret_key, region=region)
else:
conn = EC2Connection(region=region)
Expand All @@ -121,16 +126,26 @@
else:
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:
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:
Expand All @@ -143,9 +158,17 @@ 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
extra_info = machine_name + '\t' + machine_id + '\t' + device_info
#print extra_info
try:
count_total += 1
logging.info(vol)
Expand All @@ -155,11 +178,17 @@ 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 = description + '\t' + extra_info

#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))
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
Expand Down Expand Up @@ -235,4 +264,3 @@ def date_compare(snap1, snap2):
sns.publish(sns_arn, message, 'Finished AWS snapshotting')

logging.info(result)