From 6a28bba1507ab60ce2674e96b25a478d75215bf4 Mon Sep 17 00:00:00 2001 From: Pedro Serrano Date: Wed, 24 Feb 2016 13:36:08 +0100 Subject: [PATCH 1/2] Add feature to ec2-modify-ebs-volume to maintain current tags at new created volume --- .../ec2-modify-ebs-volume.py | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/ec2-modify-ebs-volume/ec2-modify-ebs-volume.py b/ec2-modify-ebs-volume/ec2-modify-ebs-volume.py index b383b9a..3df285b 100755 --- a/ec2-modify-ebs-volume/ec2-modify-ebs-volume.py +++ b/ec2-modify-ebs-volume/ec2-modify-ebs-volume.py @@ -3,6 +3,7 @@ # Date: 2013-11-17 # Version 0.1 # License Type: GNU GENERAL PUBLIC LICENSE, Version 3 +# Modified: 2016-02-24 pserrano (take tags from old volume to the new one) import argparse import logging @@ -55,6 +56,16 @@ def create_volume(snapshot_object, size, availability_zone, iops=None, logging.info('new volume id is: {!s}'.format(volume_object.id)) return volume_object +def create_tags_volume(volume_object,volume_old_tags): + ''' given a new volume we assign the older tags ''' + logging.debug('create_tag_volume called.') + logging.info('add the previous tags to the new volume: {!s}'.format(volume_object.id)) + try: + volume_object.add_tags(volume_old_tags) + volume_object.tags.update() + except exception.EC2ResponseError: + logging.debug('An error ocurred when attempting to create tags to volume {!s}.'.format(volume_object.id)) + def detach_volume(volume_object): ''' given a boto.ec2.volume.Volume object, detaches it and waits for the @@ -97,6 +108,19 @@ def get_block_device_volume(ec2_connection, volume_id): volume_object = get_all_volumes_result[0] return volume_object +def get_actual_volume_tags(ec2_connection, volume_id): + '''given and volume object get the current tags assigned on it + to create on the new volume.''' + logging.debug('get_actual_volume_tags called.') + get_all_volumes_result = ec2_connection.get_all_volumes(volume_ids=[volume_id]) + volume_object = get_all_volumes_result[0] + volume_all_tags = volume_object.tags + if len(volume_all_tags) > 0: + logging.info('Current volume_id {!s} has the following tags (will be reasigned to the new volume):.'.format(volume_id)) + for tag in volume_all_tags: + logging.info('{!s} : {!s}'.format(tag,volume_all_tags[tag])) + return volume_all_tags + def get_selected_instances(instance_id): ''' given an instance_id returns an instance object ''' @@ -281,7 +305,7 @@ def wait_aws_event(object_in, object_type, wait_for_string): # creates ec2_connection object try: - ec2_connection = ec2.connect_to_region('us-east-1') + ec2_connection = ec2.connect_to_region('eu-west-1') except: logging.critical('An error occured when attempting to connect to the AWS API.') exit(1) @@ -350,6 +374,9 @@ def wait_aws_event(object_in, object_type, wait_for_string): previous_volume = get_block_device_volume(ec2_connection, block_device_type.volume_id) +# given a device, capture the actual tags created on it +volume_old_tags = get_actual_volume_tags(ec2_connection, block_device_type.volume_id) + desired_volume_attrs = return_desired_volume_attrs(args=args, volume_object=previous_volume) @@ -377,3 +404,7 @@ def wait_aws_event(object_in, object_type, wait_for_string): .format(selected_instance.id, instance_initial_state, selected_instance.id)) start_instance(instance_object=selected_instance) + +#add tags to new volumen +if len(volume_old_tags) > 0: + create_tags_volume(new_volume,volume_old_tags) From f38fbecade81dc50756afd8190b79a75f001879f Mon Sep 17 00:00:00 2001 From: Pedro Serrano Date: Thu, 3 Mar 2016 18:09:43 +0100 Subject: [PATCH 2/2] fix region to connect --- ec2-modify-ebs-volume/ec2-modify-ebs-volume.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ec2-modify-ebs-volume/ec2-modify-ebs-volume.py b/ec2-modify-ebs-volume/ec2-modify-ebs-volume.py index 3df285b..db1f0a2 100755 --- a/ec2-modify-ebs-volume/ec2-modify-ebs-volume.py +++ b/ec2-modify-ebs-volume/ec2-modify-ebs-volume.py @@ -3,7 +3,6 @@ # Date: 2013-11-17 # Version 0.1 # License Type: GNU GENERAL PUBLIC LICENSE, Version 3 -# Modified: 2016-02-24 pserrano (take tags from old volume to the new one) import argparse import logging @@ -305,7 +304,7 @@ def wait_aws_event(object_in, object_type, wait_for_string): # creates ec2_connection object try: - ec2_connection = ec2.connect_to_region('eu-west-1') + ec2_connection = ec2.connect_to_region('us-east-1') except: logging.critical('An error occured when attempting to connect to the AWS API.') exit(1)