From 3891d02d8b3c4db054514595a8f57b3b9dfe8c28 Mon Sep 17 00:00:00 2001 From: Mate Lakat Date: Wed, 19 Jul 2017 13:41:50 +0200 Subject: [PATCH 1/2] postgresql - reload: refactor Move out logic from the template to the cookbook, so it can be re-used later. --- chef/cookbooks/postgresql/recipes/server.rb | 20 +++++++++++++++++++ .../templates/default/postgresql.conf.erb | 16 ++------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/chef/cookbooks/postgresql/recipes/server.rb b/chef/cookbooks/postgresql/recipes/server.rb index 947acc4a78..9c496c0744 100644 --- a/chef/cookbooks/postgresql/recipes/server.rb +++ b/chef/cookbooks/postgresql/recipes/server.rb @@ -98,11 +98,31 @@ change_notify = node["postgresql"]["server"]["config_change_notify"] +raw_configuration = node["postgresql"]["config"] +postgresql_configuration = {} +raw_configuration.each do |key, value| + next if value.nil? + formatted_value = case value + when String + "'#{value}'" + when TrueClass + "on" + when FalseClass + "off" + else + value + end + postgresql_configuration[key] = formatted_value +end + template "#{node['postgresql']['dir']}/postgresql.conf" do source "postgresql.conf.erb" owner "postgres" group "postgres" mode 0600 + variables( + config: postgresql_configuration.sort, + ) notifies change_notify, "service[postgresql]", :immediately end diff --git a/chef/cookbooks/postgresql/templates/default/postgresql.conf.erb b/chef/cookbooks/postgresql/templates/default/postgresql.conf.erb index d2facc2a3d..6dd85d53cb 100644 --- a/chef/cookbooks/postgresql/templates/default/postgresql.conf.erb +++ b/chef/cookbooks/postgresql/templates/default/postgresql.conf.erb @@ -3,18 +3,6 @@ # Please refer to the PostgreSQL documentation for details on # configuration settings. -<% node['postgresql']['config'].sort.each do |key, value| %> -<% next if value.nil? -%> -<%= key %> = <%= - case value - when String - "'#{value}'" - when TrueClass - 'on' - when FalseClass - 'off' - else - value - end -%> +<% @config.each do |key, value| %> +<%= key %> = <%= value %> <% end %> From e6b408f05dc9cbbc199519704f2b254b9576da98 Mon Sep 17 00:00:00 2001 From: Mate Lakat Date: Wed, 19 Jul 2017 13:43:51 +0200 Subject: [PATCH 2/2] postgresql - reload: Graceful reload of configuration This change splits the postgresql configuration file into two pieces. One should contain the options that require a server restart (the main postgresql.conf file) and that is sourcing the other file (postgresql_reloadable.conf) which is the home for reloadable parameters. This way we can use chef's notification mechanism to either reload or restart the server. Please consult postgresql's manual for reloadable configuration options. If you want to be able to gracefuly reload a specific parameter, amend that to `reloadable_parameters` --- chef/cookbooks/postgresql/recipes/server.rb | 38 ++++++++++++++++++- .../templates/default/postgresql.conf.erb | 2 + .../default/postgresql_reloadable.conf.erb | 3 ++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 chef/cookbooks/postgresql/templates/default/postgresql_reloadable.conf.erb diff --git a/chef/cookbooks/postgresql/recipes/server.rb b/chef/cookbooks/postgresql/recipes/server.rb index 9c496c0744..c3de5fb295 100644 --- a/chef/cookbooks/postgresql/recipes/server.rb +++ b/chef/cookbooks/postgresql/recipes/server.rb @@ -115,17 +115,40 @@ postgresql_configuration[key] = formatted_value end +reloadable_parameters = [ + "log_line_prefix" +] + +config_needs_restart = postgresql_configuration.reject do |key, value| + reloadable_parameters.include? key +end + +config_needs_reload = postgresql_configuration.select do |key, value| + reloadable_parameters.include? key +end + template "#{node['postgresql']['dir']}/postgresql.conf" do source "postgresql.conf.erb" owner "postgres" group "postgres" mode 0600 variables( - config: postgresql_configuration.sort, + config: config_needs_restart.sort ) notifies change_notify, "service[postgresql]", :immediately end +template "#{node['postgresql']['dir']}/postgresql_reloadable.conf" do + source "postgresql_reloadable.conf.erb" + owner "postgres" + group "postgres" + mode "0600" + variables( + config: config_needs_reload.sort + ) + notifies :run, "bash[reload-postgresql-configuration]", :immediately +end + template "#{node['postgresql']['dir']}/pg_hba.conf" do source "pg_hba.conf.erb" owner "postgres" @@ -184,3 +207,16 @@ only_if only_if_command if ha_enabled action :run end + +# Reload the configuration file +# We use the SQL interface for that. This way it will work +# in clustered mode as well, no matter which node runs it +# first +bash "reload-postgresql-configuration" do + user "postgres" + code <<-EOH + echo "SELECT pg_reload_conf();" | psql + EOH + action :nothing +end + diff --git a/chef/cookbooks/postgresql/templates/default/postgresql.conf.erb b/chef/cookbooks/postgresql/templates/default/postgresql.conf.erb index 6dd85d53cb..f90fcc6f6e 100644 --- a/chef/cookbooks/postgresql/templates/default/postgresql.conf.erb +++ b/chef/cookbooks/postgresql/templates/default/postgresql.conf.erb @@ -3,6 +3,8 @@ # Please refer to the PostgreSQL documentation for details on # configuration settings. +include 'postgresql_reloadable.conf' + <% @config.each do |key, value| %> <%= key %> = <%= value %> <% end %> diff --git a/chef/cookbooks/postgresql/templates/default/postgresql_reloadable.conf.erb b/chef/cookbooks/postgresql/templates/default/postgresql_reloadable.conf.erb new file mode 100644 index 0000000000..4eafb7a28c --- /dev/null +++ b/chef/cookbooks/postgresql/templates/default/postgresql_reloadable.conf.erb @@ -0,0 +1,3 @@ +<% @config.each do |key, value| %> +<%= key %> = <%= value %> +<% end %>