Browse Source

Add mariadb database configuration support

1. Add 2 new recipes to configure mariadb client and server
2. Partially reuse existing code for mysql support
3. Add delete empty user and users with empty password workaround.
Current mysql cookbook can do this, but mariadb cookbook doesn't.

blueprint: mariadb-support
Change-Id: I034cedb07f8bf11f271fc351f9a7ad534067b95a
tags/kilo-eol
Yi Ming Yin 4 years ago
parent
commit
9a864936c4

+ 3
- 0
metadata.rb View File

@@ -9,6 +9,8 @@ recipe 'client', 'Installs client packages for the database used by the deployme
9 9
 recipe 'server', 'Installs and configures server packages for the database used by the deployment.'
10 10
 recipe 'mysql-client', 'Installs MySQL client packages.'
11 11
 recipe 'mysql-server', 'Installs and configures MySQL server packages.'
12
+recipe 'mariadb-client', 'Installs MariaDB client packages.'
13
+recipe 'mariadb-server', 'Installs and configures MariaDB server packages.'
12 14
 recipe 'postgresql-client', 'Installs PostgreSQL client packages.'
13 15
 recipe 'postgresql-server', 'Installs and configures PostgreSQL server packages.'
14 16
 recipe 'openstack-db', 'Creates necessary tables, users, and grants for OpenStack.'
@@ -17,6 +19,7 @@ recipe 'openstack-db', 'Creates necessary tables, users, and grants for OpenStac
17 19
   supports os
18 20
 end
19 21
 
22
+depends 'mariadb', '~> 0.3.0'
20 23
 depends 'mysql', '~> 6.0.13'
21 24
 depends 'mysql2_chef_gem', '~> 1.0.1'
22 25
 depends 'postgresql', '~> 3.4.18'

+ 29
- 0
recipes/mariadb-client.rb View File

@@ -0,0 +1,29 @@
1
+# encoding: UTF-8
2
+#
3
+# Cookbook Name:: openstack-ops-database
4
+# Recipe:: mariadb-client
5
+#
6
+# Licensed under the Apache License, Version 2.0 (the "License");
7
+# you may not use this file except in compliance with the License.
8
+# You may obtain a copy of the License at
9
+#
10
+#     http://www.apache.org/licenses/LICENSE-2.0
11
+#
12
+# Unless required by applicable law or agreed to in writing, software
13
+# distributed under the License is distributed on an "AS IS" BASIS,
14
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+# See the License for the specific language governing permissions and
16
+# limitations under the License.
17
+#
18
+
19
+node.override['mariadb']['install']['prefer_os_package'] = true
20
+include_recipe 'mariadb::client'
21
+
22
+mysql2_chef_gem 'default' do
23
+  provider Chef::Provider::Mysql2ChefGem::Mariadb
24
+  action :install
25
+end
26
+
27
+node['openstack']['db']['python_packages']['mariadb'].each do |pkg|
28
+  package pkg
29
+end

+ 75
- 0
recipes/mariadb-server.rb View File

@@ -0,0 +1,75 @@
1
+# encoding: UTF-8
2
+#
3
+# Cookbook Name:: openstack-ops-database
4
+# Recipe:: mariadb-server
5
+#
6
+# Licensed under the Apache License, Version 2.0 (the "License");
7
+# you may not use this file except in compliance with the License.
8
+# You may obtain a copy of the License at
9
+#
10
+#     http://www.apache.org/licenses/LICENSE-2.0
11
+#
12
+# Unless required by applicable law or agreed to in writing, software
13
+# distributed under the License is distributed on an "AS IS" BASIS,
14
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+# See the License for the specific language governing permissions and
16
+# limitations under the License.
17
+#
18
+
19
+class ::Chef::Recipe # rubocop:disable Documentation
20
+  include ::Openstack
21
+end
22
+
23
+db_endpoint = endpoint 'db'
24
+super_password = get_password 'user', node['openstack']['db']['root_user_key']
25
+
26
+node.override['mariadb']['allow_root_pass_change'] = true
27
+node.override['mariadb']['server_root_password'] = super_password
28
+node.override['mariadb']['mysqld']['bind_address'] = db_endpoint.host
29
+node.override['mariadb']['install']['prefer_os_package'] = true
30
+
31
+unless db_endpoint.host == '127.0.0.1' || db_endpoint.host == 'localhost'
32
+  node.override['mariadb']['forbid_remote_root'] = false
33
+end
34
+
35
+include_recipe 'openstack-ops-database::mariadb-client'
36
+
37
+# reuse mysql configuration for mariadb
38
+node.override['mariadb']['mysqld']['default_storage_engine'] = node['openstack']['mysql']['default-storage-engine']
39
+node.override['mariadb']['mysqld']['max_connections'] = node['openstack']['mysql']['max_connections']
40
+include_recipe 'mariadb::server'
41
+
42
+# reuse mysql configuration file for mariadb
43
+template "#{node['mariadb']['configuration']['includedir']}/openstack.cnf" do
44
+  owner 'mysql'
45
+  group 'mysql'
46
+  source 'openstack.cnf.erb'
47
+  notifies :restart, 'service[mysql]'
48
+end
49
+
50
+# Current mariadb cookbook does not handle deleting anonymous users and default
51
+# users. We need to delete them here.
52
+mysql_connection_info = {
53
+  host: 'localhost',
54
+  username: 'root',
55
+  password: super_password
56
+}
57
+
58
+mysql_database 'drop empty and default users' do
59
+  database_name 'mysql'
60
+  sql "DELETE FROM mysql.user WHERE User = '' OR Password = ''"
61
+  connection mysql_connection_info
62
+  action :query
63
+end
64
+
65
+mysql_database 'test' do
66
+  connection mysql_connection_info
67
+  action :drop
68
+end
69
+
70
+mysql_database 'flush priviledges after cleanup' do
71
+  database_name 'mysql'
72
+  sql 'FLUSH PRIVILEGES'
73
+  connection mysql_connection_info
74
+  action :query
75
+end

+ 18
- 0
spec/mariadb-client-redhat_spec.rb View File

@@ -0,0 +1,18 @@
1
+# encoding: UTF-8
2
+
3
+require_relative 'spec_helper'
4
+
5
+describe 'openstack-ops-database::mariadb-client' do
6
+  describe 'suse' do
7
+    let(:runner) { ChefSpec::Runner.new(REDHAT_OPTS) }
8
+    let(:node) do
9
+      runner.node.set['openstack']['db']['service_type'] = 'mariadb'
10
+      runner.node
11
+    end
12
+    let(:chef_run) { runner.converge(described_recipe) }
13
+
14
+    it 'installs mariadb python client packages' do
15
+      expect(chef_run).to install_package('MySQL-python')
16
+    end
17
+  end
18
+end

+ 18
- 0
spec/mariadb-client-suse_spec.rb View File

@@ -0,0 +1,18 @@
1
+# encoding: UTF-8
2
+
3
+require_relative 'spec_helper'
4
+
5
+describe 'openstack-ops-database::mariadb-client' do
6
+  describe 'suse' do
7
+    let(:runner) { ChefSpec::Runner.new(SUSE_OPTS) }
8
+    let(:node) do
9
+      runner.node.set['openstack']['db']['service_type'] = 'mariadb'
10
+      runner.node
11
+    end
12
+    let(:chef_run) { runner.converge(described_recipe) }
13
+
14
+    it 'installs mariadb python client packages' do
15
+      expect(chef_run).to install_package('python-mysql')
16
+    end
17
+  end
18
+end

+ 32
- 0
spec/mariadb-client_spec.rb View File

@@ -0,0 +1,32 @@
1
+# encoding: UTF-8
2
+
3
+require_relative 'spec_helper'
4
+
5
+describe 'openstack-ops-database::mariadb-client' do
6
+  include_context 'database-stubs'
7
+  describe 'ubuntu' do
8
+    let(:runner) { ChefSpec::Runner.new(UBUNTU_OPTS) }
9
+    let(:node) do
10
+      runner.node.set['openstack']['db']['service_type'] = 'mariadb'
11
+      runner.node
12
+    end
13
+    let(:chef_run) { runner.converge(described_recipe) }
14
+
15
+    it 'includes mariadb client recipes' do
16
+      expect(chef_run).to include_recipe('mariadb::client')
17
+    end
18
+
19
+    it 'install mysql2 gem package' do
20
+      expect(chef_run).to install_mysql2_chef_gem('default')
21
+        .with(provider: Chef::Provider::Mysql2ChefGem::Mariadb)
22
+    end
23
+
24
+    it 'installs mariadb python client packages' do
25
+      expect(chef_run).to install_package('python-mysqldb')
26
+    end
27
+
28
+    it 'override prefer os package' do
29
+      expect(chef_run.node['mariadb']['install']['prefer_os_package']).to be true
30
+    end
31
+  end
32
+end

+ 32
- 0
spec/mariadb-server-redhat_spec.rb View File

@@ -0,0 +1,32 @@
1
+# encoding: UTF-8
2
+
3
+require_relative 'spec_helper'
4
+
5
+describe 'openstack-ops-database::mariadb-server' do
6
+  describe 'redhat' do
7
+    include_context 'database-stubs'
8
+    let(:runner) { ChefSpec::Runner.new(REDHAT_OPTS) }
9
+    let(:node) { runner.node }
10
+    let(:chef_run) { runner.converge(described_recipe) }
11
+    let(:file) { chef_run.template('/etc/my.cnf.d/openstack.cnf') }
12
+
13
+    it 'creates template /etc/my.cnf.d/openstack.cnf' do
14
+      node.set['mariadb']['install']['version'] = '5.5'
15
+      expect(chef_run).to create_template(file.name).with(
16
+        user: 'mysql',
17
+        group: 'mysql',
18
+        source: 'openstack.cnf.erb'
19
+      )
20
+      expect(file).to notify('service[mysql]')
21
+      [/^default-storage-engine = InnoDB$/,
22
+       /^innodb_thread_concurrency = 0$/,
23
+       /^innodb_commit_concurrency = 0$/,
24
+       /^innodb_flush_log_at_trx_commit = 2$/,
25
+       /^skip-name-resolve$/,
26
+       /^character-set-server = utf8$/].each do |line|
27
+        expect(chef_run).to render_config_file(file.name)\
28
+          .with_section_content('mysqld', line)
29
+      end
30
+    end
31
+  end
32
+end

+ 71
- 0
spec/mariadb-server_spec.rb View File

@@ -0,0 +1,71 @@
1
+# encoding: UTF-8
2
+
3
+require_relative 'spec_helper'
4
+
5
+describe 'openstack-ops-database::mariadb-server' do
6
+  describe 'ubuntu' do
7
+    include_context 'database-stubs'
8
+    let(:runner) { ChefSpec::Runner.new(UBUNTU_OPTS) }
9
+    let(:node) { runner.node }
10
+    let(:chef_run) { runner.converge(described_recipe) }
11
+    let(:file) { chef_run.template('/etc/mysql/conf.d/openstack.cnf') }
12
+
13
+    it 'overrides mariadb default attributes' do
14
+      expect(chef_run.node['mariadb']['mysqld']['bind_address']).to eq '127.0.0.1'
15
+      expect(chef_run.node['mariadb']['mysqld']['default_storage_engine']).to eq 'InnoDB'
16
+      expect(chef_run.node['mariadb']['mysqld']['max_connections']).to eq '1024'
17
+      expect(chef_run.node['mariadb']['forbid_remote_root']).to be true
18
+    end
19
+
20
+    it 'includes mariadb recipes' do
21
+      expect(chef_run).to include_recipe('openstack-ops-database::mariadb-client')
22
+      expect(chef_run).to include_recipe('mariadb::server')
23
+    end
24
+
25
+    it 'creates template /etc/mysql/conf.d/openstack.cnf' do
26
+      node.set['mariadb']['install']['version'] = '5.5'
27
+      expect(chef_run).to create_template(file.name).with(
28
+        user: 'mysql',
29
+        group: 'mysql',
30
+        source: 'openstack.cnf.erb'
31
+      )
32
+      expect(file).to notify('service[mysql]')
33
+      [/^default-storage-engine = InnoDB$/,
34
+       /^innodb_thread_concurrency = 0$/,
35
+       /^innodb_commit_concurrency = 0$/,
36
+       /^innodb_read_io_threads = 4$/,
37
+       /^innodb_flush_log_at_trx_commit = 2$/,
38
+       /^skip-name-resolve$/,
39
+       /^character-set-server = utf8$/,
40
+       /^max_connections = 1024$/].each do |line|
41
+        expect(chef_run).to render_config_file(file.name)\
42
+          .with_section_content('mysqld', line)
43
+      end
44
+    end
45
+
46
+    it 'creates mariadb with root password' do
47
+      # Password is fixed as 'abc123' by spec_helper
48
+      expect(chef_run.node['mariadb']['allow_root_pass_change']).to be true
49
+      expect(chef_run.node['mariadb']['server_root_password']).to eq 'abc123'
50
+    end
51
+
52
+    it 'override prefer os package' do
53
+      expect(chef_run.node['mariadb']['install']['prefer_os_package']).to be true
54
+    end
55
+
56
+    it 'allow root remote access' do
57
+      node.set['openstack']['endpoints']['db']['host'] = '192.168.1.1'
58
+      expect(chef_run.node['mariadb']['forbid_remote_root']).to be false
59
+    end
60
+
61
+    it 'drop anonymous and empty users' do
62
+      expect(chef_run).to query_mysql_database('drop empty and default users')\
63
+        .with(database_name: 'mysql',
64
+              sql: "DELETE FROM mysql.user WHERE User = '' OR Password = ''")
65
+      expect(chef_run).to drop_mysql_database('test')
66
+      expect(chef_run).to query_mysql_database('flush priviledges after cleanup')\
67
+        .with(database_name: 'mysql',
68
+              sql: 'FLUSH PRIVILEGES')
69
+    end
70
+  end
71
+end

+ 1
- 1
templates/default/openstack.cnf.erb View File

@@ -1,7 +1,7 @@
1 1
 <%= node['openstack']['db']['custom_template_banner'] %>
2 2
 [mysqld]
3 3
 default-storage-engine = <%= node['openstack']['mysql']['default-storage-engine'] %>
4
-innodb_thread_concurrency= <%= node['openstack']['mysql']['innodb_thread_concurrency'] %>
4
+innodb_thread_concurrency = <%= node['openstack']['mysql']['innodb_thread_concurrency'] %>
5 5
 innodb_commit_concurrency = <%= node['openstack']['mysql']['innodb_commit_concurrency'] %>
6 6
 innodb_read_io_threads = <%= node['openstack']['mysql']['innodb_read_io_threads'] %>
7 7
 innodb_flush_log_at_trx_commit = <%= node['openstack']['mysql']['innodb_flush_log_at_trx_commit'] %>

Loading…
Cancel
Save