2012-02-24 10:15:28 +00:00
|
|
|
#!/usr/bin/python
|
2012-08-03 01:29:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-02-24 10:15:28 +00:00
|
|
|
|
2012-02-29 00:08:09 +00:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@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/>.
|
|
|
|
|
2012-02-24 10:15:28 +00:00
|
|
|
import os
|
2012-07-04 11:26:25 +00:00
|
|
|
import shutil
|
2012-08-09 18:24:21 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
def backuplocal(fn):
|
|
|
|
"""make a date-marked backup of the specified file, return True or False on success or failure"""
|
|
|
|
# backups named basename-YYYY-MM-DD@HH:MM~
|
|
|
|
ext = time.strftime("%Y-%m-%d@%H:%M~", time.localtime(time.time()))
|
|
|
|
backupdest = '%s.%s' % (fn, ext)
|
|
|
|
|
|
|
|
try:
|
|
|
|
shutil.copy2(fn, backupdest)
|
|
|
|
except shutil.Error, e:
|
|
|
|
return False, 'Could not make backup of %s to %s: %s' % (fn, backupdest, e)
|
|
|
|
return True, backupdest
|
2012-02-24 10:15:28 +00:00
|
|
|
|
2012-07-21 21:07:42 +00:00
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2012-08-01 23:42:31 +00:00
|
|
|
# not checking because of daisy chain to file module
|
|
|
|
check_invalid_arguments = False,
|
2012-07-21 21:07:42 +00:00
|
|
|
argument_spec = dict(
|
|
|
|
src=dict(required=True),
|
2012-08-09 18:24:21 +00:00
|
|
|
dest=dict(required=True),
|
|
|
|
backup=dict(default=False, choices=BOOLEANS),
|
2012-07-21 21:07:42 +00:00
|
|
|
)
|
2012-08-07 00:07:02 +00:00
|
|
|
)
|
2012-07-21 21:07:42 +00:00
|
|
|
|
|
|
|
src = os.path.expanduser(module.params['src'])
|
|
|
|
dest = os.path.expanduser(module.params['dest'])
|
2012-08-09 18:24:21 +00:00
|
|
|
backup = module.boolean(module.params.get('backup', False))
|
|
|
|
|
2012-07-21 21:07:42 +00:00
|
|
|
if not os.path.exists(src):
|
|
|
|
module.fail_json(msg="Source %s failed to transfer" % (src))
|
|
|
|
if not os.access(src, os.R_OK):
|
|
|
|
module.fail_json(msg="Source %s not readable" % (src))
|
|
|
|
|
|
|
|
md5sum_src = module.md5(src)
|
|
|
|
md5sum_dest = None
|
|
|
|
|
|
|
|
if os.path.exists(dest):
|
|
|
|
if not os.access(dest, os.W_OK):
|
|
|
|
module.fail_json(msg="Destination %s not writable" % (dest))
|
|
|
|
if not os.access(dest, os.R_OK):
|
|
|
|
module.fail_json(msg="Destination %s not readable" % (dest))
|
2012-07-31 04:40:50 +00:00
|
|
|
# Allow dest to be directory without compromising md5 check
|
|
|
|
if (os.path.isdir(dest)):
|
2012-08-02 06:02:15 +00:00
|
|
|
module.fail_json(msg="Destination %s cannot be a directory" % (dest))
|
2012-07-21 21:07:42 +00:00
|
|
|
md5sum_dest = module.md5(dest)
|
|
|
|
else:
|
|
|
|
if not os.access(os.path.dirname(dest), os.W_OK):
|
|
|
|
module.fail_json(msg="Destination %s not writable" % (os.path.dirname(dest)))
|
|
|
|
|
2012-08-09 18:24:21 +00:00
|
|
|
backup_file = None
|
2012-07-21 21:07:42 +00:00
|
|
|
if md5sum_src != md5sum_dest:
|
|
|
|
try:
|
2012-08-09 18:24:21 +00:00
|
|
|
if backup:
|
2012-08-09 19:33:05 +00:00
|
|
|
if os.path.exists(dest):
|
|
|
|
success, msg = backuplocal(dest)
|
|
|
|
if not success:
|
|
|
|
module.fail_jason(msg=msg)
|
|
|
|
else:
|
|
|
|
backup_file = msg
|
2012-07-21 21:07:42 +00:00
|
|
|
shutil.copyfile(src, dest)
|
|
|
|
except shutil.Error:
|
2012-08-07 00:07:02 +00:00
|
|
|
module.fail_json(msg="failed to copy: %s and %s are the same" % (src, dest))
|
2012-07-21 21:07:42 +00:00
|
|
|
except IOError:
|
2012-08-07 00:07:02 +00:00
|
|
|
module.fail_json(msg="failed to copy: %s to %s" % (src, dest))
|
2012-07-21 21:07:42 +00:00
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
changed = False
|
|
|
|
|
2012-08-09 18:24:21 +00:00
|
|
|
res_args = {
|
|
|
|
'dest':dest,
|
|
|
|
'src':src,
|
|
|
|
'md5sum':md5sum_src,
|
|
|
|
'changed':changed
|
|
|
|
}
|
|
|
|
if backup_file:
|
|
|
|
res_args['backup_file'] = backup_file
|
|
|
|
|
|
|
|
module.exit_json(**res_args)
|
2012-07-21 21:07:42 +00:00
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
main()
|