Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

147 wiersze
5.3KB

  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 or a list of packages
  32. required: true
  33. executable:
  34. description:
  35. - The executable location for apm.
  36. - This is useful if apm is not in the PATH.
  37. required: false
  38. state:
  39. description:
  40. - The state of the atom library
  41. required: false
  42. default: present
  43. choices: [ "present", "absent", "latest" ]
  44. '''
  45. EXAMPLES = '''
  46. description: Install "project-manager" atom package.
  47. - apm: name=project-manager state=present
  48. description: Update the package "project-manager" to the latest version.
  49. - npm: name=project-manager state=latest
  50. description: Remove the package "project-manager".
  51. - npm: name=project-manager state=absent
  52. '''
  53. import os
  54. import json
  55. class Apm(object):
  56. def __init__(self, module, **kwargs):
  57. self.module = module
  58. if kwargs['executable']:
  59. self.executable = kwargs['executable']
  60. else:
  61. self.executable = module.get_bin_path('apm', True)
  62. self.apm_list_result = json.loads(self._exec(['list', '--json'], True))
  63. self.installed_packages = set(map(lambda p: p['name'],self.apm_list_result['user']))
  64. self.apm_installed_packages_list_map = dict(zip(map(lambda p: p['name'],self.apm_list_result['user']),self.apm_list_result['user']))
  65. self.apm_outdated_result = json.loads(self._exec(['outdated', '--json'], True))
  66. self.outdated_packages = set(map(lambda p: p['name'],self.apm_outdated_result))
  67. def _exec(self, args, run_in_check_mode=False, check_rc=True):
  68. if not self.module.check_mode or (self.module.check_mode and run_in_check_mode):
  69. cmd = [self.executable] + args
  70. rc, out, err = self.module.run_command(cmd, check_rc=check_rc)
  71. return out
  72. return ''
  73. def install(self,args):
  74. return self._exec(['install'] + args)
  75. def update(self,args):
  76. return self._exec(['update'] + args)
  77. def uninstall(self,args):
  78. return self._exec(['uninstall'] + args)
  79. def main():
  80. arg_spec = dict(
  81. packages = dict(default=None, aliases=['pkg', 'name'], type='list'),
  82. executable=dict(default=None),
  83. state=dict(default='present', choices=['present', 'absent', 'latest'])
  84. )
  85. module = AnsibleModule(
  86. argument_spec=arg_spec,
  87. supports_check_mode=True
  88. )
  89. packages = list(module.params['packages'])
  90. executable = module.params['executable']
  91. state = module.params['state']
  92. if not packages:
  93. module.fail_json(msg='package must be specified')
  94. apm = Apm(module, executable=executable)
  95. packages_with_versions = set(map(lambda p: p.split('@',1)[0],filter(lambda p: len(p.split('@'))==2,packages)))
  96. packages_without_versions = set(filter(lambda p: len(p.split('@',1))==1,packages))
  97. package_names = list(map(lambda p: p.split('@',1)[0],packages))
  98. package_version_map = dict(zip(package_names, packages))
  99. missing_packages = set(package_names).difference(apm.installed_packages)
  100. outdated_packages = packages_without_versions.intersection(apm.outdated_packages)
  101. packages_with_wrong_version = set(filter(lambda p: package_version_map[p].split('@',1)[1] != apm.apm_installed_packages_list_map[p]['version'],packages_with_versions.intersection(apm.installed_packages)))
  102. if state == 'present':
  103. install = list(map(lambda p: package_version_map[p], missing_packages.union(packages_with_wrong_version)))
  104. if len(install):
  105. output = apm.install(install)
  106. module.exit_json(changed=True,output=output,packages=packages,installed=install)
  107. elif state == 'latest':
  108. install = list(map(lambda p: package_version_map[p], missing_packages.union(outdated_packages).union(packages_with_wrong_version)))
  109. if len(install):
  110. output = apm.install(install)
  111. module.exit_json(changed=True,output=output,packages=packages,installed=install)
  112. else: #absent
  113. uninstall = package_names.intersection(apm.installed_packages)
  114. if len(uninstall):
  115. output = apm.uninstall(list(uninstall))
  116. module.exit_json(changed=True,output=output,packages=packages,uninstalled=uninstall)
  117. module.exit_json(changed=False,packages=packages)
  118. # import module snippets
  119. from ansible.module_utils.basic import *
  120. main()