def run_program(args_array, func_dict, ord_prec_list, **kwargs): """Function: run_program Description: Creates class instance(s) and controls flow of the program. Arguments: (input) args_array -> Dict of command line options and values. (input) func_dict -> Dictionary list of functions and options. (input) ord_prec_list -> Order of precedence of the arguments. """ args_array = dict(args_array) func_dict = dict(func_dict) ord_prec_list = list(ord_prec_list) server = mysql_libs.create_instance(args_array["-c"], args_array["-d"], mysql_class.Server) server.connect() # Execute functions based on order of precedence. for item in ord_prec_list: if item in args_array: func_dict[item](args_array, server) cmds_gen.disconnect([server])
def run_program(args_array, func_dict, **kwargs): """Function: run_program Description: Creates class instance(s) and controls flow of the program. Arguments: (input) args_array -> Dict of command line options and values. (input) func_dict -> Dictionary list of functions and options. """ mongo = None args_array = dict(args_array) func_dict = dict(func_dict) server = mysql_libs.create_instance(args_array["-c"], args_array["-d"], mysql_class.Server) server.connect(silent=True) if server.conn_msg and not args_array.get("-w", False): print("run_program: Error encountered on server(%s): %s" % (server.name, server.conn_msg)) elif not server.conn_msg: if args_array.get("-m", False): mongo = gen_libs.load_module(args_array["-m"], args_array["-d"]) # Call function(s) - intersection of command line and function dict. for opt in set(args_array.keys()) & set(func_dict.keys()): func_dict[opt](server, args_array, class_cfg=mongo, **kwargs) mysql_libs.disconnect(server)
def run_program(args_array, func_dict, opt_arg_list, **kwargs): """Function: run_program Description: Creates class instance(s) and controls flow of the program. Arguments: (input) args_array -> Array of command line options and values. (input) func_dict -> Dictionary list of functions and options. (input) opt_arg_list -> Arguments to be added to command line. """ args_array = dict(args_array) func_dict = dict(func_dict) opt_arg_list = list(opt_arg_list) server = mysql_libs.create_instance(args_array["-c"], args_array["-d"], mysql_class.Server) server.connect(silent=True) if not server.conn_msg: server.set_srv_binlog_crc() # Call function(s) - intersection of command line and function dict. for item in set(args_array.keys()) & set(func_dict.keys()): # Call the function requested. func_dict[item](server, args_array, opt_arg_list) cmds_gen.disconnect(server) else: print("run_program: Error encountered on master(%s): %s" % (server.name, server.conn_msg))
def run_program(args_array, opt_arg_list, opt_dump_list, **kwargs): """Function: run_program Description: Creates class instance(s) and controls flow of the program. Arguments: (input) args_array -> Array of command line options and values. (input) opt_arg_list -> List of commands to add to cmd line. (input) opt_dump_list -> Dictionary of additional options. """ args_array = dict(args_array) opt_dump_list = dict(opt_dump_list) opt_arg_list = list(opt_arg_list) mail = None server = mysql_libs.create_instance(args_array["-c"], args_array["-d"], mysql_class.Server) server.connect(silent=True) if server.conn_msg: print("run_program: Error encountered on server(%s): %s" % (server.name, server.conn_msg)) else: server.set_srv_gtid() dump_cmd = crt_dump_cmd(server, args_array, opt_arg_list, opt_dump_list) db_list = set_db_list(server, args_array, **kwargs) # Remove the -r option if database is not GTID enabled. if "-r" in args_array and not server.gtid_mode \ and opt_dump_list["-r"] in dump_cmd: dump_cmd.remove(opt_dump_list["-r"]) compress = args_array.get("-z", False) dmp_path = None if "-o" in args_array: dmp_path = args_array["-o"] + "/" if args_array.get("-e", False): dtg = datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d_%H%M%S") subj = args_array.get("-t", [server.name, ": mysql_db_dump: ", dtg]) mail = gen_class.setup_mail(args_array.get("-e"), subj=subj) err_sup = args_array.get("-w", False) dump_db(dump_cmd, db_list, compress, dmp_path, err_sup=err_sup, mail=mail, use_mailx=args_array.get("-u", False)) mysql_libs.disconnect(server)
def load_log(server, args_array, opt_arg_list, **kwargs): """Function: load_log Description: Get the binary logs from the source database, then fetch the revelant binary log entries and load them into the target database before closing all connections. Arguments: (input) server -> Server instance. (input) args_array -> Array of command line options and values. (input) opt_arg_list -> Arguments to be added to command line. """ subp = gen_libs.get_inst(subprocess) args_array = dict(args_array) opt_arg_list = list(opt_arg_list) status, binlog_list = process_logs_list(server, args_array) if status[0]: target = mysql_libs.create_instance(args_array["-e"], args_array["-d"], mysql_class.Server) target.connect(silent=True) if not target.conn_msg: cmd = mysql_libs.crt_cmd( target, arg_parser.arg_set_path(args_array, "-p") + "mysql") # Fetch binary logs and restore to target database proc1 = fetch_binlog(server, args_array.get("-s"), args_array.get("-t"), binlog_list, opt_arg_list, arg_parser.arg_set_path(args_array, "-p")) proc2 = subp.Popen(cmd, stdin=proc1) proc2.wait() cmds_gen.disconnect(target) else: print("load_log: Error encountered on slave(%s): %s" % (target.name, target.conn_msg)) else: print("load_log: Error encountered in process_logs_list: %s" % (status[1]))
def run_program(args_array, func_dict, **kwargs): """Function: run_program Description: Creates class instance(s) and controls flow of the program. Arguments: (input) args_array -> Dictionary of command line options. (input) func_dict -> Dictionary of functions. (input) **kwargs: sys_dbs -> List of system databases. multi_val -> List of options that may have multiple values. """ args_array = dict(args_array) func_dict = dict(func_dict) server = mysql_libs.create_instance(args_array["-c"], args_array["-d"], mysql_class.Server) server.connect(silent=True) if server.conn_msg: print("run_program: Error encountered on server(%s): %s" % (server.name, server.conn_msg)) else: outfile = args_array.get("-o", None) db_tbl = args_array.get("-i", None) mongo = None mail = None if args_array.get("-m", None): mongo = gen_libs.load_module(args_array["-m"], args_array["-d"]) if args_array.get("-e", None): mail = gen_class.setup_mail(args_array.get("-e"), subj=args_array.get("-s", None)) # Intersect args_array & func_dict to determine which functions to call for item in set(args_array.keys()) & set(func_dict.keys()): func_dict[item](server, args_array, ofile=outfile, db_tbl=db_tbl, class_cfg=mongo, mail=mail, **kwargs) mysql_libs.disconnect(server)
def chk_rep(clone, args_array, **kwargs): """Function: chk_rep Description: Create master and slave instances and check the status of the replication system between the two servers. Arguments: (input) clone -> Destination server instance. (input) args_array -> Array of command line options and values. """ args_array = dict(args_array) if "-n" not in args_array: cfg = gen_libs.load_module(args_array["-c"], args_array["-d"]) master = mysql_class.MasterRep(cfg.name, cfg.sid, cfg.user, cfg.japd, os_type=getattr(machine, cfg.serv_os)(), host=cfg.host, port=cfg.port, defaults_file=cfg.cfg_file, extra_def_file=cfg.__dict__.get( "extra_def_file", None), rep_user=cfg.rep_user, rep_japd=cfg.rep_japd) master.connect() mysql_libs.change_master_to(master, clone) slave = mysql_libs.create_instance(args_array["-t"], args_array["-d"], mysql_class.SlaveRep) slave.connect() slave.start_slave() # Waiting for slave to start. time.sleep(5) master.upd_mst_status() slave.upd_slv_status() chk_slv_err([slave]) chk_slv_thr([slave]) chk_mst_log(master, [slave]) cmds_gen.disconnect(master, slave)
def run_program(args_array, req_rep_cfg, opt_arg_list, **kwargs): """Function: run_program Description: Creates class instance(s) and controls flow of the program. Also determines whether the cloning operation is for replication or as a stand-along server. Arguments: (input) args_array -> Dict of command line options and values. (input) req_rep_cfg -> Required replication config settings. (input) opt_arg_list -> List of options to add to dump cmd line. (input) **kwargs: opt_dump_list -> Dictionary of additional options. """ args_array = dict(args_array) req_rep_cfg = dict(req_rep_cfg) opt_arg_list = list(opt_arg_list) source = mysql_libs.create_instance(args_array["-c"], args_array["-d"], mysql_class.Server) clone = mysql_libs.create_instance(args_array["-t"], args_array["-d"], mysql_class.Server) source.connect() source.set_srv_gtid() clone.connect() clone.set_srv_gtid() status, status_msg = mysql_libs.is_cfg_valid([source, clone]) # Master cannot be set to loopback IP if setting up replication. if source.host in ["127.0." + "0.1", "localhost"] \ and "-n" not in args_array: status = False status_msg.append("Master host entry has incorrect entry.") status_msg.append("Master host: %s" % (source.host)) if status: # Do not proceed if GTID modes don't match and rep is being configured. if source.gtid_mode != clone.gtid_mode and "-n" not in args_array: cmds_gen.disconnect(source, clone) print( "Error: Source (%s) and Clone (%s) GTID modes do not match." % (source.gtid_mode, clone.gtid_mode)) else: stop_clr_rep(clone, args_array) # Add to argument list array based on rep config. opt_arg_list = chk_rep_cfg(source, clone, args_array, req_rep_cfg, opt_arg_list) # If empty list, then failure in requirements check. if opt_arg_list: print("Starting dump-load process...") dump_load_dbs(source, clone, args_array, req_rep_cfg, opt_arg_list, **kwargs) print("Finished dump-load process...") chk_rep(clone, args_array) cmds_gen.disconnect(source, clone) else: cmds_gen.disconnect(source, clone) print("Error: Detected problem in the configuration file.") for msg in status_msg: print(msg)
def run_program(args_array, sys_ign_db, **kwargs): """Function: run_program Description: Creates class instance(s) and controls flow of the program. Arguments: (input) args_array -> Array of command line options and values. (input) sys_ign_db -> List of system databases to ignore. """ global SUBJ_LINE args_array = dict(args_array) sys_ign_db = list(sys_ign_db) mail = None use_mailx = False no_std = args_array.get("-z", False) master = mysql_libs.create_instance(args_array["-c"], args_array["-d"], mysql_class.MasterRep) master.connect(silent=True) slave = mysql_libs.create_instance(args_array["-r"], args_array["-d"], mysql_class.SlaveRep) slave.connect(silent=True) if master.conn_msg or slave.conn_msg: print("run_program: Error encountered with connection of master/slave") print("\tMaster: %s" % (master.conn_msg)) print("\tSlave: %s" % (slave.conn_msg)) else: if args_array.get("-e", None): mail = gen_class.setup_mail(args_array.get("-e"), subj=args_array.get("-s", SUBJ_LINE)) use_mailx = args_array.get("-u", False) # Determine datatype of server_id and convert appropriately. # Required for mysql.connector v1.1.6 as this version assigns the # id to a different datatype then later mysql.connector versions. slv_list = gen_libs.dict_2_list(master.show_slv_hosts(), "Server_id") slv_id = str(slave.server_id) \ if isinstance(slv_list[0], str) else slave.server_id # Is slave in replication with master if slv_id in slv_list: # Check specified tables in database if "-t" in args_array: setup_cmp(master, slave, sys_ign_db, args_array["-B"], args_array["-t"], mail=mail, no_std=no_std, use_mailx=use_mailx, **kwargs) # Check single database elif "-B" in args_array: setup_cmp(master, slave, sys_ign_db, args_array["-B"], "", mail=mail, no_std=no_std, use_mailx=use_mailx, **kwargs) # Check all tables in all databases else: setup_cmp(master, slave, sys_ign_db, "", "", mail=mail, no_std=no_std, use_mailx=use_mailx, **kwargs) mysql_libs.disconnect(master, slave) else: mysql_libs.disconnect(master, slave) print("Error: Replica is not in replication with Master.")