#!/usr/bin/python # -*- coding: utf-8 -*- # (c) 2014, Hiroaki Nakamura # # 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 . # source: https://github.com/hnakamur/ansible-role-atom-packages.git DOCUMENTATION = ''' --- module: apm short_description: Manage atom packages with apm description: - Manage atom packages with Atom Package Manager (apm) version_added: 1.6 author: Hiroaki Nakamura options: name: description: - The name of a atom library to install or a list of packages required: true executable: description: - The executable location for apm. - This is useful if apm is not in the PATH. required: false state: description: - The state of the atom library required: false default: present choices: [ "present", "absent", "latest" ] ''' EXAMPLES = ''' description: Install "project-manager" atom package. - apm: name=project-manager state=present description: Update the package "project-manager" to the latest version. - npm: name=project-manager state=latest description: Remove the package "project-manager". - npm: name=project-manager state=absent ''' import os import json class Apm(object): def __init__(self, module, **kwargs): self.module = module if kwargs['executable']: self.executable = kwargs['executable'] else: self.executable = module.get_bin_path('apm', True) self.apm_list_result = json.loads(self._exec(['list', '--json'], True)) self.installed_packages = set(map(lambda p: p['name'],self.apm_list_result['user'])) self.apm_installed_packages_list_map = dict(zip(map(lambda p: p['name'],self.apm_list_result['user']),self.apm_list_result['user'])) self.apm_outdated_result = json.loads(self._exec(['outdated', '--json'], True)) self.outdated_packages = set(map(lambda p: p['name'],self.apm_outdated_result)) def _exec(self, args, run_in_check_mode=False, check_rc=True): if not self.module.check_mode or (self.module.check_mode and run_in_check_mode): cmd = [self.executable] + args rc, out, err = self.module.run_command(cmd, check_rc=check_rc) return out return '' def install(self,args): return self._exec(['install'] + args) def update(self,args): return self._exec(['update'] + args) def uninstall(self,args): return self._exec(['uninstall'] + args) def main(): arg_spec = dict( packages = dict(default=None, aliases=['pkg', 'name'], type='list'), executable=dict(default=None), state=dict(default='present', choices=['present', 'absent', 'latest']) ) module = AnsibleModule( argument_spec=arg_spec, supports_check_mode=True ) packages = list(module.params['packages']) executable = module.params['executable'] state = module.params['state'] if not packages: module.fail_json(msg='package must be specified') apm = Apm(module, executable=executable) packages_with_versions = set(map(lambda p: p.split('@',1)[0],filter(lambda p: len(p.split('@'))==2,packages))) packages_without_versions = set(filter(lambda p: len(p.split('@',1))==1,packages)) package_names = list(map(lambda p: p.split('@',1)[0],packages)) package_version_map = dict(zip(package_names, packages)) missing_packages = set(package_names).difference(apm.installed_packages) outdated_packages = packages_without_versions.intersection(apm.outdated_packages) 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))) if state == 'present': install = list(map(lambda p: package_version_map[p], missing_packages.union(packages_with_wrong_version))) if len(install): output = apm.install(install) module.exit_json(changed=True,output=output,packages=packages,installed=install) elif state == 'latest': install = list(map(lambda p: package_version_map[p], missing_packages.union(outdated_packages).union(packages_with_wrong_version))) if len(install): output = apm.install(install) module.exit_json(changed=True,output=output,packages=packages,installed=install) else: #absent uninstall = package_names.intersection(apm.installed_packages) if len(uninstall): output = apm.uninstall(list(uninstall)) module.exit_json(changed=True,output=output,packages=packages,uninstalled=uninstall) module.exit_json(changed=False,packages=packages) # import module snippets from ansible.module_utils.basic import * main()