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

Configurable overwrite argument #27

Open
wants to merge 4 commits 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
build
dist
*.egg-info
parameters.yml
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
setup(
description = 'A tool to manage contents of AWS SSM Parameter Store',
name = 'ssm-diff',
version = '0.5',
version = '0.6',
author = 'Sergey Motovilovets',
author_email = 'motovilovets.sergey@gmail.com',
license='MIT',
url = 'https://github.com/runtheops/ssm-diff',
download_url = 'https://github.com/runtheops/ssm-diff/archive/0.5.tar.gz',
download_url = 'https://github.com/runtheops/ssm-diff/archive/0.6.tar.gz',
long_description=long_description,
long_description_content_type='text/markdown',
keywords = ['aws', 'ssm', 'parameter-store'],
Expand Down
7 changes: 4 additions & 3 deletions ssm-diff
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import os


def init(args):
r, l = RemoteState(args.profile), LocalState(args.filename)
r, l = RemoteState(args.profile, args.overwrite), LocalState(args.filename)
l.save(r.get(flat=False, paths=args.path))


def pull(args):
dictfilter = lambda x, y: dict([ (i,x[i]) for i in x if i in set(y) ])
r, l = RemoteState(args.profile), LocalState(args.filename)
r, l = RemoteState(args.profile, args.overwrite), LocalState(args.filename)
diff = helpers.FlatDictDiffer(r.get(paths=args.path), l.get(paths=args.path))
if args.force:
ref_set = diff.changed().union(diff.removed()).union(diff.unchanged())
Expand All @@ -38,7 +38,7 @@ def apply(args):


def plan(args):
r, l = RemoteState(args.profile), LocalState(args.filename)
r, l = RemoteState(args.profile, args.overwrite), LocalState(args.filename)
diff = helpers.FlatDictDiffer(r.get(paths=args.path), l.get(paths=args.path))

if diff.differ:
Expand All @@ -54,6 +54,7 @@ if __name__ == "__main__":
parser.add_argument('-f', help='local state yml file', action='store', dest='filename', default='parameters.yml')
parser.add_argument('--path', '-p', action='append', help='filter SSM path')
parser.add_argument('--profile', help='AWS profile name', action='store', dest='profile')
parser.add_argument('--overwrite', help='Overwrite on change', action='store', type=lambda x: (str(x).lower() == 'true'), dest='overwrite', default=True)
subparsers = parser.add_subparsers(dest='func', help='commands')
subparsers.required = True

Expand Down
9 changes: 7 additions & 2 deletions states/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ def save(self, state):


class RemoteState(object):
def __init__(self, profile):
def __init__(self, profile, overwrite):
if profile:
boto3.setup_default_session(profile_name=profile)
self.ssm = boto3.client('ssm')
self.overwrite = overwrite

def get(self, paths=['/'], flat=True):
p = self.ssm.get_paginator('get_parameters_by_path')
Expand Down Expand Up @@ -132,11 +133,15 @@ def apply(self, diff):
for k in diff.removed():
self.ssm.delete_parameter(Name=k)

if not self.overwrite:
print("Skipping overwrite of vars")
return

for k in diff.changed():
ssm_type = 'SecureString' if isinstance(diff.target[k], SecureTag) else 'String'

self.ssm.put_parameter(
Name=k,
Value=repr(diff.target[k]) if type(diff.target[k]) == SecureTag else str(diff.target[k]),
Overwrite=True,
Overwrite=self.overwrite,
Type=ssm_type)