如何使Chef客户端使用与NVM一起安装的用户特定nodejs版本?

时间:2016-03-14 06:35:44

标签: node.js bash chef aws-opsworks nvm

我使用nvm cookbook来安装nvm和4.3.1版本的Node.js.

include_recipe 'nvm'

nvm_install node['analyzer_agent']['nodejs']['version'] do
  user node['analyzer_agent']['user']['name']
  group node['analyzer_agent']['user']['group']
  from_source false
  alias_as_default true
  action :create
end

当我与用户(在本例中为analyzer_agent)SSH服务器时,一切都按预期工作。我的默认nodej.js版本是4.3.1,当我手动尝试使用npm install pm2 -g安装pm2时,它运行正常。

但是,当我尝试从我的食谱全局安装pm2模块时:

execute "Install PM2" do
    cwd "/home/#{node['analyzer_agent']['user']['name']}"
    user node['analyzer_agent']['user']['name']
    group node['analyzer_agent']['group']
    environment "HOME"=> "/home/#{node['analyzer_agent']['user']['name']}", "USER"=>node['analyzer_agent']['user']['name']
    command "npm install pm2 -g"
    not_if { File.exists?("pm2") }
end

我收到以下错误:

================================================================================
Error executing action `run` on resource 'execute[Install PM2]'
================================================================================

Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '243'
---- Begin output of npm install pm2 -g ----
STDOUT: 
STDERR: npm ERR! tar.unpack untar error /home/analyzer_agent/.npm/pm2/1.0.2/package.tgz
npm ERR! Linux 3.13.0-79-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "pm2" "-g"
npm ERR! node v0.12.5
npm ERR! npm  v2.11.2
npm ERR! path /usr/local/lib/node_modules/pm2
npm ERR! code EACCES
npm ERR! errno -13

npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/pm2'
npm ERR!     at Error (native)
npm ERR!  { [Error: EACCES, mkdir '/usr/local/lib/node_modules/pm2']
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/usr/local/lib/node_modules/pm2',
npm ERR!   fstream_type: 'Directory',
npm ERR!   fstream_path: '/usr/local/lib/node_modules/pm2',
npm ERR!   fstream_class: 'DirWriter',
npm ERR!   fstream_stack: 
npm ERR!    [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:35:25',
npm ERR!      '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:47:53',
npm ERR!      'FSReqWrap.oncomplete (fs.js:95:15)' ] }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/analyzer_agent/npm-debug.log
---- End output of npm install pm2 -g ----
Ran npm install pm2 -g returned 243

Resource Declaration:
---------------------
# In /var/chef/runs/f3450c3b-d2cc-4f04-a504-436cee5c488b/local-mode-cache/cache/cookbooks/analyzer_agent/recipes/nvm_nodejs.rb

13: execute "Install PM2" do
14:    cwd "/home/#{node['analyzer_agent']['user']['name']}"
15:    user node['analyzer_agent']['user']['name']
16:    group node['analyzer_agent']['group']
17:    environment "HOME"=> "/home/#{node['analyzer_agent']['user']['name']}", "USER"=>node['analyzer_agent']['user']['name']
18:    command "npm install pm2 -g"
19:    not_if { File.exists?("pm2") }
20: end

Compiled Resource:
------------------
# Declared in /var/chef/runs/f3450c3b-d2cc-4f04-a504-436cee5c488b/local-mode-cache/cache/cookbooks/analyzer_agent/recipes/nvm_nodejs.rb:13:in `from_file'

execute("Install PM2") do
action [:run]
retries 0
retry_delay 2
default_guard_interpreter :execute
command "npm install pm2 -g"
backup 5
cwd "/home/analyzer_agent"
environment {"HOME"=>"/home/analyzer_agent", "USER"=>"analyzer_agent"}
returns 0
user "analyzer_agent"
declared_type :execute
cookbook_name "analyzer_agent"
recipe_name "nvm_nodejs"
not_if { #code block }
end

据我所知,在environment内使用execute设置HOME和USER应该可以解决问题。但我没有任何机会。 Chef客户端仅使用已编译的nodejs。不是由nvm安装的那个。

可能出现什么问题?

(顺便说一句,在同一台服务器上,但我和另一个用户从源代码编译了node.js并在全局安装了pm2。我看到它可能与我的问题有关的可能性很小,但是,如果是这样,为什么在我手动在新用户帐户中安装pm2时不会造成任何伤害?)

2 个答案:

答案 0 :(得分:0)

npm install -g是全局安装,这意味着您需要具有写入安装了Node / NPM的全局文件夹的权限(在本例中为/usr/local/bin)。你可能意味着不使用-g,因为它只存在于人类,而不是工具。

答案 1 :(得分:0)

我相信在OpsWorks中,您还可以通过在应用程序的食谱中创建新文件来覆盖已安装的Node版本:attributes/customize.rb。然后:

normal[:opsworks_nodejs][:version] = '4.4.0'

这应该下载正确版本的节点而不是使用Node 0.10 ...因此内置的OpsWorks应用程序运行/监视器(monit)应该可以工作,而不必安装PM2。