From b7d48c93d9eeff3f8d5c7b6e1747c397d29ca239 Mon Sep 17 00:00:00 2001 From: Nan Liu Date: Wed, 6 Jun 2012 15:34:47 -0700 Subject: [PATCH] Add support for npm proxy configuration. Add npm proxy support and configuration of proxy prior to installing npm packages. Without this support npm install will error: npm ERR! Error: failed to fetch from registry: --- manifests/init.pp | 11 ++++++++++- manifests/npm.pp | 3 +++ spec/classes/nodejs_spec.rb | 29 +++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/manifests/init.pp b/manifests/init.pp index ee90e540..031016af 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -9,7 +9,8 @@ # Usage: # class nodejs( - $dev_package = false + $dev_package = false, + $proxy = '' ) inherits nodejs::params { case $::operatingsystem { @@ -67,6 +68,14 @@ require => Anchor['nodejs::repo'] } + if $proxy { + exec { 'npm_proxy': + command => "npm config set proxy ${proxy}", + path => $::path, + require => Package['npm'], + } + } + if $dev_package and $nodejs::params::dev_pkg { package { 'nodejs-dev': name => $nodejs::params::dev_pkg, diff --git a/manifests/npm.pp b/manifests/npm.pp index 60ed8922..4663402e 100644 --- a/manifests/npm.pp +++ b/manifests/npm.pp @@ -43,6 +43,9 @@ path => $::path, require => Class['nodejs'], } + + # Conditionally require npm_proxy only if resource exists. + Exec<| title=='npm_proxy' |> -> Exec["npm_install_${name}"] } else { exec { "npm_remove_${name}": command => "npm remove ${npm_pkg}", diff --git a/spec/classes/nodejs_spec.rb b/spec/classes/nodejs_spec.rb index c0055ebd..271d7b91 100644 --- a/spec/classes/nodejs_spec.rb +++ b/spec/classes/nodejs_spec.rb @@ -33,6 +33,10 @@ } end + let :params do + { :dev_package => true, } + end + it { should contain_class('apt') } it { should contain_apt__ppa('ppa:chris-lea/node.js') } it { should contain_package('nodejs') } @@ -40,6 +44,7 @@ 'name' => 'nodejs', 'require' => 'Anchor[nodejs::repo]', }) } + it { should contain_package('nodejs-dev') } it { should contain_package('npm').with({ 'name' => 'npm', 'require' => 'Anchor[nodejs::repo]', @@ -76,5 +81,29 @@ it { should_not contain_package('nodejs-dev') } end end + + describe 'when deploying with proxy' do + let :facts do + { + :operatingsystem => 'Ubuntu', + :lsbdistcodename => 'edgy', + } + end + + let :params do + { :proxy => 'http://proxy.puppetlabs.lan:80/' } + end + + it { should contain_package('npm').with({ + 'name' => 'npm', + 'require' => 'Anchor[nodejs::repo]', + }) } + it { should contain_exec('npm_proxy').with({ + 'command' => 'npm config set proxy http://proxy.puppetlabs.lan:80/', + 'require' => 'Package[npm]', + }) } + it { should_not contain_package('nodejs-stable-release') } + end + end