Skip to content

Instantly share code, notes, and snippets.

@sorccu
Created May 10, 2012 08:48
Show Gist options
  • Save sorccu/2651959 to your computer and use it in GitHub Desktop.
Save sorccu/2651959 to your computer and use it in GitHub Desktop.
puppet npm provider
# NPM provider for Puppet.
#
# Author:: Simo Kinnunen
# Copyright:: Copyright (c) 2012 CyberAgent, Inc.
# See:: http://rcrowley.org/talks/sv-puppet-2011-01-11/
# See:: https://github.com/newLoki/puppet-npm/
# See:: https://github.com/rcrowley/puppet-pip/
require 'puppet/provider/package'
Puppet::Type.type(:package).provide :npm, :parent => ::Puppet::Provider::Package do
desc "Node.js packages via `npm`."
has_feature :installable, :uninstallable, :upgradeable, :versionable
def self.parse(line)
if line.rstrip =~ / ([^ ]+)@([^ ]+)$/
{:ensure => $2, :name => $1, :provider => name}
else
nil
end
end
def self.instances
packages = []
cmd = which("npm") or return []
execpipe "#{cmd} -g ls" do |process|
process.collect do |line|
next unless options = parse(line)
packages << new(options)
end
end
packages
end
def install
args = %w{install -g}
case @resource[:ensure]
when String
args << "#{@resource[:name]}@#{@resource[:ensure]}"
when :latest
args << "#{@resource[:name]}@latest"
else
args << @resource[:name]
end
lazy_npm *args
end
def uninstall
lazy_npm "uninstall", "-g", @resource[:name]
end
def latest
cmd = which("npm") or return nil
output = execute ["#{cmd}", "view", "#{resource[:name]}", "version"]
self.fail "Could not get latest for #{resource[:name]}, npm not happy" if output.include?("npm not ok")
output.strip
end
def update
install
end
def query
self.class.instances.each do |package|
return package.properties if @resource[:name] == package.name
end
return nil
end
private
def lazy_npm(*args)
npm *args
rescue NoMethodError => e
if pathname = which('npm')
self.class.commands :npm => pathname
npm *args
else
raise e
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment