您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

173 行
4.9KB

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # (c) 2014, Hiroaki Nakamura <hnakamur@gmail.com>
  4. #
  5. # This file is part of Ansible
  6. #
  7. # Ansible is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Ansible is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
  19. # source: https://github.com/hnakamur/ansible-role-atom-packages.git
  20. DOCUMENTATION = '''
  21. ---
  22. module: apm
  23. short_description: Manage atom packages with apm
  24. description:
  25. - Manage atom packages with Atom Package Manager (apm)
  26. version_added: 1.6
  27. author: Hiroaki Nakamura
  28. options:
  29. name:
  30. description:
  31. - The name of a atom library to install
  32. required: true
  33. version:
  34. description:
  35. - The version to be installed
  36. required: false
  37. executable:
  38. description:
  39. - The executable location for apm.
  40. - This is useful if apm is not in the PATH.
  41. required: false
  42. state:
  43. description:
  44. - The state of the atom library
  45. required: false
  46. default: present
  47. choices: [ "present", "absent", "latest" ]
  48. '''
  49. EXAMPLES = '''
  50. description: Install "project-manager" atom package.
  51. - apm: name=project-manager state=present
  52. description: Update the package "project-manager" to the latest version.
  53. - npm: name=project-manager state=latest
  54. description: Remove the package "project-manager".
  55. - npm: name=project-manager state=absent
  56. '''
  57. import os
  58. class Apm(object):
  59. def __init__(self, module, **kwargs):
  60. self.module = module
  61. self.name = kwargs['name']
  62. self.version = kwargs['version']
  63. if kwargs['executable']:
  64. self.executable = kwargs['executable']
  65. else:
  66. self.executable = module.get_bin_path('apm', True)
  67. if kwargs['version']:
  68. self.name_version = self.name + '@' + self.version
  69. else:
  70. self.name_version = self.name
  71. def _exec(self, args, run_in_check_mode=False, check_rc=True):
  72. if not self.module.check_mode or (self.module.check_mode and run_in_check_mode):
  73. cmd = [self.executable] + args
  74. if self.name:
  75. cmd.append(self.name_version)
  76. rc, out, err = self.module.run_command(cmd, check_rc=check_rc)
  77. return out
  78. return ''
  79. def list(self):
  80. cmd = ['list']
  81. installed = list()
  82. missing = list()
  83. data = self._exec(cmd, True, False)
  84. pattern = re.compile('^(?:\xe2\x94\x9c|\xe2\x94\x94)\xe2\x94\x80\xe2\x94\x80\s+(\S+)@')
  85. for dep in data.splitlines():
  86. m = pattern.match(dep)
  87. if m:
  88. installed.append(m.group(1))
  89. if self.name not in installed:
  90. missing.append(self.name)
  91. return installed, missing
  92. def install(self):
  93. return self._exec(['install'])
  94. def update(self):
  95. return self._exec(['update'])
  96. def uninstall(self):
  97. return self._exec(['uninstall'])
  98. def list_outdated(self):
  99. outdated = list()
  100. data = self._exec(['outdated'], True, False)
  101. pattern = re.compile('^(?:\xe2\x94\x9c|\xe2\x94\x94)\xe2\x94\x80\xe2\x94\x80\s+(\S+)')
  102. for dep in data.splitlines():
  103. m = pattern.match(dep)
  104. if m and m.group(1) != '(empty)':
  105. outdated.append(m.group(1))
  106. return outdated
  107. def main():
  108. arg_spec = dict(
  109. name=dict(default=None),
  110. version=dict(default=None),
  111. executable=dict(default=None),
  112. state=dict(default='present', choices=['present', 'absent', 'latest'])
  113. )
  114. module = AnsibleModule(
  115. argument_spec=arg_spec,
  116. supports_check_mode=True
  117. )
  118. name = module.params['name']
  119. version = module.params['version']
  120. executable = module.params['executable']
  121. state = module.params['state']
  122. if not name:
  123. module.fail_json(msg='name must be specified')
  124. apm = Apm(module, name=name, version=version, executable=executable)
  125. changed = False
  126. if state == 'present':
  127. installed, missing = apm.list()
  128. if len(missing):
  129. changed = True
  130. apm.install()
  131. elif state == 'latest':
  132. installed, missing = apm.list()
  133. outdated = apm.list_outdated()
  134. if len(missing) or len(outdated):
  135. changed = True
  136. apm.install()
  137. else: #absent
  138. installed, missing = apm.list()
  139. if name in installed:
  140. changed = True
  141. apm.uninstall()
  142. module.exit_json(changed=changed)
  143. # import module snippets
  144. from ansible.module_utils.basic import *
  145. main()