/
smoke.rb
172 lines (152 loc) · 5.61 KB
/
smoke.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
require 'shellwords'
apt_update 'update'
# variables
root_pass_master = 'MyPa$$word\Has_"Special\'Chars%!'
root_pass_slave = 'An0th3r_Pa%%w0rd!'
source_data = node['mysql_test']['version'].to_i >= 8 ? '--source-data' : '--master-data'
# We're not able to use apparmor with how this test is setup so disable it for now
node.default['apparmor']['disable'] = true
include_recipe 'apparmor'
# Debug message
Chef::Log.error "\n\n" + '=' * 80 + "\n\nTesting MySQL version '#{node['mysql_test']['version']}'\n\n" + '=' * 80
# master
mysql_service 'master' do
port '3306'
version node['mysql_test']['version']
initial_root_password root_pass_master
action [:create, :start]
end
mysql_config 'master replication' do
config_name 'replication'
instance 'master'
source 'replication-master.erb'
variables(server_id: '1', mysql_instance: 'master')
notifies :restart, 'mysql_service[master]', :immediately
action :create
end
# MySQL client
mysql_client 'master' do
action :create
end
# slave-1
mysql_service 'slave-1' do
port '3307'
version node['mysql_test']['version']
initial_root_password root_pass_slave
action [:create, :start]
end
mysql_config 'replication-slave-1' do
instance 'slave-1'
source 'replication-slave.erb'
variables(server_id: '2', mysql_instance: 'slave-1')
notifies :restart, 'mysql_service[slave-1]', :immediately
action :create
end
# slave-2
mysql_service 'slave-2' do
port '3308'
version node['mysql_test']['version']
initial_root_password root_pass_slave
action [:create, :start]
end
mysql_config 'replication-slave-2' do
instance 'slave-2'
source 'replication-slave.erb'
variables(server_id: '3', mysql_instance: 'slave-2')
notifies :restart, 'mysql_service[slave-2]', :immediately
action :create
end
wait_for_command = "/usr/bin/mysql -u root -h 127.0.0.1 -P 3308 -p#{Shellwords.escape(root_pass_slave)} -e 'SELECT 0' >/dev/null 2>&1"
# Wait for slave-2 to start up, the sql below may not run properly if it isn't started,
# even if it will start properly eventually.
# Not worrying about master or slave-1 as starting slave-2 should provide enough buffer
ruby_block 'wait for slave-2' do
block do
require 'English'
times = 0
system(wait_for_command)
until $CHILD_STATUS.exitstatus == 0 || times > 60 # don't wait over 60 seconds
sleep 1
times += 1
system(wait_for_command)
end
end
not_if wait_for_command
end
# Create user repl on master
bash 'create replication user' do
code <<-EOF
/usr/bin/mysql -u root -h 127.0.0.1 -P 3306 -p#{Shellwords.escape(root_pass_master)} -D mysql -e "CREATE USER 'repl'@'127.0.0.1' IDENTIFIED BY 'REPLICAAATE';"
/usr/bin/mysql -u root -h 127.0.0.1 -P 3306 -p#{Shellwords.escape(root_pass_master)} -D mysql -e "GRANT REPLICATION SLAVE ON *.* TO 'repl'@'127.0.0.1';"
EOF
not_if "/usr/bin/mysql -u root -h 127.0.0.1 -P 3306 -p#{Shellwords.escape(root_pass_master)} -e 'select User,Host from mysql.user' | grep repl"
action :run
end
# take a dump
bash 'create /root/dump.sql' do
user 'root'
code <<-EOF
mysqldump \
--defaults-file=/etc/mysql-master/my.cnf \
-u root \
--protocol=tcp \
-p#{Shellwords.escape(root_pass_master)} \
--skip-lock-tables \
--single-transaction \
--flush-logs \
--hex-blob \
#{source_data}=2 \
-A \ > /root/dump.sql;
EOF
not_if { ::File.exist?('/root/dump.sql') }
action :run
end
# stash replication start position on the filesystem
bash 'stash position in /root/position' do
user 'root'
code <<-EOF
head /root/dump.sql -n80 \
| grep 'MASTER_LOG_POS' \
| awk '{ print $6 }' \
| cut -f2 -d '=' \
| cut -f1 -d';' \
> /root/position
EOF
not_if { ::File.exist?('/root/position') }
action :run
end
# import dump into slaves
bash 'slave-1 import' do
user 'root'
code "/usr/bin/mysql -u root -h 127.0.0.1 -P 3307 -p#{Shellwords.escape(root_pass_slave)} < /root/dump.sql"
not_if "/usr/bin/mysql -u root -h 127.0.0.1 -P 3307 -p#{Shellwords.escape(root_pass_slave)} -e 'select User,Host from mysql.user' | grep repl"
action :run
end
bash 'slave-2 import' do
user 'root'
code "/usr/bin/mysql -u root -h 127.0.0.1 -P 3308 -p#{Shellwords.escape(root_pass_slave)} < /root/dump.sql"
not_if "/usr/bin/mysql -u root -h 127.0.0.1 -P 3308 -p#{Shellwords.escape(root_pass_slave)} -e 'select User,Host from mysql.user' | grep repl"
action :run
end
# start replication on slave-1
ruby_block 'start_slave_1' do
block { start_slave_1(root_pass_slave) } # libraries/helpers.rb
not_if "/usr/bin/mysql -u root -h 127.0.0.1 -P 3307 -p#{Shellwords.escape(root_pass_slave)} -e 'SHOW SLAVE STATUS\\G' | grep Slave_IO_State"
action :run
end
# start replication on slave-2
ruby_block 'start_slave_2' do
block { start_slave_2(root_pass_slave) } # libraries/helpers.rb
not_if "/usr/bin/mysql -u root -h 127.0.0.1 -P 3308 -p#{Shellwords.escape(root_pass_slave)} -e 'SHOW SLAVE STATUS\\G' | grep Slave_IO_State"
action :run
end
# create databass on master
bash 'create databass' do
code <<-EOF
/usr/bin/mysql -u root -h 127.0.0.1 -P 3306 -p#{Shellwords.escape(root_pass_master)} -e 'CREATE DATABASE databass';
/usr/bin/mysql -u root -h 127.0.0.1 -P 3306 -p#{Shellwords.escape(root_pass_master)} -e 'CREATE TABLE databass.table1 (name VARCHAR(20), userRank VARCHAR(20))';
/usr/bin/mysql -u root -h 127.0.0.1 -P 3306 -p#{Shellwords.escape(root_pass_master)} -e "INSERT INTO databass.table1 (name,userRank) VALUES ('captain','awesome')";
EOF
not_if "/usr/bin/mysql -u root -h 127.0.0.1 -P 3306 -p#{Shellwords.escape(root_pass_master)} -e 'show databases' | grep databass"
action :run
end