Skip to content

Commit

Permalink
Use crontab to restart TEE (#18)
Browse files Browse the repository at this point in the history
* add crontab

* change version

* add stop tee

* fix path bug

* remove crontab cmd

* ' to "

* change to tee absolute path

* change crontab cycle
  • Loading branch information
LowEntropyBody authored Mar 6, 2020
1 parent e50065d commit fe7a378
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 8 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.1.1
2 changes: 1 addition & 1 deletion custom/tee-launch.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
96 changes: 92 additions & 4 deletions stcript/crust-client.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Usage:
tee-launch <tee-launch.json> launch crust-tee (if you set
api_base_url==validator_api_base_url
in config file, you need to be genesis node)
tee-stop <tee-launch.json> stop crust-tee
-b <log-file> launch commands will be started in backend
with "chain-launch-genesis", "chain-launch-normal",
"chain-launch-validator", "api-launch", "ipfs-launch",
Expand Down Expand Up @@ -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 <api-launch.json>" 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
Expand Down Expand Up @@ -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 <tee-launch.json>" 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 <tee-launch.json>" 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 <tee-launch.json>" 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 ###############
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fe7a378

Please sign in to comment.