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`
This commit is contained in:
Greg 2021-03-30 13:58:55 +02:00
parent 9fc49deafe
commit edf2e071e4
18 changed files with 234 additions and 77 deletions

View File

@ -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'

View File

@ -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)

View File

@ -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])

View File

@ -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");

View File

@ -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

View File

@ -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: '' }

View File

@ -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

View File

@ -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

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
#
# Author:: Marius Ducea (marius@promethost.com)
# Cookbook:: nodejs
# Recipe:: default

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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'

View File

@ -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

View File

@ -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']

View File

@ -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

View File

@ -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"