Module: MySQLServiceScript

Includes:
SingleServiceScript
Defined in:
lib/tungsten/script.rb

Overview

Group all MySQL validation and methods into a single module

Instance Method Summary collapse

Methods included from SingleServiceScript

#configure

Instance Method Details

#get_innobackupex_pathObject



951
952
953
954
955
956
957
# File 'lib/tungsten/script.rb', line 951

def get_innobackupex_path()
  path = TU.which("innobackupex-1.5.1")
  if path.nil?
    path = TU.which("innobackupex")
  end
  return path
end

#get_mysql_commandObject



943
944
945
# File 'lib/tungsten/script.rb', line 943

def get_mysql_command
  "mysql --defaults-file=#{@options[:my_cnf]} -h#{@options[:mysqlhost]} --port=#{@options[:mysqlport]}"
end

#get_mysql_option(opt) ⇒ Object

Read the configured value for a mysql variable



1016
1017
1018
1019
1020
1021
1022
1023
1024
# File 'lib/tungsten/script.rb', line 1016

def get_mysql_option(opt)
  begin
    val = TU.cmd_result("my_print_defaults --config-file=#{@options[:my_cnf]} mysqld | grep -e'^--#{opt.gsub(/[\-\_]/, "[-_]")}='")
  rescue CommandError => ce
    return nil
  end

  return val.split("\n")[0].split("=")[1]
end

#get_mysql_result(command, timeout = 30) ⇒ Object



981
982
983
984
985
986
987
988
989
990
991
# File 'lib/tungsten/script.rb', line 981

def get_mysql_result(command, timeout = 30)
  begin      
    Timeout.timeout(timeout.to_i()) {
      return TU.cmd_result("#{get_mysql_command()} -e \"#{command}\"")
    }
  rescue Timeout::Error
  rescue => e
  end
  
  return nil
end

#get_mysql_value(command, column = nil) ⇒ Object



993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
# File 'lib/tungsten/script.rb', line 993

def get_mysql_value(command, column = nil)
  response = get_mysql_result(command + "\\\\G")
  if response == nil
    return nil
  end
  
  response.split("\n").each{ | response_line |
    parts = response_line.chomp.split(":")
    if (parts.length != 2)
      next
    end
    parts[0] = parts[0].strip;
    parts[1] = parts[1].strip;
    
    if parts[0] == column || column == nil
      return parts[1]
    end
  }
  
  return nil
end

#get_mysql_variable(var) ⇒ Object

Read the current value for a mysql variable



1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
# File 'lib/tungsten/script.rb', line 1027

def get_mysql_variable(var)
  response = TU.cmd_result("#{get_mysql_command()} -e \"SHOW VARIABLES LIKE '#{var}'\\\\G\"")
  
  response.split("\n").each{ | response_line |
    parts = response_line.chomp.split(":")
    if (parts.length != 2)
      next
    end
    parts[0] = parts[0].strip;
    parts[1] = parts[1].strip;
    
    if parts[0] == "Value"
      return parts[1]
    end
  }
  
  return nil
end

#get_mysqldump_commandObject



947
948
949
# File 'lib/tungsten/script.rb', line 947

def get_mysqldump_command
  "mysqldump --defaults-file=#{@options[:my_cnf]} --host=#{@options[:mysqlhost]} --port=#{@options[:mysqlport]} --opt --single-transaction --all-databases --add-drop-database --master-data=2"
end

#get_xtrabackup_commandObject



959
960
961
962
963
964
965
966
967
968
969
# File 'lib/tungsten/script.rb', line 959

def get_xtrabackup_command
  # Use the configured my.cnf file, or the additional config file 
  # if we created one
  if @options[:extra_mysql_defaults_file] == nil
    defaults_file = @options[:my_cnf]
  else
    defaults_file = @options[:extra_mysql_defaults_file].path()
  end
  
  "#{get_innobackupex_path()} --defaults-file=#{defaults_file} --host=#{@options[:mysqlhost]} --port=#{@options[:mysqlport]}"
end

#require_local_mysql_service?Boolean

Allow scripts to turn off MySQL validation of the local server

Returns:

  • (Boolean)


900
901
902
# File 'lib/tungsten/script.rb', line 900

def require_local_mysql_service?
  false
end

#set_mysql_defaults_value(value) ⇒ Object

Store additional MySQL configuration values in a temporary file



1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
# File 'lib/tungsten/script.rb', line 1047

def set_mysql_defaults_value(value)
  if @options[:extra_mysql_defaults_file] == nil
    @options[:extra_mysql_defaults_file] = Tempfile.new("xtracfg")
    @options[:extra_mysql_defaults_file].puts("!include #{@options[:my_cnf]}")
    @options[:extra_mysql_defaults_file].puts("")
    @options[:extra_mysql_defaults_file].puts("[mysqld]")
  end
  
  @options[:extra_mysql_defaults_file].puts(value)
  @options[:extra_mysql_defaults_file].flush()
end

#start_mysql_serverObject



1059
1060
1061
1062
# File 'lib/tungsten/script.rb', line 1059

def start_mysql_server
  ds = TI.datasource(@options[:service])
  ds.start()
end

#stop_mysql_serverObject

Make sure that the mysql server is stopped by stopping it and checking the process has disappeared



1066
1067
1068
1069
# File 'lib/tungsten/script.rb', line 1066

def stop_mysql_server
  ds = TI.datasource(@options[:service])
  ds.stop()
end

#validateObject



904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
# File 'lib/tungsten/script.rb', line 904

def validate
  super()
  
  if @options[:service].to_s() == ""
    return
  end
  
  unless TI.replication_services().include?(@options[:service])
    return
  end
  
  if @options[:mysqlhost] == nil
    @options[:mysqlhost] = TI.setting(TI.setting_key(REPL_SERVICES, @options[:service], "repl_datasource_host"))
  end
  if @options[:mysqlport] == nil
    @options[:mysqlport] = TI.setting(TI.setting_key(REPL_SERVICES, @options[:service], "repl_datasource_port"))
  end
  
  if @options[:my_cnf] == nil
    @options[:my_cnf] = TI.setting(TI.setting_key(REPL_SERVICES, @options[:service], "repl_datasource_mysql_service_conf"))
  end
  if @options[:my_cnf] == nil
    TU.error "Unable to determine location of MySQL my.cnf file"
  else
    unless File.exist?(@options[:my_cnf])
      TU.error "The file #{@options[:my_cnf]} does not exist"
    end
  end
  
  if require_local_mysql_service?()
    if @options[:mysqluser] == nil
      @options[:mysqluser] = get_mysql_option("user")
    end
    if @options[:mysqluser].to_s() == ""
      @options[:mysqluser] = "mysql"
    end
  end
end

#xtrabackup_supports_argument(arg) ⇒ Object



971
972
973
974
975
976
977
978
979
# File 'lib/tungsten/script.rb', line 971

def xtrabackup_supports_argument(arg)
  arg = arg.tr("-", "\\-")
  supports_argument = TU.cmd_result("#{get_xtrabackup_command()} --help /tmp | grep -e\"#{arg}\" | wc -l")
  if supports_argument == "1"
    return true
  else
    return false
  end
end