Skip to content

Commit

Permalink
Merge pull request #26 from Ecodev/move_datadir
Browse files Browse the repository at this point in the history
[Feature] Move datadir after install (issue #25)
  • Loading branch information
sinfomicien committed Oct 24, 2014
2 parents 893d69e + ed6a83d commit 1a00d77
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 5 deletions.
12 changes: 12 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ platforms:
box: chef/debian-7.6
- name: ubuntu-12.04
- name: centos-6.4
- name: ubuntu-14.04
driver:
box: ffuenf-ubuntu-14.04-server-amd64
box_url: https://vagrantcloud.com/ffuenf/ubuntu-14.04-server-amd64/version/7/provider/virtualbox.box

suites:
- name: default
Expand All @@ -31,6 +35,14 @@ suites:
attributes:
mariadb:
use_default_repository: true
- name: datadir_changed
run_list:
- recipe[mariadb::default]
attributes:
mariadb:
use_default_repository: true
mysqld:
datadir: /home/mysql
- name: port_changed
run_list:
- recipe[mariadb::default]
Expand Down
2 changes: 2 additions & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
default['mariadb']['mysqld']['user'] = 'mysql'
default['mariadb']['mysqld']['port'] = '3306'
default['mariadb']['mysqld']['basedir'] = '/usr'
default['mariadb']['mysqld']['default_datadir'] = '/var/lib/mysql'
# if different from previous value, datadir will be moved after install
default['mariadb']['mysqld']['datadir'] = '/var/lib/mysql'
default['mariadb']['mysqld']['tmpdir'] = '/var/tmp'
default['mariadb']['mysqld']['lc_messages_dir'] = '/usr/share/mysql'
Expand Down
32 changes: 32 additions & 0 deletions recipes/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,38 @@

include_recipe "#{cookbook_name}::config"

# move the datadir if needed
if node['mariadb']['mysqld']['datadir'] !=
node['mariadb']['mysqld']['default_datadir']

service 'mysql' do
action :nothing
end

bash 'move-datadir' do
user 'root'
code <<-EOH
/bin/cp -a #{node['mariadb']['mysqld']['default_datadir']}/* \
#{node['mariadb']['mysqld']['datadir']} &&
/bin/rm -r #{node['mariadb']['mysqld']['default_datadir']} &&
/bin/ln -s #{node['mariadb']['mysqld']['datadir']} \
#{node['mariadb']['mysqld']['default_datadir']}
EOH
action :nothing
end

directory node['mariadb']['mysqld']['datadir'] do
owner 'mysql'
group 'mysql'
mode 00750
action :create
notifies :stop, 'service[mysql]', :immediately
notifies :run, 'bash[move-datadir]', :immediately
notifies :start, 'service[mysql]', :immediately
only_if { !File.symlink?(node['mariadb']['mysqld']['default_datadir']) }
end
end

# restart the service if needed
# workaround idea from https://github.com/stissot
Chef::Resource::Service.send(:include, MariaDB::Helper)
Expand Down
10 changes: 5 additions & 5 deletions templates/default/my.cnf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# You can copy this file to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
Expand Down Expand Up @@ -39,7 +39,7 @@ pid-file = <%= node['mariadb']['mysqld']['pid_file'] %>
socket = <%= node['mariadb']['mysqld']['socket'] %>
port = <%= node['mariadb']['mysqld']['port'] %>
basedir = <%= node['mariadb']['mysqld']['basedir'] %>
datadir = <%= node['mariadb']['mysqld']['datadir'] %>
datadir = <%= node['mariadb']['mysqld']['default_datadir'] %>
tmpdir = <%= node['mariadb']['mysqld']['tmpdir'] %>
lc_messages_dir = <%= node['mariadb']['mysqld']['lc_messages_dir'] %>
lc_messages = <%= node['mariadb']['mysqld']['lc_messages'] %>
Expand All @@ -57,10 +57,10 @@ bind-address = <%= node['mariadb']['mysqld']['bind_address'] %>
#
max_connections = <%= node['mariadb']['mysqld']['max_connections'] %>
connect_timeout = <%= node['mariadb']['mysqld']['connect_timeout'] %>
wait_timeout = <%= node['mariadb']['mysqld']['wait_timeout'] %>
wait_timeout = <%= node['mariadb']['mysqld']['wait_timeout'] %>
max_allowed_packet = <%= node['mariadb']['mysqld']['max_allowed_packet'] %>
thread_cache_size = <%= node['mariadb']['mysqld']['thread_cache_size'] %>
sort_buffer_size = <%= node['mariadb']['mysqld']['sort_buffer_size'] %>
sort_buffer_size = <%= node['mariadb']['mysqld']['sort_buffer_size'] %>
bulk_insert_buffer_size = <%= node['mariadb']['mysqld']['bulk_insert_buffer_size'] %>
tmp_table_size = <%= node['mariadb']['mysqld']['tmp_table_size'] %>
max_heap_table_size = <%= node['mariadb']['mysqld']['max_heap_table_size'] %>
Expand Down Expand Up @@ -135,7 +135,7 @@ log_slow_verbosity = query_plan
# If applications support it, this stricter sql_mode prevents some
# mistakes like inserting invalid dates etc.
#sql_mode = NO_ENGINE_SUBSTITUTION,TRADITIONAL

default_storage_engine = <%= node['mariadb']['mysqld']['default_storage_engine'] %>

#
Expand Down
2 changes: 2 additions & 0 deletions test/integration/datadir_changed/serverspec/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source 'https://rubygems.org'
gem 'serverspec', '< 2.0'
11 changes: 11 additions & 0 deletions test/integration/datadir_changed/serverspec/install_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'

package_server_name = 'mariadb-server-10.0'
case backend.check_os[:family]
when 'Fedora', 'CentOS', 'RedHat'
package_server_name = 'MariaDB-server'
end

describe package(package_server_name) do
it { should be_installed }
end
75 changes: 75 additions & 0 deletions test/integration/datadir_changed/serverspec/mariadb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require 'spec_helper'

describe service('mysql') do
it { should be_enabled }
it { should be_running }
end

describe port('3306') do
it { should be_listening }
end

includedir = '/etc/mysql/conf.d'
mysql_config_file = '/etc/mysql/my.cnf'
case backend.check_os[:family]
when 'Fedora', 'CentOS', 'RedHat'
includedir = '/etc/my.cnf.d'
mysql_config_file = '/etc/my.cnf'
end

describe "verify the tuning attributes set in #{mysql_config_file}" do
{
query_cache_size: '64M',
thread_cache_size: 128,
max_connections: 100,
wait_timeout: 600,
max_heap_table_size: '32M',
read_buffer_size: '2M',
read_rnd_buffer_size: '1M',
long_query_time: 10,
key_buffer_size: '128M',
max_allowed_packet: '16M',
sort_buffer_size: '4M',
myisam_sort_buffer_size: '512M'
}.each do |attribute, value|
describe command("grep -E \"^#{attribute}\\s+\" #{mysql_config_file}") do
it { should return_stdout(/#{value}/) }
end
end
end

describe 'verify the tuning attributes set in ' + includedir + '/innodb.cnf' do
{
innodb_buffer_pool_size: '256M',
innodb_flush_method: 'O_DIRECT',
innodb_file_per_table: 1,
innodb_open_files: 400
}.each do |attribute, value|
describe command("grep -E \"^#{attribute}\\s+\" " \
"#{includedir}/innodb.cnf") do
it { should return_stdout(/#{value}/) }
end
end
end

describe 'verify the tuning attributes set in ' \
+ includedir + '/replication.cnf' do
{
max_binlog_size: '100M',
expire_logs_days: 10
}.each do |attribute, value|
describe command("grep -E \"^#{attribute}\\s+\" " \
"#{includedir}/replication.cnf") do
it { should return_stdout(/#{value}/) }
end
end
end

describe 'verify that the datadir was moved' do
describe file('/home/mysql') do
it { should be_directory }
end
describe file('/var/lib/mysql') do
it { should be_linked_to '/home/mysql' }
end
end
4 changes: 4 additions & 0 deletions test/integration/datadir_changed/serverspec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'serverspec'

include Serverspec::Helper::Exec
include Serverspec::Helper::DetectOS

0 comments on commit 1a00d77

Please sign in to comment.