2014-10-27 110 views
0

我想在运行时根据一定条件中止配方,但使用提高厨师:: Application.fatal!如本文所述How do you abort/end a Chef run?我的食谱仅在编译时退出。中止厨师食谱在运行时不在编译期间

这里是我想(我的剧本的一部分):

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 

但是在运行上面的脚本我得到的厨师是在编译时退出只给下面的错误的错误:

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 

任何人都可以让我知道,如果有一种方法只执行基于条件的raise命令吗?

+0

目前尚不清楚你的条件是什么。你能否编辑你的问题来澄清你检查的条件是什么? – 2014-10-27 23:27:14

回答

2

所以第一关厨师不喜欢这个工作,因为你希望不管这个代码将无法正常工作,但你必须把代码中的实际块的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"说无论命令的状态如何,执行资源中都会发生,并且会在编译时发生。厨师资源没有这种方式的返回值。

+0

你可以添加一个only_if到job_failure'ruby_block'并在那里测试你的成功条件。 – 2014-10-27 19:13:35

+0

这不会改变任何事情,执行资源的成功或失败并不是您可以轻松地与其他资源通信的方式。如果执行资源失败,Chef将中止。要做到这一点,你需要编写一个LWRP,并直接使用shell_out。 – coderanger 2014-10-27 19:31:31

+0

我很清楚,但我不认为OP正打算这么做。我的印象是退出条件和执行结果是两个截然不同的东西。执行可能成功,而“条件”仍然失败。如果是这种情况,那么可以将条件作为红宝石块的条件进行测试。 – 2014-10-27 23:25:59

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 
+0

谢谢,我真的想在所有厨师资源运行后终止会话,因此我使用':delayed',但是我喜欢使用'&& exit 1;'的想法,因为在一个我尝试使用':immediately'来立即调用ruby部分的其他食谱,如果条件失败,但它不起作用..似乎有一个不同的问题,但是您是否知道为什么'immediate'不会调用ruby块立即但只在最后像'延迟'? – 2014-11-05 17:31:37

+0

我需要更多信息。没有理由为什么:立即通知不会立即。 – 2014-11-05 22:45:22