diff --git a/files/gitlab-ctl-commands-ee/lib/pgbouncer.rb b/files/gitlab-ctl-commands-ee/lib/pgbouncer.rb index b434898a3abb13d5f525e3d59460d20d6ac39459_ZmlsZXMvZ2l0bGFiLWN0bC1jb21tYW5kcy1lZS9saWIvcGdib3VuY2VyLnJi..1e0599e327a7de63e7dc3c7ded63f6e0ec93779e_ZmlsZXMvZ2l0bGFiLWN0bC1jb21tYW5kcy1lZS9saWIvcGdib3VuY2VyLnJi 100644 --- a/files/gitlab-ctl-commands-ee/lib/pgbouncer.rb +++ b/files/gitlab-ctl-commands-ee/lib/pgbouncer.rb @@ -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 diff --git a/files/gitlab-ctl-commands-ee/pgbouncer.rb b/files/gitlab-ctl-commands-ee/pgbouncer.rb index b434898a3abb13d5f525e3d59460d20d6ac39459_ZmlsZXMvZ2l0bGFiLWN0bC1jb21tYW5kcy1lZS9wZ2JvdW5jZXIucmI=..1e0599e327a7de63e7dc3c7ded63f6e0ec93779e_ZmlsZXMvZ2l0bGFiLWN0bC1jb21tYW5kcy1lZS9wZ2JvdW5jZXIucmI= 100644 --- a/files/gitlab-ctl-commands-ee/pgbouncer.rb +++ b/files/gitlab-ctl-commands-ee/pgbouncer.rb @@ -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, diff --git a/spec/chef/gitlab-ctl-commands-ee/lib/pgbouncer_spec.rb b/spec/chef/gitlab-ctl-commands-ee/lib/pgbouncer_spec.rb index b434898a3abb13d5f525e3d59460d20d6ac39459_c3BlYy9jaGVmL2dpdGxhYi1jdGwtY29tbWFuZHMtZWUvbGliL3BnYm91bmNlcl9zcGVjLnJi..1e0599e327a7de63e7dc3c7ded63f6e0ec93779e_c3BlYy9jaGVmL2dpdGxhYi1jdGwtY29tbWFuZHMtZWUvbGliL3BnYm91bmNlcl9zcGVjLnJi 100644 --- a/spec/chef/gitlab-ctl-commands-ee/lib/pgbouncer_spec.rb +++ b/spec/chef/gitlab-ctl-commands-ee/lib/pgbouncer_spec.rb @@ -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" )