| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2017 Chris Hoffman <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: npm |
| 10 |
short_description: Manage node.js packages with npm |
| 11 |
description: |
| 12 |
- Manage node.js packages with Node Package Manager (npm). |
| 13 |
author: "Chris Hoffman (@chrishoffman)" |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
name: |
| 23 |
description: |
| 24 |
- The name of a node.js library to install. |
| 25 |
type: str |
| 26 |
path: |
| 27 |
description: |
| 28 |
- The base path where to install the node.js libraries. |
| 29 |
- When O(global=true), then O(path) is not used to install the packages. |
| 30 |
type: path |
| 31 |
version: |
| 32 |
description: |
| 33 |
- The version to be installed. |
| 34 |
type: str |
| 35 |
global: |
| 36 |
description: |
| 37 |
- Install the node.js library globally. |
| 38 |
- When O(global=true), then O(path) is not used to install the packages. |
| 39 |
default: false |
| 40 |
type: bool |
| 41 |
executable: |
| 42 |
description: |
| 43 |
- The executable location for npm. |
| 44 |
- This is useful if you are using a version manager, such as nvm. |
| 45 |
type: path |
| 46 |
ignore_scripts: |
| 47 |
description: |
| 48 |
- Use the C(--ignore-scripts) flag when installing. |
| 49 |
type: bool |
| 50 |
default: false |
| 51 |
unsafe_perm: |
| 52 |
description: |
| 53 |
- Use the C(--unsafe-perm) flag when installing. |
| 54 |
type: bool |
| 55 |
default: false |
| 56 |
ci: |
| 57 |
description: |
| 58 |
- Install packages based on package-lock file, same as running C(npm ci). |
| 59 |
type: bool |
| 60 |
default: false |
| 61 |
production: |
| 62 |
description: |
| 63 |
- Install dependencies in production mode, excluding devDependencies. |
| 64 |
type: bool |
| 65 |
default: false |
| 66 |
registry: |
| 67 |
description: |
| 68 |
- The registry to install modules from. |
| 69 |
type: str |
| 70 |
state: |
| 71 |
description: |
| 72 |
- The state of the node.js library. |
| 73 |
type: str |
| 74 |
default: present |
| 75 |
choices: ["present", "absent", "latest"] |
| 76 |
no_optional: |
| 77 |
description: |
| 78 |
- Use the C(--no-optional) flag when installing. |
| 79 |
type: bool |
| 80 |
default: false |
| 81 |
version_added: 2.0.0 |
| 82 |
no_bin_links: |
| 83 |
description: |
| 84 |
- Use the C(--no-bin-links) flag when installing. |
| 85 |
type: bool |
| 86 |
default: false |
| 87 |
version_added: 2.5.0 |
| 88 |
force: |
| 89 |
description: |
| 90 |
- Use the C(--force) flag when installing. |
| 91 |
type: bool |
| 92 |
default: false |
| 93 |
version_added: 9.5.0 |
| 94 |
requirements: |
| 95 |
- npm installed in bin path (recommended /usr/local/bin) |
| 96 |
""" |
| 97 |
|
| 98 |
EXAMPLES = r""" |
| 99 |
- name: Install "coffee-script" node.js package. |
| 100 |
community.general.npm: |
| 101 |
name: coffee-script |
| 102 |
path: /app/location |
| 103 |
|
| 104 |
- name: Install "coffee-script" node.js package on version 1.6.1. |
| 105 |
community.general.npm: |
| 106 |
name: coffee-script |
| 107 |
version: '1.6.1' |
| 108 |
path: /app/location |
| 109 |
|
| 110 |
- name: Install "coffee-script" node.js package globally. |
| 111 |
community.general.npm: |
| 112 |
name: coffee-script |
| 113 |
global: true |
| 114 |
|
| 115 |
- name: Force Install "coffee-script" node.js package. |
| 116 |
community.general.npm: |
| 117 |
name: coffee-script |
| 118 |
force: true |
| 119 |
|
| 120 |
- name: Remove the globally package "coffee-script". |
| 121 |
community.general.npm: |
| 122 |
name: coffee-script |
| 123 |
global: true |
| 124 |
state: absent |
| 125 |
|
| 126 |
- name: Install "coffee-script" node.js package from custom registry. |
| 127 |
community.general.npm: |
| 128 |
name: coffee-script |
| 129 |
registry: 'http://registry.mysite.com' |
| 130 |
|
| 131 |
- name: Install packages based on package.json. |
| 132 |
community.general.npm: |
| 133 |
path: /app/location |
| 134 |
|
| 135 |
- name: Update packages based on package.json to their latest version. |
| 136 |
community.general.npm: |
| 137 |
path: /app/location |
| 138 |
state: latest |
| 139 |
|
| 140 |
- name: Install packages based on package.json using the npm installed with nvm v0.10.1. |
| 141 |
community.general.npm: |
| 142 |
path: /app/location |
| 143 |
executable: /opt/nvm/v0.10.1/bin/npm |
| 144 |
state: present |
| 145 |
""" |
| 146 |
|
| 147 |
import json |
| 148 |
import os |
| 149 |
import re |
| 150 |
|
| 151 |
from ansible.module_utils.basic import AnsibleModule |
| 152 |
|
| 153 |
from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt |
| 154 |
|
| 155 |
|
| 156 |
class Npm: |
| 157 |
def __init__(self, module, **kwargs): |
| 158 |
self.module = module |
| 159 |
self.glbl = kwargs["glbl"] |
| 160 |
self.name = kwargs["name"] |
| 161 |
self.version = kwargs["version"] |
| 162 |
self.path = kwargs["path"] |
| 163 |
self.registry = kwargs["registry"] |
| 164 |
self.production = kwargs["production"] |
| 165 |
self.ignore_scripts = kwargs["ignore_scripts"] |
| 166 |
self.unsafe_perm = kwargs["unsafe_perm"] |
| 167 |
self.state = kwargs["state"] |
| 168 |
self.no_optional = kwargs["no_optional"] |
| 169 |
self.no_bin_links = kwargs["no_bin_links"] |
| 170 |
self.force = kwargs["force"] |
| 171 |
|
| 172 |
if kwargs["executable"]: |
| 173 |
self.executable = kwargs["executable"].split(" ") |
| 174 |
else: |
| 175 |
self.executable = [module.get_bin_path("npm", True)] |
| 176 |
|
| 177 |
if kwargs["version"] and kwargs["state"] != "absent": |
| 178 |
self.name_version = f"{self.name}@{kwargs['version']}" |
| 179 |
else: |
| 180 |
self.name_version = self.name |
| 181 |
|
| 182 |
self.runner = CmdRunner( |
| 183 |
module, |
| 184 |
command=self.executable, |
| 185 |
arg_formats=dict( |
| 186 |
exec_args=cmd_runner_fmt.as_list(), |
| 187 |
global_=cmd_runner_fmt.as_bool("--global"), |
| 188 |
production=cmd_runner_fmt.as_bool("--production"), |
| 189 |
ignore_scripts=cmd_runner_fmt.as_bool("--ignore-scripts"), |
| 190 |
unsafe_perm=cmd_runner_fmt.as_bool("--unsafe-perm"), |
| 191 |
name_version=cmd_runner_fmt.as_list(), |
| 192 |
registry=cmd_runner_fmt.as_opt_val("--registry"), |
| 193 |
no_optional=cmd_runner_fmt.as_bool("--no-optional"), |
| 194 |
no_bin_links=cmd_runner_fmt.as_bool("--no-bin-links"), |
| 195 |
force=cmd_runner_fmt.as_bool("--force"), |
| 196 |
), |
| 197 |
) |
| 198 |
|
| 199 |
def _exec(self, args, run_in_check_mode=False, check_rc=True, add_package_name=True): |
| 200 |
if not self.module.check_mode or (self.module.check_mode and run_in_check_mode): |
| 201 |
# If path is specified, cd into that path and run the command. |
| 202 |
cwd = None |
| 203 |
if self.path: |
| 204 |
if not os.path.exists(self.path): |
| 205 |
os.makedirs(self.path) |
| 206 |
if not os.path.isdir(self.path): |
| 207 |
self.module.fail_json(msg=f"path {self.path} is not a directory") |
| 208 |
cwd = self.path |
| 209 |
|
| 210 |
params = dict(self.module.params) |
| 211 |
params["exec_args"] = args |
| 212 |
params["global_"] = self.glbl |
| 213 |
params["production"] = self.production and ("install" in args or "update" in args or "ci" in args) |
| 214 |
params["name_version"] = self.name_version if add_package_name else None |
| 215 |
|
| 216 |
with self.runner( |
| 217 |
"exec_args global_ production ignore_scripts unsafe_perm name_version registry no_optional no_bin_links force", |
| 218 |
check_rc=check_rc, |
| 219 |
cwd=cwd, |
| 220 |
) as ctx: |
| 221 |
rc, out, err = ctx.run(**params) |
| 222 |
return out |
| 223 |
|
| 224 |
return "" |
| 225 |
|
| 226 |
def list(self): |
| 227 |
cmd = ["list", "--json", "--long"] |
| 228 |
|
| 229 |
installed = list() |
| 230 |
missing = list() |
| 231 |
data = {} |
| 232 |
try: |
| 233 |
data = json.loads(self._exec(cmd, True, False, False) or "{}") |
| 234 |
except getattr(json, "JSONDecodeError", ValueError) as e: |
| 235 |
self.module.fail_json(msg=f"Failed to parse NPM output with error {e}") |
| 236 |
if "dependencies" in data: |
| 237 |
for dep, props in data["dependencies"].items(): |
| 238 |
if "missing" in props and props["missing"]: |
| 239 |
missing.append(dep) |
| 240 |
elif "invalid" in props and props["invalid"]: |
| 241 |
missing.append(dep) |
| 242 |
else: |
| 243 |
installed.append(dep) |
| 244 |
if "version" in props and props["version"]: |
| 245 |
dep_version = f"{dep}@{props['version']}" |
| 246 |
installed.append(dep_version) |
| 247 |
if self.name_version and self.name_version not in installed: |
| 248 |
missing.append(self.name) |
| 249 |
# Named dependency not installed |
| 250 |
else: |
| 251 |
missing.append(self.name) |
| 252 |
|
| 253 |
return installed, missing |
| 254 |
|
| 255 |
def install(self): |
| 256 |
return self._exec(["install"]) |
| 257 |
|
| 258 |
def ci_install(self): |
| 259 |
return self._exec(["ci"]) |
| 260 |
|
| 261 |
def update(self): |
| 262 |
return self._exec(["update"]) |
| 263 |
|
| 264 |
def uninstall(self): |
| 265 |
return self._exec(["uninstall"]) |
| 266 |
|
| 267 |
def list_outdated(self): |
| 268 |
outdated = list() |
| 269 |
data = self._exec(["outdated"], True, False) |
| 270 |
for dep in data.splitlines(): |
| 271 |
if dep: |
| 272 |
# node.js v0.10.22 changed the `npm outdated` module separator |
| 273 |
# from "@" to " ". Split on both for backwards compatibility. |
| 274 |
pkg, other = re.split(r"\s|@", dep, maxsplit=1) |
| 275 |
outdated.append(pkg) |
| 276 |
|
| 277 |
return outdated |
| 278 |
|
| 279 |
|
| 280 |
def main(): |
| 281 |
arg_spec = dict( |
| 282 |
name=dict(type="str"), |
| 283 |
path=dict(type="path"), |
| 284 |
version=dict(type="str"), |
| 285 |
production=dict(default=False, type="bool"), |
| 286 |
executable=dict(type="path"), |
| 287 |
registry=dict(type="str"), |
| 288 |
state=dict(default="present", choices=["present", "absent", "latest"]), |
| 289 |
ignore_scripts=dict(default=False, type="bool"), |
| 290 |
unsafe_perm=dict(default=False, type="bool"), |
| 291 |
ci=dict(default=False, type="bool"), |
| 292 |
no_optional=dict(default=False, type="bool"), |
| 293 |
no_bin_links=dict(default=False, type="bool"), |
| 294 |
force=dict(default=False, type="bool"), |
| 295 |
) |
| 296 |
arg_spec["global"] = dict(default=False, type="bool") |
| 297 |
module = AnsibleModule( |
| 298 |
argument_spec=arg_spec, |
| 299 |
required_if=[ |
| 300 |
("state", "absent", ["name"]), |
| 301 |
("global", False, ["path"]), |
| 302 |
], |
| 303 |
supports_check_mode=True, |
| 304 |
) |
| 305 |
|
| 306 |
name = module.params["name"] |
| 307 |
path = module.params["path"] |
| 308 |
version = module.params["version"] |
| 309 |
glbl = module.params["global"] |
| 310 |
state = module.params["state"] |
| 311 |
|
| 312 |
npm = Npm( |
| 313 |
module, |
| 314 |
name=name, |
| 315 |
path=path, |
| 316 |
version=version, |
| 317 |
glbl=glbl, |
| 318 |
production=module.params["production"], |
| 319 |
executable=module.params["executable"], |
| 320 |
registry=module.params["registry"], |
| 321 |
ignore_scripts=module.params["ignore_scripts"], |
| 322 |
unsafe_perm=module.params["unsafe_perm"], |
| 323 |
state=state, |
| 324 |
no_optional=module.params["no_optional"], |
| 325 |
no_bin_links=module.params["no_bin_links"], |
| 326 |
force=module.params["force"], |
| 327 |
) |
| 328 |
|
| 329 |
changed = False |
| 330 |
if module.params["ci"]: |
| 331 |
npm.ci_install() |
| 332 |
changed = True |
| 333 |
elif state == "present": |
| 334 |
installed, missing = npm.list() |
| 335 |
if missing: |
| 336 |
changed = True |
| 337 |
npm.install() |
| 338 |
elif state == "latest": |
| 339 |
installed, missing = npm.list() |
| 340 |
outdated = npm.list_outdated() |
| 341 |
if missing: |
| 342 |
changed = True |
| 343 |
npm.install() |
| 344 |
if outdated: |
| 345 |
changed = True |
| 346 |
npm.update() |
| 347 |
else: # absent |
| 348 |
installed, missing = npm.list() |
| 349 |
if name in installed: |
| 350 |
changed = True |
| 351 |
npm.uninstall() |
| 352 |
|
| 353 |
module.exit_json(changed=changed) |
| 354 |
|
| 355 |
|
| 356 |
if __name__ == "__main__": |
| 357 |
main() |