My objective is to execute a rake task on my apps running in Opsworks.
It appears to me that my opsworks cookbook is not running rake from the correct directory.
How can I tell the cookbook to run in the app home dir (so it can pick up the Gemfile)?
Do I need to specify an RAILS_ENV?
My cookbooks default.rb:
Chef::Log.info("****** Audit Photo URLS : Running Rake Task ******")
execute "rake audit:audi_image_urls" do
command "bundle exec rake audit:audi_image_urls"
end
Errors from Opsworks log:
[2014-11-28T18:36:33+00:00] INFO: Running queued delayed notifications before re-raising exception
[2014-11-28T18:36:33+00:00] ERROR: Running exception handlers
[2014-11-28T18:36:33+00:00] ERROR: Exception handlers complete
[2014-11-28T18:36:33+00:00] FATAL: Stacktrace dumped to /var/lib/aws/opsworks/cache.stage2/chef-stacktrace.out
[2014-11-28T18:36:33+00:00] ERROR: execute[rake audit:audi_image_urls] (auditphoto::default line 3) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '10'
---- Begin output of bundle exec rake audit:audi_image_urls ----
STDOUT: Could not locate Gemfile
STDERR:
---- End output of bundle exec rake audit:audi_image_urls ----
Ran bundle exec rake audit:audi_image_urls returned 10
[2014-11-28T18:36:33+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
The
executeresource can take acwdattribute for the working directory from which the command is run.OpsWorks Deploy events and Execute Recipes commands
Chef 11
OpsWorks populates
node[:deploy]['appshortname']for Deploy events and Execute Recipes stack commands to house each applications configuration. With this data you could use:You may want to source
:environment_variablesfor theenvironmentif you have anything related configured there.Chef 12
From the AWS stack settings docco
Other events and commands
It looks like OpsWorks runs a little differently to a normal Chef server and supplies it's own JSON blob to a local chef instance for each run which means (as you mentioned) the
:deployattributes will be missing for other events/commands Amazon chooses not to supply JSON for.It might be possible, but very hacky and prone to breakage, to populate the
:deployattributes from the last JSON file that contains deploy state:{"deploy": { "app_name": { "application": "app_name" }in/var/lib/aws/opsworks/chefYou would also need to source the
deploy::defaultattributes after that JSON load to fill in any defaults.