2015-02-09 01:00:06 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2012, Luis Alberto Perez Lazaro <luisperlazaro@gmail.com>
|
|
|
|
# (c) 2015, Jakub Jirutka <jakub@jirutka.cz>
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: patch
|
2015-05-14 16:45:21 +02:00
|
|
|
author:
|
2015-06-16 20:32:39 +02:00
|
|
|
- "Jakub Jirutka (@jirutka)"
|
|
|
|
- "Luis Alberto Perez Lazaro (@luisperlaz)"
|
2015-02-09 22:27:03 +01:00
|
|
|
version_added: 1.9
|
|
|
|
description:
|
|
|
|
- Apply patch files using the GNU patch tool.
|
2015-02-09 01:00:06 +01:00
|
|
|
short_description: Apply patch files using the GNU patch tool.
|
|
|
|
options:
|
|
|
|
basedir:
|
|
|
|
description:
|
|
|
|
- Path of a base directory in which the patch file will be applied.
|
|
|
|
May be ommitted when C(dest) option is specified, otherwise required.
|
|
|
|
required: false
|
|
|
|
dest:
|
|
|
|
description:
|
|
|
|
- Path of the file on the remote machine to be patched.
|
|
|
|
- The names of the files to be patched are usually taken from the patch
|
|
|
|
file, but if there's just one file to be patched it can specified with
|
|
|
|
this option.
|
|
|
|
required: false
|
|
|
|
aliases: [ "originalfile" ]
|
|
|
|
src:
|
|
|
|
description:
|
2015-03-20 17:36:33 +01:00
|
|
|
- Path of the patch file as accepted by the GNU patch tool. If
|
Update 'patch' documentation for src, remote_src, backup and binary.
The patch module has a few missing items, and inconsistencies, in its
documentation. A few of which are addressed here.
Within Ansible documentation, the choices for boolean values are
commonly 'yes', and 'no'. We standardise the options on that.
'remote_src' documentation uses 'False' and 'True' for its documentation,
so these have been updated in both the choices and default.
'src' documentation refers to 'remote_src', so is updated to use
the 'no' choice.
'backup' did not describe its options and default at all, so we add
them.
'binary' default used 'False', but specified the type as 'bool' which is
implicitly documented as 'yes'/'no', so we make that 'no' as well.
2015-11-27 01:24:08 +01:00
|
|
|
C(remote_src) is 'no', the patch source file is looked up from the
|
2015-03-20 17:36:33 +01:00
|
|
|
module's "files" directory.
|
2015-02-09 01:00:06 +01:00
|
|
|
required: true
|
|
|
|
aliases: [ "patchfile" ]
|
2015-02-09 22:27:03 +01:00
|
|
|
remote_src:
|
|
|
|
description:
|
Update 'patch' documentation for src, remote_src, backup and binary.
The patch module has a few missing items, and inconsistencies, in its
documentation. A few of which are addressed here.
Within Ansible documentation, the choices for boolean values are
commonly 'yes', and 'no'. We standardise the options on that.
'remote_src' documentation uses 'False' and 'True' for its documentation,
so these have been updated in both the choices and default.
'src' documentation refers to 'remote_src', so is updated to use
the 'no' choice.
'backup' did not describe its options and default at all, so we add
them.
'binary' default used 'False', but specified the type as 'bool' which is
implicitly documented as 'yes'/'no', so we make that 'no' as well.
2015-11-27 01:24:08 +01:00
|
|
|
- If C(no), it will search for src at originating/master machine, if C(yes) it will
|
|
|
|
go to the remote/target machine for the src. Default is C(no).
|
|
|
|
choices: [ "yes", "no" ]
|
2015-02-09 22:27:03 +01:00
|
|
|
required: false
|
Update 'patch' documentation for src, remote_src, backup and binary.
The patch module has a few missing items, and inconsistencies, in its
documentation. A few of which are addressed here.
Within Ansible documentation, the choices for boolean values are
commonly 'yes', and 'no'. We standardise the options on that.
'remote_src' documentation uses 'False' and 'True' for its documentation,
so these have been updated in both the choices and default.
'src' documentation refers to 'remote_src', so is updated to use
the 'no' choice.
'backup' did not describe its options and default at all, so we add
them.
'binary' default used 'False', but specified the type as 'bool' which is
implicitly documented as 'yes'/'no', so we make that 'no' as well.
2015-11-27 01:24:08 +01:00
|
|
|
default: "no"
|
2015-02-09 01:00:06 +01:00
|
|
|
strip:
|
|
|
|
description:
|
|
|
|
- Number that indicates the smallest prefix containing leading slashes
|
|
|
|
that will be stripped from each file name found in the patch file.
|
|
|
|
For more information see the strip parameter of the GNU patch tool.
|
|
|
|
required: false
|
|
|
|
type: "int"
|
|
|
|
default: "0"
|
2015-06-02 05:32:22 +02:00
|
|
|
backup:
|
2015-06-02 14:48:20 +02:00
|
|
|
version_added: "2.0"
|
2015-05-30 15:01:52 +02:00
|
|
|
description:
|
|
|
|
- passes --backup --version-control=numbered to patch,
|
|
|
|
producing numbered backup copies
|
Update 'patch' documentation for src, remote_src, backup and binary.
The patch module has a few missing items, and inconsistencies, in its
documentation. A few of which are addressed here.
Within Ansible documentation, the choices for boolean values are
commonly 'yes', and 'no'. We standardise the options on that.
'remote_src' documentation uses 'False' and 'True' for its documentation,
so these have been updated in both the choices and default.
'src' documentation refers to 'remote_src', so is updated to use
the 'no' choice.
'backup' did not describe its options and default at all, so we add
them.
'binary' default used 'False', but specified the type as 'bool' which is
implicitly documented as 'yes'/'no', so we make that 'no' as well.
2015-11-27 01:24:08 +01:00
|
|
|
choices: [ 'yes', 'no' ]
|
|
|
|
default: 'no'
|
2015-03-25 16:14:13 +01:00
|
|
|
binary:
|
|
|
|
version_added: "2.0"
|
|
|
|
description:
|
Update 'patch' documentation for src, remote_src, backup and binary.
The patch module has a few missing items, and inconsistencies, in its
documentation. A few of which are addressed here.
Within Ansible documentation, the choices for boolean values are
commonly 'yes', and 'no'. We standardise the options on that.
'remote_src' documentation uses 'False' and 'True' for its documentation,
so these have been updated in both the choices and default.
'src' documentation refers to 'remote_src', so is updated to use
the 'no' choice.
'backup' did not describe its options and default at all, so we add
them.
'binary' default used 'False', but specified the type as 'bool' which is
implicitly documented as 'yes'/'no', so we make that 'no' as well.
2015-11-27 01:24:08 +01:00
|
|
|
- Setting to C(yes) will disable patch's heuristic for transforming CRLF
|
2015-03-25 16:14:13 +01:00
|
|
|
line endings into LF. Line endings of src and dest must match. If set to
|
Update 'patch' documentation for src, remote_src, backup and binary.
The patch module has a few missing items, and inconsistencies, in its
documentation. A few of which are addressed here.
Within Ansible documentation, the choices for boolean values are
commonly 'yes', and 'no'. We standardise the options on that.
'remote_src' documentation uses 'False' and 'True' for its documentation,
so these have been updated in both the choices and default.
'src' documentation refers to 'remote_src', so is updated to use
the 'no' choice.
'backup' did not describe its options and default at all, so we add
them.
'binary' default used 'False', but specified the type as 'bool' which is
implicitly documented as 'yes'/'no', so we make that 'no' as well.
2015-11-27 01:24:08 +01:00
|
|
|
C(no), patch will replace CRLF in src files on POSIX.
|
2015-05-30 15:01:52 +02:00
|
|
|
required: false
|
|
|
|
type: "bool"
|
Update 'patch' documentation for src, remote_src, backup and binary.
The patch module has a few missing items, and inconsistencies, in its
documentation. A few of which are addressed here.
Within Ansible documentation, the choices for boolean values are
commonly 'yes', and 'no'. We standardise the options on that.
'remote_src' documentation uses 'False' and 'True' for its documentation,
so these have been updated in both the choices and default.
'src' documentation refers to 'remote_src', so is updated to use
the 'no' choice.
'backup' did not describe its options and default at all, so we add
them.
'binary' default used 'False', but specified the type as 'bool' which is
implicitly documented as 'yes'/'no', so we make that 'no' as well.
2015-11-27 01:24:08 +01:00
|
|
|
default: "no"
|
2015-02-09 01:00:06 +01:00
|
|
|
note:
|
|
|
|
- This module requires GNU I(patch) utility to be installed on the remote host.
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: apply patch to one file
|
|
|
|
patch: >
|
|
|
|
src=/tmp/index.html.patch
|
|
|
|
dest=/var/www/index.html
|
|
|
|
|
|
|
|
- name: apply patch to multiple files under basedir
|
|
|
|
patch: >
|
|
|
|
src=/tmp/customize.patch
|
|
|
|
basedir=/var/www
|
|
|
|
strip=1
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
from os import path, R_OK, W_OK
|
|
|
|
|
|
|
|
|
|
|
|
class PatchError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-03-25 16:14:13 +01:00
|
|
|
def is_already_applied(patch_func, patch_file, basedir, dest_file=None, binary=False, strip=0):
|
2015-02-09 01:00:06 +01:00
|
|
|
opts = ['--quiet', '--reverse', '--forward', '--dry-run',
|
|
|
|
"--strip=%s" % strip, "--directory='%s'" % basedir,
|
|
|
|
"--input='%s'" % patch_file]
|
2015-03-25 16:14:13 +01:00
|
|
|
if binary:
|
|
|
|
opts.append('--binary')
|
2015-02-09 01:00:06 +01:00
|
|
|
if dest_file:
|
|
|
|
opts.append("'%s'" % dest_file)
|
|
|
|
|
|
|
|
(rc, _, _) = patch_func(opts)
|
|
|
|
return rc == 0
|
|
|
|
|
|
|
|
|
2015-03-25 16:14:13 +01:00
|
|
|
def apply_patch(patch_func, patch_file, basedir, dest_file=None, binary=False, strip=0, dry_run=False, backup=False):
|
2015-02-09 01:00:06 +01:00
|
|
|
opts = ['--quiet', '--forward', '--batch', '--reject-file=-',
|
|
|
|
"--strip=%s" % strip, "--directory='%s'" % basedir,
|
|
|
|
"--input='%s'" % patch_file]
|
|
|
|
if dry_run:
|
|
|
|
opts.append('--dry-run')
|
2015-03-25 16:14:13 +01:00
|
|
|
if binary:
|
|
|
|
opts.append('--binary')
|
2015-02-09 01:00:06 +01:00
|
|
|
if dest_file:
|
|
|
|
opts.append("'%s'" % dest_file)
|
2015-05-30 15:01:52 +02:00
|
|
|
if backup:
|
|
|
|
opts.append('--backup --version-control=numbered')
|
2015-02-09 01:00:06 +01:00
|
|
|
|
|
|
|
(rc, out, err) = patch_func(opts)
|
|
|
|
if rc != 0:
|
2015-05-08 23:36:15 +02:00
|
|
|
msg = err or out
|
2015-02-09 01:00:06 +01:00
|
|
|
raise PatchError(msg)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec={
|
|
|
|
'src': {'required': True, 'aliases': ['patchfile']},
|
|
|
|
'dest': {'aliases': ['originalfile']},
|
|
|
|
'basedir': {},
|
2015-02-09 22:27:03 +01:00
|
|
|
'strip': {'default': 0, 'type': 'int'},
|
|
|
|
'remote_src': {'default': False, 'type': 'bool'},
|
2015-06-02 05:32:22 +02:00
|
|
|
# NB: for 'backup' parameter, semantics is slightly different from standard
|
|
|
|
# since patch will create numbered copies, not strftime("%Y-%m-%d@%H:%M:%S~")
|
2015-03-25 16:14:13 +01:00
|
|
|
'backup': {'default': False, 'type': 'bool'},
|
|
|
|
'binary': {'default': False, 'type': 'bool'},
|
2015-02-09 01:00:06 +01:00
|
|
|
},
|
|
|
|
required_one_of=[['dest', 'basedir']],
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
# Create type object as namespace for module params
|
|
|
|
p = type('Params', (), module.params)
|
|
|
|
|
2015-04-15 00:48:36 +02:00
|
|
|
p.src = os.path.expanduser(p.src)
|
2015-02-09 01:00:06 +01:00
|
|
|
if not os.access(p.src, R_OK):
|
|
|
|
module.fail_json(msg="src %s doesn't exist or not readable" % (p.src))
|
|
|
|
|
|
|
|
if p.dest and not os.access(p.dest, W_OK):
|
2015-02-28 04:07:47 +01:00
|
|
|
module.fail_json(msg="dest %s doesn't exist or not writable" % (p.dest))
|
2015-02-09 01:00:06 +01:00
|
|
|
|
|
|
|
if p.basedir and not path.exists(p.basedir):
|
|
|
|
module.fail_json(msg="basedir %s doesn't exist" % (p.basedir))
|
|
|
|
|
|
|
|
if not p.basedir:
|
|
|
|
p.basedir = path.dirname(p.dest)
|
|
|
|
|
|
|
|
patch_bin = module.get_bin_path('patch')
|
2015-03-20 17:17:16 +01:00
|
|
|
if patch_bin is None:
|
|
|
|
module.fail_json(msg="patch command not found")
|
2015-02-09 01:00:06 +01:00
|
|
|
patch_func = lambda opts: module.run_command("%s %s" % (patch_bin, ' '.join(opts)))
|
|
|
|
|
2015-03-20 17:19:15 +01:00
|
|
|
# patch need an absolute file name
|
|
|
|
p.src = os.path.abspath(p.src)
|
|
|
|
|
2015-02-09 01:00:06 +01:00
|
|
|
changed = False
|
2015-03-25 16:14:13 +01:00
|
|
|
if not is_already_applied(patch_func, p.src, p.basedir, dest_file=p.dest, binary=p.binary, strip=p.strip):
|
2015-02-09 01:00:06 +01:00
|
|
|
try:
|
2015-03-25 16:14:13 +01:00
|
|
|
apply_patch( patch_func, p.src, p.basedir, dest_file=p.dest, binary=p.binary, strip=p.strip,
|
2015-06-02 05:32:22 +02:00
|
|
|
dry_run=module.check_mode, backup=p.backup )
|
2015-02-09 01:00:06 +01:00
|
|
|
changed = True
|
|
|
|
except PatchError, e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
main()
|