def load_parser(client, parser): """ Loading parser and checking database configuration :param client: :param parser: :return: """ _path = system.config[client]['path'] output.message( output.host_to_subject(client), f'Checking database configuration {output.CliFormat.BLACK}{_path}{output.CliFormat.ENDC}', True ) if client == mode.Client.ORIGIN: if mode.is_origin_remote(): remote_client.load_ssh_client_origin() else: helper.run_script(client, 'before') else: if mode.is_target_remote(): remote_client.load_ssh_client_target() else: helper.run_script(client, 'before') # Check only if database configuration is a file if not helper.check_file_exists(client, _path) and _path[-1] != '/': sys.exit( output.message( output.Subject.ERROR, f'Database configuration for {client} not found: {_path}', False ) ) parser.check_configuration(client)
def load_ssh_client_target(): """ Loading the target ssh client :return: """ global ssh_client_target ssh_client_target = load_ssh_client(mode.Client.TARGET) helper.run_script(mode.Client.TARGET, 'before')
def load_ssh_client_origin(): """ Loading the origin ssh client :return: """ global ssh_client_origin ssh_client_origin = load_ssh_client(mode.Client.ORIGIN) helper.run_script(mode.Client.ORIGIN, 'before')
def run_ssh_command(command, ssh_client=remote_client.ssh_client_origin, client=None): """ Running ssh command :param command: String :param ssh_client: :param client: String :return: """ stdin, stdout, stderr = ssh_client.exec_command(command) exit_status = stdout.channel.recv_exit_status() err = stderr.read().decode() if err and exit_status != 0: helper.run_script(client=client, script='error') sys.exit(output.message(output.Subject.ERROR, err, False)) elif err: output.message(output.Subject.WARNING, err, True) return stdout
def run_command(command, client, force_output=False, allow_fail=False, skip_dry_run=False): """ Run command depending on the given client :param command: String :param client: String :param force_output: Boolean :param allow_fail: Boolean :param skip_dry_run: Boolean :return: """ if system.config['verbose']: output.message( output.host_to_subject(client), output.CliFormat.BLACK + command + output.CliFormat.ENDC, debug=True ) if system.config['dry_run'] and skip_dry_run: return if is_remote(client): if force_output: return ''.join(remote_system.run_ssh_command_by_client(client, command).readlines()).strip() else: return remote_system.run_ssh_command_by_client(client, command) else: res = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) # Wait for the process end and print error in case of failure out, err = res.communicate() if res.wait() != 0 and err.decode() != '' and not allow_fail: helper.run_script(script='error') sys.exit(output.message(output.Subject.ERROR, err.decode(), False)) if force_output: return out.decode().strip()
def close_ssh_clients(): """ Closing ssh client sessions :return: """ helper.run_script(mode.Client.ORIGIN, 'after') if not ssh_client_origin is None: ssh_client_origin.close() helper.run_script(mode.Client.TARGET, 'after') if not ssh_client_target is None: ssh_client_target.close() for additional_ssh_client in additional_ssh_clients: additional_ssh_client.close() helper.run_script(script='after')
def get_configuration(host_config, args = {}): """ Checking configuration information by file or dictionary :param host_config: Dictionary :param args: Dictionary :return: """ global config config[mode.Client.TARGET] = {} config[mode.Client.ORIGIN] = {} if host_config: if type(host_config) is dict: config.update(__m=host_config) else: config.update(__m=json.dumps(obj=host_config)) _config_file_path = config['config_file_path'] if not _config_file_path is None: if os.path.isfile(_config_file_path): with open(_config_file_path, 'r') as read_file: if _config_file_path.endswith('.json'): config.update(json.load(read_file)) elif _config_file_path.endswith('.yaml') or _config_file_path.endswith('.yml'): config.update(yaml.safe_load(read_file)) else: sys.exit( output.message( output.Subject.ERROR, f'Unsupported configuration file type [json,yml,yaml]: ' f'{config["config_file_path"]}', False ) ) output.message( output.Subject.LOCAL, f'Loading host configuration ' f'{output.CliFormat.BLACK}{_config_file_path}{output.CliFormat.ENDC}', True ) else: sys.exit( output.message( output.Subject.ERROR, f'Local configuration not found: {config["config_file_path"]}', False ) ) args_config = build_config(args) validation.check(config) check_options() if not config[mode.Client.TARGET] and not config[mode.Client.ORIGIN]: sys.exit( output.message( output.Subject.ERROR, f'Configuration is missing, use a separate file or provide host parameter', False ) ) helper.run_script(script='before') log.get_logger().info('Starting db_sync_tool')