Chef 简明教程
Chef - Lightweight Resource Provider
Lightweight resource provider (LWRP) 提供了通过扩展其特性来扩展可用资源列表的选项,并允许 Chef用户创建自定义资源。
Lightweight resource provider (LWRP) provides an option of extending the list of available resources by extending it features and allows Chef user to create custom resources.
通过创建自定义资源,你可以简单地编写食谱,因为你可以使用 Chef DSL 拥有丰富的自定义资源,这有助于使食谱代码更具表现力。
By creating custom resources one can simply write cookbooks because one can own enriched custom resources using Chef DSL which helps in making the recipe code more expressive.
在 Chef 社区中,许多自定义资源是使用 LWRP 实现的。有很多 LWRP 的工作示例,例如 iptables_rules 和 apt_repository 。
In Chef community, many of the custom resources are implemented using LWRPs. There are many working examples of LWRP such as iptables_rules and apt_repository.
Working Method
确保你有食谱名称 Testing_resource 和包含 Testing_resource 食谱的节点运行列表。
Make sure one has cookbook name Testing_resource and a run_list of node which contains Testing_resource cookbook.
Building LWRP
Step 1 − 在 Testing_resource 食谱中创建一个自定义资源。
Step 1 − Create a custom resource in Testing_resource cookbook.
vipin@laptop:~/chef-repo $ subl cookbooks/Testing_resource/resources/default.rb
actions :create, :remove
attribute :title, kind_of: String, default: "World"
attribute :path, kind_of: String, default: "/tmp/greeting.txt"
Step 2 − 在 Tesing_resource 食谱中为资源创建提供者。
Step 2 − Create a provider for resources in Tesing_resource cookbook.
vipin@laptop:~/chef-repo $ subl cookbooks/Testing_resource/provider/default.rb
action :create do
log "Adding '#{new_resource.name}' greeting as #{new_resource.
path}"
file new_resource.path do
content "#{new_resource.name}, #{new_resource.title}!"
action :create
end
action :remove do
Chef::Log.info "Removing '#{new_resource.name}' greeting #{new_resource.path}"
file new_resource.path do
action :delete
end
end
Step 3 − 通过编辑 Testing_resource 默认食谱来使用新资源。
Step 3 − Use a new resource by editing Testing_resource default recipe.
vipin@laptop:~/chef-repo $ subl cookbooks/Tesing_resource/recipes/default.rb
greeting "Ohai" do
title "Chef"
action :create
end
Step 4 − 将修改后的食谱上传到 Chef 服务器。
Step 4 − Upload the modified cookbook to Chef server.
vipin@laptop:~/chef-repo $ knife cookbook upload greeting
Uploading greeting [0.1.0]
Step 5 − 在节点上运行 Chef-Client。
Step 5 − Run Chef-Client on the node.
vipin@server:~$ sudo chef-client
...TRUNCATED OUTPUT...
2013-06-28T21:32:54+00:00] INFO: Processing greeting[Ohai] action
create (greeting::default line 9)
[2013-06-28T21:32:54+00:00] INFO: Adding 'Ohai' greeting as /tmp/
greeting.txt
[2013-06-28T21:32:54+00:00] INFO: Processing file[/tmp/greeting.
txt] action create (/srv/chef/file_store/cookbooks/greeting/
providers/default.rb line 7)
[2013-06-28T21:32:54+00:00] INFO: entered create
[2013-06-28T21:32:54+00:00] INFO: file[/tmp/greeting.txt] created
file /tmp/greeting.txt
...TRUNCATED OUTPUT...
Step 6 − 验证生成的文件的内容。
Step 6 − Validate the content of the generated file.
user@server:~$ cat /tmp/greeting.txt
Ohai, Chef!
Workflow Scripts
LWRP 存在于食谱中。自定义资源存在于食谱内,并且将在食谱名称下可用。在工作流中,首先定义定义,然后将属性传递给将在食谱中使用的资源。最后,在食谱中使用这些动作与属性。
LWRPs live in cookbooks. A custom resource lives inside the cookbooks, and will be available under the cookbook name. In the workflow, first we define the definitions and then pass the attributes to the resources which is going to be used in the cookbook. Finally, we use those actions and attributes in our recipe.