Search code examples
wordpressamazon-web-servicescomposer-phpchef-infracloud-init

composer.phar install issues (cloudinit vs manual)


I am facing a weird issue. My following chef recipe fails when the chef is executed from user-data (cloud-init).

It asks for HOME/COMPOSER_HOME env variable.

However it runs fine if I do a manual chef-client run from the SSH terminal (or even run the commands directly on terminal).

PS: composer.phar and composer.json are provided by Developers (Bundled in wordpress zip)

== Recipe Snippet ==

# 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

== When run from SSH terminal ==

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

== Cloud-Init-Output.log snippet ==

================================================================================
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

Solution

  • Add environment HOME: '/root' to your script resource (or similar). This is needed because Chef does not set this variable for you. When you log in via SSH it gets set by your login shell, which then inherits to the chef-client process.