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