From 5e6bf63215049ae4703819d7c9b4c81c92058921 Mon Sep 17 00:00:00 2001 From: Michael DeHaan <michael.dehaan@gmail.com> Date: Sun, 26 Feb 2012 19:21:44 -0500 Subject: [PATCH] make copy & template module take key/value parameters so we're consistent. Only the command module works differently now starter manpage for modules allow template file location to be passed into template & setup modules --- copy | 25 +++++++++++++++++++------ setup | 19 ++++++++++--------- template | 19 ++++++++++++++++--- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/copy b/copy index aa944924efa..95b70ee1a4d 100644 --- a/copy +++ b/copy @@ -8,14 +8,27 @@ try: except ImportError: import simplejson as json -source = sys.argv[1] -dest = sys.argv[2] +# =========================================== +# convert arguments of form ensure=running name=foo +# to a dictionary +# FIXME: make more idiomatic + +args = " ".join(sys.argv[1:]) +items = shlex.split(args) +params = {} +for x in items: + (k, v) = x.split("=") + params[k] = v + +src = params['src'] +dest = params['dest'] + -# raise an error if there is no source file -if not os.path.exists(source): +# raise an error if there is no src file +if not os.path.exists(src): print json.dumps({ "failed" : 1, - "msg" : "Source %s failed to transfer" % source + "msg" : "Source %s failed to transfer" % src }) sys.exit(1) @@ -24,7 +37,7 @@ changed = False if os.path.exists(dest): md5sum = os.popen("md5sum %s" % dest).read().split()[0] -os.system("cp %s %s" % (source, dest)) +os.system("cp %s %s" % (src, dest)) md5sum2 = os.popen("md5sum %s" % dest).read().split()[0] diff --git a/setup b/setup index de52cba7098..5e9fccbf352 100755 --- a/setup +++ b/setup @@ -1,7 +1,6 @@ #!/usr/bin/python -ANSIBLE_DIR = "/etc/ansible" -ANSIBLE_SETUP = "/etc/ansible/setup" +DEFAULT_ANSIBLE_SETUP = "/etc/ansible/setup" import sys import os @@ -15,28 +14,30 @@ except ImportError: # load config & template variables input_data = sys.argv[1:] -new_options = dict([ x.split("=") for x in input_data ]) +new_options = dict([ x.split('=') for x in input_data ]) +ansible_file = new_options.get('metadata', DEFAULT_ANSIBLE_SETUP) +ansible_dir = os.path.dirname(metadata) # create the config dir if it doesn't exist -if not os.path.exists(ANSIBLE_DIR): - os.makedirs(ANSIBLE_DIR) +if not os.path.exists(ansible_dir): + os.makedirs(ansible_dir) changed = False -if not os.path.exists(ANSIBLE_SETUP): +if not os.path.exists(ansible_file): changed = True else: - md5sum = os.popen("md5sum %s" % ANSIBLE_SETUP).read() + md5sum = os.popen("md5sum %s" % ansible_file).read() # write the template/settings file using # instructions from server -f = open(ANSIBLE_SETUP, "w+") +f = open(ansible_file, "w+") reformat = json.dumps(new_options) f.write(reformat) f.close() -md5sum2 = os.popen("md5sum %s" % ANSIBLE_SETUP).read() +md5sum2 = os.popen("md5sum %s" % ansible_file).read() if md5sum != md5sum2: changed = True diff --git a/template b/template index 3bbcbdfcc3d..d77c5cef92a 100644 --- a/template +++ b/template @@ -8,9 +8,22 @@ try: except ImportError: import simplejson as json -source = sys.argv[1] -dest = sys.argv[2] -metadata = sys.argv[3] +# =========================================== +# convert arguments of form ensure=running name=foo +# to a dictionary +# FIXME: make more idiomatic + +args = " ".join(sys.argv[1:]) +items = shlex.split(args) +params = {} +for x in items: + (k, v) = x.split("=") + params[k] = v + +source = params['src'] +dest = params['dest'] +metadata = params.get('metadata', '/etc/ansible/setup') + # raise an error if there is no template metadata if not os.path.exists(metadata):