github.com/klaytn/klaytn@v1.12.1/build/packaging/linux/bin/kspnd (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/kspnd.conf
     9  
    10  if [ -z $DATA_DIR ]; then
    11      echo
    12      echo "  [ERROR] : DATA_DIR in conf/kspnd.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/kspnd.conf - DATA_DIR=$DATA_DIR]"
    19      exit 1
    20  fi
    21  
    22  pidfile=$DATA_DIR/kspnd.pid
    23  auto_restart_daemon_pidfile=$DATA_DIR/restart_daemon_kspnd.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      # private network only for Service Chain
   185      if [[ -z $NETWORK_ID ]]; then
   186          echo
   187          echo "[ERROR] network id should be specified for Service Chain."
   188          exit 1
   189      else
   190          OPTIONS="$OPTIONS --networkid $NETWORK_ID"
   191          echo "[INFO] creating a private network: $NETWORK_ID"
   192          if [[ ! -z $NETWORK ]]; then
   193              echo
   194              echo "[WARN] ignoring the specified network for Service Chain: $NETWORK"
   195          fi
   196      fi
   197  
   198      if [ ! -z $DATA_DIR ]; then
   199          OPTIONS="$OPTIONS --datadir $DATA_DIR"
   200      fi
   201  
   202      if [ ! -z $PORT ]; then
   203          OPTIONS="$OPTIONS --port $PORT"
   204      fi
   205  
   206      if [ ! -z $SUBPORT ]; then
   207          OPTIONS="$OPTIONS --subport $SUBPORT"
   208      fi
   209  
   210      if [ ! -z $SERVER_TYPE ]; then
   211          OPTIONS="$OPTIONS --srvtype $SERVER_TYPE"
   212      fi
   213  
   214      if [ ! -z $VERBOSITY ]; then
   215          OPTIONS="$OPTIONS --verbosity $VERBOSITY"
   216      fi
   217  
   218      if [ ! -z $TXPOOL_EXEC_SLOTS_ALL ]; then
   219          OPTIONS="$OPTIONS --txpool.exec-slots.all $TXPOOL_EXEC_SLOTS_ALL"
   220      fi
   221  
   222      if [ ! -z $TXPOOL_NONEXEC_SLOTS_ALL ]; then
   223          OPTIONS="$OPTIONS --txpool.nonexec-slots.all $TXPOOL_NONEXEC_SLOTS_ALL"
   224      fi
   225  
   226      if [ ! -z $TXPOOL_EXEC_SLOTS_ACCOUNT ]; then
   227          OPTIONS="$OPTIONS --txpool.exec-slots.account $TXPOOL_EXEC_SLOTS_ACCOUNT"
   228      fi
   229  
   230      if [ ! -z $TXPOOL_NONEXEC_SLOTS_ACCOUNT ]; then
   231          OPTIONS="$OPTIONS --txpool.nonexec-slots.account $TXPOOL_NONEXEC_SLOTS_ACCOUNT"
   232      fi
   233  
   234      if [ ! -z $SYNCMODE ]; then
   235          OPTIONS="$OPTIONS --syncmode $SYNCMODE"
   236      fi
   237  
   238      if [ ! -z $MAXCONNECTIONS ]; then
   239          OPTIONS="$OPTIONS --maxconnections $MAXCONNECTIONS"
   240      fi
   241  
   242      if [ ! -z $LDBCACHESIZE ]; then
   243          OPTIONS="$OPTIONS --db.leveldb.cache-size $LDBCACHESIZE"
   244      fi
   245  
   246      if [[ ! -z $NO_DISCOVER ]] && [[ $NO_DISCOVER -eq 1 ]]; then
   247          OPTIONS="$OPTIONS --nodiscover"
   248      fi
   249  
   250      if [[ ! -z $BOOTNODES ]] && [[ $BOOTNODES != "" ]]; then
   251          OPTIONS="$OPTIONS --bootnodes $BOOTNODES"
   252      fi
   253  
   254      if [[ ! -z $SC_MAIN_BRIDGE ]] && [[ $SC_MAIN_BRIDGE -eq 1 ]]; then
   255          OPTIONS="$OPTIONS --mainbridge --mainbridgeport $SC_MAIN_BRIDGE_PORT"
   256          if [[ ! -z $SC_MAIN_BRIDGE_INDEXING ]] && [[ $SC_MAIN_BRIDGE_INDEXING -eq 1 ]]; then
   257              OPTIONS="$OPTIONS --childchainindexing"
   258          fi
   259      fi
   260  
   261      if [[ ! -z $SC_SUB_BRIDGE ]] && [[ $SC_SUB_BRIDGE -eq 1 ]]; then
   262          OPTIONS="$OPTIONS --subbridge --subbridgeport $SC_SUB_BRIDGE_PORT --chaintxperiod $SC_ANCHORING_PERIOD --chaintxlimit $SC_TX_LIMIT "
   263          OPTIONS="$OPTIONS --parentchainid $SC_PARENT_CHAIN_ID"
   264          if [[ ! -z $SC_ANCHORING ]] && [[ $SC_ANCHORING -eq 1 ]]; then
   265              OPTIONS="$OPTIONS --anchoring"
   266          fi
   267  
   268          if [[ ! -z $SC_KAS_ANCHOR ]] && [[ $SC_KAS_ANCHOR -eq 1 ]]; then
   269              OPTIONS="$OPTIONS --kas.sc.anchor"
   270  
   271              if [[ ! -z $SC_KAS_ANCHOR_PERIOD ]]; then
   272                  OPTIONS="$OPTIONS --kas.sc.anchor.period $SC_KAS_ANCHOR_PERIOD"
   273              fi
   274  
   275              if [[ -z $SC_KAS_ANCHOR_URL ]]; then
   276                  echo
   277                  echo "[ERROR] kas.sc.anchor.url should be specified for KAS Anchor."
   278                  exit 1
   279              fi
   280              OPTIONS="$OPTIONS --kas.sc.anchor.url $SC_KAS_ANCHOR_URL"
   281  
   282              if [[ -z $SC_KAS_ANCHOR_OPERATOR ]]; then
   283                  echo
   284                  echo "[ERROR] kas.sc.anchor.operator should be specified for KAS Anchor."
   285                  exit 1
   286              fi
   287              OPTIONS="$OPTIONS --kas.sc.anchor.operator $SC_KAS_ANCHOR_OPERATOR"
   288  
   289              if [[ -z $SC_KAS_ANCHOR_ACCESS_KEY ]]; then
   290                  echo
   291                  echo "[ERROR] kas.accesskey should be specified for KAS Anchor."
   292                  exit 1
   293              fi
   294              OPTIONS="$OPTIONS --kas.accesskey $SC_KAS_ANCHOR_ACCESS_KEY"
   295  
   296              if [[ -z $SC_KAS_ANCHOR_SECRET_KEY ]]; then
   297                  echo
   298                  echo "[ERROR] kas.secretkey should be specified for KAS Anchor."
   299                  exit 1
   300              fi
   301              OPTIONS="$OPTIONS --kas.secretkey $SC_KAS_ANCHOR_SECRET_KEY"
   302  
   303              if [[ -z $SC_KAS_ANCHOR_X_CHAIN_ID ]]; then
   304                  echo
   305                  echo "[ERROR] kas.x-chain-id should be specified for KAS Anchor."
   306                  exit 1
   307              fi
   308              OPTIONS="$OPTIONS --kas.x-chain-id $SC_KAS_ANCHOR_X_CHAIN_ID"
   309  
   310              if [ ! -z $SC_KAS_ANCHOR_REQUEST_TIMEOUT ]; then
   311                  OPTIONS="$OPTIONS --kas.sc.anchor.request.timeout $SC_KAS_ANCHOR_REQUEST_TIMEOUT"
   312              fi
   313          fi
   314      fi
   315  
   316      if [ ! -z $SC_PARENT_OPERATOR_GASLIMIT ]; then
   317          OPTIONS="$OPTIONS --sc.parentoperator.gaslimit $SC_PARENT_OPERATOR_GASLIMIT"
   318      fi
   319  
   320      if [ ! -z $SC_CHILD_OPERATOR_GASLIMIT ]; then
   321          OPTIONS="$OPTIONS --sc.childoperator.gaslimit $SC_CHILD_OPERATOR_GASLIMIT"
   322      fi
   323  
   324      if [[ (! -z $VTRECOVERY && $VTRECOVERY -eq 1 ) || (! -z $SC_VTRECOVERY && $SC_VTRECOVERY -eq 1) ]]; then
   325          OPTIONS="$OPTIONS --vtrecovery"
   326          if [[ ! -z $SC_VTRECOVERY_INTERVAL ]]; then
   327              OPTIONS="$OPTIONS --vtrecoveryinterval $SC_VTRECOVERY_INTERVAL"
   328          fi
   329      fi
   330  
   331      if [[ ! -z $ADDITIONAL ]] && [[ $ADDITIONAL != "" ]]; then
   332          OPTIONS="$OPTIONS $ADDITIONAL"
   333      fi
   334  
   335      if [[ ! -z $LOG_ROTATE ]] && [[ $LOG_ROTATE -eq 1 ]]; then
   336          OPTIONS="$OPTIONS --log.rotate"
   337          if [[ ! -z $LOG_MAXSIZE ]]; then
   338            OPTIONS="$OPTIONS --log.maxsize $LOG_MAXSIZE"
   339          fi
   340          if [[ ! -z $LOG_MAXBACKUP ]]; then
   341            OPTIONS="$OPTIONS --log.maxbackup $LOG_MAXBACKUP"
   342          fi
   343          if [[ ! -z $LOG_MAXAGE ]]; then
   344            OPTIONS="$OPTIONS --log.maxage $LOG_MAXAGE"
   345          fi
   346          if [[ ! -z $LOG_COMPRESS ]] && [[ $LOG_COMPRESS -eq 1 ]]; then
   347            OPTIONS="$OPTIONS --log.compress"
   348          fi
   349      fi
   350  }
   351  
   352  start_node() {
   353      __pid_run
   354      [ -n "$pid" ] && echo "kspnd already running...[$pid]" && return
   355  
   356      echo -n "Starting kspnd: "
   357  
   358      __check_option
   359  
   360      BASEDIR="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
   361      CURRENTFILE=`basename "$0"`
   362      OPTIONS="$OPTIONS --autorestart.daemon.path $BASEDIR/$CURRENTFILE"
   363  
   364      OPTIONS="$OPTIONS --log.file ${LOG_DIR}/kspnd.out"
   365      TERM="dumb" $BIN/kspn $OPTIONS >> ${LOG_DIR}/kspnd.out 2>&1 &
   366      RETVAL=$?
   367      PIDNUM=$!
   368      set +f
   369      if [ $RETVAL = 0 ]; then
   370          echo $PIDNUM > ${pidfile}
   371          echo "Success to start node."
   372      else
   373          echo "Fail to start node."
   374      fi
   375      return $RETVAL
   376  }
   377  
   378  stop_node() {
   379      __pid_run
   380      [ -z "$pid" ] && echo "kspnd is not running" && return
   381      echo -n "Shutting down kspnd: "
   382      __kill_timeout $(cat ${pidfile}) && rm -f ${pidfile}
   383  }
   384  
   385  status_node() {
   386      __pid_run
   387      if [ -n "$pid" ]; then
   388          echo "kspnd is running"
   389      else
   390          echo "kspnd is down"
   391      fi
   392  }
   393  
   394  #--------------------- Public functions --------------------------
   395  start() {
   396      if [ ! -d $LOG_DIR ]; then
   397          mkdir -p $LOG_DIR
   398      fi
   399  
   400      if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then
   401          start_auto_restart_daemon
   402      else
   403          start_node
   404      fi
   405  }
   406  
   407  start_docker() {
   408      echo -n "Starting kspnd: "
   409      __check_option
   410  
   411      echo "$BIN/kspn $OPTIONS"
   412      $BIN/kspn $OPTIONS
   413  }
   414  
   415  stop() {
   416      if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then
   417          stop_auto_restart_daemon
   418      fi
   419      stop_node
   420  }
   421  
   422  
   423  status() {
   424      status_node
   425      if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then
   426          status_auto_restart_daemon
   427      fi
   428  }
   429  
   430  restart() {
   431      stop
   432      sleep 3
   433      start
   434  }
   435  
   436  case "$1" in
   437      start)
   438          start
   439          ;;
   440      start-docker)
   441          start_docker
   442          ;;
   443      stop)
   444          stop
   445          ;;
   446      status)
   447          status
   448          ;;
   449      restart)
   450          restart
   451          ;;
   452      *)
   453          echo "Usages: kspnd {start|start-docker|stop|restart|status}"
   454          exit 1
   455          ;;
   456  esac
   457  exit 0