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

Adding a new --nooverwrite flag #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand Down
9 changes: 8 additions & 1 deletion graffiti_monkey/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
13 changes: 12 additions & 1 deletion graffiti_monkey/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down