Chef 将 LWRP 资源属性传递给帮助程序库
Chef pass LWRP resource attribute to helper library
我正在编写一个库说明书,其中包含一个 LWRP,它应该安装一个 tarball 并且 运行 它作为守护进程(teamcity 代理软件)。作为这本食谱的一部分,我想编写一个检查软件是否已安装的辅助方法。所以我有一个具有这种结构的辅助库:
module TeamCity
module Agent_helper
def tc_agent_installed?(install_path, archive, version)
return false unless File.symlink?(install_path)
return false unless File.basename(File.readlink(install_path)) == "#{archive.match(/^[^\.]*/).to_s}-#{version}"
return false unless ::File.exist?(::File.join(install_path,'lib','agent.jar'))
true
end
end
end
我的资源如下所示:
actions :install, :configure
default_action :install
attribute :install_path, :kind_of => String, default: '/opt/tcbuild'
...
...
attribute :version, :kind_of => String, default: '8.1.4'
下面是一个示例,说明如何从提供程序中调用我的辅助方法
link target do
to source
owner new_resource.user
group new_resource.group
not_if { tc_agent_installed?(new_resource.install_path, new_resource.install_archive, new_resource.version) }
end
理想情况下,该方法不应获取输入参数,而是能够从资源中提取属性,因为该方法仅用于一个目的。在上面的 link 资源上,我希望能够编写一个可以加载当前资源属性的库(例如上面示例中的 new_resource.version )。这样我就可以像这样简单地编写守卫:
not_if { tc_agent_installed? }
我尝试了多种方法将 'version' 属性传递给该模块,但没有成功。
将节点属性传递给帮助程序库很容易实现,但这不是我想要做的事情,因为一些资源属性使用默认值并且不会被节点属性覆盖。
有什么想法吗?将资源属性(而非节点属性)传递给库的最佳方式是什么?
我会将此方法作为私有方法放在资源本身中。
如果您不想在该方法内传递属性,那么它必须知道资源内的私有变量,因此它必须在该资源内。
只传递资源对象
module TeamCity
module Agent_helper
def tc_agent_installed?(resource)
return false unless File.symlink?(resource.install_path)
return false unless File.basename(File.readlink(resource.install_path)) == "#{resource.archive.match(/^[^\.]*/).to_s}-#{resource.version}"
return false unless ::File.exist?(::File.join(resource.install_path,'lib','agent.jar'))
true
end
end
end
link target do
to source
owner new_resource.user
group new_resource.group
not_if { tc_agent_installed?(new_resource) }
end
我正在编写一个库说明书,其中包含一个 LWRP,它应该安装一个 tarball 并且 运行 它作为守护进程(teamcity 代理软件)。作为这本食谱的一部分,我想编写一个检查软件是否已安装的辅助方法。所以我有一个具有这种结构的辅助库:
module TeamCity
module Agent_helper
def tc_agent_installed?(install_path, archive, version)
return false unless File.symlink?(install_path)
return false unless File.basename(File.readlink(install_path)) == "#{archive.match(/^[^\.]*/).to_s}-#{version}"
return false unless ::File.exist?(::File.join(install_path,'lib','agent.jar'))
true
end
end
end
我的资源如下所示:
actions :install, :configure
default_action :install
attribute :install_path, :kind_of => String, default: '/opt/tcbuild'
...
...
attribute :version, :kind_of => String, default: '8.1.4'
下面是一个示例,说明如何从提供程序中调用我的辅助方法
link target do
to source
owner new_resource.user
group new_resource.group
not_if { tc_agent_installed?(new_resource.install_path, new_resource.install_archive, new_resource.version) }
end
理想情况下,该方法不应获取输入参数,而是能够从资源中提取属性,因为该方法仅用于一个目的。在上面的 link 资源上,我希望能够编写一个可以加载当前资源属性的库(例如上面示例中的 new_resource.version )。这样我就可以像这样简单地编写守卫:
not_if { tc_agent_installed? }
我尝试了多种方法将 'version' 属性传递给该模块,但没有成功。
将节点属性传递给帮助程序库很容易实现,但这不是我想要做的事情,因为一些资源属性使用默认值并且不会被节点属性覆盖。
有什么想法吗?将资源属性(而非节点属性)传递给库的最佳方式是什么?
我会将此方法作为私有方法放在资源本身中。 如果您不想在该方法内传递属性,那么它必须知道资源内的私有变量,因此它必须在该资源内。
只传递资源对象
module TeamCity
module Agent_helper
def tc_agent_installed?(resource)
return false unless File.symlink?(resource.install_path)
return false unless File.basename(File.readlink(resource.install_path)) == "#{resource.archive.match(/^[^\.]*/).to_s}-#{resource.version}"
return false unless ::File.exist?(::File.join(resource.install_path,'lib','agent.jar'))
true
end
end
end
link target do
to source
owner new_resource.user
group new_resource.group
not_if { tc_agent_installed?(new_resource) }
end