You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

118 lines
3.7 KiB

  1. #!/usr/bin/python -tt
  2. # (c) 2012, Luis Alberto Perez Lazaro <luisperlazaro@gmail.com>
  3. #
  4. # This module is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This software is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this software. If not, see <http://www.gnu.org/licenses/>.
  16. #
  17. DOCUMENTATION = '''
  18. ---
  19. module: patch
  20. author: Luis Alberto Perez Lazaro
  21. version_added: 0.9
  22. short_description: apply patch files
  23. description:
  24. - Apply patch files using the GNU patch tool. Before using this module make sure the patch tool is installed.
  25. options:
  26. patchfile:
  27. required: true
  28. description:
  29. - A patch file as accepted by the gnu patch tool
  30. strip:
  31. required: true
  32. aliases: [ p ]
  33. description:
  34. - Number that indicates the smallest prefix containing leading slashes that
  35. will be stripped from each file name found in the patch file. For more information
  36. see the strip parameter of the gnu patch tool.
  37. basedir:
  38. required: true
  39. description:
  40. - base directory in which the patch file will be applied
  41. examples:
  42. - code: "patch: patchfile=/tmp/critical.patch strip=1 basedir=/usr/share/pyshared/paramiko"
  43. description: Example git checkout from Ansible Playbooks
  44. '''
  45. def _run(args):
  46. cmd = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  47. out, err = cmd.communicate()
  48. rc = cmd.returncode
  49. return (rc, out, err)
  50. def _is_already_applied(patch_file, strip):
  51. reverse_cmd = "patch -s -R -N -p%s --dry-run < %s" % (strip, patch_file)
  52. (rc, _, _) = _run(reverse_cmd)
  53. return rc == 0
  54. def _apply_patch(module, patch_file, strip):
  55. patch_cmd = "patch -s -N -t -r - -p%s < %s" % (strip, patch_file)
  56. (rc, out, err) = _run(patch_cmd)
  57. if rc != 0:
  58. msg = out if not err else err
  59. module.fail_json(msg=msg)
  60. def _get_params(module):
  61. patchfile = os.path.expanduser(module.params['patchfile'])
  62. strip = module.params['strip']
  63. basedir = module.params['basedir']
  64. if basedir:
  65. os.chdir(os.path.expanduser(basedir))
  66. if not os.path.exists(patchfile):
  67. module.fail_json(msg="patchfile %s doesn't exist" % (patchfile))
  68. if not os.access(patchfile, os.R_OK):
  69. module.fail_json(msg="patchfile %s not readable" % (patchfile))
  70. if not os.path.exists(basedir):
  71. module.fail_json(msg="basedir %s doesn't exist" % (patchfile))
  72. try:
  73. strip = int(strip)
  74. except Exception:
  75. module.fail_json(msg="p must be a number")
  76. return patchfile, strip, basedir
  77. # ===========================================
  78. def main():
  79. module = AnsibleModule(
  80. argument_spec = dict(
  81. patchfile=dict(required=True),
  82. basedir=dict(),
  83. strip=dict(default=0, aliases=['p'])
  84. ),
  85. supports_check_mode = True
  86. )
  87. patchfile, strip, basedir = _get_params(module)
  88. changed = False
  89. if module.check_mode:
  90. if _is_already_applied(patchfile, strip):
  91. module.exit_json(changed=changed)
  92. module.exit_json(changed=True)
  93. if not _is_already_applied(patchfile, strip):
  94. _apply_patch(module, patchfile, strip)
  95. changed = True
  96. module.exit_json(changed=changed)
  97. # include magic from lib/ansible/module_common.py
  98. #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
  99. main()