def install_dependency(self, nodedict, file=conf.MPCLIB): """ 配置服务器依赖 :param nodedict: :param file: :return: """ try: ssh, sftp, t = connect_linux( nodedict["host"], nodedict["username"], nodedict["password"]) except Exception as e: raise e pwd_list = self.run_ssh(ssh, "pwd") pwd = pwd_list[0].strip("\r\n") cmd = r"sudo -S -p '' sed -i '$a export LD_LIBRARY_PATH={}/mpclib' /etc/profile".format( pwd) is_have_mpclib = self.run_ssh(ssh, 'ls|grep "mpclib"') if not is_have_mpclib: sftp.put(file, os.path.basename(file)) self.run_ssh(ssh, "tar -zxvf ./{}".format(os.path.basename(file))) self.run_ssh(ssh, "mv ./platon-mpclib-ubuntu-amd64-0.5.0 ./mpclib") self.run_ssh(ssh, cmd, nodedict["password"]) self.run_ssh(ssh, "rm -rf ./{}".format(os.path.basename(file))) # self.run_ssh( # ssh, "sudo -S -p '' apt-get install libboost-all-dev -y", nodedict["password"]) # self.run_ssh( # ssh, "sudo -S -p '' apt-get install llvm-6.0-dev llvm-6.0 libclang-6.0-dev -y", nodedict["password"]) self.run_ssh( ssh, "sudo -S -p '' apt-get install libgmpxx4ldbl libgmp-dev libprocps4-dev", nodedict["password"]) t.close()
def deploy_supervisor(self, node): """ 部署supervisor :param node: :return: """ tmp_dir = gen_node_tmp(self.sup_tmp, node["host"], str(node["port"])) if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) tmp = os.path.join(tmp_dir, "supervisord.conf") self.update_conf(node, self.sup_template, tmp) ssh, sftp, t = connect_linux( node["host"], node["username"], node["password"]) self.run_ssh(ssh, "mkdir -p ./tmp") sftp.put(tmp, "./tmp/supervisord.conf") supervisor_pid_str = self.run_ssh( ssh, "ps -ef|grep supervisord|grep -v grep|awk {'print $2'}") if len(supervisor_pid_str) > 0: self.judge_restart_supervisor(ssh, supervisor_pid_str, node) else: self.run_ssh(ssh, "sudo -S -p '' apt update", node["password"]) self.run_ssh( ssh, "sudo -S -p '' apt install -y supervisor", node["password"]) self.run_ssh( ssh, "sudo -S -p '' cp ./tmp/supervisord.conf /etc/supervisor/", node["password"]) supervisor_pid_str = self.run_ssh( ssh, "ps -ef|grep supervisord|grep -v grep|awk {'print $2'}") if len(supervisor_pid_str) > 0: self.judge_restart_supervisor(ssh, supervisor_pid_str, node) else: self.run_ssh( ssh, "sudo -S -p '' /etc/init.d/supervisor start", node["password"]) t.close()
def boom(self, nodedict): """ 关闭机器所有platon进程 :param nodedict: :return: """ ip = nodedict.get('host') username = nodedict.get('username') password = nodedict.get('password') ssh, _, t = connect_linux(ip, username, password) self.run_ssh(ssh, "killall -9 platon") t.close()
def restart(self, node): """ 重启一个节点 :param node: 节点信息 :return: """ ip = node['host'] port = str(node.get("port", "16789")) username = node["username"] password = node["password"] ssh, sftp, t = connect_linux(ip, username, password) node_name = "node-" + str(port) self.run_ssh(ssh, "supervisorctl restart {}".format(node_name)) t.close()
def stop(self, nodedict): """ 以kill -9的方式停止一个节点,关闭后节点无法重启,只能重新部署链 :param nodedict: :return: """ ip = nodedict['host'] username = nodedict['username'] password = nodedict['password'] port = nodedict.get("port", "6789") ssh, _, t = connect_linux(ip, username, password) self.run_ssh( ssh, "ps -ef|grep platon|grep %s|grep -v grep|awk {'print $2'}|xargs kill -9" % port) t.close()
def update_node(self, node): """ 根据节点信息更新节点,替换二进制文件 :param node: 节点信息 :return: """ ip = node['host'] port = str(node.get("port", "16789")) username = node["username"] password = node["password"] ssh, sftp, t = connect_linux(ip, username, password) self.upload_platon(ssh, sftp, port) node_name = "node-" + str(port) self.run_ssh(ssh, "supervisorctl restart {}".format(node_name)) t.close()
def kill(self, nodedict): """ 使用supervisor关闭platon进程 :param nodedict: :param value: :param wait_time: :return: """ ip = nodedict['host'] username = nodedict['username'] password = nodedict['password'] ssh, _, t = connect_linux(ip, username, password) self.run_ssh( ssh, "supervisorctl stop node-{}".format(nodedict["port"])) t.close()
def kill(self, nodedict): """ 以kill方式关闭一个节点,关闭后,节点还能被重启 :param nodedict: 连接对象 :return: """ ip = nodedict['host'] username = nodedict['username'] password = nodedict['password'] port = nodedict.get("port", "16789") ssh, _, t = connect_linux(ip, username, password) self.run_ssh( ssh, "ps -ef|grep platon|grep %s|grep -v grep|awk {'print $2'}|xargs kill" % port) time.sleep(5) result = self.run_ssh(ssh, "ps -ef|grep platon|grep %s|grep -v grep|awk {'print $2'}" % port) t.close() if result: raise Exception("进程关闭失败")
def deploy_default(self, node): """ 使用默认参数部署节点(不初始化) :param node: 节点信息 :return: """ self.gen_deploy_conf(node) ip = node['host'] port = str(node.get("port", "16789")) username = node["username"] password = node["password"] ssh, sftp, t = connect_linux(ip, username, password) node_name = "node-" + str(port) self.clean_blockchain(ssh, port, password) self.clean_log(ssh, port) self.upload_platon(ssh, sftp, port) nodekey = node["nodekey"] self.upload_nodekey(ssh, sftp, nodekey, ip, port) self.upload_keystore(ssh, sftp, port) self.start_node_conf(ssh, sftp, port, node, node_name) t.close()
def start_node(self, nodedict, genesis_file, static_node_file, is_init=True, clean=False): """ 启动节点 :param nodedict: 节点信息 :param genesis_file: genesis.json文件路径 :param static_node_file: 共识节点互连文件 :param is_init: 是否需要初始化,初始化会删除原platon部署目录的所有数据 :param clean: 是否删除platon部署目录的数据,is_init=True时,该参数无效 :return: """ ip = nodedict['host'] port = nodedict.get("port", "16789") rpcport = nodedict.get("rpcport", "6789") username = nodedict["username"] password = nodedict["password"] mpcactor = nodedict.get("mpcactor", None) vcactor = nodedict.get("vcactor", None) url = nodedict["url"] ssh, sftp, t = connect_linux(ip, username, password) if clean or is_init: self.clean_blockchain(ssh, port, password) self.clean_log(ssh, port) self.upload_platon(ssh, sftp, port) if is_init: sftp.put(conf.SPLIT_LOG_SCRIPT, "{}/node-{}/split_log.py".format(self.deploy_path, port)) if genesis_file is None: raise Exception("需要初始化时,genesis_file不能为空") self.upload_genesis_json(sftp, port, genesis_file) _ = self.init(ssh, port=port) # self.upload_cbft_json(sftp, port) if static_node_file: self.upload_static_json(sftp, port, static_node_file) nodekey = nodedict["nodekey"] self.upload_config_json(sftp, port) self.upload_nodekey(ssh, sftp, nodekey, ip, port) self.upload_keystore(ssh, sftp, port) self.start( ssh, url, port=port, rpcport=rpcport, cmd=nodedict.get("cmd", None), mpcactor=mpcactor, vcactor=vcactor) t.close()
def supervisor_deploy_platon(self, node, genesis_file, static_node_file, is_init=True, clean=False): """ 使用supervisor部署platon,灵活部署 :param node: :param genesis_file: :param static_node_file: :param is_init: :param clean: :return: """ self.gen_deploy_conf(node) ip = node['host'] port = str(node.get("port", "16789")) username = node["username"] password = node["password"] ssh, sftp, t = connect_linux(ip, username, password) node_name = "node-" + str(port) if clean or is_init: self.clean_blockchain(ssh, port, password) self.clean_log(ssh, port) self.upload_platon(ssh, sftp, port) self.upload_config_json(sftp, port) if is_init: if genesis_file is None: raise Exception("需要初始化时,genesis_file不能为空") self.upload_genesis_json(sftp, port, genesis_file) _ = self.init(ssh, port=port) # self.upload_cbft_json(sftp, port) if static_node_file: self.upload_static_json(sftp, port, static_node_file) nodekey = node["nodekey"] #self.upload_config_json(sftp, port) self.upload_nodekey(ssh, sftp, nodekey, ip, port) self.upload_keystore(ssh, sftp, port) self.start_node_conf(ssh, sftp, port, node, node_name) t.close()
def gen_deploy_conf(self, node): """ 生成supervisor部署platon的配置 :param node: :return: """ port = str(node["port"]) node_name = "node-" + port ssh, sftp, t = connect_linux( node["host"], node["username"], node["password"]) pwd_list = self.run_ssh(ssh, "pwd") pwd = pwd_list[0].strip("\r\n") with open(gen_node_tmp(self.sup_tmp, node["host"], port) + "/" + node_name + ".conf", "w") as fp: fp.write("[program:" + node_name + "]\n") if not os.path.isabs(self.deploy_path): cmd = "{}/{}/{}/platon --identity platon --datadir".format( pwd, self.deploy_path, node_name) cmd = cmd + \ " {}/{}/{}/data --port ".format(pwd, self.deploy_path, node_name) + port else: cmd = "{}/{}/platon --identity platon --datadir".format( self.deploy_path, node_name) cmd = cmd + \ " {}/{}/data --port ".format(self.deploy_path, node_name) + port cmd = cmd + " --syncmode '{}'".format(self.syncmode) if self.net_type: cmd = cmd + " --" + self.net_type if node.get("mpcactor", None): cmd = cmd + \ " --mpc --mpc.actor {}".format(node.get("mpcactor")) if node.get("vcactor", None): cmd = cmd + \ " --vc --vc.actor {} --vc.password 88888888".format( node.get("vcactor")) cmd = cmd + " --debug --verbosity {}".format(conf.LOG_LEVEL) cmd = cmd + " --pprof --pprofaddr {} --pprofport {}".format( node["host"], node.get("pprofport", 6060)) cmd = cmd + " --metrics --metrics.influxdb --metrics.influxdb.username admin --metrics.influxdb.password admin --metrics.influxdb.endpoint http://192.168.10.102:8086 --metrics.influxdb.host.tag node-tag" if parse.splittype(node["url"])[0] == "ws": cmd = cmd + " --ws --wsorigins '*' --wsaddr 0.0.0.0 --wsport " + \ str(node["rpcport"]) cmd = cmd + " --wsapi platon,debug,personal,admin,net,web3" else: cmd = cmd + " --rpc --rpcaddr 0.0.0.0 --rpcport " + \ str(node["rpcport"]) cmd = cmd + " --rpcapi platon,debug,personal,admin,net,web3" cmd = cmd + " --txpool.nolocals --networkid 100" if os.path.isabs(self.deploy_path): cmd = cmd + " --gcmode archive --nodekey " + \ node["path"] + \ "{}/{}/data/nodekey".format(self.deploy_path, node_name) cmd = cmd + \ " --config {}/{}/config.json".format( self.deploy_path, node_name) else: cmd = cmd + " --gcmode archive --nodekey " + \ "{}/{}/{}/data/nodekey".format(pwd, self.deploy_path, node_name) cmd = cmd + \ " --config {}/{}/{}/config.json".format( pwd, self.deploy_path, node_name) fp.write("command=" + cmd + "\n") fp.write("environment=LD_LIBRARY_PATH={}/mpclib\n".format(pwd)) fp.write("numprocs=1\n") fp.write("autostart=false\n") fp.write("startsecs=3\n") fp.write("startretries=3\n") fp.write("autorestart=unexpected\n") fp.write("exitcode=0\n") fp.write("stopsignal=TERM\n") fp.write("stopwaitsecs=10\n") fp.write("redirect_stderr=true\n") if os.path.isabs(self.deploy_path): fp.write( "stdout_logfile={}/{}/log/platon.log\n".format(self.deploy_path, node_name)) else: fp.write( "stdout_logfile={}/{}/{}/log/platon.log\n".format(pwd, self.deploy_path, node_name)) fp.write("stdout_logfile_maxbytes=200MB\n") fp.write("stdout_logfile_backups=10\n") t.close()