2013-12-13 90 views
1

我剛開始使用廚師,對Ruby不太瞭解。什麼紅寶石功能用於廚師食譜?

我在理解配方中使用的語言語法時遇到了問題。

說,我創建在食譜菜譜目錄/ default.rb喜歡:

directory "/home/test/mydir" do 
    owner "test" 
    mode "0755" 
    action :create 
    recursive true 
end 

我想這是一個正確的Ruby腳本的一部分。 owner "test"是什麼意思?這是一個函數調用,一個變量賦值或其他什麼嗎?

+0

你在閱讀什麼文件? –

+0

主要是互聯網:目前http://gettingstartedwithchef.com/first-steps-with-chef.html – mirk

+1

我也在學習,從而證實我問。 :) –

回答

1

廚師是用Ruby編寫的,能夠廣泛使用Ruby來設計自定義DSL。幾乎每個廚師配置文件都是使用基於Ruby的DSL編寫的。

這意味着,爲了有效地使用廚師,你應該熟悉的Ruby語法包括

  • 基本的語法
  • 數據類型(主要區別與其他語言的符號)

在Ruby中,您不需要了解很多關於元編程的知識。

您發佈的代碼的情況是基於Ruby的DSL的一個很好的例子。讓我稍微解釋一下。

# Call the method directory passing the path and a block 
# containing some code to be evaluated 
directory "/home/test/mydir" do 

    # chown the directory to the test user 
    owner "test" 

    # set the permissions to 0555 
    mode "0755" 

    # create the directory if it does not exists 
    action :create 

    # equivalent of -p flag in the mkdir 
    recursive true 

end 

塊是指定一組操作(在這種情況下創建,設置權限,等等)在單一上下文進行評估(在這種情況下,該路徑的上下文中)的簡便方法。

+0

'owner','mode'等是否有方法? –

+0

是的,全部都是Ruby方法。在'owner'的情況下,'owner'是方法並''test'''String'參數。 –

+0

我已經在這裏回答過類似的問題:http://stackoverflow.com/questions/19719968/ruby-code-blocks-and-chef/19726723#19726723可能有一些更有價值的信息。 – cassianoleal

1

讓我們來分解它。

directory "/home/test/mydir" do 
    ... 
end 

你只是調用由廚師定義的全局方法叫directory,傳遞一個參數"/home/test/mydir",和塊(doend之間的一切)。

此塊可能在由廚師創建的特殊範圍中執行,其中所有選項(ownermode,action等)均爲方法。