Skip to content

Instantly share code, notes, and snippets.

@adsr
Created July 31, 2024 23:14
Show Gist options
  • Save adsr/3ab5d0253ef179c38a1c5045e3e023a0 to your computer and use it in GitHub Desktop.
Save adsr/3ab5d0253ef179c38a1c5045e3e023a0 to your computer and use it in GitHub Desktop.
diff --git a/cookbooks/fb_apache/README.md b/cookbooks/fb_apache/README.md
index 40d6f0c..daff042 100644
--- a/cookbooks/fb_apache/README.md
+++ b/cookbooks/fb_apache/README.md
@@ -11,20 +11,21 @@ Attributes
* node['fb_apache']['sites'][$SITE][$CONFIG]
* node['fb_apache']['sysconfig'][$KEY]
* node['fb_apache']['sysconfig']['_extra_lines']
* node['fb_apache']['modules']
* node['fb_apache']['modules_directory']
* node['fb_apache']['modules_mapping']
* node['fb_apache']['module_packages']
* node['fb_apache']['enable_default_site']
* node['fb_apache']['extra_configs']
* node['fb_apache']['mpm']
+* node['fb_apache']['allow_restart_on_update']
Usage
-----
### Packages
My default `fb_apache` will install and keep up to date the `apache` and
`mod_ssl` packages as relevant for your distribution. If you'd prefer to do
this on your own then you can set `node['fb_apache']['manage_packages']` to
`false`.
For modules, we keep a mapping of the package required for modules in
@@ -208,10 +209,16 @@ and we've pre-populated all the common modules on both distro variants.
Finally, `node['fb_apache']['modules_directory']` is set to the proper module
directory for your distro, but you may override it if you'd like.
### Extra Configs
Everything in `node['fb_apache']['extra_configs']` will be converted from hash
syntax to Apache Config syntax in the same 1:1 manner as the `sites` hash above
and put into an `fb_apache.conf` config file.
### MPM
Allows to chose mpm module used. It can be prefork, worker or event.
+
+### Service restarts
+By default this cookbook will restart the Apache service when related config
+file resources are updated. To prevent this behavior, set
+`node['fb_apache']['allow_restart_on_update']` to false. Note this only applies
+to service restarts. Reload actions are always permitted.
diff --git a/cookbooks/fb_apache/attributes/default.rb b/cookbooks/fb_apache/attributes/default.rb
index 14cb1ce..4d37606 100644
--- a/cookbooks/fb_apache/attributes/default.rb
+++ b/cookbooks/fb_apache/attributes/default.rb
@@ -81,20 +81,21 @@ when 'debian'
'htcacheclean_path' => '/var/cache/apache2/mod_cache_disk',
'htcacheclean_options' => ['-n'],
}.each do |k, v|
sysconfig[k] = v
end
end
default['fb_apache'] = {
'sysconfig' => sysconfig,
'manage_packages' => true,
+ 'allow_restart_on_update' => true,
'enable_default_site' => true,
'sites' => {},
'extra_configs' => {},
'modules' => modules,
'modules_directory' => moddir,
'modules_mapping' => {
'actions' => 'mod_actions.so',
'alias' => 'mod_alias.so',
'asis' => 'mod_asis.so',
'auth_basic' => 'mod_auth_basic.so',
diff --git a/cookbooks/fb_apache/recipes/default.rb b/cookbooks/fb_apache/recipes/default.rb
index 2dc152d..bd08063 100644
--- a/cookbooks/fb_apache/recipes/default.rb
+++ b/cookbooks/fb_apache/recipes/default.rb
@@ -114,21 +114,21 @@ package pkgs do
)
}
action :upgrade
end
template sysconfig do
source 'sysconfig.erb'
owner node.root_user
group node.root_group
mode '0644'
- notifies :restart, 'service[apache]'
+ notifies :run, 'ruby_block[maybe_restart_apache]'
end
[moddir, sitesdir, confdir].uniq.each do |dir|
directory dir do
owner node.root_user
group node.root_group
mode '0755'
end
end
@@ -152,21 +152,21 @@ end
fb_apache_cleanup_modules 'doit' do
mod_dir moddir
end
template "#{moddir}/fb_modules.conf" do
not_if { node.centos6? }
owner node.root_user
group node.root_group
mode '0644'
notifies :verify, 'fb_apache_verify_configs[doit]', :before
- notifies :restart, 'service[apache]'
+ notifies :run, 'ruby_block[maybe_restart_apache]'
end
template "#{sitesdir}/fb_sites.conf" do
owner node.root_user
group node.root_group
mode '0644'
notifies :verify, 'fb_apache_verify_configs[doit]', :before
notifies :reload, 'service[apache]'
end
@@ -177,32 +177,32 @@ template "#{confdir}/fb_apache.conf" do
notifies :verify, 'fb_apache_verify_configs[doit]', :before
notifies :reload, 'service[apache]'
end
template "#{moddir}/00-mpm.conf" do
owner node.root_user
group node.root_group
mode '0644'
# MPM cannot be changed on reload, only restart
notifies :verify, 'fb_apache_verify_configs[doit]', :before
- notifies :restart, 'service[apache]'
+ notifies :run, 'ruby_block[maybe_restart_apache]'
end
# We want to collect apache stats
template "#{confdir}/status.conf" do
source 'status.erb'
owner node.root_user
group node.root_group
mode '0644'
variables(:location => '/server-status')
notifies :verify, 'fb_apache_verify_configs[doit]', :before
- notifies :restart, 'service[apache]'
+ notifies :run, 'ruby_block[maybe_restart_apache]'
end
moddirbase = ::File.basename(moddir)
sitesdirbase = ::File.basename(sitesdir)
confdirbase = ::File.basename(confdir)
fb_apache_verify_configs 'doit' do
httpdir httpdir
moddir moddirbase
sitesdir sitesdirbase
confdir confdirbase
@@ -221,10 +221,32 @@ if node['platform_family'] == 'debian'
link "#{sitesdir}/000-default.conf" do
only_if { node['fb_apache']['enable_default_site'] }
to '../sites-available/000-default.conf'
end
end
service 'apache' do
service_name svc
action [:enable, :start]
end
+
+
+# This is a hacky way to perform a conditional `notifies` via an attribute
+#
+# On the resources that use this block, you may read:
+#
+# notifies :run, 'ruby_block[maybe_restart_apache]'
+#
+# as if it were:
+#
+# notifies :restart, 'service[apache]' if lazy { node['fb_apache']['allow_restart_on_update'] }
+#
+ruby_block 'maybe_restart_apache' do
+ block do
+ return unless node['fb_apache']['allow_restart_on_update']
+ r_name = 'service[apache]'
+ r = run_context.resource_collection.find(r_name)
+ raise "Expected a single #{r_name} resource" unless r.kind_of?(Chef::Resource)
+ r.run_action(:restart)
+ end
+ action :nothing
+end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment