class SnapEbs::Plugin::MysqlPlugin

Public Instance Methods

after() click to toggle source
# File lib/plugins/mysql_plugin.rb, line 33
def after
  if master?
    logger.error "This appears to be a master in a replication set. Refusing to operate."
    return false
  end
  true
ensure
  unlock_tables
  start_mysql if options.shutdown == 'yes'
end
before() click to toggle source
# File lib/plugins/mysql_plugin.rb, line 22
def before
  require 'mysql2'
  if master?
    logger.error "This appears to be a master in a replication set. Refusing to operate."
    return false
  end
  lock_tables
  stop_mysql if options.shutdown == 'yes'
  true
end
client() click to toggle source
# File lib/plugins/mysql_plugin.rb, line 48
def client
  @client ||= Mysql2::Client.new host: options.host, username: options.username, password: options.password, port: options.port
end
default_options() click to toggle source
# File lib/plugins/mysql_plugin.rb, line 12
def default_options
  {
    shutdown: 'no',
    username: 'root',
    password: 'root',
    port: 3306,
    host: 'localhost'
  }
end
defined_options() click to toggle source
# File lib/plugins/mysql_plugin.rb, line 2
def defined_options
  {
    shutdown: 'MySQL will only be shut down when this is set to "yes"',
    username: 'MySQL Username',
    password: 'MySQL Password',
    port: 'MySQL port',
    host: 'MySQL host'
  }
end
name() click to toggle source
# File lib/plugins/mysql_plugin.rb, line 44
def name
  "Mysql"
end

Private Instance Methods

lock_tables() click to toggle source
# File lib/plugins/mysql_plugin.rb, line 61
def lock_tables
  client.query("FLUSH LOCAL TABLES")
  client.query("FLUSH LOCAL TABLES WITH READ LOCK")
end
master?() click to toggle source

> If you see no Binlog Dump threads on a master server, this means that > replication is not running—that is, that no slaves are currently > connected.

dev.mysql.com/doc/refman/5.1/en/master-thread-states.html

# File lib/plugins/mysql_plugin.rb, line 75
def master?
  if @master == nil
    @master = client.query("SHOW PROCESSLIST").to_a.map { |x| x['Command'] }.include? 'Binlog Dump'
  end

  @master
end
slave?() click to toggle source
# File lib/plugins/mysql_plugin.rb, line 54
def slave?
  if @slave == nil
    @slave = logger.debug client.query("SHOW SLAVE STATUS").to_a.any?
  end
  @slave 
end
start_mysql() click to toggle source
# File lib/plugins/mysql_plugin.rb, line 88
def start_mysql
  # TODO
  system("service mysql start")
end
stop_mysql() click to toggle source
# File lib/plugins/mysql_plugin.rb, line 83
def stop_mysql
  # TODO
  system("service mysql stop")
end
unlock_tables() click to toggle source
# File lib/plugins/mysql_plugin.rb, line 66
def unlock_tables
  client.query("UNLOCK TABLES")
end