35 lines
902 B
Ruby
35 lines
902 B
Ruby
#
|
|
# Cookbook:: kosmos_postgresql
|
|
# Recipe:: replica
|
|
#
|
|
|
|
service postgresql_service do
|
|
supports restart: true, status: true, reload: true
|
|
end
|
|
|
|
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
|
|
|
|
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
|