Skip to content

Instantly share code, notes, and snippets.

@mbokman
Last active October 21, 2015 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbokman/a29592680f1dbcb05a65 to your computer and use it in GitHub Desktop.
Save mbokman/a29592680f1dbcb05a65 to your computer and use it in GitHub Desktop.
Trigger cookbook postgresql::server_conf

Cookbook [postgresql] (https://supermarket.chef.io/cookbooks/postgresql) contains a recipe called server_conf.rb whose content is:

change_notify = node['postgresql']['server']['config_change_notify']

template "#{node['postgresql']['dir']}/postgresql.conf" do
 source "postgresql.conf.erb"
 owner "postgres"
 group "postgres"
 mode 0600
 notifies change_notify, 'service[postgresql]', :immediately
end

template "#{node['postgresql']['dir']}/pg_hba.conf" do
 source "pg_hba.conf.erb"
 owner "postgres"
 group "postgres"
 mode 00600
 notifies change_notify, 'service[postgresql]', :immediately
end

This is included (via include_recipe) in recipe server_debian.rb which is in turn is included in recipe server.rb which is part of my run list.

My recipe, as mentioned, needs to do a large import into Postgresql and does the following (simplified) after Postgresql has been installed and tuned via the cookbook:

ruby_block 'update_postgresql_config_before_import' do
  block do
    node.default['postgresql']['config']['synchronous_commit'] = "off"
    node.default['postgresql']['config']['checkpoint_segments'] = 100
    node.default['postgresql']['config']['checkpoint_timeout'] = "10min"
    node.default['postgresql']['config']['checkpoint_completion_target'] = "0.9"
    node.default['postgresql']['config']['fsync'] = "off"
    node.default['postgresql']['config']['full_page_writes'] = "off"
    run_context.include_recipe(['postgresql::server_conf'])
  end
  action :nothing
end

ruby_block 'update_postgresql_config_after_import' do
  block do
    node.default['postgresql']['config']['fsync'] = "on"
    node.default['postgresql']['config']['full_page_writes'] = "on"
    run_context.include_recipe(['postgresql::server_conf'])
  end
  action :nothing
end

bash "drop DB" do
  user 'root'
  code <<-EOH
    sudo -u postgres psql postgres -c "DROP DATABASE IF EXISTS example"
  EOH
  notifies :run, 'ruby_block[update_postgresql_config_before_import]', :immediately
end

bash "import DB" do
  user 'root'
  code <<-EOH
    sudo -u postgres ./import-a-lot-of-data.sh
  EOH
  notifies :run, 'ruby_block[update_postgresql_config_after_import]', :immediately
end

So the intention of the above is to activate specific settings in postgresql.conf just before and after a big import takes place.

I can see that my ruby_blocks are executed at the right moments but I do not see the node['postgresql']['config'] settings being persisted in postgresql.conf.

Using run_context.include_recipe (or run_context.load_recipe) is my attempt to somehow trigger the postgresql.conf write using the functionality available in the cookbook.

Obviously I can also just copy the code and template from the cookbook in my recipe but I hoped that would not be necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment