convert assemble to module-magic

This commit is contained in:
Jan-Piet Mens 2012-07-24 18:35:06 +02:00
parent b7b44fbf93
commit 52ab221c92

View file

@ -38,14 +38,6 @@ except ImportError:
# =========================================== # ===========================================
# Support methods # Support methods
def exit_json(rc=0, **kwargs):
print json.dumps(kwargs)
sys.exit(rc)
def fail_json(**kwargs):
kwargs['failed'] = True
exit_json(rc=1, **kwargs)
def assemble_from_fragments(path): def assemble_from_fragments(path):
''' assemble a file from a directory of fragments ''' ''' assemble a file from a directory of fragments '''
assembled = [] assembled = []
@ -61,67 +53,52 @@ def write_temp_file(data):
os.close(fd) os.close(fd)
return path return path
def md5(filename): # ==============================================================
''' Return MD5 hex digest of local file, or None if file is not present. ''' # main
if not os.path.exists(filename):
return None
digest = _md5()
blocksize = 64 * 1024
infile = open(filename, 'rb')
block = infile.read(blocksize)
while block:
digest.update(block)
block = infile.read(blocksize)
infile.close()
return digest.hexdigest()
# =========================================== def main():
if len(sys.argv) == 1: module = AnsibleModule(
fail_json(msg="the assemble module requires arguments (-a)") argument_spec = dict(
src = dict(required=True),
dest = dict(required=True),
)
)
argfile = sys.argv[1] changed=False
if not os.path.exists(argfile): pathmd5 = None
fail_json(msg="Argument file not found") destmd5 = None
src = os.path.expanduser(module.params['src'])
dest = os.path.expanduser(module.params['dest'])
args = open(argfile, 'r').read() if src:
items = shlex.split(args) src = os.path.expanduser(src)
syslog.openlog('ansible-%s' % os.path.basename(__file__)) if dest:
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) dest = os.path.expanduser(dest)
if not len(items): if not os.path.exists(src):
fail_json(msg="the assemble module requires arguments (-a)") fail_json(msg="Source (%s) does not exist" % src)
params = {} if not os.path.isdir(src):
for x in items: fail_json(msg="Source (%s) is not a directory" % src)
(k, v) = x.split("=")
params[k] = v
changed = False path = write_temp_file(assemble_from_fragments(src))
pathmd5 = None pathmd5 = module.md5(path)
destmd5 = None
src = params.get('src', None)
dest = params.get('dest', None)
if src: if os.path.exists(dest):
src = os.path.expanduser(src) destmd5 = module.md5(dest)
if dest:
dest = os.path.expanduser(dest)
if not os.path.exists(src): if pathmd5 != destmd5:
fail_json(msg="Source (%s) does not exist" % src) shutil.copy(path, dest)
changed = True
if not os.path.isdir(src):
fail_json(msg="Source (%s) is not a directory" % src)
path = write_temp_file(assemble_from_fragments(src)) # Mission complete
pathmd5 = md5(path) module.exit_json(src=src, dest=dest, md5sum=destmd5,
changed=changed, msg="OK",
daisychain="file", daisychain_args=module.params)
if os.path.exists(dest): # this is magic, see lib/ansible/module_common.py
destmd5 = md5(dest) #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
if pathmd5 != destmd5: main()
shutil.copy(path, dest)
changed = True
exit_json(md5sum=pathmd5, changed=changed)