2013-05-14 115 views
1

我已经在一个LWRP以下,当我运行这个/使用放浪提供/输出显示它的作用正在迅速增长的.ear文件::厨师LWRP - DEFS /资源执行顺序

action :expand do 
    ear_folder = new_resource.target_folder 
    temp_folder = "#{::File.join(ear_folder, 'tmp_folder')}" 

    expand_ear(new_resource.source, ear_folder) 
    expand_wars(ear_folder,temp_folder) 

end 

def expand_ear(src,dest) 
    bash "unzip EAR" do 
    cwd dest 
    code <<-EOF 
    pwd 
    ls -l 
    jar -xvf #{src}   
    EOF 
    end 
end 

def explode_wars(src,dest) 
    Dir.glob("#{basepath}/*.war") do |file| 
      ......... ###crete tmp folder, move .war there then unzip it to 'dest' 
     end 
end 

那厨师同时启动“expand_ear”和“expand_wars”。结果是expand_wars def没有找到所有的.wars /它们仍然被提取。我试图使“expand_ear”布尔和包装“expand_wars”:

if expand_ear?(src,dest) 
    expand_war 
end 

但这产生同样的结果???

回答

2

Chef run由2个阶段组成,编译执行。在第一阶段厨师通过食谱和:

  1. 如果它看到纯粹的红宝石代码 - 它会被执行。
  2. 如果它看到资源定义 - 它被编译并放入资源集合。

你的问题是expand_ear代码被编译 - 因为它是一个资源,但在explode_wars代码马上被执行 - 因为它是纯Ruby。有2个可能的解决方案:

更改expand_ear动态定义的bash资源:

res = Chef::Resource::Bash.new "unzip EAR", run_context 
res.cwd dest 
res.code <<-EOF 
    pwd 
    ls -l 
    jar -xvf #{src}   
    EOF 
res.run_action :run 

这是纯粹的红宝石 - 因此将被执行,而不是编译。

将ruby代码放入explode_wars中,放入ruby_block资源中。

ruby_block do 
    block do 
    Dir.glob("#{basepath}/*.war") do |file| 
     ......... ###crete tmp folder, move .war there then unzip it to 'dest' 
    end 
    end 
end 

这样它也会被编译,只在第二阶段执行。