目前我的配方与重要的属性结构如下:
service 'myservice' do
action :nothing
supports :status => true, :start => true, :stop => true, :restart => true
end
package 'packagename' do
...
end
template 'configfile1'
notifies :restart, 'service[myservice]'
end
...
template 'configfileN'
notifies :restart, 'service[myservice]'
end
execute "a command from package which generates and enables the init script" do
notifies :start, 'service[myservice]', :immediately
end
execute "a command that should run once every time, that requires service to be running"
Run Code Online (Sandbox Code Playgroud)
通过这样做,我们确保服务的初始启动具有配置文件,在每次运行期间,服务正在为第二个执行块运行,如果任何配置文件发生更改,我们将重新启动服务以获取更改.
但是,如果厨师运行发生在服务的初始状态停止的地方(例如第一次运行或发生了不好的事情),并且配置文件已更改(特别是在第一次运行时,但可能用于其他运行),第一个执行块将导致服务以正确的配置文件启动,然后在运行结束时,服务将不必要地重新启动.(当然假设初始启动后的资源不会导致服务重启)
更改通知的目标操作似乎不起作用(因为立即通知仍会立即发生,然后发生延迟通知),此外也不正确.
此外,我们无法订阅第二次执行到服务启动,因为如果它已经运行,我们最终将不执行它.
这是非常挑剔的,但有没有一个更好的模式可以遵循,以最大限度地减少初始运行服务的重启?或者在采取特定操作时取消延迟通知的机制?
Chef 旨在表达配置策略(系统状态),这与表达要执行的任务序列略有不同。
幸运的是,由于 DSL 是基于 ruby 的,因此可以在运行时重新定义资源。
template "configfile1" do
notifies :create, "ruby_block[restart_service1]", :immediately
end
template "configfile2" do
notifies :create, "ruby_block[restart_service1]", :immediately
end
service "service1" do
action [:enable, :start]
end
ruby_block "restart_service1" do
block do
r = resources(:service => "service1")
a = Array.new(r.action)
a << :restart unless a.include?(:restart)
a.delete(:start) if a.include?(:restart)
r.action(a)
end
action :nothing
end
Run Code Online (Sandbox Code Playgroud)
这将重写服务资源,使其具有“action [:enable, :restart]”而不是“action [:enable, :start]”,如果任何模板资源更改了本次运行的状态。因此排序保持不变,您只能通过服务资源获得一个调用,而不是可能的三个调用。
小智 4
我使用了一个标志文件来实现这一点。
就像下面的“apt-get update”不会执行一次。
cookbook_file '/etc/apt/sources.list.d/maven.list' do
source "repo_files/ubuntu_maven.list"
cookbook "fluig-files"
mode 0644
notifies :run, "execute[should_update_repo]", :immediately
end
cookbook_file '/etc/apt/sources.list.d/couchbase.list' do
source "repo_files/ubuntu_couchbase.list"
cookbook "fluig-files"
mode 0644
notifies :run, "execute[should_update_repo]", :immediately
end
execute "apt-key couchbase" do
command "apt-key adv --keyserver keyserver.ubuntu.com --recv-keys A3FAA648D9223EDA"
action :run
not_if "apt-key list | grep couchbase"
notifies :run, "execute[should_update_repo]", :immediately
end
execute "should_update_repo" do
command "touch /tmp/should_update_repo"
action :nothing
end
# Update system
execute "apt-get update" do
command "rm -rf /tmp/should_update_repo && apt-get update"
action :run
only_if "test -f /tmp/should_update_repo"
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8370 次 |
| 最近记录: |