我想根据某个条件在运行时中止配方但是使用 raise 或 Chef :: Application.fatal!,如本文{{ 3}}我的食谱仅在编译时退出。
这是我正在尝试的(我的脚本的一部分):
execute "Execute somehthing" do
cwd node['abc']['dir']
command "something >> #{results}"
node.default['success'] = "false"
notifies :run, "ruby_block[jobFailure]", :delayed
not_if "cd #{node['abc']['dir']} && somecommand >> #{results}"
end
ruby_block "jobFailure" do
raise "Exiting the script as the job has failed" if (node.default['success'] == "false")
action :nothing
end
但是在运行上面的脚本时,我收到错误,因为Chef在编译时退出但只给出了以下错误:
Running handlers:
[2014-10-27T17:17:03+00:00] ERROR: Running exception handlers
Running handlers complete
[2014-10-27T17:17:03+00:00] ERROR: Exception handlers complete
[2014-10-27T17:17:03+00:00] FATAL: Stacktrace dumped to c:/Users/manish.a.joshi/
.chef/local-mode-cache/cache/chef-stacktrace.out
Chef Client failed. 0 resources updated in 12.400772 seconds
[2014-10-27T17:17:03+00:00] FATAL: RuntimeError: Exiting the script as the job has failed
如果有办法只根据条件执行加注命令,有人可以告诉我吗?
答案 0 :(得分:2)
所以首先关闭Chef不会像这样工作,这段代码不会像你期望的那样工作,但你必须把代码放在ruby_block的实际块中:
ruby_block "jobFailure" do
block do
raise "Exiting the script as the job has failed" if (node.default['success'] == "false")
end
action :nothing
end
执行资源中的node.default['success'] = "false"
将发生,无论命令的状态如何,都将在编译时发生。 Chef资源没有以这种方式返回值。
答案 1 :(得分:1)
听起来您只想在条件失败时执行execute
阻止。如果是这种情况,您可以使用单一资源来完成这两项任务。
execute "Execute somehthing" do
cwd node['abc']['dir']
command "something >> #{results} && exit 1"
node.default['success'] = "false"
notifies :run, "ruby_block[jobFailure]", :delayed
not_if "cd #{node['abc']['dir']} && somecommand >> #{results}"
end
添加&& exit 1
会导致execute
资源失败,从而终止主厨运行。
当然,这只适用于您想立即终止的情况。您当前的代码使用:delayed
通知,这意味着您的主厨将继续运行,直到所有资源都已执行,然后在延迟通知期间失败。这可能也可能不是你想要的。
如果您确实希望在通知期间终止,请尝试此操作(请注意,设置节点属性没有帮助)
execute "Execute somehthing" do
cwd node['abc']['dir']
command "something >> #{results}"
notifies :run, "ruby_block[jobFailure]", :delayed
not_if "cd #{node['abc']['dir']} && somecommand >> #{results}"
end
ruby_block "jobFailure" do
block
raise "Exiting the script as the job has failed"
action :nothing
end