2014-02-14 30 views
0

我在與廚師一起提供食材。我需要ruby和rubygems作爲root用戶,另一個用戶'deploy'如何在廚師中使用sudo作爲gem命令

ruby​​和rubygems被安裝並且爲root用戶工作(在測試案例中爲「vagrant」)。

我創建用於一個用戶,以便Capistrano的後部署我的應用程序

user 'deploy' do 
    password '$1$zOC.txvE$ex544C.YpxV.HqNh/2AKQ0' 
    home "/home/deploy" 
    supports :manage_home => true 
    shell "/bin/bash" 
end 

然後我試圖改變寶石源,是「部署」用戶

execute 'change sources to our gem server' do 
    command "gem sources -r http://rubygems.org/ && gem sources -a http://my.gem.server/" 
    creates "~/.gemrc" 
    user 'deploy' 
    cwd "/home/deploy" 
end 

,但得到這個錯誤

[2014-02-14T14:38:27+00:00] ERROR: execute[change sources to our gem server] (beesor-cookbook::user line 13) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'  
---- Begin output of gem sources -r http://rubygems.org/ && gem sources -a http://my.gem.server/ ----  
STDOUT: source http://rubygems.org/ not present in cache  
STDERR: ERROR: While executing gem ... (Errno::EACCES)  
    Permission denied - /home/vagrant/.gemrc  
---- End output of gem sources -r http://rubygems.org/ && gem sources -a http://my.gem.server/ ----  
Ran gem sources -r http://rubygems.org/ && gem sources -a http://my.gem.server/ returned 1 

回答

2

這是使用execute資源時出現的問題。您正在以非特權用戶身份運行執行資源。但是,您希望相關文件仍由非授權用戶擁有,對嗎?斯蒂芬建議的代碼可以工作,但.gemrc將由root用戶擁有。根據Rubygems如何讀取它是gemrcs,它可能不喜歡該文件屬於不同的用戶。因此,您需要手動輸入chown和命令中的文件,或者僅使用模板。

如果您的機器上運行的命令,你會看到生成的gemrc樣子:

--- 
:sources: 
- http://my.gem.server 

我會建議使用純template資源來代替:

template '/home/deploy/.gemrc' do 
    source 'gemrc.erb' 
    user 'deploy' 
    group 'deploy' 
    mode '0644' 
    variables(source: 'http://my.gem.server') 
end 

然後相關的erb:

--- 
:sources: 
- <%= @source %> 

這也可以展開編輯以支持多個源端點。此外,由於您使用的是冪等資源:

  • 您在廚師客戶機上運行得到一個不錯的diff輸出

    你會看到在輸出一個git風格的差異,這是非常有用的用於調試

  • 如果您需要安裝一個寶石後的模板來源有變化(例如)可以通知改變

    的其他資源,你可以放心地使用此通知。這些通知只會觸發當模板發生了變化:

template '/home/deploy/.gemrc' do 
    # ... 
    notifies :install, 'gem_package[foo]', :immediately 
end 

gem_package 'foo' do 
    action :nothing 
end 
+0

的EACCES是/home/vagrant/.gemrc不/home/deploy/.gemrc所以我認爲用戶實際運行到CHEF- 2288(即ENV ['HOME']是錯誤的),但是用模板資源替換執行資源的解決方案無論如何都會正確地減輕它的影響。 – lamont