Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service restart mechanism in chef

Tags:

chef-infra

I'm looking for an explanation on how chef's restart mechanism works behind the scenes. I can't find any documentation for it online, and I got stuck trying to trace the code (triggers are spooky action-at-a-distance).

Let's look specifically at nginx, and assume we're using a chef server and not chef-solo (I don't know if that makes a difference).

I have this (for example) in a recipe:

template '/etc/nginx/nginx.conf' do
  source 'nginx.cfg.erb'
  owner "root"
  group "root"
  mode 00755
  notifies :reload, "service[nginx]", :delayed
end

The notifies :reload bit means it triggers a reload, and the :delayed means that it will happen at the end of the chef-client run. How does that work behind the scenes? I'm having trouble following the execution thread.

Somewhere chef-client has to run service nginx reload or something along those lines. Where and how is that defined?

like image 270
cha0site Avatar asked Aug 19 '13 09:08

cha0site


2 Answers

notifies sends a notification for another Chef resource to do something.

In your example, it's telling the resource service[nginx] to :reload. service[nginx] is a service resource whose name is nginx.

For this to work, service[nginx] must have been declared in your node's run_list at some point. Otherwise, Chef will throw an error. Usually this is either done manually in a recipe by the user, or via a dependency (say, the application, or the nginx cookbooks).

Exactly how Chef runs the reload command depends on how the service[nginx] resource was declared, but it usually depends on the underlying operating system (that's one of the beauties of using a tool such as this -- it abstracts many lower level details away from you and allows you to use the same code across multiple platforms).

In the Syntax section of the service documentation you find this:

  • service tells the chef-client to use one of the following providers during the chef-client run: Chef::Provider::Service::Init, Chef::Provider::Service::Init::Debian, Chef::Provider::Service::Upstart, Chef::Provider::Service::Init::Freebsd, Chef::Provider::Service::Init::Gentoo, Chef::Provider::Service::Init::Redhat, Chef::Provider::Service::Solaris, Chef::Provider::Service::Windows, or Chef::Provider::Service::Macosx. The chef-client will detect the platform at the start of the run based on data collected by Ohai. After the platform is identified, the chef-client will determine the correct provider
like image 197
cassianoleal Avatar answered Oct 11 '22 08:10

cassianoleal


The functionality is described in the chef doco.

If the configuration file's contents change this will trigger a reload of the nginx service. The "delayed" setting means the reload action occurs at the end of the chef run. The idea is that there could be several configuration files changed by a chef run and you want a single reload at the end rather than for each changed file (which is the "immediately" option).

like image 31
Mark O'Connor Avatar answered Oct 11 '22 09:10

Mark O'Connor