/
server.rb
251 lines (222 loc) · 7.87 KB
/
server.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#
# Cookbook Name:: mysql
# Recipe:: default
#
# Copyright 2008-2013, Opscode, Inc.
#
# 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.
#
::Chef::Recipe.send(:include, Opscode::OpenSSL::Password)
include_recipe 'mysql::client'
if Chef::Config[:solo]
missing_attrs = %w[
server_debian_password
server_root_password
server_repl_password
].select { |attr| node['mysql'][attr].nil? }.map { |attr| %Q{node['mysql']['#{attr}']} }
unless missing_attrs.empty?
Chef::Application.fatal! "You must set #{missing_attrs.join(', ')} in chef-solo mode." \
" For more information, see https://github.com/opscode-cookbooks/mysql#chef-solo-note"
end
else
# generate all passwords
node.set_unless['mysql']['server_debian_password'] = secure_password
node.set_unless['mysql']['server_root_password'] = secure_password
node.set_unless['mysql']['server_repl_password'] = secure_password
node.save
end
if platform_family?('debian')
directory '/var/cache/local/preseeding' do
owner 'root'
group node['mysql']['root_group']
mode '0755'
recursive true
end
execute 'preseed mysql-server' do
command 'debconf-set-selections /var/cache/local/preseeding/mysql-server.seed'
action :nothing
end
template '/var/cache/local/preseeding/mysql-server.seed' do
source 'mysql-server.seed.erb'
owner 'root'
group node['mysql']['root_group']
mode '0600'
notifies :run, 'execute[preseed mysql-server]', :immediately
end
template "#{node['mysql']['conf_dir']}/debian.cnf" do
source 'debian.cnf.erb'
owner 'root'
group node['mysql']['root_group']
mode '0600'
end
end
if platform_family?('windows')
package_file = File.join(Chef::Config[:file_cache_path], node['mysql']['package_file'])
remote_file package_file do
source node['mysql']['url']
not_if { File.exists?(package_file) }
end
windows_package node['mysql']['server']['packages'].first do
source package_file
end
def package(*args, &blk)
windows_package(*args, &blk)
end
else
group 'mysql' do
action :create
end
user 'mysql' do
comment 'MySQL Server'
gid 'mysql'
system true
home node['mysql']['data_dir']
shell '/sbin/nologin'
end
node['mysql']['server']['packages'].each do |name|
package name do
action :install
notifies :start, 'service[mysql]', :immediately
end
end
end
unless platform_family?('mac_os_x')
[
File.dirname(node['mysql']['pid_file']),
File.dirname(node['mysql']['tunable']['slow_query_log']),
node['mysql']['conf_dir'],
node['mysql']['confd_dir'],
node['mysql']['log_dir'],
node['mysql']['data_dir']
].each do |path|
directory path do
owner 'mysql' unless platform?('windows')
group 'mysql' unless platform?('windows')
action :create
recursive true
end
end
skip_federated = case node['platform']
when 'fedora', 'ubuntu', 'amazon'
true
when 'centos', 'redhat', 'scientific'
node['platform_version'].to_f < 6.0
else
false
end
template "#{node['mysql']['conf_dir']}/my.cnf" do
source 'my.cnf.erb'
owner 'root' unless platform? 'windows'
group node['mysql']['root_group'] unless platform?('windows')
mode '0644'
case node['mysql']['reload_action']
when 'restart'
notifies :restart, 'service[mysql]', :delayed
when 'reload'
notifies :reload, 'service[mysql]', :delayed
else
Chef::Log.info "my.cnf updated but mysql.reload_action is #{node['mysql']['reload_action']}. No action taken."
end
variables :skip_federated => skip_federated
end
end
if platform_family?('windows')
require 'win32/service'
windows_path node['mysql']['bin_dir'] do
action :add
end
windows_batch 'install mysql service' do
command "\"#{node['mysql']['bin_dir']}\\mysqld.exe\" --install #{node['mysql']['service_name']}"
not_if { Win32::Service.exists?(node['mysql']['service_name']) }
end
end
# Homebrew has its own way to do databases
if platform_family?('mac_os_x')
execute 'mysql-install-db' do
command "mysql_install_db --verbose --user=`whoami` --basedir=\"$(brew --prefix mysql)\" --datadir=#{node['mysql']['data_dir']} --tmpdir=/tmp"
environment('TMPDIR' => nil)
action :run
creates "#{node['mysql']['data_dir']}/mysql"
end
# set the root password for situations that don't support pre-seeding.
# (eg. platforms other than debian/ubuntu & drop-in mysql replacements)
execute 'assign-root-password mac_os_x' do
command %Q["#{node['mysql']['mysqladmin_bin']}" -u root password '#{node['mysql']['server_root_password']}']
action :run
only_if %Q["#{node['mysql']['mysql_bin']}" -u root -e 'show databases;']
end
else
execute 'mysql-install-db' do
command 'mysql_install_db'
action :run
not_if { File.exists?(node['mysql']['data_dir'] + '/mysql/user.frm') }
end
service 'mysql' do
service_name node['mysql']['service_name']
provider Chef::Provider::Service::Upstart if node['mysql']['use_upstart']
supports :status => true, :restart => true, :reload => true
action :enable
end
template "#{node['mysql']['conf_dir']}/my.cnf" do
source 'my.cnf.erb'
owner 'root' unless platform? 'windows'
group node['mysql']['root_group'] unless platform? 'windows'
mode '0644'
case node['mysql']['reload_action']
when 'restart'
notifies :restart, 'service[mysql]', :immediately
when 'reload'
notifies :reload, 'service[mysql]', :immediately
else
Chef::Log.info "my.cnf updated but mysql.reload_action is #{node['mysql']['reload_action']}. No action taken."
end
variables :skip_federated => skip_federated
end
# set the root password for situations that don't support pre-seeding.
# (eg. platforms other than debian/ubuntu & drop-in mysql replacements)
execute 'assign-root-password' do
command %Q["#{node['mysql']['mysqladmin_bin']}" -u root password '#{node['mysql']['server_root_password']}']
action :run
only_if %Q["#{node['mysql']['mysql_bin']}" -u root -e 'show databases;']
end
grants_path = node['mysql']['grants_path']
begin
resources("template[#{grants_path}]")
rescue
Chef::Log.info('Could not find previously defined grants.sql resource')
template grants_path do
source 'grants.sql.erb'
owner 'root' unless platform_family? 'windows'
group node['mysql']['root_group'] unless platform_family? 'windows'
mode '0600'
action :create
end
end
if platform_family?('windows')
windows_batch 'mysql-install-privileges' do
command "\"#{node['mysql']['mysql_bin']}\" -u root #{node['mysql']['server_root_password'].empty? ? '' : '-p' }\"#{node['mysql']['server_root_password']}\" < \"#{grants_path}\""
action :nothing
subscribes :run, resources("template[#{grants_path}]"), :immediately
end
else
execute 'mysql-install-privileges' do
command %Q["#{node['mysql']['mysql_bin']}" -u root #{node['mysql']['server_root_password'].empty? ? '' : '-p' }'#{node['mysql']['server_root_password']}' < "#{grants_path}"]
action :nothing
subscribes :run, resources("template[#{grants_path}]"), :immediately
end
end
service 'mysql-start' do
service_name node['mysql']['service_name']
action :start
end
end