1#!/usr/local/bin/python3.8 2 3from __future__ import (absolute_import, division, print_function) 4__metaclass__ = type 5 6import logging as logg 7import os 8import sys 9import time 10import getopt 11 12from configparser import ConfigParser 13from six.moves import input 14 15import fail_back 16import fail_over 17import generate_vars 18import validator 19 20VALIDATE = 'validate' 21GENERATE = 'generate' 22FAILOVER = 'failover' 23FAILBACK = 'failback' 24LOG_FILE = 'log-file' 25LOG_LEVEL = 'log-level' 26DEF_LOG_FILE = "" 27DEF_DEBUG_LEVEL = 'DEBUG' 28DEF_CONF_FILE = 'dr.conf' 29 30 31def main(argv): 32 action, conf_file, log_file, log_level = _init_vars(argv) 33 while not os.path.isfile(conf_file): 34 conf_file = input( 35 "Conf file '" + conf_file + "' does not exist." 36 " Please provide the configuration file location: ") 37 38 if action != 'validate': 39 log_file = log_file.format(int(round(time.time() * 1000))) 40 if log_level not in ['DEBUG', 'INFO', 'WARNING', 'ERROR']: 41 print("ovirt-dr: log level must be 'DEBUG' 'INFO' 'WARNING' 'ERROR'\n" 42 "Use 'ovirt-dr --help' for more information.") 43 sys.exit(2) 44 45 create_log_dir(log_file) 46 _print_log_file_name(log_file) 47 if action == 'validate': 48 validator.ValidateMappingFile().run(conf_file) 49 elif action == 'generate': 50 generate_vars.GenerateMappingFile().run(conf_file, 51 log_file, 52 logg.getLevelName(log_level)) 53 _print_log_file_name(log_file) 54 elif action == 'failover': 55 fail_over.FailOver().run(conf_file, 56 log_file, 57 logg.getLevelName(log_level)) 58 _print_log_file_name(log_file) 59 elif action == 'failback': 60 fail_back.FailBack().run(conf_file, 61 log_file, 62 logg.getLevelName(log_level)) 63 _print_log_file_name(log_file) 64 elif action == '--help': 65 help_log() 66 else: 67 print("\tError: action '%s' is not defined" % action) 68 help_log() 69 70 71def _print_log_file_name(log_file): 72 if log_file is not None and log_file != '': 73 print("Log file: '%s'" % log_file) 74 75 76def _init_vars(argv): 77 conf_file = DEF_CONF_FILE 78 log_file = '' 79 log_level = '' 80 81 if len(argv) == 0: 82 print("ovirt-dr: missing action operand\n" 83 "Use 'ovirt-dr --help' for more information.") 84 sys.exit(2) 85 action = argv[0] 86 87 try: 88 opts, args = \ 89 getopt.getopt(argv[1:], "f:log:level:", 90 ["conf-file=", "log-file=", "log-level="]) 91 except getopt.GetoptError: 92 help_log() 93 sys.exit(2) 94 95 for opt, arg in opts: 96 if opt in ("-f", "--conf-file"): 97 conf_file = arg 98 if opt in ("-log", "--log-file"): 99 log_file = arg 100 if opt in ("-level", "--log-level"): 101 log_level = arg 102 103 log_file, log_level = _get_log_conf(conf_file, log_file, log_level) 104 return action, conf_file, log_file, log_level.upper() 105 106 107def _get_log_conf(conf_file, log_file, log_level): 108 log_section = "log" 109 log_file_conf = "log_file" 110 log_level_conf = "log_level" 111 while not os.path.isfile(conf_file): 112 conf_file = input( 113 "Conf file '" + conf_file + "' does not exist." 114 " Please provide the configuration file location: ") 115 settings = ConfigParser() 116 settings.read(conf_file) 117 if log_section not in settings.sections(): 118 settings.add_section(log_section) 119 if settings.has_option(log_section, log_file_conf) and \ 120 (log_file is None or log_file == ''): 121 log_file = settings.get(log_section, log_file_conf) 122 if settings.has_option(log_section, log_level_conf) and \ 123 (log_level is None or log_level == ''): 124 log_level = settings.get(log_section, log_level_conf) 125 else: 126 log_level = "DEBUG" 127 return log_file, log_level 128 129 130def create_log_dir(fname): 131 _dir = os.path.dirname(fname) 132 if _dir != '' and not os.path.exists(_dir): 133 os.makedirs(_dir) 134 135 136def help_log(): 137 print( 138 ''' 139 \tusage: ovirt-dr <%s/%s/%s/%s> 140 [--conf-file=dr.conf] 141 [--log-file=log_file.log] 142 [--log-level=DEBUG/INFO/WARNING/ERROR]\n 143 \tHere is a description of the following actions:\n 144 \t\t%s\tGenerate the mapping var file based on primary setup 145 \t\t%s\tValidate the var file mapping 146 \t\t%s\tStart a failover process to the target setup 147 \t\t%s\tStart a failback process to the source setup 148 ''' % (GENERATE, 149 VALIDATE, 150 FAILOVER, 151 FAILBACK, 152 GENERATE, 153 VALIDATE, 154 FAILOVER, 155 FAILBACK)) 156 157 158if __name__ == "__main__": 159 main(sys.argv[1:]) 160