mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
npm: fix idempotence (#22238)
* npm: fix idempotence * Better idempotency fix More intelligently add --production rather than depending on hard coded order in args list Cleanup boilderplate imports and license PEP8 fixes
This commit is contained in:
parent
f9b3f4f934
commit
468e71bf71
2 changed files with 23 additions and 14 deletions
|
@ -1,16 +1,17 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
# Copyright (c) 2017 Chris Hoffman <christopher.hoffman@gmail.com>
|
||||||
# (c) 2013, Chris Hoffman <christopher.hoffman@gmail.com>
|
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
|
|
||||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
ANSIBLE_METADATA = {
|
||||||
|
'metadata_version': '1.1',
|
||||||
'status': ['preview'],
|
'status': ['preview'],
|
||||||
'supported_by': 'community'}
|
'supported_by': 'community'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
|
@ -115,12 +116,20 @@ EXAMPLES = '''
|
||||||
state: present
|
state: present
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
|
||||||
|
try:
|
||||||
|
import json
|
||||||
|
except ImportError:
|
||||||
|
try:
|
||||||
|
import simplejson as json
|
||||||
|
except ImportError:
|
||||||
|
# Let snippet from module_utils/basic.py return a proper error in this case
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Npm(object):
|
class Npm(object):
|
||||||
def __init__(self, module, **kwargs):
|
def __init__(self, module, **kwargs):
|
||||||
|
@ -132,13 +141,14 @@ class Npm(object):
|
||||||
self.registry = kwargs['registry']
|
self.registry = kwargs['registry']
|
||||||
self.production = kwargs['production']
|
self.production = kwargs['production']
|
||||||
self.ignore_scripts = kwargs['ignore_scripts']
|
self.ignore_scripts = kwargs['ignore_scripts']
|
||||||
|
self.state = kwargs['state']
|
||||||
|
|
||||||
if kwargs['executable']:
|
if kwargs['executable']:
|
||||||
self.executable = kwargs['executable'].split(' ')
|
self.executable = kwargs['executable'].split(' ')
|
||||||
else:
|
else:
|
||||||
self.executable = [module.get_bin_path('npm', True)]
|
self.executable = [module.get_bin_path('npm', True)]
|
||||||
|
|
||||||
if kwargs['version']:
|
if kwargs['version'] and self.state != 'absent':
|
||||||
self.name_version = self.name + '@' + str(self.version)
|
self.name_version = self.name + '@' + str(self.version)
|
||||||
else:
|
else:
|
||||||
self.name_version = self.name
|
self.name_version = self.name
|
||||||
|
@ -149,7 +159,7 @@ class Npm(object):
|
||||||
|
|
||||||
if self.glbl:
|
if self.glbl:
|
||||||
cmd.append('--global')
|
cmd.append('--global')
|
||||||
if self.production:
|
if self.production and ('install' in cmd or 'update' in cmd):
|
||||||
cmd.append('--production')
|
cmd.append('--production')
|
||||||
if self.ignore_scripts:
|
if self.ignore_scripts:
|
||||||
cmd.append('--ignore-scripts')
|
cmd.append('--ignore-scripts')
|
||||||
|
@ -159,7 +169,7 @@ class Npm(object):
|
||||||
cmd.append('--registry')
|
cmd.append('--registry')
|
||||||
cmd.append(self.registry)
|
cmd.append(self.registry)
|
||||||
|
|
||||||
#If path is specified, cd into that path and run the command.
|
# If path is specified, cd into that path and run the command.
|
||||||
cwd = None
|
cwd = None
|
||||||
if self.path:
|
if self.path:
|
||||||
if not os.path.exists(self.path):
|
if not os.path.exists(self.path):
|
||||||
|
@ -188,7 +198,7 @@ class Npm(object):
|
||||||
installed.append(dep)
|
installed.append(dep)
|
||||||
if self.name and self.name not in installed:
|
if self.name and self.name not in installed:
|
||||||
missing.append(self.name)
|
missing.append(self.name)
|
||||||
#Named dependency not installed
|
# Named dependency not installed
|
||||||
else:
|
else:
|
||||||
missing.append(self.name)
|
missing.append(self.name)
|
||||||
|
|
||||||
|
@ -248,8 +258,8 @@ def main():
|
||||||
if state == 'absent' and not name:
|
if state == 'absent' and not name:
|
||||||
module.fail_json(msg='uninstalling a package is only available for named packages')
|
module.fail_json(msg='uninstalling a package is only available for named packages')
|
||||||
|
|
||||||
npm = Npm(module, name=name, path=path, version=version, glbl=glbl, production=production, \
|
npm = Npm(module, name=name, path=path, version=version, glbl=glbl, production=production,
|
||||||
executable=executable, registry=registry, ignore_scripts=ignore_scripts)
|
executable=executable, registry=registry, ignore_scripts=ignore_scripts, state=state)
|
||||||
|
|
||||||
changed = False
|
changed = False
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
|
@ -266,7 +276,7 @@ def main():
|
||||||
if len(outdated):
|
if len(outdated):
|
||||||
changed = True
|
changed = True
|
||||||
npm.update()
|
npm.update()
|
||||||
else: #absent
|
else: # absent
|
||||||
installed, missing = npm.list()
|
installed, missing = npm.list()
|
||||||
if name in installed:
|
if name in installed:
|
||||||
changed = True
|
changed = True
|
||||||
|
|
|
@ -368,7 +368,6 @@ lib/ansible/modules/packaging/language/cpanm.py
|
||||||
lib/ansible/modules/packaging/language/easy_install.py
|
lib/ansible/modules/packaging/language/easy_install.py
|
||||||
lib/ansible/modules/packaging/language/gem.py
|
lib/ansible/modules/packaging/language/gem.py
|
||||||
lib/ansible/modules/packaging/language/maven_artifact.py
|
lib/ansible/modules/packaging/language/maven_artifact.py
|
||||||
lib/ansible/modules/packaging/language/npm.py
|
|
||||||
lib/ansible/modules/packaging/language/pear.py
|
lib/ansible/modules/packaging/language/pear.py
|
||||||
lib/ansible/modules/packaging/language/pip.py
|
lib/ansible/modules/packaging/language/pip.py
|
||||||
lib/ansible/modules/packaging/os/apk.py
|
lib/ansible/modules/packaging/os/apk.py
|
||||||
|
|
Loading…
Add table
Reference in a new issue