簡體   English   中英

如何在廚師中使用sudo for gem命令

[英]How to use sudo for gem command in chef

我正在哄騙廚師。 我需要root用戶使用ruby和rubygems以及另一個用戶'deploy'

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

然后我嘗試更改此“部署”用戶的gem源

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 

這是使用execute資源的問題。 您正在以非特權用戶身份運行執行資源。 但是,您希望關聯文件仍由非特權用戶擁有,對吧? Stephen建議的代碼可以使用,但.gemrc將由root用戶擁有。 根據Rubygems如何讀取它的gemrcs,它可能不喜歡該文件由不同的用戶擁有。 因此,您需要手動chownchmod execute命令中的文件,或者只使用模板。

如果您在計算機上本地運行該命令,您將看到生成的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 %>

這也可以擴展為支持多個源端點。 此外,因為您正在使用冪等資源:

  • 您在Chef Client運行中獲得了一個很好的diff輸出

    你會在輸出中看到一個git-style diff,這對於調試非常有用

  • 您可以通知其他資源更改

    如果您需要在模板源更改后安裝gem(例如),則可以安全地使用通知。 這些通知僅在模板更改時觸發:

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

gem_package 'foo' do
  action :nothing
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM