github.com/klaytn/klaytn@v1.12.1/build/packaging/linux/bin/kend (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/kend.conf
     9  
    10  if [ -z $DATA_DIR ]; then
    11      echo
    12      echo "  [ERROR] : DATA_DIR in conf/kend.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/kend.conf - DATA_DIR=$DATA_DIR]"
    19      exit 1
    20  fi
    21  
    22  pidfile=$DATA_DIR/kend.pid
    23  auto_restart_daemon_pidfile=$DATA_DIR/restart_daemon_kend.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          if [ ! -z $RPC_CONCURRENCYLIMIT ]; then
   166              OPTIONS="$OPTIONS --rpc.concurrencylimit $RPC_CONCURRENCYLIMIT"
   167          fi
   168          if [ ! -z $RPC_READ_TIMEOUT ]; then
   169              OPTIONS="$OPTIONS --rpcreadtimeout $RPC_READ_TIMEOUT"
   170          fi
   171          if [ ! -z $RPC_WRITE_TIMEOUT ]; then
   172              OPTIONS="$OPTIONS --rpcwritetimeout $RPC_WRITE_TIMEOUT"
   173          fi
   174          if [ ! -z $RPC_IDLE_TIMEOUT ]; then
   175              OPTIONS="$OPTIONS --rpcidletimeout $RPC_IDLE_TIMEOUT"
   176          fi
   177          if [ ! -z $RPC_EXECUTION_TIMEOUT ]; then
   178              OPTIONS="$OPTIONS --rpcexecutiontimeout $RPC_EXECUTION_TIMEOUT"
   179          fi
   180      fi
   181  
   182      if [[ ! -z $WS_ENABLE ]] && [[ $WS_ENABLE -eq 1 ]]; then
   183          OPTIONS="$OPTIONS --ws"
   184          WS_API=`echo $WS_API | tr -d "[:space:]"`
   185          if [ ! -z $WS_API ]; then
   186              OPTIONS="$OPTIONS --wsapi $WS_API"
   187          fi
   188          if [ ! -z $WS_PORT ]; then
   189              OPTIONS="$OPTIONS --wsport $WS_PORT"
   190          fi
   191          if [ ! -z $WS_ADDR ]; then
   192              OPTIONS="$OPTIONS --wsaddr $WS_ADDR"
   193          fi
   194          if [ ! -z $WS_ORIGINS ]; then
   195              OPTIONS="$OPTIONS --wsorigins $WS_ORIGINS"
   196          fi
   197      fi
   198  
   199      # Cypress network => NETWORK_ID is null && NETWORK = "cypress"
   200      # Baobab network => NETWORK_ID is null && NETWORK = "baobab"
   201      # Else => private network
   202      if [[ -z $NETWORK_ID ]]; then
   203          if [[ $NETWORK == "baobab" ]]; then
   204              OPTIONS="$OPTIONS --baobab"
   205          elif [[ $NETWORK == "cypress" ]]; then
   206              OPTIONS="$OPTIONS --cypress"
   207          else
   208              echo
   209              echo "[ERROR] network id is not specified and network is not available."
   210              echo "Available network: baobab, cypress"
   211              exit 1
   212          fi
   213      else
   214          OPTIONS="$OPTIONS --networkid $NETWORK_ID"
   215          echo "[INFO] creating a private network: $NETWORK_ID"
   216          if [[ ! -z $NETWORK ]]; then
   217              echo
   218              echo "[WARN] ignoring the specified network: $NETWORK"
   219          fi
   220      fi
   221  
   222      if [ ! -z $DATA_DIR ]; then
   223          OPTIONS="$OPTIONS --datadir $DATA_DIR"
   224      fi
   225  
   226      if [ ! -z $PORT ]; then
   227          OPTIONS="$OPTIONS --port $PORT"
   228      fi
   229  
   230      if [ ! -z $SUBPORT ]; then
   231          OPTIONS="$OPTIONS --subport $SUBPORT"
   232      fi
   233  
   234      if [ ! -z $SERVER_TYPE ]; then
   235          OPTIONS="$OPTIONS --srvtype $SERVER_TYPE"
   236      fi
   237  
   238      if [ ! -z $VERBOSITY ]; then
   239          OPTIONS="$OPTIONS --verbosity $VERBOSITY"
   240      fi
   241  
   242      if [ ! -z $TXPOOL_EXEC_SLOTS_ALL ]; then
   243          OPTIONS="$OPTIONS --txpool.exec-slots.all $TXPOOL_EXEC_SLOTS_ALL"
   244      fi
   245  
   246      if [ ! -z $TXPOOL_NONEXEC_SLOTS_ALL ]; then
   247          OPTIONS="$OPTIONS --txpool.nonexec-slots.all $TXPOOL_NONEXEC_SLOTS_ALL"
   248      fi
   249  
   250      if [ ! -z $TXPOOL_EXEC_SLOTS_ACCOUNT ]; then
   251          OPTIONS="$OPTIONS --txpool.exec-slots.account $TXPOOL_EXEC_SLOTS_ACCOUNT"
   252      fi
   253  
   254      if [ ! -z $TXPOOL_NONEXEC_SLOTS_ACCOUNT ]; then
   255          OPTIONS="$OPTIONS --txpool.nonexec-slots.account $TXPOOL_NONEXEC_SLOTS_ACCOUNT"
   256      fi
   257  
   258      if [ ! -z $SYNCMODE ]; then
   259          OPTIONS="$OPTIONS --syncmode $SYNCMODE"
   260      fi
   261  
   262      if [ ! -z $MAXCONNECTIONS ]; then
   263          OPTIONS="$OPTIONS --maxconnections $MAXCONNECTIONS"
   264      fi
   265  
   266      if [ ! -z $LDBCACHESIZE ]; then
   267          OPTIONS="$OPTIONS --db.leveldb.cache-size $LDBCACHESIZE"
   268      fi
   269  
   270      if [[ ! -z $SC_MAIN_BRIDGE ]] && [[ $SC_MAIN_BRIDGE -eq 1 ]]; then
   271          OPTIONS="$OPTIONS --mainbridge --mainbridgeport $SC_MAIN_BRIDGE_PORT"
   272          if [[ ! -z $SC_MAIN_BRIDGE_INDEXING ]] && [[ $SC_MAIN_BRIDGE_INDEXING -eq 1 ]]; then
   273              OPTIONS="$OPTIONS --childchainindexing"
   274          fi
   275      fi
   276  
   277      if [[ ! -z $NO_DISCOVER ]] && [[ $NO_DISCOVER -eq 1 ]]; then
   278          OPTIONS="$OPTIONS --nodiscover"
   279      fi
   280  
   281      if [[ ! -z $BOOTNODES ]] && [[ $BOOTNODES != "" ]]; then
   282          OPTIONS="$OPTIONS --bootnodes $BOOTNODES"
   283      fi
   284  
   285      if [[ ! -z $ADDITIONAL ]] && [[ $ADDITIONAL != "" ]]; then
   286          OPTIONS="$OPTIONS $ADDITIONAL"
   287      fi
   288  
   289      if [[ ! -z $AUTO_RESTART ]] && [[ $AUTO_RESTART -eq 1 ]]; then
   290          OPTIONS="$OPTIONS --autorestart.enable"
   291      fi
   292  
   293      if [[ ! -z $LOG_ROTATE ]] && [[ $LOG_ROTATE -eq 1 ]]; then
   294          OPTIONS="$OPTIONS --log.rotate"
   295          if [[ ! -z $LOG_MAXSIZE ]]; then
   296            OPTIONS="$OPTIONS --log.maxsize $LOG_MAXSIZE"
   297          fi
   298          if [[ ! -z $LOG_MAXBACKUP ]]; then
   299            OPTIONS="$OPTIONS --log.maxbackup $LOG_MAXBACKUP"
   300          fi
   301          if [[ ! -z $LOG_MAXAGE ]]; then
   302            OPTIONS="$OPTIONS --log.maxage $LOG_MAXAGE"
   303          fi
   304          if [[ ! -z $LOG_COMPRESS ]] && [[ $LOG_COMPRESS -eq 1 ]]; then
   305            OPTIONS="$OPTIONS --log.compress"
   306          fi
   307      fi
   308  
   309      if [[ ! -z $DB_TYPE ]] && [[ $DB_TYPE != "" ]]; then
   310          OPTIONS="$OPTIONS --dbtype $DB_TYPE"
   311      fi
   312  
   313      if [[ ! -z $DB_SINGLE ]] && [[ $DB_SINGLE -eq 1 ]]; then
   314          OPTIONS="$OPTIONS --db.single"
   315      fi
   316  
   317      if [[ ! -z $DB_DYNAMO_TABLENAME ]] && [[ $DB_DYNAMO_TABLENAME != "" ]]; then
   318          OPTIONS="$OPTIONS --db.dynamo.tablename $DB_DYNAMO_TABLENAME"
   319      fi
   320  
   321      if [[ ! -z $DB_DYNAMO_READONLY ]] && [[ $DB_DYNAMO_READONLY -eq 1 ]]; then
   322          OPTIONS="$OPTIONS --db.dynamo.read-only"
   323      fi
   324  
   325      if [[ ! -z $GCMODE ]] && [[ $GCMODE != "" ]]; then
   326          OPTIONS="$OPTIONS --gcmode $GCMODE"
   327      fi
   328  
   329      if [[ ! -z $STATE_TRIE_CACHE_LIMIT ]] && [[ $STATE_TRIE_CACHE_LIMIT != "" ]]; then
   330          OPTIONS="$OPTIONS --state.trie-cache-limit $STATE_TRIE_CACHE_LIMIT"
   331      fi
   332  
   333      if [[ ! -z $STATEDB_CACHE_TYPE ]] && [[ $STATEDB_CACHE_TYPE != "" ]]; then
   334          OPTIONS="$OPTIONS --statedb.cache.type $STATEDB_CACHE_TYPE"
   335          if [[ ! -z $STATEDB_REDIS_ENDPOINT ]] && [[ $STATEDB_REDIS_ENDPOINT != "" ]]; then
   336              OPTIONS="$OPTIONS --statedb.cache.redis.endpoints $STATEDB_REDIS_ENDPOINT"
   337          fi
   338  
   339          if [[ ! -z $STATEDB_REDIS_PUBLISH ]] && [[ $STATEDB_REDIS_PUBLISH -eq 1 ]]; then
   340              OPTIONS="$OPTIONS --statedb.cache.redis.publish"
   341          fi
   342  
   343          if [[ ! -z $STATEDB_REDIS_SUBSCRIBE ]] && [[ $STATEDB_REDIS_SUBSCRIBE -eq 1 ]]; then
   344              OPTIONS="$OPTIONS --statedb.cache.redis.subscribe"
   345          fi
   346      fi
   347  
   348      if [[ ! -z $VM_INTERNALTX ]] && [[ $VM_INTERNALTX -eq 1 ]]; then
   349          OPTIONS="$OPTIONS --vm.internaltx"
   350      fi
   351  
   352      if [[ ! -z $KES_NODETYPE_SERVICE_ENABLE ]] && [[ $KES_NODETYPE_SERVICE_ENABLE -eq 1 ]]; then
   353          OPTIONS="$OPTIONS --kes.nodetype.service"
   354      fi
   355  }
   356  
   357  start_node() {
   358      __pid_run
   359      [ -n "$pid" ] && echo "kend already running...[$pid]" && return
   360  
   361      echo -n "Starting kend: "
   362  
   363      __check_option
   364  
   365      BASEDIR="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
   366      CURRENTFILE=`basename "$0"`
   367      OPTIONS="$OPTIONS --autorestart.daemon.path $BASEDIR/$CURRENTFILE"
   368  
   369      OPTIONS="$OPTIONS --log.file ${LOG_DIR}/kend.out"
   370      TERM="dumb" $BIN/ken $OPTIONS >> ${LOG_DIR}/kend.out 2>&1 &
   371      RETVAL=$?
   372      PIDNUM=$!
   373      set +f
   374      if [ $RETVAL = 0 ]; then
   375          echo $PIDNUM > ${pidfile}
   376          echo "Success to start node."
   377      else
   378          echo "Fail to start node."
   379      fi
   380      return $RETVAL
   381  }
   382  
   383  stop_node() {
   384      __pid_run
   385      [ -z "$pid" ] && echo "kend is not running" && return
   386      echo -n "Shutting down kend: "
   387      __kill_timeout $(cat ${pidfile}) && rm -f ${pidfile}
   388  }
   389  
   390  status_node() {
   391      __pid_run
   392      if [ -n "$pid" ]; then
   393          echo "kend is running"
   394      else
   395          echo "kend is down"
   396      fi
   397  }
   398  
   399  #--------------------- Public functions --------------------------
   400  start() {
   401      if [ ! -d $LOG_DIR ]; then
   402          mkdir -p $LOG_DIR
   403      fi
   404  
   405      if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then
   406          start_auto_restart_daemon
   407      else
   408          start_node
   409      fi
   410  }
   411  
   412  start_docker() {
   413      echo -n "Starting kend: "
   414      __check_option
   415  
   416      echo "$BIN/ken $OPTIONS"
   417      $BIN/ken $OPTIONS
   418  }
   419  
   420  stop() {
   421      if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then
   422          stop_auto_restart_daemon
   423      fi
   424      stop_node
   425  }
   426  
   427  
   428  status() {
   429      status_node
   430      if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then
   431          status_auto_restart_daemon
   432      fi
   433  }
   434  
   435  
   436  restart() {
   437      stop
   438      sleep 3
   439      start
   440  }
   441  
   442  case "$1" in
   443      start)
   444          start
   445          ;;
   446      start-docker)
   447          start_docker
   448          ;;
   449      stop)
   450          stop
   451          ;;
   452      status)
   453          status
   454          ;;
   455      restart)
   456          restart
   457          ;;
   458      *)
   459          echo "Usages: kend {start|start-docker|stop|restart|status}"
   460          exit 1
   461          ;;
   462  esac
   463  exit 0