2013-02-22 56 views
2

我需要檢查與廚師的聯繫。檢查與廚師的聯繫

我在努力。

execute "check_sayc" do 
command "$comprobacionPuerto='nc -zw3 server port && echo 'opened' 
|| echo 'closed'|grep 'opened' if [[ -z $comprobacionPuerto ]] 
then Chef::Log.fatal 'connections refuse' else Chef::Log.info 'connections open' fi'" 
end 

Mixlib::ShellOut::ShellCommandFailed: execute[check_sayc] 
(cb_prueba_frontal_deploy_databag::default line 7) had an error:  
Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received 
'127' 

什麼這是錯的?

回答

4

您在execute中混合使用shell代碼和Ruby代碼來執行無法以此方式工作的事情。另外,您在command中混合了您的報價。

我真的不知道,如果是有意義的執行你nc嘗試那裏的行動,但它可能更容易使用純Ruby:

ruby_block "check sayc" do 
    block do 
    server = "www.google.com" 
    port = 80 

    begin 
     Timeout.timeout(5) do 
     Socket.tcp(server, port){} 
     end 
     Chef::Log.info 'connections open' 
    rescue 
     Chef::Log.fatal 'connections refused' 
    end 
    end 
end 

這應該做的嘗試同樣的事情實現但避免了訂單問題以及如何將你的shellout的輸出轉換回ruby以在Chef中處理的問題。

編輯:我將連接嘗試封裝到超時模塊中。這可能會泄露半開放的套接字,直到他們稍後收集垃圾。但我認爲這在廚師的情況下是安全的。

+0

我怎麼能超時? – 2013-02-22 13:37:10

+0

請參閱我的編輯。請注意,您將體驗與'nc'一樣的長時間默認超時。 – 2013-02-22 13:58:01

+0

完美,謝謝。 – 2013-02-25 06:57:01