如何在 Chef 中获取 Poise LWRP 之间的通知
How to get notifications between Poise LWRP in Chef
我正在尝试了解有关 LWRP 的更多信息,并且有一个可以安装软件包的工作提供商。当配置更改时,我无法尝试通知我的服务 - 基本上我在下面有这个。
我可以让 runit 服务订阅包 installed/config 更改的通知吗?在解决方案中,是否可以将服务和包分离,即。基于观察者而不是通知 runit_service["abc"] ?我想为该服务创建一个单独的提供者,但还没有找到明确的例子。
module MyApp
class Resource < Chef::Resource
include Poise
provides(:my_app)
actions(:enable)
end
class Provider < Chef::Provider
include Poise
provides(:my_app)
def action_enable
notifying_block do
template new_resource.database_config do
source 'database.erb'
#how to notify runit?
end
deploy_revision new_resource.process_id do
#how to notify runit?
end
end
end
end
end
在食谱中
my_app 'one' do
#some attributes
process_user 'nobody'
process_group 'nogroup'
end
runit_service "myapp" do
cookbook "myapp"
run_template_name "myapp"
log_template_name "myapp"
options({
:app_env => "development",
:app_home => "/srv/myapp/current",
:data_dir => "/srv/myapp/data"
})
retries 3
retry_delay 5
end
干杯!
您不应从 LWRP 内的资源发出通知或向其发出通知。
根据 Chef 版本和 use_inline_resources
的值,您无法通知,因为 LWRP 内的资源在单独的上下文中聚合。
但是如果LWRP里面的一个资源被更新了,LWRP会被标记为已更新并且可以通知一个资源。
您可能有一个 notification 从您的 my_app resource
重新启动服务,如下所示:
my_app 'one' do
#some attributes
process_user 'nobody'
process_group 'nogroup'
notifies :restart,"runit_service[myapp]" # adapt action and may use timer :immediately, see the doc link above
end
或者,如果您希望以其他方式工作,您可以使用 subscribe 方式,如下所示:
runit_service "myapp" do
cookbook "myapp"
run_template_name "myapp"
log_template_name "myapp"
options({
:app_env => "development",
:app_home => "/srv/myapp/current",
:data_dir => "/srv/myapp/data"
})
retries 3
retry_delay 5
subscribes :restart,"my_app[one]" # again adapt action and may use timer :immediately, see the doc link above
end
通常订阅语法很有用,可以避免在彼此的资源中发出通知,可以有很多,所以如果你的服务托管多个应用程序,最容易获得它会在每次应用程序更改时重新启动,其中有多个订阅行服务比每个应用程序中的通知。
即:
runit_service "tomcat" do
[...]
subscribe :restart,"my_app[one]"
subscribe :restart,"my_app[two]"
subscribe :restart,"my_app[three]"
end
保持平衡
根据the poise documentation你可以通知任何资源,甚至是那些在资源之外的动作。 Poise 将在子上下文中搜索匹配的资源,直到找到匹配项。因此,您可以在 template
或 deploy_revision
资源上执行 notifies
,这将通知您的 runit_service
。
此外,notifying_block
的行为类似于普通 LWRP 中的 use_inline_resources
。因此,如果您的 runit_service
订阅了 my_app
资源,当 deploy_revision
或 template
更新时,它会收到通知。
这就是 MWRP(中等重量资源提供者,又名 poise)的力量。它让您可以做在普通 LWRP 中做不到的事情。正如 Tensibai 在他的回答中解释的那样,如果您不使用 poise,则无法在提供者自己的资源堆栈之外进行通知。
我正在尝试了解有关 LWRP 的更多信息,并且有一个可以安装软件包的工作提供商。当配置更改时,我无法尝试通知我的服务 - 基本上我在下面有这个。
我可以让 runit 服务订阅包 installed/config 更改的通知吗?在解决方案中,是否可以将服务和包分离,即。基于观察者而不是通知 runit_service["abc"] ?我想为该服务创建一个单独的提供者,但还没有找到明确的例子。
module MyApp
class Resource < Chef::Resource
include Poise
provides(:my_app)
actions(:enable)
end
class Provider < Chef::Provider
include Poise
provides(:my_app)
def action_enable
notifying_block do
template new_resource.database_config do
source 'database.erb'
#how to notify runit?
end
deploy_revision new_resource.process_id do
#how to notify runit?
end
end
end
end
end
在食谱中
my_app 'one' do
#some attributes
process_user 'nobody'
process_group 'nogroup'
end
runit_service "myapp" do
cookbook "myapp"
run_template_name "myapp"
log_template_name "myapp"
options({
:app_env => "development",
:app_home => "/srv/myapp/current",
:data_dir => "/srv/myapp/data"
})
retries 3
retry_delay 5
end
干杯!
您不应从 LWRP 内的资源发出通知或向其发出通知。
根据 Chef 版本和 use_inline_resources
的值,您无法通知,因为 LWRP 内的资源在单独的上下文中聚合。
但是如果LWRP里面的一个资源被更新了,LWRP会被标记为已更新并且可以通知一个资源。
您可能有一个 notification 从您的 my_app resource
重新启动服务,如下所示:
my_app 'one' do
#some attributes
process_user 'nobody'
process_group 'nogroup'
notifies :restart,"runit_service[myapp]" # adapt action and may use timer :immediately, see the doc link above
end
或者,如果您希望以其他方式工作,您可以使用 subscribe 方式,如下所示:
runit_service "myapp" do
cookbook "myapp"
run_template_name "myapp"
log_template_name "myapp"
options({
:app_env => "development",
:app_home => "/srv/myapp/current",
:data_dir => "/srv/myapp/data"
})
retries 3
retry_delay 5
subscribes :restart,"my_app[one]" # again adapt action and may use timer :immediately, see the doc link above
end
通常订阅语法很有用,可以避免在彼此的资源中发出通知,可以有很多,所以如果你的服务托管多个应用程序,最容易获得它会在每次应用程序更改时重新启动,其中有多个订阅行服务比每个应用程序中的通知。
即:
runit_service "tomcat" do
[...]
subscribe :restart,"my_app[one]"
subscribe :restart,"my_app[two]"
subscribe :restart,"my_app[three]"
end
保持平衡
根据the poise documentation你可以通知任何资源,甚至是那些在资源之外的动作。 Poise 将在子上下文中搜索匹配的资源,直到找到匹配项。因此,您可以在 template
或 deploy_revision
资源上执行 notifies
,这将通知您的 runit_service
。
此外,notifying_block
的行为类似于普通 LWRP 中的 use_inline_resources
。因此,如果您的 runit_service
订阅了 my_app
资源,当 deploy_revision
或 template
更新时,它会收到通知。
这就是 MWRP(中等重量资源提供者,又名 poise)的力量。它让您可以做在普通 LWRP 中做不到的事情。正如 Tensibai 在他的回答中解释的那样,如果您不使用 poise,则无法在提供者自己的资源堆栈之外进行通知。