Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Vagrant provisioning and manual execution of mysql_role_swap.rb succe…

…ssful!
  • Loading branch information...
commit f35ba6efdc98f4c98b0a019ddeecc560f98e23a7 1 parent dc980f0
@jthurman42 jthurman42 authored
View
10 Vagrantfile
@@ -11,24 +11,18 @@ Vagrant::Config.run do |config|
#
- # Provisioning
- #
- config.vm.provision :shell, :path => 'vagrant-configs/setup.sh'
-
-
- #
# Targets
#
config.vm.define :mysql1 do |target_config|
target_config.vm.host_name = 'test-mysql-1'
target_config.vm.network :hostonly, "192.168.50.31"
- target_config.vm.provision :shell, :inline => 'if [ ! -L /etc/my.cnf ]; then rm -f /etc/my.cnf && ln -s /vagrant/vagrant-configs/master.cnf /etc/my.cnf && service mysqld restart ; fi'
+ target_config.vm.provision :shell, :path => 'vagrant-configs/setup.sh', :args => "master"
end
config.vm.define :mysql2 do |target_config|
target_config.vm.host_name = 'test-mysql-2'
target_config.vm.network :hostonly, "192.168.50.32"
- target_config.vm.provision :shell, :inline => 'if [ ! -L /etc/my.cnf ]; then rm -f /etc/my.cnf && ln -s /vagrant/vagrant-configs/slave.cnf /etc/my.cnf && service mysqld restart ; fi'
+ target_config.vm.provision :shell, :path => 'vagrant-configs/setup.sh', :args => "slave"
end
end
View
4 cluster.yml
@@ -13,7 +13,7 @@ database_one:
host: 192.168.50.31
port: 3306
slave_password: slavepw
- vip_interface: eth1
+ floating_dev: eth1
database_two:
adapter: mysql
@@ -23,5 +23,5 @@ database_two:
host: 192.168.50.32
port: 3306
slave_password: slavepw
- vip_interface: eth1
+ floating_dev: eth1
View
72 mysql_role_swap.rb
@@ -113,9 +113,9 @@ def self.config
CONFIG[database]
end
- def self.vip_interface
- if CONFIG['vip_interface']
- CONFIG['vip_interface']
+ def self.floating_dev
+ if self.config['floating_dev']
+ self.config['floating_dev']
else
"bond0"
end
@@ -148,9 +148,9 @@ def self.ip_role
def self.add_vip
if self.config['host'] == `hostname`.chomp
- `sudo /sbin/ip addr add #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.vip_interface}`
+ `sudo /sbin/ip addr add #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.floating_dev}`
else
- `ssh #{SSH_OPTIONS} #{self.config['host']} 'sudo /sbin/ip addr add #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.vip_interface}'`
+ `ssh #{SSH_OPTIONS} #{self.config['host']} 'sudo /sbin/ip addr add #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.floating_dev}'`
end
if $?.exitstatus == 0
true
@@ -161,9 +161,9 @@ def self.add_vip
def self.remove_vip
if self.config['host'] == `hostname`.chomp
- `sudo /sbin/ip addr del #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.vip_interface}`
+ `sudo /sbin/ip addr del #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.floating_dev}`
else
- `ssh #{SSH_OPTIONS} #{self.config['host']} 'sudo /sbin/ip addr del #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.vip_interface}'`
+ `ssh #{SSH_OPTIONS} #{self.config['host']} 'sudo /sbin/ip addr del #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.floating_dev}'`
end
if $?.exitstatus == 0
true
@@ -185,9 +185,9 @@ def self.arping_path
def self.arping
if self.config['host'] == `hostname`.chomp
- `sudo #{self.arping_path} -U -c 4 -I #{self.vip_interface} #{FLOATING_IP}`
+ `sudo #{self.arping_path} -U -c 4 -I #{self.floating_dev} #{FLOATING_IP}`
else
- `ssh #{SSH_OPTIONS} #{self.config['host']} 'sudo #{self.arping_path} -U -c 4 -I #{self.vip_interface} #{FLOATING_IP}'`
+ `ssh #{SSH_OPTIONS} #{self.config['host']} 'sudo #{self.arping_path} -U -c 4 -I #{self.floating_dev} #{FLOATING_IP}'`
end
if $?.exitstatus == 0
true
@@ -229,6 +229,16 @@ def self.hostname
end
end
+ def self.print_info
+ printf "%-22s: %s:%d\n", self.role.capitalize, self.hostname, self.config['port']
+ puts "MySQL Replication Role: #{self.mysql_rep_role}"
+ puts "Floating IP Role : #{self.ip_role}"
+ puts "Floating IP Interface : #{self.floating_dev}"
+ puts "MySQL Version : [#{self.version}]"
+ puts "Read-Only : #{self.read_only?}"
+ puts "Arping Path : #{self.arping_path}\n\n"
+ end
+
end
class DatabaseTwo < ActiveRecord::Base
@@ -243,9 +253,9 @@ def self.config
CONFIG[database]
end
- def self.vip_interface
- if CONFIG['vip_interface']
- CONFIG['vip_interface']
+ def self.floating_dev
+ if self.config['floating_dev']
+ self.config['floating_dev']
else
"bond0"
end
@@ -277,9 +287,9 @@ def self.ip_role
def self.add_vip
if self.config['host'] == `hostname`.chomp
- `sudo /sbin/ip addr add #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.vip_interface}`
+ `sudo /sbin/ip addr add #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.floating_dev}`
else
- `ssh #{SSH_OPTIONS} #{self.config['host']} 'sudo /sbin/ip addr add #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.vip_interface}'`
+ `ssh #{SSH_OPTIONS} #{self.config['host']} 'sudo /sbin/ip addr add #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.floating_dev}'`
end
if $?.exitstatus == 0
true
@@ -290,9 +300,9 @@ def self.add_vip
def self.remove_vip
if self.config['host'] == `hostname`.chomp
- `sudo /sbin/ip addr del #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.vip_interface}`
+ `sudo /sbin/ip addr del #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.floating_dev}`
else
- `ssh #{SSH_OPTIONS} #{self.config['host']} 'sudo /sbin/ip addr del #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.vip_interface}'`
+ `ssh #{SSH_OPTIONS} #{self.config['host']} 'sudo /sbin/ip addr del #{FLOATING_IP}#{FLOATING_IP_CIDR} dev #{self.floating_dev}'`
end
if $?.exitstatus == 0
true
@@ -314,9 +324,9 @@ def self.arping_path
def self.arping
if self.config['host'] == `hostname`.chomp
- `sudo #{self.arping_path} -U -c 4 -I #{slef.vip_interface} #{FLOATING_IP}`
+ `sudo #{self.arping_path} -U -c 4 -I #{slef.floating_dev} #{FLOATING_IP}`
else
- `ssh #{SSH_OPTIONS} #{self.config['host']} 'sudo #{self.arping_path} -U -c 4 -I #{self.vip_interface} #{FLOATING_IP}'`
+ `ssh #{SSH_OPTIONS} #{self.config['host']} 'sudo #{self.arping_path} -U -c 4 -I #{self.floating_dev} #{FLOATING_IP}'`
end
if $?.exitstatus == 0
true
@@ -358,6 +368,16 @@ def self.hostname
end
end
+ def self.print_info
+ printf "%-22s: %s:%d\n", self.role.capitalize, self.hostname, self.config['port']
+ puts "MySQL Replication Role: #{self.mysql_rep_role}"
+ puts "Floating IP Role : #{self.ip_role}"
+ puts "Floating IP Interface : #{self.floating_dev}"
+ puts "MySQL Version : [#{self.version}]"
+ puts "Read-Only : #{self.read_only?}"
+ puts "Arping Path : #{self.arping_path}\n\n"
+ end
+
end
class MysqlSwitchRoleContext
@@ -381,15 +401,8 @@ def initialize
databases.sort {|x,y| x.role <=> y.role}.each do |db|
begin
+ db.print_info
- #puts "#{db.role.capitalize}:".white + " #{db.hostname}:#{db.config['port']}"
- printf "%-22s: %s:%d\n", db.role.capitalize, db.hostname, db.config['port']
- puts "MySQL Replication Role: #{db.mysql_rep_role}"
- puts "Floating IP Role : #{db.ip_role}"
- puts "Floating IP Interface : #{db.vip_interface}"
- puts "MySQL Version : [#{db.version}]"
- puts "Read-Only : #{db.read_only?}"
- puts "Arping Path : #{db.arping_path}\n\n"
if db.role == "slave"
unless @slave
@slave = db
@@ -654,12 +667,7 @@ def done
databases = [DatabaseOne, DatabaseTwo]
databases.sort {|x,y| x.role <=> y.role}.each do |db|
begin
-
- puts "#{db.role.capitalize}:".white + " #{db.hostname}:#{db.config['port']}"
- puts "MySQL Replication Role: #{db.mysql_rep_role}"
- puts "Floating IP Role: #{db.ip_role}"
- puts "MySQL Version: [#{db.version}]"
- puts "Read-Only: #{db.read_only?}\n\n"
+ db.print_info
rescue
end
end
View
46 vagrant-configs/setup.sh
@@ -1,21 +1,57 @@
#!/bin/bash
+ROLE=$1
+ADMIN_USER=root
+MASTER_HOST=192.168.50.31
+SLAVE_USER=slave
+SLAVE_PASS=slavepw
+FLOATING_IP=192.168.50.30
+FLOATING_DEV=eth1
+
+
+[ -z $ROLE ] && exit
+
# Disable iptables
-echo "Stop firewall..."
+echo "Stopping firewall..."
service iptables stop 2>&1 >> /dev/null
# Install mysql-server if not already installed
-echo "Verify mysql server is installed..."
+echo "Verifying mysql server is installed..."
rpm -q --quiet mysql-server || yum install -y mysql-server
# Start mysqld
-echo "Verify mysqld is running..."
+echo "Verifying mysqld is running..."
service mysqld status 2>&1 >> /dev/null || service mysqld start 2>&1 >> /dev/null
# MySQL user setup
echo "Granting remote root access..."
-/usr/bin/mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'; FLUSH PRIVILEGES;"
+/usr/bin/mysql -u $ADMIN_USER -e "GRANT ALL PRIVILEGES ON *.* TO '${ADMIN_USER}'@'%'; FLUSH PRIVILEGES;"
echo "Granting replication access..."
-/usr/bin/mysql -u root -e "GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'slavepw'; FLUSH PRIVILEGES;"
+/usr/bin/mysql -u $ADMIN_USER -e "GRANT REPLICATION SLAVE ON *.* TO '${SLAVE_USER}'@'%' IDENTIFIED BY '${SLAVE_PASS}'; FLUSH PRIVILEGES;"
+
+
+# Link in our configuration files
+if [ ! -L /etc/my.cnf ]; then
+ rm -f /etc/my.cnf
+ ln -s /vagrant/vagrant-configs/${ROLE}.cnf /etc/my.cnf
+ service mysqld restart
+fi
+
+# Setup one system to be a slave to start with
+if [ "${ROLE}" == "slave" ]; then
+ echo "Checking Replication Status..."
+ SLAVE_RUNNING=$(/usr/bin/mysql -u $ADMIN_USER -e "SHOW SLAVE STATUS\G" | grep -c -e "Slave_IO_State: Waiting")
+ if [ "${SLAVE_RUNNING}" != "1" ]; then
+ echo "Initiating Replication..."
+ /usr/bin/mysql -u $ADMIN_USER -e "CHANGE MASTER TO MASTER_HOST='${MASTER_HOST}', MASTER_USER='${SLAVE_USER}', MASTER_PASSWORD='${SLAVE_PASS}', MASTER_LOG_FILE='', MASTER_LOG_POS=4"
+ fi
+ echo "Forcing slave to be read-only..."
+ /usr/bin/mysql -u $ADMIN_USER -e "SET GLOBAL read_only = on"
+fi
+
+if [ "${ROLE}" == "master" ]; then
+ echo "Installing Floating VIP ${FLOATING_UP}..."
+ /sbin/ip addr add ${FLOATING_IP} dev ${FLOATING_DEV}
+fi
Please sign in to comment.
Something went wrong with that request. Please try again.