Skip to content

Commit

Permalink
Fixes #163: Ensure configuration reload and server start after config…
Browse files Browse the repository at this point in the history
…uration

Does not apply to galera.
Also fixes restorecon path
  • Loading branch information
Fred Boismenu committed Aug 8, 2017
1 parent 18486ff commit f8bbde8
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 91 deletions.
3 changes: 3 additions & 0 deletions .kitchen.yml
Expand Up @@ -12,6 +12,9 @@ platforms:
- name: centos-6.8
- name: centos-7.3
- name: debian-7.11
provisioner:
name: chef_zero
require_chef_omnibus: 12
- name: debian-8.7
- name: ubuntu-14.04
- name: ubuntu-16.04
Expand Down
18 changes: 18 additions & 0 deletions recipes/_debian_server_postinstall.rb
@@ -0,0 +1,18 @@
#
# Cookbook Name:: mariadb
# Recipe:: _debian_server_postinstall
#
# Copyright 2014, blablacar.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
@@ -1,6 +1,6 @@
#
# Cookbook Name:: mariadb
# Recipe:: _debian_server
# Recipe:: _debian_server_preinstall
#
# Copyright 2014, blablacar.com
#
Expand Down
58 changes: 58 additions & 0 deletions recipes/_mariadb_postinstall.rb
@@ -0,0 +1,58 @@
#
# Cookbook Name:: mariadb
# Recipe:: _server_postinstall
#
# Copyright 2014, blablacar.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

if node['mariadb']['allow_root_pass_change']
# Used to change root password after first install
# Still experimental
md5 = if node['mariadb']['server_root_password'].empty?
Digest::MD5.hexdigest('empty')
else
Digest::MD5.hexdigest(node['mariadb']['server_root_password'])
end

file '/etc/mysql_root_change' do
content md5
action :create
notifies :run, 'execute[install-grants]', :immediately
end
end

if node['mariadb']['allow_root_pass_change'] ||
node['mariadb']['remove_anonymous_users'] ||
node['mariadb']['forbid_remote_root'] ||
node['mariadb']['remove_test_database']

execute 'install-grants' do
command '/bin/bash -e /etc/mariadb_grants \'' + \
node['mariadb']['server_root_password'] + '\''
only_if { File.exist?('/etc/mariadb_grants') }
sensitive true
action :nothing
end

template '/etc/mariadb_grants' do
sensitive true
source 'mariadb_grants.erb'
owner 'root'
group 'root'
mode '0600'
helpers MariaDB::Helper
notifies :run, 'execute[install-grants]', :immediately
end
end
2 changes: 1 addition & 1 deletion recipes/_mariadb_repository.rb
@@ -1,6 +1,6 @@
#
# Cookbook Name:: mariadb
# Recipe:: _redhat_repository
# Recipe:: _mariadb_repository
#
# Copyright 2014, blablacar.com
#
Expand Down
27 changes: 27 additions & 0 deletions recipes/_redhat_server_postinstall.rb
@@ -0,0 +1,27 @@
#
# Cookbook Name:: mariadb
# Recipe:: _redhat_server_postinstall
#
# Copyright 2014, blablacar.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


execute 'change first install root password' do
command '/usr/bin/mysqladmin -u root password \'' + \
node['mariadb']['server_root_password'] + '\''
sensitive true
not_if { node['mariadb']['server_root_password'].empty? }
end

@@ -1,6 +1,6 @@
#
# Cookbook Name:: mariadb
# Recipe:: _redhat_server
# Recipe:: _redhat_server_preinstall
#
# Copyright 2014, blablacar.com
#
Expand Down Expand Up @@ -30,29 +30,3 @@
action :install
not_if { use_os_native || use_scl }
end

ruby_block 'MariaDB first start' do
block do
true
end
action :nothing
subscribes :run, 'package[MariaDB-server]', :immediately
notifies :create, 'directory[/var/log/mysql]', :immediately
notifies :start, 'service[mysql]', :immediately
notifies :run, 'execute[change first install root password]', :immediately
end

directory '/var/log/mysql' do
action :nothing
user 'mysql'
group 'mysql'
mode '0755'
end

execute 'change first install root password' do
command '/usr/bin/mysqladmin -u root password \'' + \
node['mariadb']['server_root_password'] + '\''
action :nothing
sensitive true
not_if { node['mariadb']['server_root_password'].empty? }
end
12 changes: 12 additions & 0 deletions recipes/config.rb
Expand Up @@ -17,6 +17,15 @@
# limitations under the License.
#


begin
resources('ruby_block[restart_mysql]')
no_mysql_restart_rc = false
rescue Chef::Exceptions::ResourceNotFound
no_mysql_restart_rc = true
# see Galera SST issue, justifying why we tolerate this.
end

template '/root/.my.cnf' do
source 'root.cnf.erb'
owner 'root'
Expand All @@ -30,6 +39,7 @@
owner 'root'
group 'root'
mode '0644'
notifies :create, 'ruby_block[restart_mysql]', :immediately unless no_mysql_restart_rc
end

directory '/etc/my.cnf.d/' do
Expand Down Expand Up @@ -85,6 +95,7 @@
section 'mysqld'
option innodb_options
action :add
notifies :create, 'ruby_block[restart_mysql]', :immediately unless no_mysql_restart_rc
end

replication_opts = {}
Expand Down Expand Up @@ -112,4 +123,5 @@
section 'mysqld'
option replication_opts
action :add
notifies :create, 'ruby_block[restart_mysql]', :immediately unless no_mysql_restart_rc
end
98 changes: 54 additions & 44 deletions recipes/server.rb
Expand Up @@ -22,6 +22,9 @@
extend Chef::Util::Selinux
selinux_enabled = selinux_enabled?


# --[ INSTALL ]------------------------

case node['mariadb']['install']['type']
when 'package'
# Determine service name and register the resource
Expand All @@ -31,21 +34,18 @@
node['mariadb']['install']['prefer_os_package'],
node['mariadb']['install']['prefer_scl_package'])
node.default['mariadb']['mysqld']['service_name'] = service_name unless service_name.nil?
service 'mysql' do
service_name node['mariadb']['mysqld']['service_name']
supports restart: true
action :nothing
end

# Include recipe to install required repositories
include_recipe "#{cookbook_name}::_mariadb_repository"

# Include platform specific recipes
case node['platform']
when 'debian', 'ubuntu'
include_recipe "#{cookbook_name}::_debian_server"
include_recipe "#{cookbook_name}::_debian_server_preinstall"
platform_post_install_recipe = "#{cookbook_name}::_debian_server_postinstall"
when 'redhat', 'centos', 'scientific', 'amazon'
include_recipe "#{cookbook_name}::_redhat_server"
include_recipe "#{cookbook_name}::_redhat_server_preinstall"
platform_post_install_recipe = "#{cookbook_name}::_redhat_server_postinstall"
end

# Install server package
Expand All @@ -57,12 +57,22 @@
package 'MariaDB-server' do
package_name server_package_name
action :install
notifies :enable, 'service[mysql]'
notifies :create, 'ruby_block[restart_mysql]', :immediately
end

end


# --[ CONFIGURE ]------------------------

include_recipe "#{cookbook_name}::config"

directory '/var/log/mysql' do
owner 'mysql'
group 'mysql'
mode '0755'
end

# move the datadir if needed
if node['mariadb']['mysqld']['datadir'] !=
node['mariadb']['mysqld']['default_datadir']
Expand All @@ -81,7 +91,7 @@

bash 'Restore security context' do
user 'root'
code "/usr/sbin/restorecon -v #{node['mariadb']['mysqld']['default_datadir']}"
code "/sbin/restorecon -v #{node['mariadb']['mysqld']['default_datadir']}"
only_if { selinux_enabled }
subscribes :run, 'bash[move-datadir]', :immediately
action :nothing
Expand All @@ -94,11 +104,14 @@
action :create
notifies :stop, 'service[mysql]', :immediately
notifies :run, 'bash[move-datadir]', :immediately
notifies :start, 'service[mysql]', :immediately
notifies :create, 'ruby_block[restart_mysql]', :immediately
only_if { !File.symlink?(node['mariadb']['mysqld']['default_datadir']) }
end
end


# --[ START SERVICE ]------------------------

# restart the service if needed
# workaround idea from https://github.com/stissot
Chef::Resource::Execute.send(:include, MariaDB::Helper)
Expand All @@ -111,47 +124,44 @@
node['mariadb']['mysqld']['socket']
)
end
notifies :restart, 'service[mysql]', :immediately
notifies :create, 'ruby_block[restart_mysql]', :immediately
end

if node['mariadb']['allow_root_pass_change']
# Used to change root password after first install
# Still experimental
md5 = if node['mariadb']['server_root_password'].empty?
Digest::MD5.hexdigest('empty')
else
Digest::MD5.hexdigest(node['mariadb']['server_root_password'])
end

file '/etc/mysql_root_change' do
content md5
action :create
notifies :run, 'execute[install-grants]', :immediately
end

# Service definition allowing timely restart

service 'mysql' do
service_name node['mariadb']['mysqld']['service_name']
supports :restart => true, :reload => true
action [:start, :enable]
end

if node['mariadb']['allow_root_pass_change'] ||
node['mariadb']['remove_anonymous_users'] ||
node['mariadb']['forbid_remote_root'] ||
node['mariadb']['remove_test_database']
execute 'install-grants' do
command '/bin/bash -e /etc/mariadb_grants \'' + \
node['mariadb']['server_root_password'] + '\''
only_if { File.exist?('/etc/mariadb_grants') }
sensitive true
action :nothing
end
template '/etc/mariadb_grants' do
sensitive true
source 'mariadb_grants.erb'
owner 'root'
group 'root'
mode '0600'
helpers MariaDB::Helper
notifies :run, 'execute[install-grants]', :immediately

ruby_block "restart_mysql" do
block do

r = resources(:service => "mysql")
a = Array.new(r.action)

a << :restart unless a.include?(:restart)
a.delete(:start) if a.include?(:restart)

r.action(a)

end
action :nothing
end

# --[ POST INSTALL ]------------------------

case node['mariadb']['install']['type']
when 'package'
include_recipe platform_post_install_recipe
end

include_recipe "#{cookbook_name}::_mariadb_postinstall"


# MariaDB Plugins
include_recipe "#{cookbook_name}::plugins" if \
node['mariadb']['plugins_options']['auto_install']

0 comments on commit f8bbde8

Please sign in to comment.