Created
May 10, 2012 08:48
-
-
Save sorccu/2651959 to your computer and use it in GitHub Desktop.
puppet npm provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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