Chef LWRP-Defs /资源执行顺序 [英] Chef LWRP - defs/resources execution order
问题描述
我在LWRP中具有以下内容,它所做的只是分解一个.ear文件:
I have the following in a LWRP, all it does is exploding an .ear file::
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
当我运行此命令/使用Vagrant提供/时,输出显示Chef并行启动了 expand_ear和 expand_wars。结果,expand_wars def无法找到所有.wars /它们仍在提取中。我尝试将'expand_ear'布尔值包装在'expand_wars'中:
When I run this /using Vagrant provide/ the output shows that Chef starts 'expand_ear' and 'expand_wars' in parallel. As a consequence the expand_wars def fails to find all .wars /they are still being extracted. I tried making 'expand_ear' boolean and wrapping 'expand_wars' in :
if expand_ear?(src,dest)
expand_war
end
但这会产生相同的结果。
but this yields the same result.???
推荐答案
厨师跑步由两个阶段组成,分别是 compile 和 execution 。在第一阶段,厨师检查食谱,然后进行以下操作:
Chef run consists of 2 phases, the compile and execution. During the first phase chef goes through recipes and:
- 如果看到纯红宝石代码-它将被执行。
- 如果看到资源定义-将其编译并放入资源集合。
您的问题是<$中的代码c $ c> expand_ear 被编译-因为它是资源,但是 explode_wars
中的代码立即被执行-因为它是纯红宝石。有两种可能的解决方案:
Your problem is that code in expand_ear
gets compiled - because it's a resource, but the code in explode_wars
gets executed straight away - because it's pure ruby. There are 2 possible solutions:
更改您的expand_ear以动态定义bash资源:
Change your expand_ear to define bash resource dynamically:
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
这是纯红宝石-因此将执行而不是编译。
This is pure ruby - and therefore will be executed instead of compiling.
OR 将红宝石代码放入explode_wars转换为ruby_block资源。
OR put your ruby code in explode_wars into ruby_block resource.
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
这种方式也将被编译,并且仅在第二阶段执行。
This way it will be compiled too, and executed only during the 2nd phase.
这篇关于Chef LWRP-Defs /资源执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!