2012-06-12 06:09:54 +00:00
|
|
|
#!/usr/bin/python
|
2012-08-03 01:29:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-12 06:09:54 +00:00
|
|
|
|
|
|
|
# (c) 2012, Stephen Fromm <sfromm@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
2012-07-09 07:52:00 +00:00
|
|
|
|
2012-06-12 06:09:54 +00:00
|
|
|
# ===========================================
|
|
|
|
# Support methods
|
|
|
|
|
|
|
|
def assemble_from_fragments(path):
|
|
|
|
''' assemble a file from a directory of fragments '''
|
|
|
|
assembled = []
|
|
|
|
for f in sorted(os.listdir(path)):
|
|
|
|
fragment = "%s/%s" % (path, f)
|
|
|
|
if os.path.isfile(fragment):
|
|
|
|
assembled.append(file(fragment).read())
|
|
|
|
return "".join(assembled)
|
|
|
|
|
|
|
|
def write_temp_file(data):
|
|
|
|
fd, path = tempfile.mkstemp()
|
|
|
|
os.write(fd, data)
|
|
|
|
os.close(fd)
|
|
|
|
return path
|
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
# ==============================================================
|
|
|
|
# main
|
|
|
|
|
|
|
|
def main():
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
src = dict(required=True),
|
|
|
|
dest = dict(required=True),
|
2012-09-04 14:00:55 +00:00
|
|
|
backup=dict(default=False, choices=BOOLEANS),
|
2012-07-24 16:35:06 +00:00
|
|
|
)
|
|
|
|
)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
changed=False
|
|
|
|
pathmd5 = None
|
|
|
|
destmd5 = None
|
|
|
|
src = os.path.expanduser(module.params['src'])
|
|
|
|
dest = os.path.expanduser(module.params['dest'])
|
2012-09-04 14:00:55 +00:00
|
|
|
backup = module.boolean(module.params.get('backup', False))
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
if not os.path.exists(src):
|
2012-07-25 16:24:10 +00:00
|
|
|
module.fail_json(msg="Source (%s) does not exist" % src)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
if not os.path.isdir(src):
|
2012-07-25 16:24:10 +00:00
|
|
|
module.fail_json(msg="Source (%s) is not a directory" % src)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
path = write_temp_file(assemble_from_fragments(src))
|
|
|
|
pathmd5 = module.md5(path)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
if os.path.exists(dest):
|
|
|
|
destmd5 = module.md5(dest)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
if pathmd5 != destmd5:
|
2012-09-04 14:00:55 +00:00
|
|
|
if backup and destmd5 is not None:
|
2012-09-04 23:49:49 +00:00
|
|
|
module.backup_local(dest)
|
2012-07-24 16:35:06 +00:00
|
|
|
shutil.copy(path, dest)
|
|
|
|
changed = True
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
|
|
|
|
# Mission complete
|
2012-08-07 00:07:02 +00:00
|
|
|
module.exit_json(src=src, dest=dest, md5sum=destmd5,
|
2012-07-24 16:35:06 +00:00
|
|
|
changed=changed, msg="OK",
|
|
|
|
daisychain="file", daisychain_args=module.params)
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
|
|
|
|
main()
|