From fc7ce12497f4455002ec7f81de64d400f4d2ba68 Mon Sep 17 00:00:00 2001 From: cjcchen Date: Thu, 13 Oct 2022 16:00:42 +0000 Subject: [PATCH] add node manager for resilientdb.com --- deploy/node_manager/BUILD | 7 ++ deploy/node_manager/README.md | 10 +++ deploy/node_manager/deploy_manager.py | 89 ++++++++++++++++++++++++++ deploy/node_manager/generate_config.sh | 48 ++++++++++++++ deploy/node_manager/generate_key.sh | 18 ++++++ deploy/node_manager/nohup.out | 13 ++++ deploy/node_manager/start_kv_server.sh | 30 +++++++++ deploy/node_manager/stop.sh | 2 + 8 files changed, 217 insertions(+) create mode 100644 deploy/node_manager/BUILD create mode 100644 deploy/node_manager/README.md create mode 100644 deploy/node_manager/deploy_manager.py create mode 100755 deploy/node_manager/generate_config.sh create mode 100755 deploy/node_manager/generate_key.sh create mode 100644 deploy/node_manager/nohup.out create mode 100755 deploy/node_manager/start_kv_server.sh create mode 100755 deploy/node_manager/stop.sh diff --git a/deploy/node_manager/BUILD b/deploy/node_manager/BUILD new file mode 100644 index 000000000..b5f218850 --- /dev/null +++ b/deploy/node_manager/BUILD @@ -0,0 +1,7 @@ +py_binary( + name = "deploy_manager", + srcs = ["deploy_manager.py"], + deps = [ + "//proto:replica_info_py_proto", + ], +) diff --git a/deploy/node_manager/README.md b/deploy/node_manager/README.md new file mode 100644 index 000000000..ccac2fce3 --- /dev/null +++ b/deploy/node_manager/README.md @@ -0,0 +1,10 @@ +Run Node Manager as backend to lauch Nexres locally. + +If you are under the repo root path, go the deploy/node_manager. +Then +``` +bazel run :deploy_manager $PWD +``` + +Launch your front end from : [resilientdb](https://github.com/resilientdb/resilientdb.github.io/tree/nexres) +Change the [endpoint address](https://github.com/resilientdb/resilientdb.github.io/blob/nexres/src/api/endpoint.ts#L22) so all the deployment request will be sent to your node manager backend. diff --git a/deploy/node_manager/deploy_manager.py b/deploy/node_manager/deploy_manager.py new file mode 100644 index 000000000..81220771c --- /dev/null +++ b/deploy/node_manager/deploy_manager.py @@ -0,0 +1,89 @@ +from http.server import BaseHTTPRequestHandler +from urllib import parse +import subprocess +import json +import sys + +from proto.replica_info_pb2 import ResConfigData,ReplicaInfo +from google.protobuf.json_format import MessageToJson +from google.protobuf.json_format import Parse, ParseDict + +script_path="/home/ubuntu/nexres/deploy/node_manager" + +def RunCmd(cmd): + p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + print("run:{}".format(cmd)) + print("res:{}".format(p.stdout.readlines())) + +class GetHandler(BaseHTTPRequestHandler): + + def do_GET(self): + parsed_path = parse.urlparse(self.path) + print("path data:",parsed_path.path) + if parsed_path.path == "/node/stop": + self.Stop(); + self.send_response_only(200) + #self.send_header('Content-Type', + # 'text/plain; charset=utf-8') + #self.end_headers() + #self.wfile.write(bytes(json.dumps(js).encode('utf-8'))) + + + def do_POST(self): + parsed_path = parse.urlparse(self.path) + print("path data:",parsed_path.path) + if parsed_path.path == "/node/deploy": + content_len = int(self.headers['Content-Length']) + post_body = str(self.rfile.read(content_len).decode()) + post_body=post_body.replace("%3A",":") + data_list = post_body.split('&') + addresses = [] + for data in data_list: + if data.split("=")[0] == "address": + addresses.append(data.split("=")[1]) + self.StartServer(addresses) + self.send_response_only(200) + + def GenerateServerConfig(self, addresses): + config_data=ResConfigData() + config_data.self_region_id=1 + region = config_data.region.add() + region.region_id = 1 + idx=1 + with open("{}/deploy/iplist.txt".format(script_path),"w") as f: + for address in addresses: + (ip,port) = address.split(':') + f.write("{}:{}\n".format(ip,port)) + replica = ReplicaInfo() + replica.id = idx + replica.ip = ip + replica.port = int(port) + region.replica_info.append(replica) + idx+=1 + json_obj = MessageToJson(config_data) + with open("{}/deploy/server.config".format(script_path),"w") as f: + f.write(json_obj) + return idx-2 + + def Stop(self): + RunCmd("{}/stop.sh".format(script_path)) + + def Start(self, server_num): + RunCmd("{}/generate_key.sh {}".format(script_path, script_path)) + RunCmd("{}/generate_config.sh {}".format(script_path, script_path)) + RunCmd("{}/start_kv_server.sh {} {}".format(script_path, script_path, server_num)) + + def StartServer(self,addresses): + server_num = self.GenerateServerConfig(addresses) + self.Start(server_num) + +if __name__ == '__main__': + if len(sys.argv) == 1: + print ("please provide the script path paramaters. Usage: bazel run :deploy_manager $PWD") + exit(0) + print(" working path:",sys.argv[1]) + script_path = sys.argv[1] + from http.server import HTTPServer + server = HTTPServer(('0.0.0.0', 4080), GetHandler) + print('Starting server, use to stop') + server.serve_forever() diff --git a/deploy/node_manager/generate_config.sh b/deploy/node_manager/generate_config.sh new file mode 100755 index 000000000..4c6cf7a2e --- /dev/null +++ b/deploy/node_manager/generate_config.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +WORK_PATH=$1 +WORK_SPACE=${WORK_PATH}/../../ + +cd "${WORK_PATH}" + +IPS=`cat ./deploy/iplist.txt` + +KEY_TOOLS_BIN=${WORK_SPACE}/bazel-bin/tools/key_generator_tools +CERT_TOOLS_BIN=${WORK_SPACE}/bazel-bin/tools/certificate_tools +ADMIN_PRIVATE_KEY=${WORK_SPACE}/cert/admin.key.pri +ADMIN_PUBLIC_KEY=${WORK_SPACE}/cert/admin.key.pub +CERT_PATH=deploy/cert/ +DEPLOY_PATH=deploy +CLIENT_NUM=1 + +bazel build //tools:certificate_tools + +echo "" > ${DEPLOY_PATH}/client.config +idx=1 +tot=0 +for ip in ${IPS[@]} +do + ((tot++)) +done +echo $tot + +for addr in ${IPS}; +do + t=(${addr//:/ }) + ip=${t[0]} + port=${t[1]} + echo "ip:",${ip},"port",${port} + public_key=${CERT_PATH}/node_${idx}.key.pub + + # create public key + # create server config + # create the public key and certificate + if [ $(($idx+$CLIENT_NUM)) -gt $tot ] ; then + $CERT_TOOLS_BIN ${CERT_PATH} ${ADMIN_PRIVATE_KEY} ${ADMIN_PUBLIC_KEY} ${public_key} ${idx} ${ip} ${port} client + echo "${idx} ${ip} ${port}" >> ${DEPLOY_PATH}/client.config + else + $CERT_TOOLS_BIN ${CERT_PATH} ${ADMIN_PRIVATE_KEY} ${ADMIN_PUBLIC_KEY} ${public_key} ${idx} ${ip} ${port} replica + fi + + idx=$(($idx+1)) +done diff --git a/deploy/node_manager/generate_key.sh b/deploy/node_manager/generate_key.sh new file mode 100755 index 000000000..a8d7fb79c --- /dev/null +++ b/deploy/node_manager/generate_key.sh @@ -0,0 +1,18 @@ + +WORK_PATH=$1 +WORK_SPACE=${WORK_PATH}/../../ + +cd "${WORK_PATH}" + +NUM=`cat deploy/iplist.txt| awk '{if (length($0) > 0) {print $0}}' | wc -l` + +echo "server num:$NUM" + +mkdir -p deploy/cert +cd deploy +bazel build //tools:key_generator_tools + +for idx in `seq 1 ${NUM}`; +do + echo `${WORK_SPACE}/bazel-bin/tools/key_generator_tools "./cert/node_${idx}" "AES"` +done diff --git a/deploy/node_manager/nohup.out b/deploy/node_manager/nohup.out new file mode 100644 index 000000000..f666df9b0 --- /dev/null +++ b/deploy/node_manager/nohup.out @@ -0,0 +1,13 @@ +Loading: +Loading: 0 packages loaded +Analyzing: target //deploy/node_manager:deploy_manager (0 packages loaded, 0 targets configured) +INFO: Analyzed target //deploy/node_manager:deploy_manager (0 packages loaded, 0 targets configured). +INFO: Found 1 target... +[0 / 1] [Prepa] BazelWorkspaceStatusAction stable-status.txt +Target //deploy/node_manager:deploy_manager up-to-date: + bazel-bin/deploy/node_manager/deploy_manager +INFO: Elapsed time: 0.316s, Critical Path: 0.01s +INFO: 1 process: 1 internal. +INFO: Build completed successfully, 1 total action +INFO: Running command line: bazel-bin/deploy/node_manager/deploy_manager /home/ubuntu/nexres/deploy/node_manager +INFO: Build completed successfully, 1 total action diff --git a/deploy/node_manager/start_kv_server.sh b/deploy/node_manager/start_kv_server.sh new file mode 100755 index 000000000..211812417 --- /dev/null +++ b/deploy/node_manager/start_kv_server.sh @@ -0,0 +1,30 @@ +!/bin/bash + +killall -9 kv_server + +WORK_PATH=$1 +WORK_SPACE=${WORK_PATH}/../../ +SERVER_PATH=${WORK_SPACE}/bazel-bin/kv_server/kv_server +SERVER_CONFIG=${WORK_PATH}/deploy/server.config +LOG_PATH=${WORK_PATH}/deploy/log + +cd ${WORK_PATH} + +bazel build //kv_server:kv_server +mkdir -p ${LOG_PATH} + +SVR_NUM=$2 + +echo "server num:"${SVR_NUM} +METRIC_PORT=8091 +for i in `seq 1 ${SVR_NUM}`; +do +echo ${i} +nohup $SERVER_PATH $SERVER_CONFIG $WORK_PATH/deploy/cert/node_${i}.key.pri $WORK_PATH/deploy/cert/cert_${i}.cert 0.0.0.0:${METRIC_PORT} > ${LOG_PATH}/server${i}.log 2>&1 & +METRIC_PORT=`expr ${METRIC_PORT} + 1` +done + +SVR_NUM=`expr ${SVR_NUM} + 1` +echo ${SVR_NUM} + +nohup $SERVER_PATH $SERVER_CONFIG $WORK_PATH/deploy/cert/node_${SVR_NUM}.key.pri $WORK_PATH/deploy/cert/cert_${SVR_NUM}.cert 0.0.0.0:${METRIC_PORT} > ${LOG_PATH}/client.log 2>&1 & diff --git a/deploy/node_manager/stop.sh b/deploy/node_manager/stop.sh new file mode 100755 index 000000000..18175c8b8 --- /dev/null +++ b/deploy/node_manager/stop.sh @@ -0,0 +1,2 @@ + +killall -9 kv_server