2013-05-14 113 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 

這樣它也會被編譯,只在第二階段執行。