【问题标题】:Opsworks Custom Chef RecipiesOpsworks 定制厨师食谱
【发布时间】:2014-11-07 02:26:11
【问题描述】:

我已经使用 AWS OpsWorks 成功部署了我的应用程序,现在我正在尝试实现一个自定义 Chef Cookbook,它允许我设置 bash 环境变量。我已经设置了 Git 存储库,食谱正在使用 OpsWorks 进行更新。我在我的开发盒上使用刀命令生成了食谱,它实际上只是一个目录结构,其中包含一个包含几行代码的recipes/default.rb 文件。

当我尝试执行以下操作时,我似乎不断收到错误

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

(注意:ls -la 仅用于测试我知道这不会设置环境变量)

我收到以下错误: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

如果我尝试类似的东西

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

我收到以下错误:

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

我是 Chef 的新手,所以我确定我做错了一些简单的事情,我只是无法弄清楚。提前感谢您的帮助。

【问题讨论】:

    标签: ruby ruby-on-rails-3 amazon-web-services chef-infra


    【解决方案1】:

    在看到下面的回复之前,我已经解决了我的问题,他们可能已经解决了这个问题,但我现在没有时间回去尝试。

    我的解决方案是使用 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

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

    Opsworks 堆栈层中使用的自定义 Chef JSON:

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

    【讨论】:

    • 这似乎有效。但是,很想知道您最近是否找到了替代解决方案。希望不需要重新启动网络服务器。
    【解决方案2】:

    您没有指定要运行什么命令,所以它实际上是在尝试运行setting up the environment,这不是一个有效的命令。

    尝试在块内指定command 属性:

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

    或者,将资源名称设置为命令本身:

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

    【讨论】:

      【解决方案3】:

      clb 正确回答了您的第二个问题。至于你的第一个,“命令”不是有效的厨师资源,你想要这样的东西:

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

      【讨论】:

      • 是的。我完全错过了第一个。 :)
      【解决方案4】:

      OpsWorks 有一个新功能,您可以在应用程序上添加环境变量

      您可以通过以下方式访问它们

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

      (见http://docs.aws.amazon.com/opsworks/latest/userguide/attributes-json-deploy.html#attributes-json-deploy-app-environment

      所以你可以像这样直接为你的厨师环境设置它们:

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

      您可以将其“转储”到 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
      

      【讨论】:

        【解决方案5】:

        我们做了类似的事情,并通过将我们的环境变量添加到 Opsworks 仪表板中的 json 配置文件来解决它。在 chef 中,我们使用这个模板编写部署文件:https://github.com/octocall/opsworks-cookbooks/blob/34e60267a4d3b5b9cf84e91829cc80c98c26f8ed/deploy/definitions/opsworks_rails.rb#L26,然后我们使用 json 文件的“symlink_before_migrate”属性对其进行符号链接,如下所示:

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

        【讨论】:

          猜你喜欢
          • 2015-11-24
          • 2014-07-21
          • 2016-05-28
          • 2014-09-06
          • 1970-01-01
          • 1970-01-01
          • 2019-03-06
          • 2016-07-19
          • 1970-01-01
          相关资源
          最近更新 更多