Advertisement
Guest User

Untitled

a guest
Apr 17th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. ## mysql::master
  2. ruby_block "store_mysql_master_status" do
  3. block do
  4. node.set[:mysql][:master] = true
  5. m = Mysql.new("localhost", "root", node[:mysql][:server_root_password])
  6. m.query("show master status") do |row|
  7. row.each_hash do |h|
  8. node.set[:mysql][:master_file] = h['File']
  9. node.set[:mysql][:master_position] = h['Position']
  10. end
  11. end
  12. Chef::Log.info "Storing database master replication status: #{node[:mysql][:master_file]} #{node[:mysql][:master_position]}"
  13. node.save
  14. end
  15. # only execute if mysql is running
  16. only_if "pgrep 'mysqld$'"
  17. # subscribe to mysql service to catch restarts
  18. subscribes :create, resources(:service => "mysql")
  19. end
  20.  
  21. ## mysql::slave
  22. ruby_block "start_replication" do
  23. block do
  24. dbmasters = search(:node, "mysql_master:true AND mysql_cluster_name:#{node[:mysql][:cluster_name]}")
  25.  
  26. if dbmasters.size != 1
  27. Chef::Log.error("#{dbmasters.size} database masters, cannot set up replication!")
  28. else
  29. dbmaster = dbmasters.first
  30. Chef::Log.info("Using #{dbmaster.name} as master")
  31.  
  32. m = Mysql.new("localhost", "root", node[:mysql][:server_root_password])
  33. command = %Q{
  34. CHANGE MASTER TO
  35. MASTER_HOST="#{dbmaster.mysql.bind_address}",
  36. MASTER_USER="repl",
  37. MASTER_PASSWORD="#{dbmaster.mysql.server_repl_password}",
  38. MASTER_LOG_FILE="#{dbmaster.mysql.master_file}",
  39. MASTER_LOG_POS=#{dbmaster.mysql.master_position};
  40. }
  41. Chef::Log.info "Sending start replication command to mysql: "
  42. Chef::Log.info "#{command}"
  43.  
  44. m.query("stop slave")
  45. m.query(command)
  46. m.query("start slave")
  47. end
  48. end
  49. not_if do
  50. #TODO this fails if mysql is not running - check first
  51. m = Mysql.new("localhost", "root", node[:mysql][:server_root_password])
  52. slave_sql_running = ""
  53. m.query("show slave status") {|r| r.each_hash {|h| slave_sql_running = h['Slave_SQL_Running'] } }
  54. slave_sql_running == "Yes"
  55. end
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement