2015-07-03 24 views
2

我正在嘗試編寫自動執行/etc/init.d/oracleasm configure命令的代碼,因爲它需要4個輸入來繼續。如何自動化廚師配方中的用戶交互式命令

Default user to own the driver interface [grid]: grid 
Default group to own the driver interface [y]: oinstall 
Start Oracle ASM library driver on boot (y/n) [y]: y 
Scan for Oracle ASM disks on boot (y/n) [y]: y 

一般來說,編寫代碼以實現此目的的最佳方法是什麼。請建議我。

回答

1

如果您有可以非交互式運行的命令,但可以使用expect運行交互式命令會更好。根據你的榜樣將是:

bash 'OracleASM' do 
     user 'root' 
     code <<-EOF 
     /usr/bin/expect -c 'spawn /etc/init.d/oracleasm configure 
     expect "Default user to own the driver interface [grid]: " 
     send "grid\r" 
     expect "Default group to own the driver interface [y]: " 
     send "oinstall\r" 
     expect "Start Oracle ASM library driver on boot (y/n) [y]: " 
     send "y\r" 
     expect "Scan for Oracle ASM disks on boot (y/n) [y]: " 
     send "y\r" 
     expect eof' 
     EOF 
    end 

是很重要的/etc/init.d/oracleasm configure是一個冪等命令,這意味着你可以運行一次,兩次或一百倍,而且其行爲和結果系統將是相同的。如果不是這種情況,則需要命令的一些警衛(only_ifnot_if)在需要時僅運行命令:

bash 'OracleASM' do 
    user 'root' 
    code <<-EOF 
    .... 
    EOF 
    not_if { ::File.exists?('/...') } 
end