From 3c8035dd6b6543351167b1d6eeee556be7a3305f Mon Sep 17 00:00:00 2001 From: Ashwanth Kumar Date: Sun, 14 Aug 2016 00:02:40 +0530 Subject: [PATCH] Adding a new --nooverwrite flag With --nooverwrite if you've manually assigned a tag value to the volume as part of your automation system, even though the instance to which the volume is mounted to has changed, we will not overwrite the values. This holds for both volume tags and snapshot tags. --- README.rst | 1 + graffiti_monkey/cli.py | 9 ++++++++- graffiti_monkey/core.py | 13 ++++++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index b60d830..bc34075 100644 --- a/README.rst +++ b/README.rst @@ -39,6 +39,7 @@ Usage --snapshots snapshot(s) to tag --novolumes do not perform volume tagging --nosnapshots do not perform snapshot tagging + --nooverwrite do not overwrite tags that are already present (goes well with --append) Examples -------- diff --git a/graffiti_monkey/cli.py b/graffiti_monkey/cli.py index 2d51186..4372465 100644 --- a/graffiti_monkey/cli.py +++ b/graffiti_monkey/cli.py @@ -41,6 +41,7 @@ def __init__(self): } self.dryrun = False self.append = False + self.nooverwrite = False self.volumes = None self.snapshots = None self.instancefilter = None @@ -75,6 +76,8 @@ def set_cli_args(self): help='dryrun only, display tagging actions but do not perform them') parser.add_argument('--append', action='store_true', help='append propagated tags to existing tags (up to a total of ten tags)') + parser.add_argument('--nooverwrite', action='store_true', + help='do not overwrite tags that are already present (goes well with --append)') parser.add_argument('--volumes', action='append', help='volume-ids to tag') parser.add_argument('--snapshots', action='append', @@ -142,6 +145,9 @@ def set_dryrun(self): def set_append(self): self.append = self.args.append + def set_nooverwrite(self): + self.nooverwrite = self.args.nooverwrite + def set_volumes(self): if self.args.volumes: self.volumes = self.args.volumes @@ -182,7 +188,8 @@ def initialize_monkey(self): self.snapshots, self.instancefilter, self.novolumes, - self.nosnapshots + self.nosnapshots, + self.nooverwrite ) def start_tags_propagation(self): diff --git a/graffiti_monkey/core.py b/graffiti_monkey/core.py index 6d562f4..b3d336c 100644 --- a/graffiti_monkey/core.py +++ b/graffiti_monkey/core.py @@ -26,7 +26,7 @@ class GraffitiMonkey(object): - def __init__(self, region, profile, instance_tags_to_propagate, volume_tags_to_propagate, volume_tags_to_be_set, snapshot_tags_to_be_set, dryrun, append, volumes_to_tag, snapshots_to_tag, instance_filter, novolumes, nosnapshots): + def __init__(self, region, profile, instance_tags_to_propagate, volume_tags_to_propagate, volume_tags_to_be_set, snapshot_tags_to_be_set, dryrun, append, volumes_to_tag, snapshots_to_tag, instance_filter, novolumes, nosnapshots, nooverwrite): # This list of tags associated with an EC2 instance to propagate to # attached EBS volumes self._instance_tags_to_propagate = instance_tags_to_propagate @@ -53,6 +53,9 @@ def __init__(self, region, profile, instance_tags_to_propagate, volume_tags_to_p # If we are appending tags self._append = append + # If we should not overwrite any existing tags + self._nooverwrite = nooverwrite + # Volumes we will tag self._volumes_to_tag = volumes_to_tag @@ -194,6 +197,10 @@ def tag_volume(self, volume, instances): instance_tags = instances[instance_id].tags + if self._nooverwrite: + for tag_name in volume.tags: + del instance_tags[tag_name] + tags_to_set = {} if self._append: tags_to_set = volume.tags @@ -295,6 +302,10 @@ def tag_snapshot(self, snapshot, volumes): volume_tags = volumes[volume_id].tags + if self._nooverwrite: + for tag_name in snapshot.tags: + del volume_tags[tag_name] + tags_to_set = {} if self._append: tags_to_set = snapshot.tags