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