34 lines
954 B
Ruby
34 lines
954 B
Ruby
#
|
|
# Cookbook:: kosmos_postgresql
|
|
# Recipe:: replica
|
|
#
|
|
|
|
postgresql_custom_server postgresql_version do
|
|
role "replica"
|
|
end
|
|
|
|
postgresql_data_bag_item = data_bag_item('credentials', 'postgresql')
|
|
|
|
primary = postgresql_primary
|
|
|
|
if primary.nil?
|
|
Chef::Log.warn("No PostgreSQL primary node found. Skipping replication setup.")
|
|
return
|
|
end
|
|
|
|
# TODO Replace pg.kosmos.local with private IP once available
|
|
# via proper node attribute
|
|
# https://gitea.kosmos.org/kosmos/chef/issues/263
|
|
execute "set up replication" do
|
|
command <<-EOF
|
|
systemctl stop #{postgresql_service}
|
|
mv #{postgresql_data_dir} #{postgresql_data_dir}.old
|
|
pg_basebackup -h pg.kosmos.local -U replication -D #{postgresql_data_dir} -R
|
|
chown -R postgres:postgres #{postgresql_data_dir}
|
|
systemctl start #{postgresql_service}
|
|
EOF
|
|
environment 'PGPASSWORD' => postgresql_data_bag_item['replication_password']
|
|
sensitive true
|
|
not_if { ::File.exist? "#{postgresql_data_dir}/standby.signal" }
|
|
end
|