From 375cdcde148a827e84b7e8db4bda4a06337834ce Mon Sep 17 00:00:00 2001 From: Min Min Ren Date: Thu, 9 Jul 2015 02:04:39 -0500 Subject: [PATCH] Add database slave connection support - Add new attributes for database slave connection - Change db_uri method to generate slave connection db_uri Implements: blueprint sql-slave-connection-support Change-Id: I9996e4944e58264b5195d625c98adaf28a04acd7 --- attributes/database.rb | 6 ++++++ libraries/endpoints.rb | 12 +++++++++--- spec/endpoints_spec.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/attributes/database.rb b/attributes/database.rb index df94a1ca..2454f7c9 100644 --- a/attributes/database.rb +++ b/attributes/database.rb @@ -76,6 +76,9 @@ default['openstack']['endpoints']['db']['scheme'] = nil default['openstack']['endpoints']['db']['port'] = '3306' default['openstack']['endpoints']['db']['path'] = nil default['openstack']['endpoints']['db']['bind_interface'] = nil +default['openstack']['endpoints']['db']['enabled_slave'] = false +default['openstack']['endpoints']['db']['slave_host'] = '127.0.0.1' +default['openstack']['endpoints']['db']['slave_port'] = '3316' # Default database attributes default['openstack']['db']['server_role'] = 'os-ops-database' @@ -141,6 +144,9 @@ node['openstack']['common']['services'].each do |service, project| default['openstack']['db'][service]['username'] = project default['openstack']['db'][service]['options'] = node['openstack']['db']['options'] + default['openstack']['db'][service]['slave_host'] = node['openstack']['endpoints']['db']['slave_host'] + default['openstack']['db'][service]['slave_port'] = node['openstack']['endpoints']['db']['slave_port'] + case service when 'dashboard' default['openstack']['db'][service]['migrate'] = true diff --git a/libraries/endpoints.rb b/libraries/endpoints.rb index e47bc37d..db228fd8 100644 --- a/libraries/endpoints.rb +++ b/libraries/endpoints.rb @@ -52,11 +52,17 @@ module ::Openstack end # Shortcut to get the SQLAlchemy DB URI for a named service - def db_uri(service, user, pass) # rubocop:disable MethodLength, CyclomaticComplexity + def db_uri(service, user, pass, is_slave = false) # rubocop:disable MethodLength, CyclomaticComplexity info = db(service) return unless info - host = info['host'] - port = info['port'].to_s + + if is_slave + host = info['slave_host'] + port = info['slave_port'].to_s + else + host = info['host'] + port = info['port'].to_s + end type = info['service_type'] name = info['db_name'] options = info['options'][type] diff --git a/spec/endpoints_spec.rb b/spec/endpoints_spec.rb index f6f972b0..94566a71 100644 --- a/spec/endpoints_spec.rb +++ b/spec/endpoints_spec.rb @@ -403,6 +403,48 @@ describe 'openstack-common::set_endpoints_by_interface' do ).to eq(expected) end end + + it 'returns compute slave db info hash when service found for default mysql' do + node.set['openstack']['endpoints']['db']['enabled_slave'] = true + allow(subject).to receive(:node).and_return(chef_run.node) + expected = 'mysql://user:pass@127.0.0.1:3316/nova?charset=utf8' + expect( + subject.db_uri('compute', 'user', 'pass', true) + ).to eq(expected) + end + + it 'returns block-storage slave db info hash when service found for db2 with options' do + node.set['openstack']['endpoints']['db']['enabled_slave'] = true + node.set['openstack']['db']['service_type'] = 'db2' + node.set['openstack']['db']['options'] = { 'db2' => '?options' } + allow(subject).to receive(:node).and_return(chef_run.node) + expected = 'ibm_db_sa://user:pass@127.0.0.1:3316/cinder?options' + expect( + subject.db_uri('block-storage', 'user', 'pass', true) + ).to eq(expected) + end + + it 'returns image slave db info hash when service found for mariadb' do + node.set['openstack']['db']['service_type'] = 'mariadb' + node.set['openstack']['endpoints']['db']['enabled_slave'] = true + allow(subject).to receive(:node).and_return(chef_run.node) + expected = 'mysql://user:pass@127.0.0.1:3316/glance?charset=utf8' + expect( + subject.db_uri('image', 'user', 'pass', true) + ).to eq(expected) + end + + %w(galera percona-cluster).each do |db| + it "returns network slave db info hash when service found for #{db}" do + node.set['openstack']['db']['service_type'] = db + node.set['openstack']['endpoints']['db']['enabled_slave'] = true + allow(subject).to receive(:node).and_return(chef_run.node) + expected = 'mysql://user:pass@127.0.0.1:3316/neutron?charset=utf8' + expect( + subject.db_uri('network', 'user', 'pass', true) + ).to eq(expected) + end + end end describe '#address' do