convert assemble to module-magic

reviewable/pr18780/r1
Jan-Piet Mens 12 years ago
parent b7b44fbf93
commit 52ab221c92

@ -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 def main():
digest = _md5()
blocksize = 64 * 1024 module = AnsibleModule(
infile = open(filename, 'rb') argument_spec = dict(
block = infile.read(blocksize) src = dict(required=True),
while block: dest = dict(required=True),
digest.update(block) )
block = infile.read(blocksize) )
infile.close()
return digest.hexdigest() changed=False
pathmd5 = None
# =========================================== destmd5 = None
src = os.path.expanduser(module.params['src'])
if len(sys.argv) == 1: dest = os.path.expanduser(module.params['dest'])
fail_json(msg="the assemble module requires arguments (-a)")
if src:
argfile = sys.argv[1] src = os.path.expanduser(src)
if not os.path.exists(argfile): if dest:
fail_json(msg="Argument file not found") dest = os.path.expanduser(dest)
args = open(argfile, 'r').read() if not os.path.exists(src):
items = shlex.split(args) fail_json(msg="Source (%s) does not exist" % src)
syslog.openlog('ansible-%s' % os.path.basename(__file__))
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) if not os.path.isdir(src):
fail_json(msg="Source (%s) is not a directory" % src)
if not len(items):
fail_json(msg="the assemble module requires arguments (-a)") path = write_temp_file(assemble_from_fragments(src))
pathmd5 = module.md5(path)
params = {}
for x in items: if os.path.exists(dest):
(k, v) = x.split("=") destmd5 = module.md5(dest)
params[k] = v
if pathmd5 != destmd5:
changed = False shutil.copy(path, dest)
pathmd5 = None changed = True
destmd5 = None
src = params.get('src', None)
dest = params.get('dest', None) # Mission complete
module.exit_json(src=src, dest=dest, md5sum=destmd5,
if src: changed=changed, msg="OK",
src = os.path.expanduser(src) daisychain="file", daisychain_args=module.params)
if dest:
dest = os.path.expanduser(dest) # this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
if not os.path.exists(src):
fail_json(msg="Source (%s) does not exist" % src) main()
if not os.path.isdir(src):
fail_json(msg="Source (%s) is not a directory" % src)
path = write_temp_file(assemble_from_fragments(src))
pathmd5 = md5(path)
if os.path.exists(dest):
destmd5 = md5(dest)
if pathmd5 != destmd5:
shutil.copy(path, dest)
changed = True
exit_json(md5sum=pathmd5, changed=changed)

Loading…
Cancel
Save