diff --git a/app/create_db.py b/app/create_db.py index af07b24d..4e3df8ce 100644 --- a/app/create_db.py +++ b/app/create_db.py @@ -988,7 +988,7 @@ def update_db_v_6_1_4(): def update_ver(): - query = Version.update(version='6.1.5.0') + query = Version.update(version='6.2.0.0') try: query.execute() except Exception: diff --git a/app/funct.py b/app/funct.py index 9c12d896..1b10875a 100644 --- a/app/funct.py +++ b/app/funct.py @@ -17,8 +17,8 @@ def is_ip_or_dns(server_from_request: str) -> str: try: if server_from_request in ( 'roxy-wi-checker', 'roxy-wi-keep_alive', 'roxy-wi-keep-alive', 'roxy-wi-metrics', - 'roxy-wi-portscanner', 'roxy-wi-smon', 'roxy-wi-socket', 'fail2ban', 'prometheus', - 'all', 'grafana-server', 'rabbitmq-server' + 'roxy-wi-portscanner', 'roxy-wi-smon', 'roxy-wi-socket', 'roxy-wi-prometheus-exporter', + 'prometheus', 'fail2ban', 'all', 'grafana-server', 'rabbitmq-server' ): return server_from_request if re.match(ip_regex, server_from_request): @@ -378,9 +378,8 @@ def return_ssh_keys_path(server_ip: str, **kwargs): def ssh_connect(server_ip): - import paramiko - from paramiko import SSHClient import sql + from modules import ssh_connection ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(server_ip) servers = sql.select_servers(server=server_ip) @@ -389,37 +388,9 @@ def ssh_connect(server_ip): for server in servers: ssh_port = server[10] - ssh = SSHClient() - ssh.load_system_host_keys() - ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + ssh = ssh_connection.SshConnection(server_ip, ssh_port, ssh_user_name, ssh_user_password, ssh_enable, ssh_key_name) - try: - if ssh_enable == 1: - k = paramiko.pkey.load_private_key_file(ssh_key_name) - ssh.connect(hostname=server_ip, port=ssh_port, username=ssh_user_name, pkey=k, timeout=11, banner_timeout=200) - else: - ssh.connect(hostname=server_ip, port=ssh_port, username=ssh_user_name, password=ssh_user_password, timeout=11, banner_timeout=200) - return ssh - except paramiko.AuthenticationException as e: - logging('localhost', ' ' + str(e), haproxywi=1) - print('error: Authentication failed, please verify your credentials') - except paramiko.SSHException as sshException: - logging('localhost', ' ' + str(sshException), haproxywi=1) - print('error: Unable to establish SSH connection: %s ') % sshException - except paramiko.PasswordRequiredException as e: - logging('localhost', ' ' + str(e), haproxywi=1) - print('error: %s ') % e - except paramiko.BadHostKeyException as badHostKeyException: - logging('localhost', ' ' + str(badHostKeyException), haproxywi=1) - print('error: Unable to verify server\'s host key: %s ') % badHostKeyException - except Exception as e: - logging('localhost', ' ' + str(e), haproxywi=1) - if e == "No such file or directory": - print('error: %s. Check SSH key') % e - elif e == "Invalid argument": - print('error: Check the IP of the server') - else: - print(str(e)) + return ssh def get_config(server_ip, cfg, **kwargs): @@ -441,25 +412,12 @@ def get_config(server_ip, cfg, **kwargs): else: config_path = sql.get_setting('haproxy_config_path') - ssh = ssh_connect(server_ip) - try: - sftp = ssh.open_sftp() - except Exception as e: - logging('localhost', str(e), haproxywi=1) - return try: - sftp.get(config_path, cfg) - except Exception as e: - logging('localhost', str(e), haproxywi=1) - sftp.close() - ssh.close() - return - - try: - sftp.close() - ssh.close() + with ssh_connect(server_ip) as ssh: + ssh.get_sftp(config_path, cfg) except Exception as e: + print('error: cannot get config') logging('localhost', str(e), haproxywi=1) return @@ -906,38 +864,14 @@ def upload(server_ip, path, file, **kwargs): full_path = path try: - ssh = ssh_connect(server_ip) + with ssh_connect(server_ip) as ssh: + ssh.put_sftp(file, full_path) except Exception as e: error = str(e.args) logging('localhost', error, haproxywi=1) print(' Cannot upload ' + file + ' to ' + full_path + ' to server: ' + server_ip + ' error: ' + error) return error - try: - sftp = ssh.open_sftp() - except Exception as e: - error = str(e.args) - logging('localhost', error, haproxywi=1) - print('Cannot upload ' + file + ' to ' + full_path + ' to server: ' + server_ip + ' error: ' + error) - return error - - try: - file = sftp.put(file, full_path) - except Exception as e: - error = str(e.args) - print('Cannot upload ' + file + ' to ' + full_path + ' to server: ' + server_ip + ' error: ' + error) - logging('localhost', ' Cannot upload ' + file + ' to ' + full_path + ' to server: ' + server_ip + ' Error: ' + error, haproxywi=1) - return error - - try: - sftp.close() - ssh.close() - except Exception as e: - error = str(e.args) - logging('localhost', error, haproxywi=1) - print('Cannot upload ' + file + ' to ' + full_path + ' to server: ' + server_ip + ' error: ' + error) - return error - def upload_and_restart(server_ip, cfg, **kwargs): import sql @@ -1214,27 +1148,26 @@ def check_haproxy_config(server_ip): else: commands = ["haproxy -q -c -f %s" % config_path] - ssh = ssh_connect(server_ip) - for command in commands: - stdin, stdout, stderr = ssh.exec_command(command, get_pty=True) - if not stderr.read(): - return True - else: - return False - ssh.close() + with ssh_connect(server_ip) as ssh: + for command in commands: + stdin, stdout, stderr = ssh.run_command(command) + if not stderr.read(): + return True + else: + return False def check_nginx_config(server_ip): import sql commands = ["nginx -q -t -p {}".format(sql.get_setting('nginx_dir'))] - ssh = ssh_connect(server_ip) - for command in commands: - stdin, stdout, stderr = ssh.exec_command(command, get_pty=True) - if not stderr.read(): - return True - else: - return False - ssh.close() + + with ssh_connect(server_ip) as ssh: + for command in commands: + stdin, stdout, stderr = ssh.run_command(command) + if not stderr.read(): + return True + else: + return False def show_log(stdout, **kwargs): @@ -1432,41 +1365,37 @@ def server_status(stdout): def ssh_command(server_ip, commands, **kwargs): - ssh = ssh_connect(server_ip) + with ssh_connect(server_ip) as ssh: + for command in commands: + try: + stdin, stdout, stderr = ssh.run_command(command) + except Exception as e: + logging('localhost', ' ' + str(e), haproxywi=1) + return str(e) - for command in commands: - try: - stdin, stdout, stderr = ssh.exec_command(command, get_pty=True) - except Exception as e: - logging('localhost', ' ' + str(e), haproxywi=1) - ssh.close() - return str(e) + if kwargs.get('raw'): + return stdout + try: + if kwargs.get("ip") == "1": + show_ip(stdout) + elif kwargs.get("show_log") == "1": + return show_log(stdout, grep=kwargs.get("grep")) + elif kwargs.get("server_status") == "1": + server_status(stdout) + elif kwargs.get('print_out'): + print(stdout.read().decode(encoding='UTF-8')) + return stdout.read().decode(encoding='UTF-8') + elif kwargs.get('return_err') == 1: + return stderr.read().decode(encoding='UTF-8') + else: + return stdout.read().decode(encoding='UTF-8') + except Exception as e: + logging('localhost', str(e), haproxywi=1) - if kwargs.get('raw'): - return stdout - try: - if kwargs.get("ip") == "1": - show_ip(stdout) - elif kwargs.get("show_log") == "1": - return show_log(stdout, grep=kwargs.get("grep")) - elif kwargs.get("server_status") == "1": - server_status(stdout) - elif kwargs.get('print_out'): - print(stdout.read().decode(encoding='UTF-8')) - return stdout.read().decode(encoding='UTF-8') - elif kwargs.get('return_err') == 1: - return stderr.read().decode(encoding='UTF-8') - else: - return stdout.read().decode(encoding='UTF-8') - except Exception as e: - logging('localhost', str(e), haproxywi=1) - finally: - ssh.close() - - for line in stderr.read().decode(encoding='UTF-8'): - if line: - print("