在Chef中动态创建资源

时间:2014-02-28 13:43:34

标签: chef aws-opsworks

我正在尝试创建一个主厨配方,它将根据使用我的应用程序源代码从git存储库下载的yaml文件的内容动态创建资源。到目前为止我有这个:

git "/home/a_user/#{ node[:my_node][:application] }" do
  repository node[:my_node][:git_repository]
  revision node[:my_node][:git_branch]
  action :sync
  user "a_user"
  group "a_user"
end

require 'yaml'

ruby_block "load the process into the node" do
  block do
     yml = YAML::load(File.open("/home/a_user/#{node[:my_node][:application]}/processes.yml"))
     node.set[:my_node][:worker][:processes] = yml[:processes]
  end
  subscribes :create, "git[/home/a_user/#{ node[:my_node][:application] }]" :immediately
end



node[:my_node][:worker][:processes].each do | name, cmd |
   supervisor_service name do
     command "bash -c \"source /home/a_user/.profile && #{ cmd }\""
     action :enable
   end   
 end 

 service "supervisor" do
    action :restart
 end  

yaml文件格式为:

processes:
    process_a: python myscript.py --a
    process_b: python myscript.py --b

但是当我执行它时,node[:my_node][:worker][:processes]的值在编译阶段为空,因此在执行阶段不会执行超级用户资源。

有人可以给我一个关于如何使这项工作的指针吗?我错过了一些明显的东西,或者我只是做错了?

1 个答案:

答案 0 :(得分:0)

在编译阶段填充属性:

git "/home/a_user/#{ node[:my_node][:application] }" do
  repository node[:my_node][:git_repository]
  revision node[:my_node][:git_branch]
  action :nothing
  user "a_user"
  group "a_user"
end.run_action(:sync)

ruby_block "load the process into the node" do
  block do
     yml = YAML::load(File.open("/home/a_user/#{node[:my_node][:application]}/processes.yml"))
     node.set[:my_node][:worker][:processes] = yml[:processes]
  end
  subscribes :create, "git[/home/a_user/#{ node[:my_node][:application] }]" :immediately
  action :nothing
end.run_action(:create)