Skip to content
Snippets Groups Projects
Commit 1e0599e327a7 authored by Balasankar 'Balu' C's avatar Balasankar 'Balu' C
Browse files

Merge branch '6757-allow-pgbouncer-to-handle-multiple-databases' into 'master'

Enable multi-Patroni services support in PGBouncer

Closes #6757

See merge request gitlab-org/omnibus-gitlab!6014
No related branches found
No related tags found
2 merge requests!71heptapod#685: making 0.32 the new stable,!69GitLab 14.10 / Heptapod 0.32
......@@ -9,7 +9,9 @@
end
module Pgbouncer
DEFAULT_RAILS_DATABASE = 'gitlabhq_production'.freeze
class Databases
attr_accessor :install_path, :databases, :options, :ini_file, :json_file, :template_file, :attributes, :userinfo, :groupinfo, :database
attr_reader :data_path
......@@ -12,7 +14,17 @@
class Databases
attr_accessor :install_path, :databases, :options, :ini_file, :json_file, :template_file, :attributes, :userinfo, :groupinfo, :database
attr_reader :data_path
def self.rails_database(attributes: nil)
attributes = GitlabCtl::Util.get_public_node_attributes if attributes.nil?
if attributes.key?('gitlab')
attributes['gitlab']['gitlab-rails']['db_database']
else
DEFAULT_RAILS_DATABASE
end
end
def initialize(options, install_path, base_data_path)
self.data_path = base_data_path
@attributes = GitlabCtl::Util.get_public_node_attributes
......@@ -21,13 +33,9 @@
@ini_file = options['databases_ini'] || database_ini
@json_file = options['databases_json'] || database_json
@template_file = "#{@install_path}/embedded/cookbooks/gitlab-ee/templates/default/databases.ini.erb"
@database = if attributes.key?('gitlab')
attributes['gitlab']['gitlab-rails']['db_database']
else
'gitlabhq_production'
end
@database = options['database'] || Databases.rails_database(attributes: @attributes)
@databases = update_databases(JSON.parse(File.read(@json_file))) if File.exist?(@json_file)
@userinfo = GitlabCtl::Util.userinfo(options['host_user']) if options['host_user']
@groupinfo = GitlabCtl::Util.groupinfo(options['host_group']) if options['host_group']
end
......@@ -29,11 +37,13 @@
@databases = update_databases(JSON.parse(File.read(@json_file))) if File.exist?(@json_file)
@userinfo = GitlabCtl::Util.userinfo(options['host_user']) if options['host_user']
@groupinfo = GitlabCtl::Util.groupinfo(options['host_group']) if options['host_group']
end
def update_databases(original)
def update_databases(original = {})
updated = {}
if original.empty?
original = {
database => {}
}
end
......@@ -35,8 +45,8 @@
if original.empty?
original = {
database => {}
}
end
updated = Chef::Mixin::DeepMerge.merge(updated, original)
original.each do |db, settings|
settings.delete('password')
......@@ -41,3 +51,4 @@
original.each do |db, settings|
settings.delete('password')
updated[db] = ''
......@@ -43,4 +54,3 @@
updated[db] = ''
settings['host'] = options['newhost'] if options['newhost']
settings['port'] = options['port'] if options['port']
settings['auth_user'] = settings.delete('user') if settings.key?('user')
......@@ -46,6 +56,12 @@
settings['auth_user'] = settings.delete('user') if settings.key?('user')
settings['auth_user'] = options['user'] if options['user']
settings['dbname'] = options['pg_database'] if options['pg_database']
if db == @database
settings['host'] = options['newhost'] if options['newhost']
settings['port'] = options['port'] if options['port']
settings['auth_user'] = options['user'] if options['user']
settings['dbname'] = options['pg_database'] if options['pg_database']
end
settings.each do |setting, value|
updated[db] << " #{setting}=#{value}"
end
......@@ -49,5 +65,6 @@
settings.each do |setting, value|
updated[db] << " #{setting}=#{value}"
end
updated[db].strip!
end
......@@ -52,5 +69,6 @@
updated[db].strip!
end
updated
end
......
......@@ -59,7 +59,7 @@
pgpass = GitlabCtl::PostgreSQL::Pgpass.new(
hostname: options['host'],
port: options['port'],
database: options['database'],
database: options['database'] || Pgbouncer::Databases.rails_database,
username: options['user'],
password: password,
host_user: options['host_user'],
......@@ -70,7 +70,7 @@
def get_pg_options
options = {
'database' => 'gitlabhq_production',
'database' => nil,
'databases_ini' => nil,
'databases_json' => nil,
'host' => nil,
......
......@@ -66,7 +66,8 @@
'databases_ini' => '/another/databases.ini',
'databases_json' => '/another/databases.json',
'port' => 8888,
'user' => 'fakeuser'
'user' => 'fakeuser',
'database' => 'fakedb'
}
allow(File).to receive(:exist?).with('/another/databases.json').and_return(true)
allow(File).to receive(:read).with('/another/databases.json').and_return(fake_databases_json)
......@@ -91,7 +92,7 @@
@obj = Pgbouncer::Databases.new({}, '/fakeinstall', '/fakedata')
end
it 'should generate an databases.ini with sane defaults' do
it 'should generate an databases.ini with the default rails database' do
expect(@obj.render).to eq(
"[databases]\n\nfake_database = \n\n"
)
......
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