forked from luisperlaz/ansible-misc-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch
executable file
·111 lines (94 loc) · 3.54 KB
/
patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python -tt
# (c) 2012, Luis Alberto Perez Lazaro <[email protected]>
#
# This module 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.
#
# This software 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 this software. If not, see <http://www.gnu.org/licenses/>.
#
DOCUMENTATION = '''
---
module: patch
author: Luis Alberto Perez Lazaro
version_added: 0.9
short_description: apply patch files
description:
- Apply patch files using the GNU patch tool. Before using this module make sure the patch tool is installed.
options:
patchfile:
required: true
description:
- A patch file as accepted by the gnu patch tool
strip:
required: true
aliases: [ p ]
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.
basedir:
required: true
description:
- base directory in which the patch file will be applied
examples:
- code: "patch: patchfile=/tmp/critical.patch strip=1 basedir=/usr/share/pyshared/paramiko"
description: Example git checkout from Ansible Playbooks
'''
def _run(args):
cmd = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = cmd.communicate()
rc = cmd.returncode
return (rc, out, err)
def _is_already_applied(patch_file, strip):
reverse_cmd = "patch -s -R -N -p%s --dry-run < %s" % (strip, patch_file)
(rc, _, _) = _run(reverse_cmd)
return rc == 0
def _apply_patch(module, patch_file, strip):
patch_cmd = "patch -s -N -t -r - -p%s < %s" % (strip, patch_file)
(rc, out, err) = _run(patch_cmd)
if rc != 0:
msg = out if not err else err
module.fail_json(msg=msg)
def _get_params(module):
patchfile = os.path.expanduser(module.params['patchfile'])
strip = module.params['strip']
basedir = module.params['basedir']
if basedir:
os.chdir(os.path.expanduser(basedir))
if not os.path.exists(patchfile):
module.fail_json(msg="patchfile %s doesn't exist" % (patchfile))
if not os.access(patchfile, os.R_OK):
module.fail_json(msg="patchfile %s not readable" % (patchfile))
if not os.path.exists(basedir):
module.fail_json(msg="basedir %s doesn't exist" % (patchfile))
try:
strip = int(strip)
except Exception:
module.fail_json(msg="p must be a number")
return patchfile, strip, basedir
# ===========================================
def main():
module = AnsibleModule(
argument_spec = dict(
patchfile=dict(required=True),
basedir=dict(),
strip=dict(default=0, aliases=['p'])
),
)
patchfile, strip, basedir = _get_params(module)
changed = False
if not _is_already_applied(patchfile, strip):
_apply_patch(module, patchfile, strip)
changed = True
module.exit_json(changed=changed)
# include magic from lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()