厨师“通知”无法重新启动或重新加载服务

我正在使用chef-solo v10.12.0来configuration一个Ubuntu 12.04虚拟机,并且在configuration文件发生更改时,我仍然遇到服务无法重新启动或重新加载的问题。

日志中没有任何错误,尽pipe它明显在配方中做其他事情。 作为一种解决方法,我已经手动重新启动服务,或者每次执行配方时强制重新加载/重新启动,但是我更愿意弄清楚发生了什么问题,并按预期工作。

一个例子配方,一贯无法按预期工作:

package "pgbouncer" cookbook_file "/etc/default/pgbouncer" do source "pgbouncer/pgbouncer" owner "root" group "root" mode 0644 end service "pgbouncer" do supports :start => true, :stop => true, :restart => true, :reload => true, :status => true action [:enable, :start] end cookbook_file "/etc/pgbouncer/userlist.txt" do source "pgbouncer/userlist.txt" owner "postgres" group "postgres" mode 0640 notifies :restart, "service[pgbouncer]" end template "/etc/pgbouncer/pgbouncer.ini" do source "pgbouncer/pgbouncer.ini" owner "postgres" group "postgres" mode 0640 variables :postgres_host => node[:postgres_host] notifies :restart, "service[pgbouncer]" end 

我要检查的第一件事是运行chef-client的用户有权启动/重启服务(通常不是问题)。

接下来,我会检查是否没有其他食谱正在运行,这是抵消这个食谱的逻辑(有时是问题,但不经常)。

我真正认为导致你的问题是厨师处理它是通过壳执行需要的队列。 对同一个服务的多个有些冲突的调用可能会导致意外的行为(如您已经看到的)。 默认情况下,所有的'shell'调用都作为chef-client运行的收敛阶段的最后一部分处理。 此外,厨师并不保证任何特定的执行顺序,所以事情可能经常发生不按顺序,并可能产生不良行为取决于您正在操作的服务的软件。 通常用下面的技术来解决这个问题是你所需要的。

你的问题的快速和肮脏的答案是添加一个:定时器参数给你的通知电话。 DOC: http : //docs.opscode.com/resource_common.html#notifications-timers

这里是上面示例代码的build议更新:

 package "pgbouncer" service "pgbouncer" do supports :start => true, :stop => true, :restart => true, :reload => true, :status => true action [:enable, :start] end cookbook_file "/etc/default/pgbouncer" do source "pgbouncer/pgbouncer" owner "root" group "root" mode 0644 end cookbook_file "/etc/pgbouncer/userlist.txt" do source "pgbouncer/userlist.txt" owner "postgres" group "postgres" mode 0640 notifies :restart, "service[pgbouncer]", :immediately end template "/etc/pgbouncer/pgbouncer.ini" do source "pgbouncer/pgbouncer.ini" owner "postgres" group "postgres" mode 0640 variables :postgres_host => node[:postgres_host] notifies :restart, "service[pgbouncer]", :immediately end 

这不是最有效的方法,因为它可能导致守护进程执行太多冗余操作(在一次运行中多达3次“开始”调用:启动,重新启动,重新启动)。 还有另外一种更好的面向对象的方式,通过利用定义(DOC: http : //docs.opscode.com/essentials_cookbook_definitions.html )来做到这一点。 这基本上是您定义的pgbouncer服务资源的自定义包装,以减less执行冗余调用的低效率,同时确保它们被有效地执行,但是我将留给您决定什么是最适合您的用例。

这似乎是一个普遍的资源和通知相当普遍的问题。

我使用了Opscode JIRA票据跟踪器,并且有一张票据 ,讨论了大量修改和修复通知和资源行为,将在10.14.0

我会尝试将您的服务定义更改为以下

 service "pgbouncer" do supports :start => true, :stop => true, :restart => true, :reload => true, :status => true action :enable end 

我想我记得前一段时间经历过类似的事情,这是罪魁祸首。 让我知道这是否适合你,但由于某种原因,这是我一直在定义我的服务一段时间,我从来没有任何问题。

不知道是否pgbouncer提供了Upstart或Init(看起来是从这里初始化: http : //packages.ubuntu.com/precise/amd64/pgbouncer/filelist ),但我会给你这个服务资源一枪,如果你仍然有问题:

 service "pgbouncer" do provider Chef::Provider::Service::Init supports :start => true, :stop => true, :restart => true, :reload => true, :status => true action [:enable, :start] end 

另外,我还要加上:immediately Gregory Patmore也build议的:immediately论点。