github.com/platonnetwork/platon-go@v0.7.6/cases/environment/server.py (about) 1 import configparser 2 import os 3 4 from common.connect import run_ssh, connect_linux 5 from environment.config import TestConfig 6 from common.log import log 7 from environment.mock import mock_connect_linux 8 9 10 class Server: 11 def __init__(self, server_conf, cfg: TestConfig): 12 self.cfg = cfg 13 self.host = server_conf["host"] 14 self.username = server_conf["username"] 15 self.password = server_conf["password"] 16 self.ssh_port = server_conf.get("sshport", 22) 17 if self.cfg.can_deploy: 18 self.ssh, self.sftp, self.t = connect_linux(self.host, self.username, self.password, self.ssh_port) 19 else: 20 self.ssh, self.sftp, self.t = mock_connect_linux() 21 self.remote_supervisor_conf = "{}/supervisord.conf".format(self.cfg.remote_supervisor_tmp) 22 23 def run_ssh(self, cmd, need_password=False): 24 if need_password: 25 return run_ssh(self.ssh, cmd, self.password) 26 return run_ssh(self.ssh, cmd) 27 28 def put_compression(self): 29 try: 30 ls = self.run_ssh("cd {};ls".format(self.cfg.remote_compression_tmp_path)) 31 gz_name = self.cfg.env_id + ".tar.gz" 32 local_gz = os.path.join(self.cfg.env_tmp, gz_name) 33 if (gz_name + "\n") in ls: 34 return True, "need not upload" 35 self.run_ssh("rm -rf {};mkdir -p {}".format(self.cfg.remote_compression_tmp_path, 36 self.cfg.remote_compression_tmp_path)) 37 self.sftp.put(local_gz, self.cfg.remote_compression_tmp_path + "/" + os.path.basename(local_gz)) 38 self.run_ssh("tar -zxvf {}/{}.tar.gz -C {}".format(self.cfg.remote_compression_tmp_path, self.cfg.env_id, 39 self.cfg.remote_compression_tmp_path)) 40 except Exception as e: 41 return False, "{}-upload compression failed:{}".format(self.host, e) 42 return True, "upload compression success" 43 44 def install_dependency(self): 45 try: 46 self.run_ssh("sudo -S -p '' ntpdate 0.centos.pool.ntp.org", True) 47 self.run_ssh("sudo -S -p '' apt install llvm g++ libgmp-dev libssl-dev -y", True) 48 except Exception as e: 49 return False, "{}-install dependency failed:{}".format(self.host, e) 50 return True, "install dependency success" 51 52 def install_supervisor(self): 53 try: 54 test_name = "test-node" 55 result = self.run_ssh("sudo -S -p '' supervisorctl stop {}".format(test_name), True) 56 if len(result) == 0 or test_name not in result[0]: 57 tmp_dir = os.path.join(self.cfg.server_tmp, self.host) 58 if not os.path.exists(tmp_dir): 59 os.makedirs(tmp_dir) 60 tmp = os.path.join(tmp_dir, "supervisord.conf") 61 self.__rewrite_supervisor_conf(tmp) 62 self.run_ssh("mkdir -p {}".format(self.cfg.remote_supervisor_tmp)) 63 self.sftp.put(tmp, self.remote_supervisor_conf) 64 supervisor_pid_str = self.run_ssh("ps -ef|grep supervisord|grep -v grep|awk {'print $2'}") 65 if len(supervisor_pid_str) > 0: 66 self.__reload_supervisor(supervisor_pid_str) 67 else: 68 self.run_ssh("sudo -S -p '' apt update", True) 69 self.run_ssh("sudo -S -p '' apt install -y supervisor", True) 70 self.run_ssh("sudo -S -p '' cp {} /etc/supervisor/".format(self.remote_supervisor_conf), True) 71 supervisor_pid_str = self.run_ssh("ps -ef|grep supervisord|grep -v grep|awk {'print $2'}") 72 if len(supervisor_pid_str) > 0: 73 self.__reload_supervisor(supervisor_pid_str) 74 else: 75 self.run_ssh("sudo -S -p '' /etc/init.d/supervisor start", True) 76 except Exception as e: 77 return False, "{}-install supervisor failed:{}".format(self.host, e) 78 return True, "install supervisor success" 79 80 def __reload_supervisor(self, supervisor_pid_str): 81 supervisor_pid = supervisor_pid_str[0].strip("\n") 82 self.run_ssh("sudo -S -p '' kill {}".format(supervisor_pid), True) 83 self.run_ssh("sudo -S -p '' killall supervisord", True) 84 self.run_ssh("sudo -S -p '' sudo apt remove supervisor -y", True) 85 self.run_ssh("sudo -S -p '' apt update", True) 86 self.run_ssh("sudo -S -p '' apt install -y supervisor", True) 87 self.run_ssh("sudo -S -p '' cp {} /etc/supervisor/".format(self.remote_supervisor_conf), True) 88 self.run_ssh("sudo -S -p '' /etc/init.d/supervisor start", True) 89 90 def __rewrite_supervisor_conf(self, sup_tmp): 91 con = configparser.ConfigParser() 92 con.read(self.cfg.supervisor_file) 93 con.set("inet_http_server", "username", self.username) 94 con.set("inet_http_server", "password", self.password) 95 con.set("supervisorctl", "username", self.username) 96 con.set("supervisorctl", "password", self.password) 97 with open(sup_tmp, "w") as file: 98 con.write(file)