微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Chef - 使用 dnmsasq 防止多次服务重启或重新加载?

如何解决Chef - 使用 dnmsasq 防止多次服务重启或重新加载?

如何在一次厨师运行中重新启动/重新加载并符合厨师最佳实践?

使用状态文件是一种好习惯还是重写服务 dnsmasq 信息?

这个问题是7年前问的,有没有人找到更好的方法? 此处记录的重写服务--
Minimize service restarts from chef notifications?

问题:

当前代码导致 1 次重启和 1 次重新加载。这导致了问题。

dnsmasq 有 3 个配置文件,需要使用不同的启动/重启/重新加载方法进行管理。 dnsmasq 在设计上没有轮询作为参数,以防止它在每次更改时重新加载 /etc/resolv.conf。我想控制这里的重新加载。

package "install dnsmasq" do
  name 'dnsmasq'
  action :install
  notifies :create,'cookbook_file[/etc/dnsmasq.conf]',:delayed
  notifies :create,'template[/etc/resolv.dnsmasq]','template[/etc/resolv.conf]',:delayed
  notifies :restart,'service[dnsmasq]',:delayed
end

template '/etc/resolv.dnsmasq' do
  ...
  notifies :reload,:delayed
end

file '/etc/dnsmasq.conf' do
  ...
  notifies :restart,:delayed
end

template '/etc/resolv.conf' do
  ...
  notifies :reload,:delayed
end

service 'dnsmasq' do
  supports [:restart,:status,:start,:reload]
  action [ :enable,:start ]
  reload_command "/usr/bin/killall -s SIGHUP dnsmasq"
end

解决方法

如果你做的不止这些:

package "install dnsmasq" do
  name 'dnsmasq'
  action :install
end

file '/etc/dnsmasq.conf' do
  ...
  notifies :restart,'service[dnsmasq]',:delayed
end

template '/etc/resolv.dnsmasq' do
  ...
  notifies :reload,:delayed
end

template '/etc/resolv.conf' do
  ...
  notifies :reload,:delayed
end

service 'dnsmasq' do
  supports [:restart,:status,:start,:reload]
  action [ :enable,:start ]
  reload_command "/usr/bin/killall -s SIGHUP dnsmasq"
end

那么您的代码中可能存在关于您放入这些文件的内容的逻辑问题,这些资源是幂等的,从包安装通知模板和文件然后通知服务重启对于声明式模型来说是无稽之谈。

无需在这里处理通知,:delayed 也不是必需的,因为它是默认值,但它使阅读时很明显。

首先放置 /etc/dnsmasq.conf 文件,确保在需要重新加载之前首先触发重新启动通知,以避免因需要重新启动的配置更改而重新加载。重启后重新加载完全没有问题。

,

这个“累加器”解决方案会导致一次启动、重新启动或重新加载,具体取决于传递的内容。该过程以前称为倒带。这可能不是非常有效的编码或在 Chef 中执行此类活动的正确方法,但是目前该解决方案对我有用。

食谱:dnsmasq.rb

dnsmasq_service 'dnsmasq' do
  action :nothing
end

package 'dnsmasq' do
  action :install
end

# This has to come first,as it requires a restart
cookbook_file '/etc/dnsmasq.conf' do
  delayed_action :create
  notifies :restart,'dnsmasq_service[dnsmasq]'
end

# This comes second
template '/etc/resolv.dnsmasq' do
  delayed_action :create
  notifies :reload,'dnsmasq_service[dnsmasq]'
end

# This comes third.  Do not create until the other configs are down.
template '/etc/resolv.conf' do
  delayed_action :create
  notifies :reload,'dnsmasq_service[dnsmasq]'
end

# This comes fourth
# Make sure the service is always started and enabled.
service 'dnsmasq' do
  action [ :enable,:start ]
  delayed_action :start
end

资源:dnsmasq_service.rb

#https://github.com/chef/chef/issues/5454
# Be aware of the cookbook name being lost
#https://github.com/chef/chef/issues/5438
# Could not get this to work without the log notifications.

#https://coderanger.net/rewind/
#https://docs.chef.io/infra_language/

provides :dnsmasq_service
resource_name :dnsmasq_service

default_action :start

action :start do
  # Find resource,if it does not exist create with this action
  with_run_context :root do
    find_resource(:service,'dnsmasq') do
      action [ :enable,:start ]
    end
  end
  log "force :start dnsmasq notification" do
    notifies :start,:delayed
  end
end

action :reload do
  r = with_run_context :root do
    find_resource(:service,:reload ]
      reload_command "/usr/bin/killall -s SIGHUP dnsmasq"
    end
  end

  a = Array.new(r.action)
  f_act = :reload
  if a.include?(:restart)
    f_act = :restart
    with_run_context :root do
      edit_resource!(:service,'dnsmasq') do
        action [ :enable,:restart ]
      end
    end
  else
    with_run_context :root do
      edit_resource!(:service,:reload ]
        reload_command "/usr/bin/killall -s SIGHUP dnsmasq"
      end
    end
  end
  log "force #{f_act} dnsmasq notification" do
    notifies f_act,:delayed
  end
end

action :restart do
  r = with_run_context :root do
    find_resource(:service,:reload ]
    end
  end

  with_run_context :root do
    edit_resource!(:service,:restart ]
    end
  end
  log "force :restart dnsmasq notification" do
    notifies :restart,:delayed
  end
end

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。