Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit e7f5c2c20e47024afc53386e1c1f7a97370404ab @matmannion matmannion committed Apr 5, 2012
Showing with 96 additions and 0 deletions.
  1. +15 −0 README.md
  2. +81 −0 lib/puppet/provider/package/npm.rb
15 README.md
@@ -0,0 +1,15 @@
+A Puppet Package provider for npm
+=================================
+
+Based loosely on the work done by @boundvariable: https://github.com/boundvariable/puppet-npm
+
+This allows installation of node modules with npm to be puppetised. Confirmed working with node.js v0.6.14
+
+Usage:
+
+ # Some useful NPM modules
+ package { ["connect","redis","connect-redis","jade","express","express-resource","futures","emailjs"]:
+ provider=>npm,
+ ensure=>installed
+ }
+
81 lib/puppet/provider/package/npm.rb
@@ -0,0 +1,81 @@
+require 'puppet/provider/package'
+
+Puppet::Type.type(:package).provide :npm, :parent => Puppet::Provider::Package do
+ desc "node.js package management with npm"
+
+ raise Puppet::Error, "The npm provider can only be used as root" if Process.euid != 0
+
+ def self.exec_as_user(op, pkg)
+# Puppet::Util::SUIDManager.asuser("node", "http") do
+ s = execute ["/usr/bin/npm", "#{op}", "-glp", "#{pkg}"]
+ s.split("\n").collect do | line |
+ yield line
+ end
+# end
+ end
+
+ def self.npm_list(hash)
+ begin
+ list = []
+ exec_as_user("list", "") do | line |
+ if npm_hash = npm_split(line)
+ npm_hash[:provider] = :npm
+ if (npm_hash[:name] == hash[:justme]) or hash[:local]
+ list << npm_hash
+ end
+ end
+ end
+ list.compact!
+ rescue Puppet::ExecutionFailure => detail
+ raise Puppet::Error, "Could not list npm packages: #{detail}"
+ end
+ if hash[:local]
+ list
+ else
+ list.shift
+ end
+ end
+
+ def self.npm_split(desc)
+ split_desc = desc.split(/:/)
+
+ if (split_desc.length < 2)
+ return nil
+ end
+
+ installed = split_desc[1]
+ name = installed.split(/@/)[0]
+ version = installed.split(/@/)[1]
+ if (name.nil? || version.nil?)
+ nil
+ else
+ return {
+ :name => name,
+ :ensure => version
+ }
+ end
+ end
+
+ def self.instances
+ npm_list(:local => true).collect do |hash|
+ new(hash)
+ end
+ end
+
+ def install
+ output = self.class.exec_as_user("install", resource[:name]) { | line | line }.collect
+ self.fail "Could not install: #{resource[:name]}" if output.include?("npm not ok")
+ end
+
+ def uninstall
+ output = self.class.exec_as_user("uninstall", resource[:name]) { | line | line }.collect
+ self.fail "Could not install: #{resource[:name]}" if output.include?("npm not ok")
+ end
+
+ def query
+ version = nil
+ self.class.npm_list(:justme => resource[:name])
+ end
+
+end
+

0 comments on commit e7f5c2c

Please sign in to comment.