Skip to content
Snippets Groups Projects
Commit e74d1844 authored by Ian Baum's avatar Ian Baum
Browse files

Merge branch 'nhxnguyen-5575' into 'master'

Update replicate-geo-database to support PostgreSQL 12

See merge request gitlab-org/omnibus-gitlab!4495
parents 21ce8bc4 3fe6e893
No related branches found
No related tags found
No related merge requests found
Showing
with 187 additions and 7 deletions
---
title: 'Update replicate-geo-database to support PostgreSQL 12'
merge_request: 4495
author:
type: changed
Loading
Loading
@@ -17,7 +17,11 @@
account_helper = AccountHelper.new(node)
omnibus_helper = OmnibusHelper.new(node)
 
pg_helper = PgHelper.new(node)
gitlab_user = account_helper.gitlab_user
postgresql_username = account_helper.postgresql_user
postgresql_group = account_helper.postgresql_group
 
gitlab_rails_source_dir = '/opt/gitlab/embedded/service/gitlab-rails'
gitlab_rails_dir = node['gitlab']['gitlab-rails']['dir']
Loading
Loading
@@ -50,3 +54,12 @@ end
file '/opt/gitlab/embedded/service/gitlab-rails/ee/db/geo/schema.rb' do
owner gitlab_user
end
# This is included by postgresql.conf for replication settings in PostgreSQL 12 and higher
if node['postgresql']['enable']
file pg_helper.geo_config do
owner postgresql_username
group postgresql_group
mode 0640
end
end
Loading
Loading
@@ -300,6 +300,10 @@ class BasePgHelper < BaseHelper
::File.join(config_dir, 'pg_ident.conf')
end
 
def geo_config
::File.join(config_dir, 'gitlab-geo.conf')
end
def ssl_cert_file
::File.absolute_path(node['postgresql']['ssl_cert_file'], config_dir)
end
Loading
Loading
Loading
Loading
@@ -6,11 +6,12 @@ action :create do
postgresql_helper = new_resource.pg_helper
 
template postgresql_helper.postgresql_config do
geo_config = { geo_secondary_enabled: node.dig('gitlab', 'geo-secondary', 'enable') }
source 'postgresql.conf.erb'
owner new_resource.username
mode '0644'
helper(:pg_helper) { postgresql_helper }
variables(node['postgresql'].to_hash)
variables(node['postgresql'].to_hash.merge(geo_config))
end
 
template postgresql_helper.postgresql_runtime_config do
Loading
Loading
Loading
Loading
@@ -474,3 +474,6 @@ max_locks_per_transaction = <%= @max_locks_per_transaction %> # min 10
#custom_variable_classes = '' # list of custom variable class names
 
include 'runtime.conf'
<% if @geo_secondary_enabled %>
include_if_exists 'gitlab-geo.conf'
<% end %>
Loading
Loading
@@ -25,6 +25,14 @@ module Geo
@postgresql_user ||= GitlabCtl::PostgreSQL.postgresql_username
end
 
def postgresql_group
@postgresql_group ||= GitlabCtl::PostgreSQL.postgresql_group
end
def postgresql_version
@postgresql_version ||= GitlabCtl::PostgreSQL.postgresql_version(data_path)
end
def check_gitlab_active?
return unless gitlab_is_active?
 
Loading
Loading
@@ -104,14 +112,13 @@ module Geo
run_command(pg_basebackup_command,
live: true, timeout: @options[:backup_timeout])
 
puts "* Writing recovery.conf file with sslmode=#{@options[:sslmode]} and sslcompression=#{@options[:sslcompression]}".color(:green)
create_recovery_file!
puts '* Restoring postgresql.conf'.color(:green)
run_command("mv #{data_path}/postgresql/postgresql.conf #{data_path}/postgresql/data/")
 
write_replication_settings!
puts '* Setting ownership permissions in PostgreSQL data directory'.color(:green)
run_command("chown -R #{postgresql_user}:#{postgresql_user} #{data_path}/postgresql/data")
run_command("chown -R #{postgresql_user}:#{postgresql_group} #{data_path}/postgresql/data")
 
puts '* Starting PostgreSQL and all GitLab services'.color(:green)
run_command('gitlab-ctl start')
Loading
Loading
@@ -127,6 +134,17 @@ module Geo
create_replication_slot!
end
 
def write_replication_settings!
if postgresql_version >= 12
puts "* PostgreSQL 12 or newer. Writing settings to postgresql.conf and creating standby.signal".color(:green)
write_recovery_settings!
create_standby_file!
else
puts "* Writing recovery.conf file with sslmode=#{@options[:sslmode]} and sslcompression=#{@options[:sslcompression]}".color(:green)
create_recovery_file!
end
end
private
 
def create_gitlab_backup!
Loading
Loading
@@ -144,7 +162,27 @@ module Geo
EOF
)
end
run_command("chown #{postgresql_user} #{@pgpass}")
run_command("chown #{postgresql_user}:#{postgresql_group} #{@pgpass}")
end
def write_recovery_settings!
geo_conf_file = "#{data_path}/postgresql/data/gitlab-geo.conf"
File.open(geo_conf_file, 0640) do |file|
settings = <<~EOF
# - Added by GitLab Omnibus for Geo replication -
recovery_target_timeline = '#{@options[:recovery_target_timeline]}'
primary_conninfo = 'host=#{@options[:host]} port=#{@options[:port]} user=#{@options[:user]} password=#{@options[:password]} sslmode=#{@options[:sslmode]} sslcompression=#{@options[:sslcompression]}'
EOF
file.write(settings)
file.write("primary_slot_name = '#{@options[:slot_name]}'\n") if @options[:slot_name]
end
end
def create_standby_file!
standby_file = "#{data_path}/postgresql/data/standby.signal"
File.write(standby_file, "")
run_command("chown #{postgresql_user}:#{postgresql_group} #{standby_file}")
end
 
def create_recovery_file!
Loading
Loading
@@ -158,7 +196,7 @@ module Geo
)
file.write("primary_slot_name = '#{@options[:slot_name]}'\n") if @options[:slot_name]
end
run_command("chown #{postgresql_user} #{recovery_file}")
run_command("chown #{postgresql_user}:#{postgresql_group} #{recovery_file}")
end
 
def ask_pass
Loading
Loading
Loading
Loading
@@ -35,6 +35,19 @@ module GitlabCtl
# TODO: Remove support for legacy attributes in GitLab 13.0
(node_attributes.dig('gitlab', 'postgresql', 'username') || node_attributes.dig('postgresql', 'username')).to_s
end
def postgresql_group
node_attributes = GitlabCtl::Util.get_node_attributes
node_attributes.dig('postgresql', 'group')
end
def postgresql_version(data_path)
version_file = "#{data_path}/postgresql/data/PG_VERSION"
return nil unless File.exist?(version_file)
File.read(version_file).strip.to_i
end
end
end
end
Loading
Loading
@@ -212,6 +212,26 @@ RSpec.describe 'gitlab-ee::geo-secondary' do
end
end
 
describe 'PostgreSQL gitlab-geo.conf', focus: true do
let(:chef_run) { ChefSpec::SoloRunner.converge('gitlab-ee::default') }
let(:geo_conf) { '/var/opt/gitlab/postgresql/data/gitlab-geo.conf' }
let(:postgresql_conf) { '/var/opt/gitlab/postgresql/data/postgresql.conf' }
context 'when postgresql enabled on the node' do
it 'renders gitlab-geo.conf' do
expect(chef_run).to render_file(geo_conf)
end
end
context 'when postgresql disabled on the node' do
before { stub_gitlab_rb(postgresql: { enable: false }) }
it 'does not render gitlab-geo.conf' do
expect(chef_run).not_to render_file(geo_conf)
end
end
end
describe 'Restart geo-secondary dependent services' do
let(:chef_run) { ChefSpec::SoloRunner.converge('gitlab-ee::default') }
 
Loading
Loading
Loading
Loading
@@ -401,6 +401,26 @@ RSpec.describe 'postgresql 9.6' do
).with_content(/max_locks_per_transaction = 128/)
end
 
context 'with geo_secondary_role enabled' do
before { stub_gitlab_rb(geo_secondary_role: { enable: true }) }
it 'includes gitlab-geo.conf in postgresql.conf' do
expect(chef_run).to render_file(postgresql_conf)
.with_content(/include_if_exists 'gitlab-geo.conf'/)
end
end
context 'with geo_secondary_role disabled' do
before { stub_gitlab_rb(geo_secondary_role: { enable: false }) }
it 'does not gitlab-geo.conf in postgresql.conf' do
expect(chef_run).to render_file(postgresql_conf)
.with_content { |content|
expect(content).not_to match('gitlab-geo.conf')
}
end
end
context 'with custom logging settings set' do
before do
stub_gitlab_rb({
Loading
Loading
Loading
Loading
@@ -33,6 +33,8 @@ RSpec.describe Geo::Replication, '#execute' do
 
allow(GitlabCtl::Util).to receive(:run_command).and_return(command)
allow(subject).to receive(:postgresql_user).and_return('gitlab-psql')
allow(subject).to receive(:postgresql_group).and_return('gitlab-psql')
allow(subject).to receive(:postgresql_version).and_return(11)
end
 
it 'replicates geo database' do
Loading
Loading
@@ -51,6 +53,13 @@ RSpec.describe Geo::Replication, '#execute' do
subject.execute
end
 
it 'creates a recovery file' do
allow(subject).to receive(:ask_pass).and_return('password')
expect(subject).to receive(:create_recovery_file!)
subject.execute
end
context 'when there is TTY available' do
before do
allow(STDIN).to receive(:tty?).and_return(true)
Loading
Loading
@@ -116,4 +125,19 @@ RSpec.describe Geo::Replication, '#execute' do
subject.execute
end
end
context 'when node has PostgreSQL 12 installed' do
it 'writes recovery settings to postgresql.conf and creates a standby file' do
allow(File).to receive(:write)
allow(STDIN).to receive(:gets).and_return("pass\n")
allow(subject).to receive(:ask_pass).and_return('password')
allow(subject).to receive(:postgresql_version).and_return(12)
expect(subject).to receive(:write_recovery_settings!)
expect(subject).to receive(:create_standby_file!)
expect(subject).not_to receive(:create_recovery_file!)
subject.execute
end
end
end
Loading
Loading
@@ -38,4 +38,43 @@ RSpec.describe GitlabCtl::PostgreSQL do
end
end
end
describe '#postgresql_group' do
before do
allow(GitlabCtl::Util).to receive(:get_node_attributes).and_return(
{
'postgresql' => {
'group' => 'foo'
}
}
)
end
it 'returns the correct group' do
expect(described_class.postgresql_group).to eq('foo')
end
end
describe '#postgresql_version' do
context 'when PG_VERSION file exists' do
before do
allow(File).to receive(:exist?).and_return(true)
allow(File).to receive(:read).and_return('12\n')
end
it 'returns the version' do
expect(described_class.postgresql_version('/var/opt/gitlab')).to eq(12)
end
end
context 'when PG_VERSION file exists' do
before do
allow(File).to receive(:exist?).and_return(false)
end
it 'returns nil' do
expect(described_class.postgresql_version('/var/opt/gitlab')).to be_nil
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment