在通知重启后,在Chef中停止服务

时间:2014-12-17 21:31:52

标签: ruby chef

因此我们使用在第三方食谱中声明的服务:

service "some-service" do
  action [:enable, :stop]
  supports :restart => true, :status => true, :start => true, :stop => true
end

在我们的主要食谱中,我们经常做这样的事情:

resource "mah_resurce" do
  stuff
  notifies :restart, "service[some-service]"
end

对于我们对本手册部署的代码进行单元测试,我们希望禁用此服务。

service "some-service" do
  action :disable, :stop
end

除非这不起作用。所以我尝试了这个:

file "/tmp/why_you_no_worky" do
  action :touch
  notifies :disable, "service[some-service]"
end

这感觉不对。在单独的单元测试模块中有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

我能想到的最佳选择是暂时用无操作替换restart_command

ruby_block 'stop service' do
  block do
    r = resources('service[some-service]')
    r.restart_command('/bin/true')
    r.run_action(:stop)
    r.run_action(:disable)
  end
end

Chef仍然认为它正在重新启动,但服务将被停止。如果您的服务资源没有显式重启命令,请对start_command执行相同的操作。

答案 1 :(得分:0)

另一个机会是取消现有的延迟通知。我认为这是一种更好的方法,因为您不必修改您的食谱。不幸的是,Chef没有提供这样的功能,你必须“hack”进入Chef内部部分。

查看此问题:chef client cancel notifies delayed

并查看this cookbook