Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent provider blowing up on ruby 1.8 agents #282

Merged
merged 1 commit into from
Apr 18, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions lib/puppet/provider/package/npm.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# We don't support using this module on puppet 3/ruby 1.8 systems.
# However, some puppet environments contain a mix of puppet 4 (supported) and older puppet 3 EL6 agents.
# We don't want puppet runs to fail on those old systems just because this module is in the same environment.
# rubocop:disable Style/HashSyntax
require 'puppet/provider/package'

Puppet::Type.type(:package).provide :npm, parent: Puppet::Provider::Package do
Puppet::Type.type(:package).provide :npm, :parent => Puppet::Provider::Package do
desc 'npm is the package manager for Node.js. This provider only handles global packages.'

confine feature: :npm
# We explicitly don't support ruby 1.8
confine :false => %r{^1\.8}.match(RUBY_VERSION)
confine :feature => :npm

has_feature :versionable, :install_options

if Puppet::Util::Package.versioncmp(Puppet.version, '3.0') >= 0
has_command(:npm, 'npm') do
is_optional
environment HOME: '/root'
end
else
optional_commands npm: 'npm'
has_command(:npm, 'npm') do
is_optional
environment :HOME => '/root'
end

def self.npmlist
# Ignore non-zero exit codes as they can be minor, just try and parse JSON
output = execute([command(:npm), 'list', '--json', '--global'], combine: false)
output = execute([command(:npm), 'list', '--json', '--global'], :combine => false)
Puppet.debug("Warning: npm list --json exited with code #{$CHILD_STATUS.exitstatus}") unless $CHILD_STATUS.success?
begin
# ignore any npm output lines to be a bit more robust
output = PSON.parse(output.lines.select { |l| l =~ %r{^((?!^npm).*)$} }.join("\n"), max_nesting: 100)
output = PSON.parse(output.lines.select { |l| l =~ %r{^((?!^npm).*)$} }.join("\n"), :max_nesting => 100)
@npmlist = output['dependencies'] || {}
rescue PSON::ParserError => e
Puppet.debug("Error: npm list --json command error #{e.message}")
Expand All @@ -37,7 +39,7 @@ def npmlist
def self.instances
@npmlist ||= npmlist
@npmlist.map do |k, v|
new(name: k, ensure: v['version'], provider: 'npm')
new(:name => k, :ensure => v['version'], :provider => 'npm')
end
end

Expand All @@ -46,9 +48,9 @@ def query

if list.key?(resource[:name]) && list[resource[:name]].key?('version')
version = list[resource[:name]]['version']
{ ensure: version, name: resource[:name] }
{ :ensure => version, :name => resource[:name] }
else
{ ensure: :absent, name: resource[:name] }
{ :ensure => :absent, :name => resource[:name] }
end
end

Expand Down