0

我面临一个奇怪的问题。从用户数据(cloud-init)执行厨师时,我的以下厨师食谱失败。

它要求 HOME/COMPOSER_HOME 环境变量。

但是,如果我从 SSH 终端执行手动厨师客户端运行(甚至直接在终端上运行命令),它运行良好。

PS:composer.phar 和 composer.json 由开发者提供(捆绑在 wordpress zip 中)

== 食谱片段 ==

# Install Composer

script "install_composer" do
    interpreter "bash"
    user "root"
    cwd "#{node.apache.default_docroot}/html"
    code <<-EOH
    /opt/rh/rh-php56/root/usr/bin/php composer.phar install
    EOH
    not_if {File.exists?("#{node.default_docroot}/html/composer.lock")}
end

== 从 SSH 终端运行时 ==

  * script[install_composer] action run
    - execute "bash"  "/tmp/chef-script20160722-5397-1sjwxg9"

== Cloud-Init-Output.log 片段 ==

================================================================================
Error executing action `run` on resource 'script[install_composer]'
================================================================================

Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '1'
---- Begin output of "bash"  "/tmp/chef-script20160722-2013-r6sdpo" ----
STDOUT: 
STDERR: [RuntimeException]                                                                        
  The HOME or COMPOSER_HOME environment variable must be set for composer to run correctly
---- End output of "bash"  "/tmp/chef-script20160722-2013-r6sdpo" ----
Ran "bash"  "/tmp/chef-script20160722-2013-r6sdpo" returned 1

Resource Declaration:
---------------------
# In /var/chef/cache/cookbooks/webps-commportal/recipes/wordpress.rb

 49: script "install_composer" do
 50:    interpreter "bash"
 51:    user "root"
 52:    cwd "#{node.apache.default_docroot}/html"
 53:    code <<-EOH
 54:    /opt/rh/rh-php56/root/usr/bin/php composer.phar install
 55:    EOH
 56:    not_if {File.exists?("#{node.apache.default_docroot}/html/composer.lock")}
 57: end
 58: 

Compiled Resource:
------------------
# Declared in /var/chef/cache/cookbooks/webps-commportal/recipes/wordpress.rb:49:in `from_file'

script("install_composer") do
  action "run"
  retries 0
  retry_delay 2
  guard_interpreter :default
  command "\"bash\"  \"/tmp/chef-script20160722-2013-r6sdpo\""
  backup 5
  cwd "/opt/rh/httpd24/root/var/www/html"
  returns 0
  user "root"
  code "\t/opt/rh/rh-php56/root/usr/bin/php composer.phar install\n"
  interpreter "bash"
  cookbook_name "webps-commportal"
  recipe_name "wordpress"
  not_if { #code block }
end

---- Begin output of "bash"  "/tmp/chef-script20160722-2013-r6sdpo" ----
STDOUT: 
STDERR: [RuntimeException]                                                                        
  The HOME or COMPOSER_HOME environment variable must be set for composer to run correctly
---- End output of "bash"  "/tmp/chef-script20160722-2013-r6sdpo" ----
Ran "bash"  "/tmp/chef-script20160722-2013-r6sdpo" returned 1
4

1 回答 1

1

添加environment HOME: '/root'到您的script资源(或类似资源)。这是必需的,因为 Chef 不会为您设置此变量。当您通过 SSH 登录时,它由您的登录 shell 设置,然后继承到 chef-client 进程。

于 2016-07-22T09:53:33.970 回答