TomDavies
TomDavies

Reputation: 672

How can I make chef restart a service with additional parameters passed in?

I have a template for a Rails site for Sphinx configuration. There can be multiple different Sphinx services on the same machine running on different ports, one per app. Therefore, I I only want to restart Sphinx for each site if their corresponding configuration template changes. I've created an /etc/init.d/sphinx script that restarts just one sphinx based on a parameter similar to:

/etc/init.d/sphinx restart /etc/sphinx/site1.conf

Where site1.conf is defined by a Chef template. I'd really love to use the notifies functionality for Chef Templates to pass in the correct site1.conf parameter if the template changes. Is this possible?

Alternatively, I suppose I could just register a different service for each site similar to:

/etc/init.d/sphinx_site1

However, I'd prefer to pass in the parameters to the script instead.

Upvotes: 1

Views: 2020

Answers (1)

andrewle
andrewle

Reputation: 1731

When defining a service resource, you can customize the start, stop, and restart commands that will be run. You can define a service resource for each site that you have using these customized commands and set up their corresponding notifications.

For example:

service "sphinx_site1" do
  supports :restart => true
  restart_command "/etc/init.d/sphinx restart /etc/sphinx/site1.conf"
  action :nothing
end

template "/etc/sphinx/site1.conf" do
  notifies :restart, "service[sphix_site1]"
end

Upvotes: 7

Related Questions