github.com/klaytn/klaytn@v1.12.1/build/packaging/linux/bin/kpnd (about) 1 #!/usr/bin/env bash 2 3 BIN=$(cd "$(dirname $0)"; pwd) 4 CMD_HOME=$(dirname $BIN) 5 CONF=$CMD_HOME/conf 6 STOP_TIMEOUT=10 # seconds to wait for a clean exit 7 8 source $CONF/kpnd.conf 9 10 if [ -z $DATA_DIR ]; then 11 echo 12 echo " [ERROR] : DATA_DIR in conf/kpnd.conf is not defined." 13 exit 1 14 fi 15 16 if [ ! -d $DATA_DIR ]; then 17 echo 18 echo " [ERROR] : Genesis block is not initiated: [conf/kpnd.conf - DATA_DIR=$DATA_DIR]" 19 exit 1 20 fi 21 22 pidfile=$DATA_DIR/kpnd.pid 23 auto_restart_daemon_pidfile=$DATA_DIR/restart_daemon_kpnd.pid 24 25 __pid_run() { 26 unset pid 27 if [ ! -f $pidfile ]; then 28 return 29 fi 30 PID_NUM=$(cat $pidfile) 31 if [[ ! -z "$PID_NUM" ]]; then 32 export pid=$(ps -p $PID_NUM -o pid=) 33 fi 34 } 35 36 __auto_restart_daemon_pid_run() { 37 unset auto_restart_daemon_pid 38 if [ ! -f $auto_restart_daemon_pidfile ]; then 39 return 40 fi 41 AUTO_RESTART_DAEMON_PID_NUM=$(cat $auto_restart_daemon_pidfile) 42 if [[ ! -z "$AUTO_RESTART_DAEMON_PID_NUM" ]]; then 43 export auto_restart_daemon_pid=$(ps -p $AUTO_RESTART_DAEMON_PID_NUM -o pid=) 44 fi 45 } 46 47 __kill_timeout() { 48 local PIDNUM=$1 49 kill $PIDNUM 50 for i in `seq 0 100 $((1000 * $STOP_TIMEOUT))`; do 51 if ! kill -0 $PIDNUM 2> /dev/null; then 52 echo "OK" 53 return 54 fi 55 sleep 0.1 56 done 57 kill -9 $PIDNUM && echo "Killed" 58 } 59 60 #------------------------Related Auto restart daemon functions----------------------------- 61 __auto_restart_daemon() { 62 local backOffTime=$AUTO_RESTART_INTERVAL 63 local coeff=2 64 while : 65 do 66 sleep 1 67 __pid_run 68 if [ -z "$pid" ]; then 69 echo "INFO[`date`] node[${PID_NUM}] is down" 70 if [ -f $pidfile ]; then 71 echo "INFO[`date`] remove redundant pid file" 72 rm -f ${pidfile} 73 fi 74 echo "INFO[`date`] Sleep for backOffTime.... ${backOffTime} seconds." 75 sleep $backOffTime 76 echo -n "INFO[`date`] " 77 start_node 78 backOffTime=$(echo $backOffTime $coeff | awk '{printf "%.1f\n",$1*$2}') 79 80 echo "INFO[`date`] backOffTime = ${backOffTime}, Restarted node pid = ${PID_NUM}" 81 PID_NUM=$(cat $pidfile) 82 echo "" 83 fi 84 done 85 } 86 87 start_auto_restart_daemon() { 88 __auto_restart_daemon_pid_run 89 if [ -z $auto_restart_daemon_pid ]; then 90 __auto_restart_daemon >> ${LOG_DIR}/restart_daemon.out 2>&1 & 91 disown 92 AUTO_RESTART_DAEMON_PID_NUM=$! 93 AUTO_RESTART_DAEMON_RETVAL=$? 94 95 set +f 96 if [ $AUTO_RESTART_DAEMON_RETVAL = 0 ]; then 97 echo $AUTO_RESTART_DAEMON_PID_NUM > ${auto_restart_daemon_pidfile} 98 echo "Success to start auto restart daemon." 99 else 100 echo "Fail to start auto restart daemon." 101 fi 102 fi 103 } 104 105 stop_auto_restart_daemon() { 106 __auto_restart_daemon_pid_run 107 [ -z "$auto_restart_daemon_pid" ] && echo "auto restart daemon is not running" && return 108 echo -n "Shutting down auto restart daemon: " 109 __kill_timeout $(cat ${auto_restart_daemon_pidfile}) && rm -f ${auto_restart_daemon_pidfile} 110 } 111 112 status_auto_restart_daemon() { 113 __auto_restart_daemon_pid_run 114 if [ -n "$auto_restart_daemon_pid" ]; then 115 echo "auto restart daemon is running." 116 else 117 echo "auto restart daemon is down." 118 fi 119 } 120 121 #------------------------Related to Klaytn node functions----------------------------- 122 123 __check_option() { 124 if [ ! -d $LOG_DIR ]; then 125 mkdir -p $LOG_DIR 126 fi 127 128 set -f 129 OPTIONS="" 130 131 if [[ ! -z $METRICS ]] && [[ $METRICS -eq 1 ]]; then 132 OPTIONS="$OPTIONS --metrics" 133 fi 134 135 if [[ ! -z $PROMETHEUS ]] && [[ $PROMETHEUS -eq 1 ]]; then 136 OPTIONS="$OPTIONS --prometheus" 137 fi 138 139 if [[ ! -z $DB_NO_PARALLEL_WRITE ]] && [[ $DB_NO_PARALLEL_WRITE -eq 1 ]]; then 140 OPTIONS="$OPTIONS --db.no-parallel-write" 141 fi 142 143 if [[ ! -z $MULTICHANNEL ]] && [[ $MULTICHANNEL -eq 1 ]]; then 144 OPTIONS="$OPTIONS --multichannel" 145 fi 146 147 if [[ ! -z $RPC_ENABLE ]] && [[ $RPC_ENABLE -eq 1 ]]; then 148 OPTIONS="$OPTIONS --rpc" 149 RPC_API=`echo $RPC_API | tr -d "[:space:]"` 150 if [ ! -z $RPC_API ]; then 151 OPTIONS="$OPTIONS --rpcapi $RPC_API" 152 fi 153 if [ ! -z $RPC_PORT ]; then 154 OPTIONS="$OPTIONS --rpcport $RPC_PORT" 155 fi 156 if [ ! -z $RPC_ADDR ]; then 157 OPTIONS="$OPTIONS --rpcaddr $RPC_ADDR" 158 fi 159 if [ ! -z $RPC_CORSDOMAIN ]; then 160 OPTIONS="$OPTIONS --rpccorsdomain $RPC_CORSDOMAIN" 161 fi 162 if [ ! -z $RPC_VHOSTS ]; then 163 OPTIONS="$OPTIONS --rpcvhosts $RPC_VHOSTS" 164 fi 165 fi 166 167 if [[ ! -z $WS_ENABLE ]] && [[ $WS_ENABLE -eq 1 ]]; then 168 OPTIONS="$OPTIONS --ws" 169 WS_API=`echo $WS_API | tr -d "[:space:]"` 170 if [ ! -z $WS_API ]; then 171 OPTIONS="$OPTIONS --wsapi $WS_API" 172 fi 173 if [ ! -z $WS_PORT ]; then 174 OPTIONS="$OPTIONS --wsport $WS_PORT" 175 fi 176 if [ ! -z $WS_ADDR ]; then 177 OPTIONS="$OPTIONS --wsaddr $WS_ADDR" 178 fi 179 if [ ! -z $WS_ORIGINS ]; then 180 OPTIONS="$OPTIONS --wsorigins $WS_ORIGINS" 181 fi 182 fi 183 184 # Cypress network => NETWORK_ID is null && NETWORK = "cypress" 185 # Baobab network => NETWORK_ID is null && NETWORK = "baobab" 186 # Else => private network 187 if [[ -z $NETWORK_ID ]]; then 188 if [[ $NETWORK == "baobab" ]]; then 189 OPTIONS="$OPTIONS --baobab" 190 elif [[ $NETWORK == "cypress" ]]; then 191 OPTIONS="$OPTIONS --cypress" 192 else 193 echo 194 echo "[ERROR] network id is not specified and network is not available." 195 echo "Available network: baobab, cypress" 196 exit 1 197 fi 198 else 199 OPTIONS="$OPTIONS --networkid $NETWORK_ID" 200 echo "[INFO] creating a private network: $NETWORK_ID" 201 if [[ ! -z $NETWORK ]]; then 202 echo 203 echo "[WARN] ignoring the specified network: $NETWORK" 204 fi 205 fi 206 207 if [ ! -z $DATA_DIR ]; then 208 OPTIONS="$OPTIONS --datadir $DATA_DIR" 209 fi 210 211 if [ ! -z $PORT ]; then 212 OPTIONS="$OPTIONS --port $PORT" 213 fi 214 215 if [ ! -z $SUBPORT ]; then 216 OPTIONS="$OPTIONS --subport $SUBPORT" 217 fi 218 219 if [ ! -z $SERVER_TYPE ]; then 220 OPTIONS="$OPTIONS --srvtype $SERVER_TYPE" 221 fi 222 223 if [ ! -z $VERBOSITY ]; then 224 OPTIONS="$OPTIONS --verbosity $VERBOSITY" 225 fi 226 227 if [ ! -z $TXPOOL_EXEC_SLOTS_ALL ]; then 228 OPTIONS="$OPTIONS --txpool.exec-slots.all $TXPOOL_EXEC_SLOTS_ALL" 229 fi 230 231 if [ ! -z $TXPOOL_NONEXEC_SLOTS_ALL ]; then 232 OPTIONS="$OPTIONS --txpool.nonexec-slots.all $TXPOOL_NONEXEC_SLOTS_ALL" 233 fi 234 235 if [ ! -z $TXPOOL_EXEC_SLOTS_ACCOUNT ]; then 236 OPTIONS="$OPTIONS --txpool.exec-slots.account $TXPOOL_EXEC_SLOTS_ACCOUNT" 237 fi 238 239 if [ ! -z $TXPOOL_NONEXEC_SLOTS_ACCOUNT ]; then 240 OPTIONS="$OPTIONS --txpool.nonexec-slots.account $TXPOOL_NONEXEC_SLOTS_ACCOUNT" 241 fi 242 243 if [ ! -z $SYNCMODE ]; then 244 OPTIONS="$OPTIONS --syncmode $SYNCMODE" 245 fi 246 247 if [ ! -z $MAXCONNECTIONS ]; then 248 OPTIONS="$OPTIONS --maxconnections $MAXCONNECTIONS" 249 fi 250 251 if [ ! -z $LDBCACHESIZE ]; then 252 OPTIONS="$OPTIONS --db.leveldb.cache-size $LDBCACHESIZE" 253 fi 254 255 if [[ ! -z $NO_DISCOVER ]] && [[ $NO_DISCOVER -eq 1 ]]; then 256 OPTIONS="$OPTIONS --nodiscover" 257 fi 258 259 if [[ ! -z $BOOTNODES ]] && [[ $BOOTNODES != "" ]]; then 260 OPTIONS="$OPTIONS --bootnodes $BOOTNODES" 261 fi 262 263 if [[ ! -z $ADDITIONAL ]] && [[ $ADDITIONAL != "" ]]; then 264 OPTIONS="$OPTIONS $ADDITIONAL" 265 fi 266 267 if [[ ! -z $LOG_ROTATE ]] && [[ $LOG_ROTATE -eq 1 ]]; then 268 OPTIONS="$OPTIONS --log.rotate" 269 if [[ ! -z $LOG_MAXSIZE ]]; then 270 OPTIONS="$OPTIONS --log.maxsize $LOG_MAXSIZE" 271 fi 272 if [[ ! -z $LOG_MAXBACKUP ]]; then 273 OPTIONS="$OPTIONS --log.maxbackup $LOG_MAXBACKUP" 274 fi 275 if [[ ! -z $LOG_MAXAGE ]]; then 276 OPTIONS="$OPTIONS --log.maxage $LOG_MAXAGE" 277 fi 278 if [[ ! -z $LOG_COMPRESS ]] && [[ $LOG_COMPRESS -eq 1 ]]; then 279 OPTIONS="$OPTIONS --log.compress" 280 fi 281 fi 282 } 283 284 start_node() { 285 __pid_run 286 [ -n "$pid" ] && echo "kpnd already running...[$pid]" && return 287 288 echo -n "Starting kpnd: " 289 290 __check_option 291 292 BASEDIR="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" 293 CURRENTFILE=`basename "$0"` 294 OPTIONS="$OPTIONS --autorestart.daemon.path $BASEDIR/$CURRENTFILE" 295 296 OPTIONS="$OPTIONS --log.file ${LOG_DIR}/kpnd.out" 297 TERM="dumb" $BIN/kpn $OPTIONS >> ${LOG_DIR}/kpnd.out 2>&1 & 298 RETVAL=$? 299 PIDNUM=$! 300 set +f 301 if [ $RETVAL = 0 ]; then 302 echo $PIDNUM > ${pidfile} 303 echo "Success to start node." 304 else 305 echo "Fail to start node." 306 fi 307 return $RETVAL 308 } 309 310 stop_node() { 311 __pid_run 312 [ -z "$pid" ] && echo "kpnd is not running" && return 313 echo -n "Shutting down kpnd: " 314 __kill_timeout $(cat ${pidfile}) && rm -f ${pidfile} 315 } 316 317 status_node() { 318 __pid_run 319 if [ -n "$pid" ]; then 320 echo "kpnd is running" 321 else 322 echo "kpnd is down" 323 fi 324 } 325 326 #--------------------- Public functions -------------------------- 327 start() { 328 if [ ! -d $LOG_DIR ]; then 329 mkdir -p $LOG_DIR 330 fi 331 332 if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then 333 start_auto_restart_daemon 334 else 335 start_node 336 fi 337 } 338 339 start_docker() { 340 echo -n "Starting kpnd: " 341 __check_option 342 343 echo "$BIN/kpn $OPTIONS" 344 $BIN/kpn $OPTIONS 345 } 346 347 stop() { 348 if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then 349 stop_auto_restart_daemon 350 fi 351 stop_node 352 } 353 354 status() { 355 status_node 356 if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then 357 status_auto_restart_daemon 358 fi 359 } 360 361 restart() { 362 stop 363 sleep 3 364 start 365 } 366 367 case "$1" in 368 start) 369 start 370 ;; 371 start-docker) 372 start_docker 373 ;; 374 stop) 375 stop 376 ;; 377 status) 378 status 379 ;; 380 restart) 381 restart 382 ;; 383 *) 384 echo "Usages: kpnd {start|start-docker|stop|restart|status}" 385 exit 1 386 ;; 387 esac 388 exit 0