gpt4 book ai didi

ruby - 无法弄清楚如何编写自定义类型

转载 作者:太空宇宙 更新时间:2023-11-03 16:42:18 25 4
gpt4 key购买 nike

更新:

我正在尝试自学如何编写 Puppet 自定义类型。我看过这个文档:https://docs.puppet.com/puppet/4.10/custom_types.htmlhttps://docs.puppet.com/puppet/4.10/provider_development.html

这是我人为地尝试创建一个自定义类型,该类型简单地接受一个字符串数组并将它们写入文件“/tmp/track-titles.txt”。

这是我的类型代码(modules/hello_world/lib/puppet/type/track_titles.rb):

# blah blah blah
Puppet::Type.newtype(:track_titles) do
@doc = "Create track title file."

ensurable

newparam(:name) do
desc "Mandaorty paramteter name ."
end
newproperty(:tracks) do
desc "an arrary of strings"
end

end

这是我的提供商代码:(modules/hello_world/lib/puppet/provider/track_titles.rb)

Puppet::Type.type(:track_titles).provide(:foo) do
desc "contrived example."

def create
filename = @resource[:name]
tracks.each do |t|
system ( "echo #{t} >> #{filename}" )
end
end

def destroy
File.unlink(@resource[:name])
end

def exists?
File.exists?(@resource[:name]))
end
end

这是我的 puppet 模块,它使用了上面的内容:(modules/hello_world/manifests/init.pp)

class hello_world (
$msg = 'Hello World',
$track_titles = ['one','two','three'],
) {
# notify { $msg: }
track_titles { '/tmp/track-titles.txt':
tracks => $track_titles,
}
}

我这样执行这段代码:

$ puppet apply \
> --modulepath=/home/red/PUPPET/modules \
> --hiera_config=/home/red/PUPPET/hiera.yaml \
> -e 'include hello_world'

这是我得到的输出:

Notice: Compiled catalog for localhost in environment production in 0.06 seconds
Error: /Stage[main]/Hello_world/Track_titles[/tmp/track-titles.txt]: Could not evaluate: No ability to determine if track_titles exists
Notice: Finished catalog run in 0.82 seconds

我做错了什么。还有部分提供商代码我不喜欢:

Puppet::Type.type(:track_titles).provide(:ruby) do

这个 .provide(:ruby) 是什么意思?

请帮忙:)

最佳答案

好的。这是我做错了什么。一方面,我没有仔细阅读文档 ( https://docs.puppet.com/puppet/4.10/custom_types.html#properties-and-parameters)。它说:

提供程序文件应位于 lib/puppet/provider/<TYPE NAME>/<PROVIDER NAME>.rb

了解这一点有助于弄清楚 .provide(:thing) 是什么意味着。 :thing<PROVIDER NAME>是同一件事,需要匹配。所以这是我更新的工作代码:

这是 Puppet 类:

$ cat modules/hello_world/manifests/init.pp
class hello_world (
$track_titles = ['one','two','three'],
) {
track_titles { '/tmp/track-titles.txt':
tracks => $track_titles,
ensure => present,
}
}

这是类型定义:

$ cat modules/hello_world/lib/puppet/type/track_titles.rb
# blah blah blah
Puppet::Type.newtype(:track_titles) do
@doc = "Create track title file."

ensurable

newparam(:name) do
desc "Mandaorty paramteter name ."
end
newparam(:tracks) do
desc "an arrary of strings"
end
end

这是提供者代码。 这是我将文件放在错误目录中的代码,这就是我得到 Could not evaluate: No ability to determine if track_titles exists 的原因错误。

$ cat modules/hello_world/lib/puppet/provider/track_titles/track_titles.rb
Puppet::Type.type(:track_titles).provide(:track_titles) do
desc "Contrived example"

def create
filename = @resource[:name]
tracks = @resource[:tracks]
tracks.each do |t|
system ( "echo #{t} >> #{filename}" )
end
end

def destroy
File.unlink(@resource[:name])
end

def exists?
File.exists?(@resource[:name])
end
end

现在这是一个成功的执行:

$ puppet apply \
> --modulepath=/home/red/PUPPET/modules \
> --hiera_config=/home/red/PUPPET/hiera.yaml \
> -e 'include hello_world'

Notice: Compiled catalog for localhost in environment production in 0.07 seconds
Notice: /Stage[main]/Hello_world/Track_titles[/tmp/track-titles.txt]/ensure: created
Notice: Finished catalog run in 0.85 seconds

$ cat /tmp/track-titles.txt
one
two
three

关于ruby - 无法弄清楚如何编写自定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43693027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com