generators/vulcanize/templates/mysql/config/rubber/deploy-mysql.rb in sml-rubber-0.9.13 vs generators/vulcanize/templates/mysql/config/rubber/deploy-mysql.rb in sml-rubber-1.5.5
- old
+ new
@@ -34,18 +34,19 @@
task task_name, :hosts => ic.full_name do
env = rubber_cfg.environment.bind("mysql_master", ic.name)
exists = capture("echo $(ls #{env.db_data_dir}/ 2> /dev/null)")
if exists.strip.size == 0
common_bootstrap("mysql_master")
- sudo "dpkg-reconfigure --frontend=noninteractive mysql-server-5.0"
- sleep 5
+
pass = "identified by '#{env.db_pass}'" if env.db_pass
- sudo "mysql -u root -e 'create database #{env.db_name};'"
- sudo "mysql -u root -e \"grant all on *.* to '#{env.db_user}'@'%' #{pass};\""
- sudo "mysql -u root -e \"grant select on *.* to '#{env.db_slave_user}'@'%' #{pass};\""
- sudo "mysql -u root -e \"grant replication slave on *.* to '#{env.db_replicator_user}'@'%' #{pass};\""
- sudo "mysql -u root -e \"flush privileges;\""
+ rubber.sudo_script "create_master_db", <<-ENDSCRIPT
+ mysql -u root -e "create database #{env.db_name};"
+ mysql -u root -e "grant all on *.* to '#{env.db_user}'@'%' #{pass};"
+ mysql -u root -e "grant select on *.* to '#{env.db_slave_user}'@'%' #{pass};"
+ mysql -u root -e "grant replication slave on *.* to '#{env.db_replicator_user}'@'%' #{pass};"
+ mysql -u root -e "flush privileges;"
+ ENDSCRIPT
end
end
send task_name
end
@@ -55,12 +56,10 @@
task task_name, :hosts => ic.full_name do
env = rubber_cfg.environment.bind("mysql_slave", ic.name)
exists = capture("echo $(ls #{env.db_data_dir}/ 2> /dev/null)")
if exists.strip.size == 0
common_bootstrap("mysql_slave")
- sudo "dpkg-reconfigure --frontend=noninteractive mysql-server-5.0"
- sleep 5
master = rubber_instances.for_role("mysql_master").first
# Doing a mysqldump locks the db, so ideally we'd do it against a slave replica thats
# not serving traffic (mysql_util role), but if thats not available try a regular
@@ -77,79 +76,93 @@
master_host = master.full_name
source_host = source.full_name
if source == master
logger.info "Creating slave from a dump of master #{source_host}"
- sudo "mysql -u root -e \"change master to master_host='#{master_host}', master_user='#{env.db_replicator_user}' #{master_pass}\""
- sudo "mysqldump -u #{env.db_user} #{pass} -h #{source_host} --all-databases --master-data=1 | mysql -u root"
+ rubber.sudo_script "create_slave_db_from_master", <<-ENDSCRIPT
+ mysql -u root -e "change master to master_host='#{master_host}', master_user='#{env.db_replicator_user}' #{master_pass}"
+ mysqldump -u #{env.db_user} #{env.db_pass.nil? ? '' : '--password ' + env.db_pass} -h #{source_host} --all-databases --master-data=1 | mysql -u root
+ ENDSCRIPT
else
logger.info "Creating slave from a dump of slave #{source_host}"
- sudo "mysql -u #{env.db_user} #{pass} -h #{source_host} -e \"stop slave;\""
+ rsudo "mysql -u #{env.db_user} --password #{env.db_pass} -h #{source_host} -e \"stop slave;\""
slave_status = capture("mysql -u #{env.db_user} #{pass} -h #{source_host} -e \"show slave status\\G\"")
slave_config = Hash[*slave_status.scan(/([^\s:]+): ([^\s]*)/).flatten]
log_file = slave_config['Master_Log_File']
log_pos = slave_config['Read_Master_Log_Pos']
- sudo "mysqldump -u #{env.db_user} #{pass} -h #{source_host} --all-databases --master-data=1 | mysql -u root"
- sudo "mysql -u root -e \"change master to master_host='#{master_host}', master_user='#{env.db_replicator_user}', master_log_file='#{log_file}', master_log_pos=#{log_pos} #{master_pass}\""
- sudo "mysql -u #{env.db_user} #{pass} -h #{source_host} -e \"start slave;\""
+ rubber.sudo_script "create_slave_db_from_slave", <<-ENDSCRIPT
+ mysqldump -u #{env.db_user} --password #{env.db_pass} -h #{source_host} --all-databases --master-data=1 | mysql -u root
+ mysql -u root -e "change master to master_host='#{master_host}', master_user='#{env.db_replicator_user}', master_log_file='#{log_file}', master_log_pos=#{log_pos} #{master_pass}"
+ mysql -u #{env.db_user} --password #{env.db_pass} -h #{source_host} -e "start slave;"
+ ENDSCRIPT
end
- sudo "mysql -u root -e \"flush privileges;\""
- sudo "mysql -u root -e \"start slave;\""
+ # this doesn't work without agent forwarding which sudo breaks, as well as not having your
+ # ec2 private key ssh-added on workstation
+ # sudo "scp -o \"StrictHostKeyChecking=no\" #{source_host}:/etc/mysql/debian.cnf /etc/mysql"
+
+ rsudo "mysql -u root -e \"flush privileges;\""
+ rsudo "mysql -u root -e \"start slave;\""
end
end
send task_name
end
end
# TODO: Make the setup/update happen just once per host
def common_bootstrap(role)
# mysql package install starts mysql, so stop it
- sudo "/etc/init.d/mysql stop" rescue nil
+ rsudo "service mysql stop" rescue nil
# After everything installed on machines, we need the source tree
# on hosts in order to run rubber:config for bootstrapping the db
- deploy.setup
- deploy.update_code
+ rubber.update_code_for_bootstrap
# Gen just the conf for the given mysql role
- rubber.run_config(:RUBBER_ENV => RUBBER_ENV, :FILE => "role/#{role}|role/db/my.cnf", :deploy_path => release_path)
+ rubber.run_config(:RUBBER_ENV => RUBBER_ENV, :FILE => "role/#{role}|role/db/", :FORCE => true, :deploy_path => release_path)
+
+ # reconfigure mysql so that it sets up data dir in /mnt with correct files
+ sudo_script 'reconfigure-mysql', <<-ENDSCRIPT
+ server_package=`dpkg -l | grep mysql-server-[0-9] | awk '{print $2}'`
+ dpkg-reconfigure --frontend=noninteractive $server_package
+ ENDSCRIPT
+ sleep 5
end
before "rubber:munin:custom_install", "rubber:mysql:custom_install_munin"
desc <<-DESC
Installs some extra munin graphs
DESC
task :custom_install_munin, :roles => [:mysql_master, :mysql_slave] do
- rubber.run_script 'install_munin_mysql', <<-ENDSCRIPT
+ rubber.sudo_script 'install_munin_mysql', <<-ENDSCRIPT
if [ ! -f /usr/share/munin/plugins/mysql_ ]; then
- wget -q -O /usr/share/munin/plugins/mysql_ http://github.com/kjellm/munin-mysql/raw/master/mysql_
- wget -q -O /etc/munin/plugin-conf.d/mysql_.conf http://github.com/kjellm/munin-mysql/raw/master/mysql_.conf
+ wget -qN -O /usr/share/munin/plugins/mysql_ http://github.com/kjellm/munin-mysql/raw/master/mysql_
+ wget -qN -O /etc/munin/plugin-conf.d/mysql_.conf http://github.com/kjellm/munin-mysql/raw/master/mysql_.conf
fi
ENDSCRIPT
end
desc <<-DESC
Starts the mysql daemons
DESC
task :start, :roles => [:mysql_master, :mysql_slave] do
- sudo "/etc/init.d/mysql start"
+ rsudo "service mysql start"
end
desc <<-DESC
Stops the mysql daemons
DESC
task :stop, :roles => [:mysql_master, :mysql_slave] do
- sudo "/etc/init.d/mysql stop"
+ rsudo "service mysql stop"
end
desc <<-DESC
Restarts the mysql daemons
DESC
task :restart, :roles => [:mysql_master, :mysql_slave] do
- sudo "/etc/init.d/mysql restart"
+ rsudo "service mysql restart"
end
end
end