Added pkgs support to the npm module/state

This commit is contained in:
Seth House 2014-08-07 12:36:09 -04:00
parent ca30b7783c
commit ed4bb5119e
3 changed files with 80 additions and 23 deletions

View File

@ -47,6 +47,7 @@ def _check_valid_version():
def install(pkg=None, def install(pkg=None,
pkgs=None,
dir=None, dir=None,
runas=None, runas=None,
registry=None): registry=None):
@ -61,6 +62,11 @@ def install(pkg=None,
A package name in any format accepted by NPM, including a version A package name in any format accepted by NPM, including a version
identifier identifier
pkgs
A list of package names in the same format as the ``name`` parameter
.. versionaddedd:: 2014.7
dir dir
The target directory in which to install the package, or None for The target directory in which to install the package, or None for
global installation global installation
@ -94,6 +100,8 @@ def install(pkg=None,
if pkg: if pkg:
cmd += ' "{0}"'.format(pkg) cmd += ' "{0}"'.format(pkg)
elif pkgs:
cmd += ' "{0}"'.format('" "'.join(pkgs))
result = __salt__['cmd.run_all'](cmd, cwd=dir, runas=runas) result = __salt__['cmd.run_all'](cmd, cwd=dir, runas=runas)

View File

@ -32,6 +32,7 @@ def __virtual__():
def installed(name, def installed(name,
pkgs=None,
dir=None, dir=None,
runas=None, runas=None,
user=None, user=None,
@ -52,6 +53,15 @@ def installed(name,
npm: npm:
- installed - installed
name
The package to install
pkgs
A list of packages to install with a single npm invocation; specifying
this argument will ignore the ``name`` argument
.. versionadded:: 2014.7
dir dir
The target directory in which to install the package, or None for The target directory in which to install the package, or None for
global installation global installation
@ -100,50 +110,80 @@ def installed(name,
user = runas user = runas
runas = None runas = None
prefix = name.split('@')[0].strip() if pkgs is not None:
pkg_list = pkgs
else:
pkg_list = [name]
try: try:
installed_pkgs = __salt__['npm.list'](pkg=name, dir=dir) installed_pkgs = __salt__['npm.list'](dir=dir)
except (CommandNotFoundError, CommandExecutionError) as err: except (CommandNotFoundError, CommandExecutionError) as err:
ret['result'] = False ret['result'] = False
ret['comment'] = 'Error installing {0!r}: {1}'.format(name, err) ret['comment'] = 'Error looking up {0!r}: {1}'.format(name, err)
return ret return ret
else:
installed_pkgs = dict((p.lower(), info)
for p, info in installed_pkgs.items())
installed_pkgs = dict((p.lower(), info) for p, info in installed_pkgs.items()) pkgs_satisfied = []
pkgs_to_install = []
for pkg_name in pkg_list:
prefix = pkg_name.split('@')[0].strip()
if prefix.lower() in installed_pkgs: if prefix.lower() in installed_pkgs:
if force_reinstall is False: if force_reinstall is False:
ret['result'] = True pkgs_satisfied.append('{1}@{2}'.format(
ret['comment'] = 'Package {0!r} satisfied by {1}@{2}'.format( pkg_name,
name, prefix, installed_pkgs[prefix.lower()]['version']) prefix,
return ret installed_pkgs[prefix.lower()]['version']))
else:
pkgs_to_install.append(pkg_name)
if __opts__['test']: if __opts__['test']:
ret['result'] = None ret['result'] = None
ret['comment'] = 'NPM package {0!r} is set to be installed'.format(name)
comment_msg = []
if pkgs_to_install:
comment_msg.append('NPM package(s) {0!r} are set to be installed'
.format(', '.join(pkgs_to_install)))
ret['changes'] = {'old': [], 'new': pkgs_to_install}
if pkgs_satisfied:
comment_msg.append('Package(s) {0!r} satisfied by {1}@{2}'
.format(', '.join(pkgs_satisfied)))
ret['comment'] = '. '.join(comment_msg)
return ret return ret
try: try:
call = __salt__['npm.install']( cmd_args = {
pkg=name, 'dir': dir,
dir=dir, 'runas': user,
runas=user, 'registry': registry,
registry=registry }
)
if pkgs is not None:
cmd_args['pkgs'] = pkgs
else:
cmd_args['pkg'] = pkg_name
call = __salt__['npm.install'](**cmd_args)
except (CommandNotFoundError, CommandExecutionError) as err: except (CommandNotFoundError, CommandExecutionError) as err:
ret['result'] = False ret['result'] = False
ret['comment'] = 'Error installing {0!r}: {1}'.format(name, err) ret['comment'] = 'Error installing {0!r}: {1}'.format(
', '.join(pkg_list), err)
return ret return ret
if call and (isinstance(call, list) or isinstance(call, dict)): if call and (isinstance(call, list) or isinstance(call, dict)):
ret['result'] = True ret['result'] = True
version = call[0]['version'] ret['changes'] = {'old': [], 'new': pkgs_to_install}
pkg_name = call[0]['name'] ret['comment'] = 'Package(s) {0!r} were successfully installed'.format(
ret['changes']['{0}@{1}'.format(pkg_name, version)] = 'Installed' ', '.join(pkgs_to_install))
ret['comment'] = 'Package {0!r} was successfully installed'.format(name)
else: else:
ret['result'] = False ret['result'] = False
ret['comment'] = 'Could not install package {0!r}'.format(name) ret['comment'] = 'Could not install package(s) {0!r}'.format(
', '.join(pkg_list))
return ret return ret

View File

@ -29,6 +29,15 @@ class NpmStateTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
ret = self.run_state('npm.removed', name='pm2') ret = self.run_state('npm.removed', name='pm2')
self.assertSaltTrueReturn(ret) self.assertSaltTrueReturn(ret)
@destructiveTest
def test_npm_installed_pkgs(self):
'''
Basic test to determine if NPM module successfully installs multiple
packages.
'''
ret = self.run_state('npm.installed', pkgs=['pm2', 'grunt'])
self.assertSaltTrueReturn(ret)
if __name__ == '__main__': if __name__ == '__main__':
from integration import run_tests from integration import run_tests