From edf2e071e41a46545e71e7641212fa7856153b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Kar=C3=A9kinian?= Date: Tue, 30 Mar 2021 13:58:55 +0200 Subject: [PATCH] Upgrade nodejs to the latest version from the repo This is possible in newer versions of the nodejs cookbook, by setting the `node["nodejs"]["package_action"]["nodejs"]` attribute to `:upgrade` --- Berksfile | 2 +- Berksfile.lock | 7 +- cookbooks/nodejs/CHANGELOG.md | 72 ++++++++++++++++--- cookbooks/nodejs/README.md | 43 ++++++++--- cookbooks/nodejs/attributes/default.rb | 13 ++-- cookbooks/nodejs/attributes/packages.rb | 10 ++- cookbooks/nodejs/attributes/repo.rb | 6 +- cookbooks/nodejs/libraries/nodejs_helper.rb | 23 +++++- cookbooks/nodejs/metadata.json | 49 ++++++++++++- cookbooks/nodejs/recipes/default.rb | 2 +- .../nodejs/recipes/nodejs_from_binary.rb | 4 +- .../nodejs/recipes/nodejs_from_package.rb | 5 +- .../nodejs/recipes/nodejs_from_source.rb | 12 ++-- cookbooks/nodejs/recipes/npm_from_source.rb | 2 +- cookbooks/nodejs/recipes/npm_packages.rb | 22 +++--- cookbooks/nodejs/recipes/repo.rb | 3 +- cookbooks/nodejs/resources/npm_package.rb | 17 ++++- .../kosmos-nodejs/recipes/default.rb | 19 ++--- 18 files changed, 234 insertions(+), 77 deletions(-) diff --git a/Berksfile b/Berksfile index 374c10b..bcbf7f6 100644 --- a/Berksfile +++ b/Berksfile @@ -34,7 +34,7 @@ cookbook 'apt', '~> 7.3.0' cookbook 'git', '~> 10.0.0' cookbook 'hostsfile', '~> 3.0.1' cookbook 'ohai', '~> 5.2.5' -cookbook 'nodejs', '~> 5.0.0' +cookbook 'nodejs', '~> 7.3.1' cookbook 'timezone_iii', '= 1.0.4' cookbook 'ark', '>= 5.0.0' cookbook 'logrotate', '= 2.2.0' diff --git a/Berksfile.lock b/Berksfile.lock index 20ece54..aa8574a 100644 --- a/Berksfile.lock +++ b/Berksfile.lock @@ -29,7 +29,7 @@ DEPENDENCIES ref: nginx mysql (~> 8.7.3) nginx (= 9.0.0) - nodejs (~> 5.0.0) + nodejs (~> 7.3.1) ntp (= 3.4.0) ohai (~> 5.2.5) openssl (~> 8.5.5) @@ -74,6 +74,7 @@ GRAPH mingw (>= 1.1) seven_zip (>= 0.0.0) chef-sugar (3.3.0) + chocolatey (3.0.0) compat_resource (12.19.0) composer (2.7.0) apt (>= 0.0.0) @@ -114,9 +115,9 @@ GRAPH build-essential (>= 5.0) ohai (>= 4.1.0) yum-epel (>= 0.0.0) - nodejs (5.0.0) + nodejs (7.3.1) ark (>= 2.0.2) - build-essential (>= 0.0.0) + chocolatey (>= 3.0) ntp (3.4.0) ohai (5.2.5) openssl (8.5.5) diff --git a/cookbooks/nodejs/CHANGELOG.md b/cookbooks/nodejs/CHANGELOG.md index 752de3f..fb7fd29 100644 --- a/cookbooks/nodejs/CHANGELOG.md +++ b/cookbooks/nodejs/CHANGELOG.md @@ -1,5 +1,57 @@ # NodeJS Cookbook Changelog +## 7.3.1 - *2020-12-31* + +- resolved cookstyle error: attributes/packages.rb:15:55 convention: `Layout/TrailingEmptyLines` +- resolved cookstyle error: test/cookbooks/test/recipes/resource.rb:118:1 convention: `Layout/TrailingEmptyLines` +- Enable builds for opensuse-leap-15 +- Add a library method test + +## 7.3.0 (2020-10-21) + +- Add rspec tests for the library methods +- Update the url_invalid? method to return false if it detects an invalid uri +- Add the auto_update option to the npm_package resource. Allows turning off auto_update of npm packages. +- Allow actions and options for OS package installation to be specified as attributes +- Add the live_stream parameter to the npm_package execution to get better installation diagnostics +- Add the auto_update option to the npm_package resource. Allows turning off auto_update of npm packages. +- Update testing + +## 7.2.0 (2020-10-07) + +- Verify the URI of installed packages to help determine if a good URI has been installed +- Add tests that verify npm-package installed packages +- Get the example test cookbook working +- Add support for installing node on windows + +## 7.1.0 (2020-10-01) + +- resolved cookstyle error: recipes/nodejs_from_binary.rb:19:1 refactor: `ChefCorrectness/IncorrectLibraryInjection` +- resolved cookstyle error: recipes/nodejs_from_source.rb:21:1 refactor: `ChefCorrectness/IncorrectLibraryInjection` +- resolved cookstyle error: recipes/npm_from_source.rb:21:1 refactor: `ChefCorrectness/IncorrectLibraryInjection` +- Have ark setup node and npm binaries into PATH +- Add `node_env` to `npm_package` in order to set `NODE_ENV` (useful for some packages) +- Include `npx` as a binary in addition to `npm`, it has been [included since `npm` v5.2.0](https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b) + +## 7.0.1 (2020-06-04) + +- Minor readme fix + +## 7.0.0 (2020-06-04) + +- Require Chef Infra Client 14+ and remove the build-essential dependency +- Updated the default to Node.js v10.16.3 +- Added compatibility with Chef Infra Client 16.2+ +- Removed Foodcritic testing +- Updated ChefSpec and Kitchen platforms +- Resolved multiple minor cookstyle issues in the cookbook +- Added a vscode editor config + +## 6.0.0 (2018-10-11) + +- Use the build_essential resource in the source install recipe instead of the build-essential::default recipe. This way we can use the new built-in build_essential resource in Chef 14+ +- Set default version to Node.js v8.12.0 + ## 5.0.0 (2017-11-15) - nodejs_npm resource has been converted to a custom resource and renamed to npm_package. The existing resource name will continue to function, but over time code should be updated for the new name. This name change has been made so we can eventually merge this resource into the chef-client. @@ -102,34 +154,34 @@ - implement installation from package on RedHat - ([@vaskas]) -## v1.1.3: +## v1.1.3 - update default version of node to 0.10.13 - and npm - v1.3.4 ([@jodosha][]) -## v1.1.2: +## v1.1.2 - update default version of node to 0.10.2 - ([@bakins]) - fully migrated to test-kitchen 1.alpha and vagrant 1.1.x/berkshelf 1.3.1 -## v1.1.1: +## v1.1.1 - update default versions to the latest: node - v0.10.0 and npm - v1.2.14 - `make_thread` is now a real attribute - ([@ChrisLundquist]) -## v1.1.0: +## v1.1.0 - rewrite the package install; remove rpm support since there are no longer any packages available anywhere - add support to install `legacy_packages` from ubuntu repo as well as the latest 0.10.x branch (this is default). -## v1.0.4: +## v1.0.4 - add support for binary installation method ([@JulesAU]) -## v1.0.3: +## v1.0.3 -- unreleased +- 7.3.1 - *2020-12-31* -## v1.0.2: +## v1.0.2 - add smartos support for package install ([@sax]) - support to compile with all processors available (default 2 if unknown) - ([@ChrisLundquist]) @@ -137,12 +189,12 @@ - ensure npm recipe honours the 'source' or 'package' setting - ([@markbirbeck]) - updated the default versions to the latest stable node/npm -## v1.0.1: +## v1.0.1 - fixed bug that prevented overwritting the node/npm versions (moved the `src_url`s as local variables instead of attributes) - ([@johannesbecker]) - updated the default versions to the latest node/npm -## v1.0.0: +## v1.0.0 - added packages installation support ([@smith]) diff --git a/cookbooks/nodejs/README.md b/cookbooks/nodejs/README.md index 6aa7fc2..bb3f27a 100644 --- a/cookbooks/nodejs/README.md +++ b/cookbooks/nodejs/README.md @@ -1,6 +1,6 @@ # [nodejs-cookbook](https://github.com/redguide/nodejs) -[![CK Version](http://img.shields.io/cookbook/v/nodejs.svg?branch=master)](https://supermarket.chef.io/cookbooks/nodejs) [![Build Status](https://img.shields.io/travis/redguide/nodejs.svg)](https://travis-ci.org/redguide/nodejs) [![Gitter chat](https://badges.gitter.im/redguide/nodejs.svg)](https://gitter.im/redguide/nodejs) +[![CK Version](http://img.shields.io/cookbook/v/nodejs.svg?branch=master)](https://supermarket.chef.io/cookbooks/nodejs) [![Build Status](https://img.shields.io/travis/redguide/nodejs.svg)](https://travis-ci.org/redguide/nodejs) Installs node.js/npm and includes a resource for managing npm packages @@ -11,23 +11,23 @@ Installs node.js/npm and includes a resource for managing npm packages - Debian/Ubuntu - RHEL/CentOS/Scientific/Amazon/Oracle - openSUSE +- Windows Note: Source installs require GCC 4.8+, which is not included on older distro releases ### Chef -- Chef 12.14+ +- Chef Infra Client 14+ ### Cookbooks -- build-essential - ark ## Usage Include the nodejs recipe to install node on your system based on the default installation method: -```chef +```ruby include_recipe "nodejs" ``` @@ -37,7 +37,7 @@ include_recipe "nodejs" Install node from packages: -```chef +```ruby node['nodejs']['install_method'] = 'package' # Not necessary because it's the default include_recipe "nodejs" # Or @@ -50,7 +50,7 @@ By default this will setup deb/rpm repositories from nodesource.com, which inclu Install node from official prebuilt binaries: -```chef +```ruby node['nodejs']['install_method'] = 'binary' include_recipe "nodejs" @@ -72,18 +72,29 @@ node.default['nodejs']['binary']['checksum'] = '0bd86f2a39221b532172c7d1acb57f0b Install node from sources: -```chef +```ruby node['nodejs']['install_method'] = 'source' include_recipe "nodejs" # Or include_recipe "nodejs::nodejs_from_source" ``` +#### Chocolatey + +Install node from chocolatey: + +```chef +node['nodejs']['install_method'] = 'chocolatey' +include_recipe "nodejs" +# Or +include_recipe "nodejs::nodejs_from_chocolatey" +``` + ## NPM Npm is included in nodejs installs by default. By default, we are using it and call it `embedded`. Adding recipe `nodejs::npm` assure you to have npm installed and let you choose install method with `node['nodejs']['npm']['install_method']` -```chef +```ruby include_recipe "nodejs::npm" ``` @@ -95,7 +106,7 @@ _Warning:_ This recipe will include the `nodejs` recipe, which by default includ note: This resource was previously named nodejs_npm. Calls to that resource name will still function, but cookbooks should be updated for the new npm_package resource name. -`npm_package` let you install npm packages from various sources: +`npm_package` lets you install npm packages from various sources: - npm registry: @@ -115,11 +126,17 @@ Packages can be installed globally (by default) or in a directory (by using `att You can specify an `NPM_TOKEN` environment variable for accessing [NPM private modules](https://docs.npmjs.com/private-modules/intro) by using `attribute :npm_token` +You can specify a `NODE_ENV` environment variable, in the case that some element of your installation depends on this by using `attribute :node_env`. E.g., using [`node-config`](https://www.npmjs.com/package/config) as part of your postinstall script. Please note that adding the `--production` option will override this to `NODE_ENV=production`. + You can append more specific options to npm command with `attribute :options` array : +You can specify auto_update as false to stop the npm install command from running and updating an installed package. Running the command will update packages within restrictions imposed by a package.json file. The default behavior is to update automatically. + - use an array of options (w/ dash), they will be added to npm call. - ex: `['--production','--force']` or `['--force-latest']` +You can specify live_stream true for the resource to have the package install information included in the chef-client log outout for better npm package diagnostics and trouble shooting. + This LWRP attempts to use vanilla npm as much as possible (no custom wrapper). ### Packages @@ -139,6 +156,7 @@ npm_package 'grunt' do path '/home/random/grunt' json true user 'random' + node_env 'staging' end npm_package 'my_private_module' do @@ -180,9 +198,12 @@ Or add packages via attributes (which accept the same attributes as the LWRP abo ## License & Authors -**Author:** Marius Ducea (marius@promethost.com) **Author:** Nathan L Smith (nlloyds@gmail.com) **Author:** Guilhem Lettron (guilhem@lettron.fr) **Author:** Barthelemy Vessemont (bvessemont@gmail.com) +**Author:** Marius Ducea (marius@promethost.com) +**Author:** Nathan L Smith (nlloyds@gmail.com) +**Author:** Guilhem Lettron (guilhem@lettron.fr) +**Author:** Barthelemy Vessemont (bvessemont@gmail.com) -**Copyright:** 2008-2017, Chef Software, Inc. +**Copyright:** Chef Software, Inc. ``` Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/cookbooks/nodejs/attributes/default.rb b/cookbooks/nodejs/attributes/default.rb index 2ab61bf..3e043c6 100644 --- a/cookbooks/nodejs/attributes/default.rb +++ b/cookbooks/nodejs/attributes/default.rb @@ -20,21 +20,24 @@ case node['platform_family'] when 'smartos', 'rhel', 'debian', 'fedora', 'mac_os_x', 'suse', 'amazon' default['nodejs']['install_method'] = 'package' +when 'windows' + default['nodejs']['install_method'] = 'chocolatey' else default['nodejs']['install_method'] = 'source' end -default['nodejs']['version'] = '6.10.2' +default['nodejs']['version'] = '10.16.3' default['nodejs']['prefix_url']['node'] = 'https://nodejs.org/dist/' +default['nodejs']['tmpdir'] = '/tmp' default['nodejs']['source']['url'] = nil # Auto generated -default['nodejs']['source']['checksum'] = '9b897dd6604d50ae5fff25fd14b1c4035462d0598735799e0cfb4f17cb6e0d19' +default['nodejs']['source']['checksum'] = 'db5a5e03a815b84a1266a4b48bb6a6d887175705f84fd2472f0d28e5e305a1f8' default['nodejs']['binary']['url'] = nil # Auto generated -default['nodejs']['binary']['checksum']['linux_x64'] = '35accd2d9ccac747eff0f236e2843bc2198ba7765e2340441d6230861bae4e1b' -default['nodejs']['binary']['checksum']['linux_x86'] = '6721221fab4e3b3a1be6573900b9e368c7a74ac1c1c3ae982e49c5583e8962e3' -default['nodejs']['binary']['checksum']['linux_arm64'] = '97de0340b6dbf38e3d995df880a94c58d403c3054676d8fc9192b83a3735f0b8' +default['nodejs']['binary']['checksum']['linux_x64'] = '2f0397bb81c1d0c9901b9aff82a933257bf60f3992227b86107111a75b9030d9' +default['nodejs']['binary']['checksum']['linux_arm64'] = '3bab16e7107092e43426e082ee9fd88ef0a43a35816f662f14563bcc5152600d' +default['nodejs']['binary']['append_env_path'] = true default['nodejs']['make_threads'] = node['cpu'] ? node['cpu']['total'].to_i : 2 diff --git a/cookbooks/nodejs/attributes/packages.rb b/cookbooks/nodejs/attributes/packages.rb index 169e28a..38754b1 100644 --- a/cookbooks/nodejs/attributes/packages.rb +++ b/cookbooks/nodejs/attributes/packages.rb @@ -2,10 +2,14 @@ include_attribute 'nodejs::default' include_attribute 'nodejs::repo' default['nodejs']['packages'] = value_for_platform_family( - 'debian' => node['nodejs']['install_repo'] ? ['nodejs'] : ['nodejs', 'npm', 'nodejs-dev'], - %w(rhel fedora amazon) => node['nodejs']['install_repo'] ? ['nodejs', 'nodejs-devel'] : ['nodejs', 'npm', 'nodejs-dev'], - 'suse' => node['platform_version'].to_i < 42 ? ['nodejs', 'nodejs-devel'] : ['nodejs4', 'npm4', 'nodejs4-devel'], + 'debian' => node['nodejs']['install_repo'] ? ['nodejs'] : %w(nodejs npm nodejs-dev), + %w(rhel fedora amazon) => node['nodejs']['install_repo'] ? %w(nodejs nodejs-devel) : %w(nodejs npm nodejs-dev), + 'suse' => node['platform_version'].to_i < 42 ? %w(nodejs10 npm10 nodejs10-devel) : %w(nodejs4 npm4 nodejs4-devel), 'mac_os_x' => ['node'], 'freebsd' => %w(node npm), 'default' => ['nodejs'] ) + +# Add options and actions by package name +default['nodejs']['package_action'] = { default: :install } +default['nodejs']['package_options'] = { default: '' } diff --git a/cookbooks/nodejs/attributes/repo.rb b/cookbooks/nodejs/attributes/repo.rb index fa95c19..6e6a344 100644 --- a/cookbooks/nodejs/attributes/repo.rb +++ b/cookbooks/nodejs/attributes/repo.rb @@ -1,12 +1,12 @@ case node['platform_family'] when 'debian' default['nodejs']['install_repo'] = true - default['nodejs']['repo'] = 'https://deb.nodesource.com/node_6.x' + default['nodejs']['repo'] = 'https://deb.nodesource.com/node_10.x' default['nodejs']['keyserver'] = 'keyserver.ubuntu.com' default['nodejs']['key'] = '1655a0ab68576280' -when 'rhel', 'amazon' +when 'rhel', 'fedora', 'amazon' default['nodejs']['install_repo'] = true release_ver = platform?('amazon') ? 6 : node['platform_version'].to_i - default['nodejs']['repo'] = "https://rpm.nodesource.com/pub_6.x/el/#{release_ver}/$basearch" + default['nodejs']['repo'] = "https://rpm.nodesource.com/pub_10.x/el/#{release_ver}/$basearch" default['nodejs']['key'] = 'https://rpm.nodesource.com/pub/el/NODESOURCE-GPG-SIGNING-KEY-EL' end diff --git a/cookbooks/nodejs/libraries/nodejs_helper.rb b/cookbooks/nodejs/libraries/nodejs_helper.rb index d566065..f6f46c7 100644 --- a/cookbooks/nodejs/libraries/nodejs_helper.rb +++ b/cookbooks/nodejs/libraries/nodejs_helper.rb @@ -22,7 +22,25 @@ module NodeJs Mixlib::ShellOut.new("npm list #{package} -global -json", environment: environment) end - JSON.parse(cmd.run_command.stdout, max_nesting: false) + begin + JSON.parse(cmd.run_command.stdout, max_nesting: false) + rescue JSON::ParserError => e + Chef::Log.error("nodejs::library::nodejs_helper::npm_list exception #{e}") + {} + end + end + + def url_valid?(list, package) + require 'open-uri' + + begin + URI.parse(list.fetch(package, {}).fetch('resolved')) + rescue KeyError + # the package may have been installed without using a url + true + rescue URI::InvalidURIError + false + end end def version_valid?(list, package, version) @@ -34,7 +52,8 @@ module NodeJs list = npm_list(package, path, environment)['dependencies'] # Return true if package installed and installed to good version - !list.nil? && list.key?(package) && version_valid?(list, package, version) + # see if we really want to add the url check + !list.nil? && list.key?(package) && version_valid?(list, package, version) && url_valid?(list, package) end end end diff --git a/cookbooks/nodejs/metadata.json b/cookbooks/nodejs/metadata.json index 0c1afd9..44a8dc0 100644 --- a/cookbooks/nodejs/metadata.json +++ b/cookbooks/nodejs/metadata.json @@ -1 +1,48 @@ -{"name":"nodejs","version":"5.0.0","description":"Installs/Configures node.js","long_description":"# [nodejs-cookbook](https://github.com/redguide/nodejs)\n\n[![CK Version](http://img.shields.io/cookbook/v/nodejs.svg?branch=master)](https://supermarket.chef.io/cookbooks/nodejs) [![Build Status](https://img.shields.io/travis/redguide/nodejs.svg)](https://travis-ci.org/redguide/nodejs) [![Gitter chat](https://badges.gitter.im/redguide/nodejs.svg)](https://gitter.im/redguide/nodejs)\n\nInstalls node.js/npm and includes a resource for managing npm packages\n\n## Requirements\n\n### Platforms\n\n- Debian/Ubuntu\n- RHEL/CentOS/Scientific/Amazon/Oracle\n- openSUSE\n\nNote: Source installs require GCC 4.8+, which is not included on older distro releases\n\n### Chef\n\n- Chef 12.14+\n\n### Cookbooks\n\n- build-essential\n- ark\n\n## Usage\n\nInclude the nodejs recipe to install node on your system based on the default installation method:\n\n```chef\ninclude_recipe \"nodejs\"\n```\n\n### Install methods\n\n#### Package\n\nInstall node from packages:\n\n```chef\nnode['nodejs']['install_method'] = 'package' # Not necessary because it's the default\ninclude_recipe \"nodejs\"\n# Or\ninclude_recipe \"nodejs::nodejs_from_package\"\n```\n\nBy default this will setup deb/rpm repositories from nodesource.com, which include up to date NodeJS packages. If you prefer to use distro provided package you can disable this behavior by setting `node['nodejs']['install_repo']` to `false`.\n\n#### Binary\n\nInstall node from official prebuilt binaries:\n\n```chef\nnode['nodejs']['install_method'] = 'binary'\ninclude_recipe \"nodejs\"\n\n# Or\ninclude_recipe \"nodejs::nodejs_from_binary\"\n\n# Or set a specific version of nodejs to be installed\nnode.default['nodejs']['install_method'] = 'binary'\nnode.default['nodejs']['version'] = '5.9.0'\nnode.default['nodejs']['binary']['checksum'] = '99c4136cf61761fac5ac57f80544140a3793b63e00a65d4a0e528c9db328bf40'\n\n# Or fetch the binary from your own location\nnode.default['nodejs']['install_method'] = 'binary'\nnode.default['nodejs']['binary']['url'] = 'https://s3.amazonaws.com/my-bucket/node-v7.8.0-linux-x64.tar.gz'\nnode.default['nodejs']['binary']['checksum'] = '0bd86f2a39221b532172c7d1acb57f0b0cba88c7b82ea74ba9d1208b9f6f9697'\n```\n\n#### Source\n\nInstall node from sources:\n\n```chef\nnode['nodejs']['install_method'] = 'source'\ninclude_recipe \"nodejs\"\n# Or\ninclude_recipe \"nodejs::nodejs_from_source\"\n```\n\n## NPM\n\nNpm is included in nodejs installs by default. By default, we are using it and call it `embedded`. Adding recipe `nodejs::npm` assure you to have npm installed and let you choose install method with `node['nodejs']['npm']['install_method']`\n\n```chef\ninclude_recipe \"nodejs::npm\"\n```\n\n_Warning:_ This recipe will include the `nodejs` recipe, which by default includes `nodejs::nodejs_from_package` if you did not set `node['nodejs']['install_method']`.\n\n## Resources\n\n### npm_package\n\nnote: This resource was previously named nodejs_npm. Calls to that resource name will still function, but cookbooks should be updated for the new npm_package resource name.\n\n`npm_package` let you install npm packages from various sources:\n\n- npm registry:\n\n - name: `property :package`\n - version: `property :version` (optional)\n\n- url: `property :url`\n\n - for git use `git://{your_repo}`\n\n- from a json (package.json by default): `property :json`\n\n - use `true` for default\n - use a `String` to specify json file\n\nPackages can be installed globally (by default) or in a directory (by using `attribute :path`)\n\nYou can specify an `NPM_TOKEN` environment variable for accessing [NPM private modules](https://docs.npmjs.com/private-modules/intro) by using `attribute :npm_token`\n\nYou can append more specific options to npm command with `attribute :options` array :\n\n- use an array of options (w/ dash), they will be added to npm call.\n- ex: `['--production','--force']` or `['--force-latest']`\n\nThis LWRP attempts to use vanilla npm as much as possible (no custom wrapper).\n\n### Packages\n\n```ruby\nnpm_package 'express'\n\nnpm_package 'async' do\n version '0.6.2'\nend\n\nnpm_package 'request' do\n url 'github mikeal/request'\nend\n\nnpm_package 'grunt' do\n path '/home/random/grunt'\n json true\n user 'random'\nend\n\nnpm_package 'my_private_module' do\n path '/home/random/myproject' # The root path to your project, containing a package.json file\n json true\n npm_token '12345-abcde-e5d4c3b2a1'\n user 'random'\n options ['--production'] # Only install dependencies. Skip devDependencies\nend\n```\n\n[Working Examples](test/cookbooks/nodejs_test/recipes/npm.rb)\n\nOr add packages via attributes (which accept the same attributes as the LWRP above):\n\n```json\n\"nodejs\": {\n \"npm_packages\": [\n {\n \"name\": \"express\"\n },\n {\n \"name\": \"async\",\n \"version\": \"0.6.2\"\n },\n {\n \"name\": \"request\",\n \"url\": \"github mikeal/request\"\n }\n {\n \"name\": \"grunt\",\n \"path\": \"/home/random/grunt\",\n \"json\": true,\n \"user\": \"random\"\n }\n ]\n}\n```\n\n## License & Authors\n\n**Author:** Marius Ducea (marius@promethost.com) **Author:** Nathan L Smith (nlloyds@gmail.com) **Author:** Guilhem Lettron (guilhem@lettron.fr) **Author:** Barthelemy Vessemont (bvessemont@gmail.com)\n\n**Copyright:** 2008-2017, Chef Software, Inc.\n\n```\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n```\n","maintainer":"redguide","maintainer_email":"guilhem@lettron.fr","license":"Apache-2.0","platforms":{"debian":">= 0.0.0","ubuntu":">= 0.0.0","centos":">= 0.0.0","redhat":">= 0.0.0","scientific":">= 0.0.0","oracle":">= 0.0.0","amazon":">= 0.0.0","smartos":">= 0.0.0","mac_os_x":">= 0.0.0","opensuseleap":">= 0.0.0","suse":">= 0.0.0"},"dependencies":{"build-essential":">= 0.0.0","ark":">= 2.0.2"},"recommendations":{},"suggestions":{},"conflicting":{},"providing":{},"replacing":{},"attributes":{},"groupings":{},"recipes":{},"source_url":"https://github.com/redguide/nodejs","issues_url":"https://github.com/redguide/nodejs/issues","chef_version":[[">= 12.14"]],"ohai_version":[]} \ No newline at end of file +{ + "name": "nodejs", + "description": "Installs/Configures node.js", + "long_description": "", + "maintainer": "redguide", + "maintainer_email": "guilhem@lettron.fr", + "license": "Apache-2.0", + "platforms": { + "debian": ">= 0.0.0", + "ubuntu": ">= 0.0.0", + "centos": ">= 0.0.0", + "redhat": ">= 0.0.0", + "scientific": ">= 0.0.0", + "oracle": ">= 0.0.0", + "amazon": ">= 0.0.0", + "smartos": ">= 0.0.0", + "mac_os_x": ">= 0.0.0", + "opensuseleap": ">= 0.0.0", + "suse": ">= 0.0.0", + "windows": ">= 0.0.0" + }, + "dependencies": { + "ark": ">= 2.0.2", + "chocolatey": ">= 3.0" + }, + "providing": { + + }, + "recipes": { + + }, + "version": "7.3.1", + "source_url": "https://github.com/redguide/nodejs", + "issues_url": "https://github.com/redguide/nodejs/issues", + "privacy": false, + "chef_versions": [ + [ + ">= 14" + ] + ], + "ohai_versions": [ + + ], + "gems": [ + + ], + "eager_load_libraries": true +} diff --git a/cookbooks/nodejs/recipes/default.rb b/cookbooks/nodejs/recipes/default.rb index a4234be..aac903e 100644 --- a/cookbooks/nodejs/recipes/default.rb +++ b/cookbooks/nodejs/recipes/default.rb @@ -1,4 +1,4 @@ -# + # Author:: Marius Ducea (marius@promethost.com) # Cookbook:: nodejs # Recipe:: default diff --git a/cookbooks/nodejs/recipes/nodejs_from_binary.rb b/cookbooks/nodejs/recipes/nodejs_from_binary.rb index 640e12c..0c05710 100644 --- a/cookbooks/nodejs/recipes/nodejs_from_binary.rb +++ b/cookbooks/nodejs/recipes/nodejs_from_binary.rb @@ -16,7 +16,7 @@ # limitations under the License. # -Chef::Recipe.send(:include, NodeJs::Helper) +Chef::DSL::Recipe.include NodeJs::Helper # Shamelessly borrowed from http://docs.chef.io/dsl_recipe_method_platform.html # Surely there's a more canonical way to get arch? @@ -47,6 +47,7 @@ archive_name = 'nodejs-binary' binaries = ['bin/node'] binaries.push('bin/npm') if node['nodejs']['npm']['install_method'] == 'embedded' +binaries.push('bin/npx') if node['nodejs']['npm']['install_method'] == 'embedded' if node['nodejs']['binary']['url'] nodejs_bin_url = node['nodejs']['binary']['url'] @@ -61,5 +62,6 @@ ark archive_name do version node['nodejs']['version'] checksum checksum has_binaries binaries + append_env_path node['nodejs']['binary']['append_env_path'] action :install end diff --git a/cookbooks/nodejs/recipes/nodejs_from_package.rb b/cookbooks/nodejs/recipes/nodejs_from_package.rb index 6bd995d..fdd80e7 100644 --- a/cookbooks/nodejs/recipes/nodejs_from_package.rb +++ b/cookbooks/nodejs/recipes/nodejs_from_package.rb @@ -29,5 +29,8 @@ unless node['nodejs']['packages'] end node['nodejs']['packages'].each do |node_pkg| - package node_pkg + package node_pkg do + action node['nodejs']['package_action'][node_pkg] if node['nodejs']['package_action'][node_pkg] + options node['nodejs']['package_options'][node_pkg] if node['nodejs']['package_options'][node_pkg] + end end diff --git a/cookbooks/nodejs/recipes/nodejs_from_source.rb b/cookbooks/nodejs/recipes/nodejs_from_source.rb index ace91e1..865dbbf 100644 --- a/cookbooks/nodejs/recipes/nodejs_from_source.rb +++ b/cookbooks/nodejs/recipes/nodejs_from_source.rb @@ -18,15 +18,18 @@ # limitations under the License. # -Chef::Recipe.send(:include, NodeJs::Helper) +Chef::DSL::Recipe.include NodeJs::Helper -include_recipe 'build-essential' +build_essential 'install build tools' case node['platform_family'] when 'rhel', 'fedora', 'amazon' - package %w(openssl-devel tar) + # The ark resource uses and requires python2 for builds + package %w(openssl-devel python2 tar) when 'debian' - package 'libssl-dev' + package %w(libssl-dev python) +when 'suse' + package %w(python) end version = "v#{node['nodejs']['version']}/" @@ -42,4 +45,5 @@ ark archive_name do checksum node['nodejs']['source']['checksum'] make_opts ["-j #{node['nodejs']['make_threads']}"] action :install_with_make + environment(PYTHON: 'python2') end diff --git a/cookbooks/nodejs/recipes/npm_from_source.rb b/cookbooks/nodejs/recipes/npm_from_source.rb index bfe6871..d2dc71d 100644 --- a/cookbooks/nodejs/recipes/npm_from_source.rb +++ b/cookbooks/nodejs/recipes/npm_from_source.rb @@ -18,7 +18,7 @@ # limitations under the License. # -Chef::Recipe.send(:include, NodeJs::Helper) +Chef::DSL::Recipe.include NodeJs::Helper include_recipe 'nodejs::nodejs_from_source' diff --git a/cookbooks/nodejs/recipes/npm_packages.rb b/cookbooks/nodejs/recipes/npm_packages.rb index 3331fb3..ec0d3c5 100644 --- a/cookbooks/nodejs/recipes/npm_packages.rb +++ b/cookbooks/nodejs/recipes/npm_packages.rb @@ -1,11 +1,13 @@ -node['nodejs']['npm_packages'].each do |pkg| - pkg_action = pkg.key?('action') ? pkg['action'] : :install - f = npm_package "nodejs_npm-#{pkg['name']}-#{pkg_action}" do - action :nothing - package pkg['name'] +if node['nodejs'].key?('npm_packages') + node['nodejs']['npm_packages'].each do |pkg| + pkg_action = pkg.key?('action') ? pkg['action'] : :install + f = npm_package "nodejs_npm-#{pkg['name']}-#{pkg_action}" do + action :nothing + package pkg['name'] + end + pkg.each do |key, value| + f.send(key, value) unless key == 'name' || key == 'action' + end + f.action(pkg_action) end - pkg.each do |key, value| - f.send(key, value) unless key == 'name' || key == 'action' - end - f.action(pkg_action) -end if node['nodejs'].key?('npm_packages') +end diff --git a/cookbooks/nodejs/recipes/repo.rb b/cookbooks/nodejs/recipes/repo.rb index 6ac4ca2..14b98e4 100644 --- a/cookbooks/nodejs/recipes/repo.rb +++ b/cookbooks/nodejs/recipes/repo.rb @@ -6,12 +6,11 @@ when 'debian' apt_repository 'node.js' do uri node['nodejs']['repo'] - distribution node['lsb']['codename'] components ['main'] keyserver node['nodejs']['keyserver'] key node['nodejs']['key'] end -when 'rhel', 'amazon' +when 'rhel', 'fedora', 'amazon' yum_repository 'node.js' do description 'nodesource.com nodejs repository' baseurl node['nodejs']['repo'] diff --git a/cookbooks/nodejs/resources/npm_package.rb b/cookbooks/nodejs/resources/npm_package.rb index 7bd170a..71ae487 100644 --- a/cookbooks/nodejs/resources/npm_package.rb +++ b/cookbooks/nodejs/resources/npm_package.rb @@ -20,19 +20,23 @@ # resource_name :npm_package +provides :npm_package # backwards compatibility for the old resource name provides :nodejs_npm -property :package, name_property: true +property :package, String, name_property: true property :version, String property :path, String property :url, String -property :json, [String, true] +property :json, [String, true, false] property :npm_token, String property :options, Array, default: [] property :user, String property :group, String +property :live_stream, [false, true], default: false +property :node_env, String +property :auto_update, [true, false], default: true def initialize(*args) super @@ -46,7 +50,8 @@ action :install do user new_resource.user group new_resource.group environment npm_env_vars - not_if { package_installed? } + live_stream new_resource.live_stream + not_if { package_installed? && no_auto_update? } end end @@ -57,6 +62,7 @@ action :uninstall do user new_resource.user group new_resource.group environment npm_env_vars + live_stream new_resource.live_stream only_if { package_installed? } end end @@ -69,6 +75,7 @@ action_class do env_vars['HOME'] = ::Dir.home(new_resource.user) if new_resource.user env_vars['USER'] = new_resource.user if new_resource.user env_vars['NPM_TOKEN'] = new_resource.npm_token if new_resource.npm_token + env_vars['NODE_ENV'] = new_resource.node_env if new_resource.node_env env_vars end @@ -77,6 +84,10 @@ action_class do new_resource.package && npm_package_installed?(new_resource.package, new_resource.version, new_resource.path, new_resource.npm_token) end + def no_auto_update? + new_resource.package && !new_resource.auto_update + end + def npm_options options = '' options << ' -global' unless new_resource.path diff --git a/site-cookbooks/kosmos-nodejs/recipes/default.rb b/site-cookbooks/kosmos-nodejs/recipes/default.rb index 1c8773a..de8e58a 100644 --- a/site-cookbooks/kosmos-nodejs/recipes/default.rb +++ b/site-cookbooks/kosmos-nodejs/recipes/default.rb @@ -24,18 +24,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -# Get package for trusty when still using vivid -if node['lsb']['codename'] == "vivid" - node.override["nodejs"]["install_repo"] = false - apt_repository "node.js" do - uri "https://deb.nodesource.com/node_8.x" - distribution "trusty" - components ["main"] - keyserver node["nodejs"]["keyserver"] - key node["nodejs"]["key"] - end - include_recipe "nodejs" -else - node.override["nodejs"]["repo"] = "https://deb.nodesource.com/node_12.x" - include_recipe "nodejs::nodejs_from_package" -end +node.override["nodejs"]["repo"] = "https://deb.nodesource.com/node_12.x" +# Allows upgrading +node.override["nodejs"]["package_action"]["nodejs"] = :upgrade +include_recipe "nodejs::nodejs_from_package"