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



911
912
913
914
915
916
917
# File 'lib/tungsten/script.rb', line 911

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



903
904
905
# File 'lib/tungsten/script.rb', line 903

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



976
977
978
979
980
981
982
983
984
# File 'lib/tungsten/script.rb', line 976

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



941
942
943
944
945
946
947
948
949
950
951
# File 'lib/tungsten/script.rb', line 941

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



953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
# File 'lib/tungsten/script.rb', line 953

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



987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
# File 'lib/tungsten/script.rb', line 987

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



907
908
909
# File 'lib/tungsten/script.rb', line 907

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



919
920
921
922
923
924
925
926
927
928
929
# File 'lib/tungsten/script.rb', line 919

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)


860
861
862
# File 'lib/tungsten/script.rb', line 860

def require_local_mysql_service?
  false
end

#set_mysql_defaults_value(value) ⇒ Object

Store additional MySQL configuration values in a temporary file



1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
# File 'lib/tungsten/script.rb', line 1007

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



1019
1020
1021
1022
# File 'lib/tungsten/script.rb', line 1019

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



1026
1027
1028
1029
# File 'lib/tungsten/script.rb', line 1026

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

#validateObject



864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
# File 'lib/tungsten/script.rb', line 864

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



931
932
933
934
935
936
937
938
939
# File 'lib/tungsten/script.rb', line 931

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