Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #214 #220 #233

Closed
wants to merge 22 commits into from
2 changes: 1 addition & 1 deletion resources/client_install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
action :install do
mariadb_repository 'Add mariadb.org repository' do
version new_resource.version
only_if { new_resource.setup_repo }
only_if { !new_resource.setup_repo.nil? }
end

case node['platform_family']
Expand Down
42 changes: 33 additions & 9 deletions resources/server_install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,45 @@

log 'Enable and start MariaDB service' do
notifies :enable, "service[#{platform_service_name}]", :immediately
notifies :start, "service[#{platform_service_name}]", :immediately
notifies :stop, "service[#{platform_service_name}]", :immediately
notifies :run, 'execute[apply-mariadb-root-password]', :immediately
end

mariadb_root_password = new_resource.password == 'generate' || new_resource.password.nil? ? secure_random : new_resource.password
# here we want to generate a new password if: 1- the user passed 'generate' to the password argument
# 2- the user did not pass anything to the password argument OR
# the user did not define node['mariadb']['server_root_password'] attribute
mariadb_root_password = (new_resource.password == 'generate' || (new_resource.password.nil? && node['mariadb']['server_root_password'].nil?)) ? secure_random : new_resource.password

# Generate a ramdom password or set the a password defined with node['mariadb']['password']['root'].
# Generate a random password or set a password defined with node['mariadb']['server_root_password'].
# The password is set or change at each run. It is good for security if you choose to set a random password and
# allow you to change the root password if needed.
bash 'generate-mariadb-root-password' do
file 'generate-mariadb-root-password' do
path "#{data_dir}/recovery.conf"
owner 'mysql'
group 'root'
mode '640'
sensitive true
content "ALTER USER 'root'@'localhost' IDENTIFIED BY '#{mariadb_root_password}';"
action :nothing
end

pid_file = default_pid_file.nil? ? '/var/run/mysql/mysqld.pid' : default_pid_file

# make sure that mysqld is not running, and then set the root password
execute 'apply-mariadb-root-password' do
user 'root'
code <<-EOH
echo "ALTER USER 'root'@'localhost' IDENTIFIED BY \'#{mariadb_root_password}\';" | /usr/bin/mysql
EOH
not_if { ::File.exist? "#{data_dir}/recovery.conf" }
only_if { new_resource.password }
command "(test -f #{pid_file} && kill $(< #{pid_file})); /usr/sbin/mysqld --init-file=#{data_dir}/recovery.conf&>/dev/null&"
notifies :create, 'file[generate-mariadb-root-password]', :before
notifies :start, "service[#{platform_service_name}]", :immediately
notifies :run, 'execute[verify-root-password-okay]', :delayed
action :nothing
end

# make sure the password was properly set
execute 'verify-root-password-okay' do
user 'root'
command "mysql -p#{mariadb_root_password} -e '\\s'&>/dev/null"
action :nothing
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/cookbooks/test/recipes/server_install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
find_resource(:service, 'mariadb') do
extend MariaDBCookbook::Helpers
service_name lazy { platform_service_name }
supports restart: true, status: true, reload: true
supports restart: true, status: true
action [:enable, :start]
end