From fe7a378c41a5b094cf9813fdcadc76c712bf0414 Mon Sep 17 00:00:00 2001 From: LowEntropyBody Date: Fri, 6 Mar 2020 21:41:10 +0800 Subject: [PATCH] Use crontab to restart TEE (#18) * add crontab * change version * add stop tee * fix path bug * remove crontab cmd * ' to " * change to tee absolute path * change crontab cycle --- README.md | 3 +- VERSION | 2 +- custom/tee-launch.json | 2 +- stcript/crust-client.sh | 96 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 95 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5a078aa..d29ada8 100644 --- a/README.md +++ b/README.md @@ -99,9 +99,8 @@ crust_chain_endpoint="ws://127.0.0.1:9944/" # the ws address of chain ``` ### tee-launch.json -```json { - "base_path" : "crust_store/node1/tee/", # All files will be stored in this directory + "base_path" : "/home/user/crust-alphanet/crust_store/node1/tee/", # All files will be stored in this directory, must be absolute path "empty_capacity" : 4, # empty disk storage in Gb "ipfs_api_base_url" : "http://127.0.0.1:5001/api/v0", # for connect to ipfs diff --git a/VERSION b/VERSION index 6e8bf73..17e51c3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.0 +0.1.1 diff --git a/custom/tee-launch.json b/custom/tee-launch.json index 1283c3e..6d31e94 100644 --- a/custom/tee-launch.json +++ b/custom/tee-launch.json @@ -1,5 +1,5 @@ { - "base_path" : "crust_store/node1/tee/", + "base_path" : "/home/user/crust-alphanet/crust_store/node1/tee/", "empty_capacity" : 4, "ipfs_api_base_url" : "http://127.0.0.1:5001/api/v0", diff --git a/stcript/crust-client.sh b/stcript/crust-client.sh index 66383b5..08a27a1 100755 --- a/stcript/crust-client.sh +++ b/stcript/crust-client.sh @@ -28,6 +28,7 @@ Usage: tee-launch launch crust-tee (if you set api_base_url==validator_api_base_url in config file, you need to be genesis node) + tee-stop stop crust-tee -b launch commands will be started in backend with "chain-launch-genesis", "chain-launch-normal", "chain-launch-validator", "api-launch", "ipfs-launch", @@ -532,6 +533,18 @@ apiLaunch() verbose INFO " SUCCESS" t cmd_run="node $crust_api_main_install_dir/node_modules/.bin/ts-node $crust_api_main_install_dir/src/index.ts $crust_api_port $crust_chain_endpoint" + + verbose INFO "Try to kill old crust api with same " h + api_pid=$(ps -ef | grep "$cmd_run" | grep -v grep | awk '{print $2}') + if [ x"$api_pid" != x"" ]; then + kill -9 $api_pid &>/dev/null + if [ $? -ne 0 ]; then + sudo "kill -9 $api_pid" &>/dev/null + fi + fi + verbose INFO " SUCCESS" t + + if [ -z "$2" ]; then verbose INFO "Launch crust API with $1 configurations\n" $cmd_run @@ -565,17 +578,84 @@ teeLaunch() verbose WARN "TEE verifier address is the same as yourself, please confirm that you are one of genesis nodes\n" fi - cmd_run="$crust_tee_main_install_dir/bin/crust-tee -c $1" + local_pwd=$(pwd) + config_path=$1 + log_path=$2 + + if [[ $config_path != /* ]]; then + config_path=$local_pwd/$config_path + fi + if [[ $log_path != /* ]]; then + log_path=$local_pwd/$log_path + fi + + cmd_run="$crust_tee_main_install_dir/bin/crust-tee -c $config_path" + crontab_cmd="crust-client tee-launch $config_path -b $log_path" + crontab -l 2>/dev/null | grep -v "$crontab_cmd" | crontab - + + verbose INFO "Try to kill old crust tee with same " h + + tee_pid=$(ps -ef | grep "$cmd_run" | grep -v grep | awk '{print $2}') + if [ x"$tee_pid" != x"" ]; then + kill -9 $tee_pid &>/dev/null + if [ $? -ne 0 ]; then + sudo "kill -9 $tee_pid" &>/dev/null + fi + fi + verbose INFO " SUCCESS" t + + if [ -z "$2" ]; then verbose INFO "Launch crust TEE with $1 configurations\n" eval $cmd_run else - nohup $cmd_run &>$2 & + nohup $cmd_run &>$log_path & + (crontab -l 2>/dev/null; echo "* 3 * * * $crontab_cmd") | crontab - sleep 3 tee_pid=$(ps -ef | grep "$cmd_run" | grep -v grep | awk '{print $2}') - mv $2 $2.${tee_pid[0]: 0: 5} - verbose INFO "Launch tee with $1 configurations in backend (pid is $tee_pid), log information will be saved in $2.${tee_pid[0]: 0: 5}\n" + verbose INFO "Launch tee with $1 configurations in backend (pid is $tee_pid), log information will be saved in $2\n" + fi +} + +teeStop() +{ + verbose INFO "Check " h + if [ x"$1" = x"" ]; then + help + exit 1 + fi + + if [ ! -f "$1" ]; then + verbose ERROR " Failed" t + verbose ERROR "Can't find tee-launch.json!" + exit 1 + fi + verbose INFO " SUCCESS" t + + local_pwd=$(pwd) + config_path=$1 + log_path=$2 + + if [[ $config_path != /* ]]; then + config_path=$local_pwd/$config_path + fi + if [[ $log_path != /* ]]; then + log_path=$local_pwd/$log_path fi + + cmd_run="$crust_tee_main_install_dir/bin/crust-tee -c $config_path" + crontab_cmd="crust-client tee-launch $config_path -b $log_path" + crontab -l 2>/dev/null | grep -v "$crontab_cmd" | crontab - + + verbose INFO "Try to kill old crust tee with same " h + tee_pid=$(ps -ef | grep "$cmd_run" | grep -v grep | awk '{print $2}') + if [ x"$tee_pid" != x"" ]; then + kill -9 $tee_pid &>/dev/null + if [ $? -ne 0 ]; then + sudo "kill -9 $tee_pid" &>/dev/null + fi + fi + verbose INFO " SUCCESS" t } ############### MAIN BODY ############### @@ -626,6 +706,14 @@ while true ; do shift 2 fi ;; + tee-stop) + cmd_run="teeStop $2" + if [ -z $2 ]; then + shift 1 + else + shift 2 + fi + ;; api-launch) cmd_run="apiLaunch $2" if [ -z $2 ]; then