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