2014-10-27 52 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