我正在将一些木偶清单转换成厨师。在其中一个模块中,我遇到了一些已定义的资源类型。
据我所知,到目前为止,大厨为此功能提供的是轻量级资源提供商(LWRP),但不是那些用纯Ruby编写的吗?
答案 0 :(得分:3)
将Puppet的自定义资源转换为类似Chef的代码有很多选择。
正如您所提到的,LWRP(或轻量级资源和提供程序)是实现自定义Chef扩展的最简单方法(我更喜欢使用术语"扩展"因为它最有意义)。 Chef中的所有东西都是用#34; plain-Ruby"编写的,用于Ruby的某些子集。您可以阅读有关LWRPs in the docs的更多信息,但这只是一个简单的例子:
# peanuts/resources/eat.rb
actions :eat, :stomp
default_action :delete
attribute :thing, kind_of: String
attribute :other_thing, kind_of: Fixnum
# peanuts/providers/eat.rb
action :eat do
# This is Ruby, but in the context of Chef, so you can use recipe snippets:
template '/foo/bar/blitz.txt'
# You can also use straight-up Ruby
File.open('/path', 'wb') { |f| f.write('...') }
# You can also use "raw" Chef
remote = Chef::Resource::RemoteFile.new('/path/to/save', run_context)
remote.source('https://github.com/file/file.tar.gz')
remote.run_action(:create_if_missing)
end
这将在名为peanuts_eat
的食谱中公开自定义资源:
peanuts_eat 'whatever' do
thing 'string'
other_thing 1
end
如果您熟悉Ruby,这是最简单的模式。 LWRP只是创建真正的Ruby类的一个方便的DSL。您可以read more about them here,但它们看起来像这样:
class Chef
class Resource::Peanuts < Resource
def thing
set_or_return(:thing, kind_of: String)
end
def other_thing
set_or_return(:thing, kind_of: Fixnum)
end
end
class Provider::PeanutsEat < Provider
def action_eat
# This is Ruby, but in the context of Chef, so you can use recipe snippets:
template '/foo/bar/blitz.txt'
# You can also use straight-up Ruby
File.open('/path', 'wb') { |f| f.write('...') }
# You can also use "raw" Chef
remote = Chef::Resource::RemoteFile.new('/path/to/save', run_context)
remote.source('https://github.com/file/file.tar.gz')
remote.run_action(:create_if_missing)
end
end
end
用法与LWRP示例相同
最后,你可以将两者结合起来:
class Chef
class Resource::Peanuts < Resource::LWRPBase
actions :eat, :stomp
default_action :delete
attribute :thing, kind_of: String
attribute :other_thing, kind_of: Fixnum
end
class Provider::PeanutsEat < Provider::LWRPBase
action(:eat) do
# This is Ruby, but in the context of Chef, so you can use recipe snippets:
template '/foo/bar/blitz.txt'
# You can also use straight-up Ruby
File.open('/path', 'wb') { |f| f.write('...') }
# You can also use "raw" Chef
remote = Chef::Resource::RemoteFile.new('/path/to/save', run_context)
remote.source('https://github.com/file/file.tar.gz')
remote.run_action(:create_if_missing)
end
end
end