简体   繁体   English

Opsworks自定义厨师食谱

[英]Opsworks Custom Chef Recipies

I have successfully deployed my application using AWS OpsWorks, now I am trying to implement a custom Chef Cookbook that will allow me to set the bash environment variables. 我已经使用AWS OpsWorks成功部署了我的应用程序,现在我正在尝试实现自定义Chef Cookbook,这将允许我设置bash环境变量。 I have setup the Git repo the cookbook is being updated with OpsWorks. 我已经设置了Git存储库,该菜谱正在用OpsWorks更新。 I generated the cookbook using the knife command on my dev box which is really just the directory structure with a recipes/default.rb file containing a few lines of code. 我在我的dev框上使用了knife命令生成了食谱,它实际上只是目录结构,其中包含几行代码的recipes/default.rb文件。

When I try to do something like the following I seem to keep getting errors 当我尝试执行以下操作时,我似乎不断出错

node[:deploy].each do |application, deploy|
    deploy = node[:deploy][application]
    command "ls -la"
end

(Note: ls -la is just for testing i know this will not set the environment variables) (注意:ls -la仅用于测试,我知道这不会设置环境变量)

I get the following error: ERROR: Caught exception during execution of custom recipe: xyz-enviroment: NoMethodError - undefined method command' for #<Chef::Recipe:0x7feb59200c00> - /opt/aws/opsworks/releases/20130328224322_109/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/mixin/recipe_definition_dsl_core.rb:56:in method_missing 我收到以下错误: ERROR: Caught exception during execution of custom recipe: xyz-enviroment: NoMethodError - undefined method command' for #<Chef::Recipe:0x7feb59200c00> - /opt/aws/opsworks/releases/20130328224322_109/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/mixin/recipe_definition_dsl_core.rb:56:in method_missing

Also if I try something like 另外,如果我尝试类似

execute "setting up the enviroment" do
    # TODO: Add code that does something here
end

I get the following error: 我收到以下错误:

execute[setting up the enviroment] (/opt/aws/opsworks/current/site-cookbooks/xyz-enviroment/recipes/default.rb:18:in `from_file') had an error:
No such file or directory - setting up the enviroment

I'm new to Chef so I'm sure there is something simple I'm doing wrong I just haven't been able to figure it out. 我是Chef的新手,所以我确定我做错了一些简单的事情,只是无法弄清楚。 Thanks in advance for the help. 先谢谢您的帮助。

I already solved my issue before seeing the responses below, they may have worked to solve the issue but I don't have the time to go back and try them now. 在看到下面的答复之前,我已经解决了我的问题,他们可能已经解决了问题,但是我没有时间回去尝试一下。

My solution was to use a Chef template to create an initializer file to set the variables when rails boots up the application. 我的解决方案是使用Chef模板创建一个初始化程序文件,以在Rails启动应用程序时设置变量。

# deafult.rb

node[:deploy].each do |application, deploy|
    deploy = node[:deploy][application]

    execute "restart Rails app #{application}" do
        cwd deploy[:current_path]
        command node[:opsworks][:rails_stack][:restart_command]
        action :nothing
    end

    template "#{deploy[:deploy_to]}/current/config/initializers/dev_enviroment.rb" do
        source "dev_enviroment.erb"
        cookbook 'dev-enviroment'
        group deploy[:group]
        owner deploy[:user]
        variables(:dev_env => deploy[:dev_env])

        notifies :run, resources(:execute => "restart Rails app #{application}")

        only_if do
            File.exists?("#{deploy[:deploy_to]}") && File.exists?("#{deploy[:deploy_to]}/current/config/")
        end
    end
end

dev_enviroment.erb dev_enviroment.erb

ENV['VAR1'] = "<%= @dev_env[:VAR1] %>"
ENV['VAR2'] = "<%= @dev_env[:VAR2] %>"

The custom Chef JSON used in the Opsworks stack layer: Opsworks堆栈层中使用的自定义Chef JSON:

{
    "deploy": {
        "myapp": {
            "dev_env": {
                "VAR1": "INFO1",
                "VAR2": "INFO2",
            }
        }
    }
}

You didn't specify what command to run, so it's actually trying to run setting up the environment , which isn't a valid command. 您没有指定要运行的命令,因此实际上是在尝试setting up the environment ,这不是有效命令。

Try instead specifying the command attribute inside the block: 尝试改为在块内指定command属性:

execute "setting up the enviroment" do
  command "/path/to/command --flags"
end

Alternatively, set the resource name to the command itself: 或者,将资源名称设置为命令本身:

execute "/path/to/command --flags" do
  # TODO: Add code that does something here
end

Your second question was correctly answered by clb. clb正确回答了您的第二个问题。 As for your first, 'command' is not a valid chef resource, you want something like: 首先,“命令”不是有效的厨师资源,您需要以下内容:

node[:deploy].each do |application, deploy|
  deploy = node[:deploy][application]
  execute "running a command for #{application}" do
    command "ls -la"
  end
end

OpsWorks has a new feature, where you can add Environmentvariables on the App OpsWorks具有一项新功能,您可以在其中向App添加环境变量

You can access them via 您可以通过访问它们

node[:deploy]['appshortname'][:environment_variables][:variable_name]

( see http://docs.aws.amazon.com/opsworks/latest/userguide/attributes-json-deploy.html#attributes-json-deploy-app-environment ) (请参阅http://docs.aws.amazon.com/opsworks/latest/userguide/attributes-json-deploy.html#attributes-json-deploy-app-environment

So you can directly set them for your chef context like this: 因此,您可以像这样直接为您的厨师环境设置它们:

node[:deploy]['magento']['environment_variables'].each {|key, value| ENV[key]=value }

And you can "dump" that into a shell script for example like this: 您可以将其“转储”到shell脚本中,例如:

file "#{release_path}/environment.sh" do
  content ENV.reduce("#!/bin/bash\n") { |a, e| a + "export #{e[0]}=\"#{e[1]}\"\n" }
  mode '0775'
end

We do something similar, and solved it by adding our environment variables to the json config file in Opsworks dashboard. 我们做了类似的事情,并通过将环境变量添加到Opsworks仪表板中的json配置文件中来解决它。 In chef, we write the file on deploy with this template: https://github.com/octocall/opsworks-cookbooks/blob/34e60267a4d3b5b9cf84e91829cc80c98c26f8ed/deploy/definitions/opsworks_rails.rb#L26 , and then we symlink it using the "symlink_before_migrate" property of the json file, like this: 在Chef中,我们使用以下模板在部署时编写文件: https : //github.com/octocall/opsworks-cookbooks/blob/34e60267a4d3b5b9cf84e91829cc80c98c26f8ed/deploy/definitions/opsworks_rails.rb#L26 ,然后使用“ symratelink_before json文件的属性,如下所示:

{
  "deploy": {
    "APPNAME": {
      "symlink_before_migrate": {
        "config/.env": ".env"
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM