厨师:Opsworks:运行rake任务

时间:2014-11-28 18:45:55

标签: rake chef aws-opsworks

我的目标是对在Opsworks中运行的应用程序执行rake任务 在我看来,我的opsworks cookbook没有从正确的目录运行rake。

如何告诉食谱在app home dir中运行(所以它可以拿起Gemfile)?
我需要指定RAILS_ENV吗?

我的食谱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

Opsworks日志中的错误:

[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)

1 个答案:

答案 0 :(得分:1)

execute resource可以使用cwd attribute作为运行命令的工作目录。

execute "rake audit:audi_image_urls" do
  command       "bundle exec rake audit:audi_image_urls"
  cwd           '/over/there'
  environment   'RAILS_ENV' => 'production'
end

OpsWorks 部署事件和执行配方命令

厨师11

OpsWorks为部署事件和执行配方堆栈命令填充node[:deploy]['appshortname']以容纳每个应用程序配置。有了这些数据,您可以使用:

execute "rake audit:audi_image_urls" do
  command       "bundle exec rake audit:audi_image_urls"
  cwd           node[:deploy]['appshortname'][:deploy_to]
  user          node[:deploy]['appshortname'][:user]
  group         node[:deploy]['appshortname'][:group]
  environment(  { 'RAILS_ENV' => node[:deploy]['appshortname'][:rails_env] } )
end

如果您在此处配置了任何相关内容,则可能需要为environment提供:environment_variables

厨师12

From the AWS stack settings docco

  

在Chef 12 Linux中,堆栈设置以Chef data bags形式提供,只能通过Chef搜索访问。数据包存储在{OntWorks Stacks实例中/var/chef/runs/run-ID/data_bags目录中的一组JSON文件中,其中run-ID是AWS OpsWorks Stacks为实例上运行的每个Chef分配的唯一ID。堆栈设置不再作为Chef属性提供,因此无法再通过Chef节点对象访问堆栈设置。有关详细信息,请参阅AWS OpsWorks Stacks Data Bag Reference.

app = search("aws_opsworks_app").first

execute "rake audit:audi_image_urls" do
  command       "bundle exec rake audit:audi_image_urls"
  cwd           app['app_source']['deploy_to']
  user          app['app_source']['user']
  group         app['app_source']['group']
  environment(  { 'RAILS_ENV' => app['app_source']['rails_env'] } )
end

其他事件和命令

看起来OpsWorks与普通的Chef服务器运行方式略有不同,并为每次运行将其自己的JSON blob提供给本地厨师实例,这意味着(如您所述):deploy属性将是缺少其他事件/命令亚马逊选择不提供JSON。

:deploy

中包含部署状态{"deploy": { "app_name": { "application": "app_name" }的最后一个JSON文件填充/var/lib/aws/opsworks/chef属性可能,但非常容易破坏,容易破损

您还需要在该JSON加载之后获取deploy::default属性以填充任何defaults