厨师按顺序停止和启动服务

How*_*ard 6 chef chef-solo

我的食谱中有以下几行

service "apache" do
  action :stop
end

# Do something..

service "apache" do
  action :start
end
Run Code Online (Sandbox Code Playgroud)

我发现第二个块没有执行。任何原因?

小智 10

通知是处理这个问题的正确方法。

假设您要执行以下操作:

  • 有条件地下载文件
  • 如果文件被下载
    • 立即停止 apache
    • 处理文件(例如解压缩或移动它)
    • 再次启动apache

你会这样做:

# Define the apache service but don't do anything with it
service "apache" do
  action :nothing
end

# Define your post-fetch script but don't actually do it
execute "process my file" do
   ... your code here
  action :nothing
  notifies :start, "service[apache]"
end

# Fetch the file. Maybe the file won't be fetched because of not_if or checksum.
# In that case apache won't be stopped or started, it will just keep running.
remote_file "/tmp/myfile" do
  source "http://fileserver/myfile"
  notifies :stop, "service[apache]", :immediately
  notifies :run, execute["process my file"]
end
Run Code Online (Sandbox Code Playgroud)


小智 8

问题是您的资源具有相同的名称。服务“apache”不是唯一的,所以厨师正在对它们进行重复数据删除。您的选择是给他们这样的单独名称

service "apache stop" do
  service_name "apache"
  action :stop
end

# Do something

service "apache start" do
  service_name "apache"
  action :start
end
Run Code Online (Sandbox Code Playgroud)

您还可以使用来自“#Do something”块的通知来发送 :restart 到服务“apache”。这是人们通常使用的模式,单个服务,向其发送通知(或使用订阅)。更多在这里:

http://wiki.opscode.com/display/chef/Resources#Resources-Notifications


Tim*_*ter -1

服务启动和重新启动的默认设置是将其全部保存到厨师客户端运行结束。如果您确实想要启动或重新启动两个服务,请指定它们立即发生:

service "apache" do 
  action :start, :immediately
end
Run Code Online (Sandbox Code Playgroud)

这样做通常不是一个好主意,因为多次重新启动可能会导致不必要的服务中断。这就是 Chef 尝试保存所有服务重新启动直到运行结束的原因。

  • 对于寻找此解决方案的其他人来说,服务资源上的“操作”不支持“:立即”。立即标志仅存在于厨师通知中(http://docs.opscode.com/resource_common_notifications.html)。 (8认同)
  • 只需让它在部署结束时重新启动即可。您真的需要在部署期间禁用 apache 吗?如果有疑问,您可以创建一个维护页面,该页面会阻止所有流量。 (2认同)