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
- #get_innobackupex_path ⇒ Object
- #get_mysql_command ⇒ Object
-
#get_mysql_option(opt) ⇒ Object
Read the configured value for a mysql variable.
- #get_mysql_result(command, timeout = 30) ⇒ Object
- #get_mysql_value(command, column = nil) ⇒ Object
-
#get_mysql_variable(var) ⇒ Object
Read the current value for a mysql variable.
- #get_mysqldump_command ⇒ Object
- #get_xtrabackup_command ⇒ Object
-
#require_local_mysql_service? ⇒ Boolean
Allow scripts to turn off MySQL validation of the local server.
-
#set_mysql_defaults_value(value) ⇒ Object
Store additional MySQL configuration values in a temporary file.
- #start_mysql_server ⇒ Object
-
#stop_mysql_server ⇒ Object
Make sure that the mysql server is stopped by stopping it and checking the process has disappeared.
- #validate ⇒ Object
- #xtrabackup_supports_argument(arg) ⇒ Object
Methods included from SingleServiceScript
Instance Method Details
#get_innobackupex_path ⇒ Object
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_command ⇒ Object
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_command ⇒ Object
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_command ⇒ Object
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 [:extra_mysql_defaults_file] == nil defaults_file = [:my_cnf] else defaults_file = [: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
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 [:extra_mysql_defaults_file] == nil [:extra_mysql_defaults_file] = Tempfile.new("xtracfg") [:extra_mysql_defaults_file].puts("!include #{@options[:my_cnf]}") [:extra_mysql_defaults_file].puts("") [:extra_mysql_defaults_file].puts("[mysqld]") end [:extra_mysql_defaults_file].puts(value) [:extra_mysql_defaults_file].flush() end |
#start_mysql_server ⇒ Object
1059 1060 1061 1062 |
# File 'lib/tungsten/script.rb', line 1059 def start_mysql_server ds = TI.datasource([:service]) ds.start() end |
#stop_mysql_server ⇒ Object
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([:service]) ds.stop() end |
#validate ⇒ Object
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 [:service].to_s() == "" return end unless TI.replication_services().include?([:service]) return end if [:mysqlhost] == nil [:mysqlhost] = TI.setting(TI.setting_key(REPL_SERVICES, [:service], "repl_datasource_host")) end if [:mysqlport] == nil [:mysqlport] = TI.setting(TI.setting_key(REPL_SERVICES, [:service], "repl_datasource_port")) end if [:my_cnf] == nil [:my_cnf] = TI.setting(TI.setting_key(REPL_SERVICES, [:service], "repl_datasource_mysql_service_conf")) end if [:my_cnf] == nil TU.error "Unable to determine location of MySQL my.cnf file" else unless File.exist?([:my_cnf]) TU.error "The file #{@options[:my_cnf]} does not exist" end end if require_local_mysql_service?() if [:mysqluser] == nil [:mysqluser] = get_mysql_option("user") end if [:mysqluser].to_s() == "" [: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 |