github.com/klaytn/klaytn@v1.12.1/build/rpm/etc/init.d/kspnd (about)

     1  #!/bin/bash
     2  #
     3  # kspnd         Startup script for the kspn
     4  #
     5  # chkconfig: - 85 15
     6  # description : kspnd is the Klaytn proxy node daemon
     7  #
     8  # processname: kspnd
     9  # config:  /etc/kspnd/conf/kspnd.conf
    10  # pidfile: /var/run/kspnd.pid
    11  #
    12  
    13  # Source function library.
    14  . /etc/init.d/functions
    15  
    16  if [ -f /etc/kspnd/conf/kspnd.conf ]; then
    17          . /etc/kspnd/conf/kspnd.conf
    18  fi
    19  
    20  kspn=/usr/bin/kspn
    21  prog=kspnd
    22  lockfile=${LOCKFILE-/var/lock/subsys/kspnd}
    23  pidfile=${PIDFILE-/var/run/kspnd.pid}
    24  auto_restart_daemon_pidfile=/var/run/restart_daemon_kspnd.pid
    25  RETVAL=0
    26  STOP_TIMEOUT=${STOP_TIMEOUT-10}
    27  
    28  set -f
    29  OPTIONS=""
    30  
    31  __pid_run() {
    32    __pids_var_run $prog $pidfile
    33  }
    34  
    35  __auto_restart_daemon_pid_run() {
    36      unset auto_restart_daemon_pid
    37      if [ ! -f $auto_restart_daemon_pidfile ]; then
    38          return
    39      fi
    40      AUTO_RESTART_DAEMON_PID_NUM=$(cat $auto_restart_daemon_pidfile)
    41      if [[ ! -z "$AUTO_RESTART_DAEMON_PID_NUM" ]]; then
    42          export auto_restart_daemon_pid=$(ps -p $AUTO_RESTART_DAEMON_PID_NUM -o pid=)
    43      fi
    44  }
    45  #------------------------Related Auto restart daemon functions-----------------------------
    46  __auto_restart_daemon() {
    47      local backOffTime=$AUTO_RESTART_INTERVAL
    48      local coeff=2
    49      while :
    50      do
    51          sleep 1
    52          __pid_run
    53          if [ -z "$pid" ]; then
    54              echo "INFO[`date`] node[${PID_NUM}] is down"
    55              if [ -f $pidfile ]; then
    56                  echo "INFO[`date`] remove redundant pid file"
    57                  rm -f ${lockfile} ${pidfile}
    58              fi
    59              echo "INFO[`date`] Sleep for backOffTime.... ${backOffTime} seconds."
    60              sleep $backOffTime
    61              echo -n "INFO[`date`] "
    62              start_node
    63              backOffTime=$(echo $backOffTime $coeff | awk '{printf "%.1f\n",$1*$2}')
    64  
    65              echo "INFO[`date`] backOffTime = ${backOffTime}, Restarted node pid = ${PID_NUM}"
    66              PID_NUM=$(cat $pidfile)
    67              echo ""
    68          fi
    69      done
    70  }
    71  
    72  start_auto_restart_daemon() {
    73      __auto_restart_daemon_pid_run
    74      if [ -z $auto_restart_daemon_pid ]; then
    75          __auto_restart_daemon >> ${LOG_DIR}/restart_daemon.out 2>&1 &
    76          disown
    77          AUTO_RESTART_DAEMON_PID_NUM=$!
    78          AUTO_RESTART_DAEMON_RETVAL=$?
    79  
    80          set +f
    81          if [ $AUTO_RESTART_DAEMON_RETVAL = 0 ]; then
    82              echo $AUTO_RESTART_DAEMON_PID_NUM > ${auto_restart_daemon_pidfile}
    83              echo "Success to start auto restart daemon."
    84          else
    85              echo "Fail to start auto restart daemon."
    86          fi
    87      fi
    88  }
    89  
    90  stop_auto_restart_daemon() {
    91      echo -n "Shutting down auto restart daemon: "
    92      killproc -p ${auto_restart_daemon_pidfile} -d ${STOP_TIMEOUT}
    93      RETVAL=$?
    94      echo
    95      [ $RETVAL = 0 ] && rm -f ${auto_restart_daemon_lockfile} ${auto_restart_daemon_pidfile}
    96  }
    97  
    98  status_auto_restart_daemon() {
    99      __auto_restart_daemon_pid_run
   100      if [ -n "$auto_restart_daemon_pid" ]; then
   101          echo "auto restart daemon is running."
   102      else
   103          echo "auto restart daemon is down."
   104      fi
   105  }
   106  
   107  #------------------------Related to Klaytn node functions-----------------------------
   108  
   109  if [[ ! -z $METRICS ]] && [[ $METRICS -eq 1 ]]; then
   110      OPTIONS="$OPTIONS --metrics"
   111  fi
   112  
   113  if [[ ! -z $PROMETHEUS ]] && [[ $PROMETHEUS -eq 1 ]]; then
   114      OPTIONS="$OPTIONS --prometheus"
   115  fi
   116  
   117  if [[ ! -z $DB_NO_PARALLEL_WRITE ]] && [[ $DB_NO_PARALLEL_WRITE -eq 1 ]]; then
   118      OPTIONS="$OPTIONS --db.no-parallel-write"
   119  fi
   120  
   121  if [[ ! -z $MULTICHANNEL ]] && [[ $MULTICHANNEL -eq 1 ]]; then
   122      OPTIONS="$OPTIONS --multichannel"
   123  fi
   124  
   125  if [[ ! -z $RPC_ENABLE ]] && [[ $RPC_ENABLE -eq 1 ]]; then
   126      OPTIONS="$OPTIONS --rpc"
   127      RPC_API=`echo $RPC_API | tr -d "[:space:]"`
   128      if [ ! -z $RPC_API ]; then
   129          OPTIONS="$OPTIONS --rpcapi $RPC_API"
   130      fi
   131      if [ ! -z $RPC_PORT ]; then
   132          OPTIONS="$OPTIONS --rpcport $RPC_PORT"
   133      fi
   134      if [ ! -z $RPC_ADDR ]; then
   135          OPTIONS="$OPTIONS --rpcaddr $RPC_ADDR"
   136      fi
   137      if [ ! -z $RPC_CORSDOMAIN ]; then
   138          OPTIONS="$OPTIONS --rpccorsdomain $RPC_CORSDOMAIN"
   139      fi
   140      if [ ! -z $RPC_VHOSTS ]; then
   141          OPTIONS="$OPTIONS --rpcvhosts $RPC_VHOSTS"
   142      fi
   143  fi
   144  
   145  if [[ ! -z $WS_ENABLE ]] && [[ $WS_ENABLE -eq 1 ]]; then
   146      OPTIONS="$OPTIONS --ws"
   147      WS_API=`echo $WS_API | tr -d "[:space:]"`
   148      if [ ! -z $WS_API ]; then
   149          OPTIONS="$OPTIONS --wsapi $WS_API"
   150      fi
   151      if [ ! -z $WS_PORT ]; then
   152          OPTIONS="$OPTIONS --wsport $WS_PORT"
   153      fi
   154      if [ ! -z $WS_ADDR ]; then
   155          OPTIONS="$OPTIONS --wsaddr $WS_ADDR"
   156      fi
   157      if [ ! -z $WS_ORIGINS ]; then
   158          OPTIONS="$OPTIONS --wsorigins $WS_ORIGINS"
   159      fi
   160  fi
   161  
   162  # private network only for Service Chain
   163  if [[ -z $NETWORK_ID ]]; then
   164      echo
   165      echo "[ERROR] network id should be specified for Service Chain."
   166      exit 1
   167  else
   168      OPTIONS="$OPTIONS --networkid $NETWORK_ID"
   169      echo "[INFO] creating a private network: $NETWORK_ID"
   170      if [[ ! -z $NETWORK ]]; then
   171          echo
   172          echo "[WARN] ignoring the specified network for Service Chain: $NETWORK"
   173      fi
   174  fi
   175  
   176  if [ ! -z $DATA_DIR ]; then
   177      OPTIONS="$OPTIONS --datadir $DATA_DIR"
   178  fi
   179  
   180  if [ ! -z $PORT ]; then
   181      OPTIONS="$OPTIONS --port $PORT"
   182  fi
   183  
   184  if [ ! -z $SUBPORT ]; then
   185      OPTIONS="$OPTIONS --subport $SUBPORT"
   186  fi
   187  
   188  if [ ! -z $SERVER_TYPE ]; then
   189      OPTIONS="$OPTIONS --srvtype $SERVER_TYPE"
   190  fi
   191  
   192  if [ ! -z $VERBOSITY ]; then
   193      OPTIONS="$OPTIONS --verbosity $VERBOSITY"
   194  fi
   195  
   196  if [ ! -z $TXPOOL_EXEC_SLOTS_ALL ]; then
   197      OPTIONS="$OPTIONS --txpool.exec-slots.all $TXPOOL_EXEC_SLOTS_ALL"
   198  fi
   199  
   200  if [ ! -z $TXPOOL_NONEXEC_SLOTS_ALL ]; then
   201      OPTIONS="$OPTIONS --txpool.nonexec-slots.all $TXPOOL_NONEXEC_SLOTS_ALL"
   202  fi
   203  
   204  if [ ! -z $TXPOOL_EXEC_SLOTS_ACCOUNT ]; then
   205      OPTIONS="$OPTIONS --txpool.exec-slots.account $TXPOOL_EXEC_SLOTS_ACCOUNT"
   206  fi
   207  
   208  if [ ! -z $TXPOOL_NONEXEC_SLOTS_ACCOUNT ]; then
   209      OPTIONS="$OPTIONS --txpool.nonexec-slots.account $TXPOOL_NONEXEC_SLOTS_ACCOUNT"
   210  fi
   211  
   212  if [ ! -z $TXPOOL_LIFE_TIME ]; then
   213      OPTIONS="$OPTIONS --txpool.lifetime $TXPOOL_LIFE_TIME"
   214  fi
   215  
   216  if [ ! -z $SYNCMODE ]; then
   217      OPTIONS="$OPTIONS --syncmode $SYNCMODE"
   218  fi
   219  
   220  if [ ! -z $MAXCONNECTIONS ]; then
   221      OPTIONS="$OPTIONS --maxconnections $MAXCONNECTIONS"
   222  fi
   223  
   224  if [ ! -z $LDBCACHESIZE ]; then
   225      OPTIONS="$OPTIONS --db.leveldb.cache-size $LDBCACHESIZE"
   226  fi
   227  
   228  if [[ ! -z $NO_DISCOVER ]] && [[ $NO_DISCOVER -eq 1 ]]; then
   229      OPTIONS="$OPTIONS --nodiscover"
   230  fi
   231  
   232  if [[ ! -z $BOOTNODES ]] && [[ $BOOTNODES != "" ]]; then
   233      OPTIONS="$OPTIONS --bootnodes $BOOTNODES"
   234  fi
   235  
   236  if [[ ! -z $SC_MAIN_BRIDGE ]] && [[ $SC_MAIN_BRIDGE -eq 1 ]]; then
   237      OPTIONS="$OPTIONS --mainbridge --mainbridgeport $SC_MAIN_BRIDGE_PORT"
   238      if [[ ! -z $SC_MAIN_BRIDGE_INDEXING ]] && [[ $SC_MAIN_BRIDGE_INDEXING -eq 1 ]]; then
   239          OPTIONS="$OPTIONS --childchainindexing"
   240      fi
   241  fi
   242  
   243  if [[ ! -z $SC_SUB_BRIDGE ]] && [[ $SC_SUB_BRIDGE -eq 1 ]]; then
   244      OPTIONS="$OPTIONS --subbridge --subbridgeport $SC_SUB_BRIDGE_PORT --chaintxperiod $SC_ANCHORING_PERIOD --chaintxlimit $SC_TX_LIMIT "
   245      OPTIONS="$OPTIONS --parentchainid $SC_PARENT_CHAIN_ID"
   246      if [[ ! -z $SC_ANCHORING ]] && [[ $SC_ANCHORING -eq 1 ]]; then
   247              OPTIONS="$OPTIONS --anchoring"
   248      fi
   249  
   250      if [[ ! -z $SC_KAS_ANCHOR ]] && [[ $SC_KAS_ANCHOR -eq 1 ]]; then
   251          OPTIONS="$OPTIONS --kas.sc.anchor"
   252  
   253          if [[ ! -z $SC_KAS_ANCHOR_PERIOD ]]; then
   254              OPTIONS="$OPTIONS --kas.sc.anchor.period $SC_KAS_ANCHOR_PERIOD"
   255          fi
   256  
   257          if [[ -z $SC_KAS_ANCHOR_URL ]]; then
   258              echo
   259              echo "[ERROR] kas.sc.anchor.url should be specified for KAS Anchor."
   260              exit 1
   261          fi
   262          OPTIONS="$OPTIONS --kas.sc.anchor.url $SC_KAS_ANCHOR_URL"
   263  
   264          if [[ -z $SC_KAS_ANCHOR_OPERATOR ]]; then
   265              echo
   266              echo "[ERROR] kas.sc.anchor.operator should be specified for KAS Anchor."
   267              exit 1
   268          fi
   269          OPTIONS="$OPTIONS --kas.sc.anchor.operator $SC_KAS_ANCHOR_OPERATOR"
   270  
   271          if [[ -z $SC_KAS_ANCHOR_ACCESS_KEY ]]; then
   272              echo
   273              echo "[ERROR] kas.accesskey should be specified for KAS Anchor."
   274              exit 1
   275          fi
   276          OPTIONS="$OPTIONS --kas.accesskey $SC_KAS_ANCHOR_ACCESS_KEY"
   277  
   278          if [[ -z $SC_KAS_ANCHOR_SECRET_KEY ]]; then
   279              echo
   280              echo "[ERROR] kas.secretkey should be specified for KAS Anchor."
   281              exit 1
   282          fi
   283          OPTIONS="$OPTIONS --kas.secretkey $SC_KAS_ANCHOR_SECRET_KEY"
   284  
   285          if [[ -z $SC_KAS_ANCHOR_X_CHAIN_ID ]]; then
   286              echo
   287              echo "[ERROR] kas.x-chain-id should be specified for KAS Anchor."
   288              exit 1
   289          fi
   290          OPTIONS="$OPTIONS --kas.x-chain-id $SC_KAS_ANCHOR_X_CHAIN_ID"
   291  
   292          if [ ! -z $SC_KAS_ANCHOR_REQUEST_TIMEOUT ]; then
   293              OPTIONS="$OPTIONS --kas.sc.anchor.request.timeout $SC_KAS_ANCHOR_REQUEST_TIMEOUT"
   294          fi
   295      fi
   296  fi
   297  
   298  if [ ! -z $SC_PARENT_OPERATOR_GASLIMIT ]; then
   299      OPTIONS="$OPTIONS --sc.parentoperator.gaslimit $SC_PARENT_OPERATOR_GASLIMIT"
   300  fi
   301  
   302  if [ ! -z $SC_CHILD_OPERATOR_GASLIMIT ]; then
   303      OPTIONS="$OPTIONS --sc.childoperator.gaslimit $SC_CHILD_OPERATOR_GASLIMIT"
   304  fi
   305  
   306  if [[ (! -z $VTRECOVERY && $VTRECOVERY -eq 1 ) || (! -z $SC_VTRECOVERY && $SC_VTRECOVERY -eq 1) ]]; then
   307      OPTIONS="$OPTIONS --vtrecovery"
   308      if [[ ! -z $SC_VTRECOVERY_INTERVAL ]]; then
   309          OPTIONS="$OPTIONS --vtrecoveryinterval $SC_VTRECOVERY_INTERVAL"
   310      fi
   311  fi
   312  
   313  if [[ ! -z $ADDITIONAL ]] && [[ $ADDITIONAL != "" ]]; then
   314      OPTIONS="$OPTIONS $ADDITIONAL"
   315  fi
   316  
   317  if [[ ! -z $LOG_ROTATE ]] && [[ $LOG_ROTATE -eq 1 ]]; then
   318      OPTIONS="$OPTIONS --log.rotate"
   319      if [[ ! -z $LOG_MAXSIZE ]]; then
   320        OPTIONS="$OPTIONS --log.maxsize $LOG_MAXSIZE"
   321      fi
   322      if [[ ! -z $LOG_MAXBACKUP ]]; then
   323        OPTIONS="$OPTIONS --log.maxbackup $LOG_MAXBACKUP"
   324      fi
   325      if [[ ! -z $LOG_MAXAGE ]]; then
   326        OPTIONS="$OPTIONS --log.maxage $LOG_MAXAGE"
   327      fi
   328      if [[ ! -z $LOG_COMPRESS ]] && [[ $LOG_COMPRESS -eq 1 ]]; then
   329        OPTIONS="$OPTIONS --log.compress"
   330      fi
   331  fi
   332  
   333  BASEDIR="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
   334  CURRENTFILE=`basename "$0"`
   335  OPTIONS="$OPTIONS --autorestart.daemon.path $BASEDIR/$CURRENTFILE"
   336  set +f
   337  
   338  start_node() {
   339      echo -n "Starting $prog: "
   340      if [ ! -d ${LOG_DIR} ]; then
   341              mkdir -p ${LOG_DIR}
   342      fi
   343  
   344      __pids_var_run "$prog" "$pidfile"
   345      [ -n "$pid" ] && echo && return
   346  
   347      set -f
   348      OPTIONS="$OPTIONS --log.file ${LOG_DIR}/kspnd.out"
   349      TERM="dumb" $kspn $OPTIONS >> ${LOG_DIR}/kspnd.out 2>&1 &
   350      RETVAL=$?
   351      PIDNUM=$!
   352      set +f
   353      if [ $RETVAL = 0 ]; then
   354              echo $PIDNUM > ${pidfile}
   355              touch ${lockfile}
   356              success "$prog startup"
   357      else
   358              failure "$prog startup"
   359      fi
   360      echo
   361      return $RETVAL
   362  }
   363  
   364  stop_node() {
   365      echo -n "Shutting down $prog: "
   366      killproc -p ${pidfile} -d ${STOP_TIMEOUT} $kspn
   367      RETVAL=$?
   368      echo
   369      [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
   370  }
   371  
   372  #--------------------- Public functions --------------------------
   373  start() {
   374      if [ ! -d $LOG_DIR ]; then
   375          mkdir -p $LOG_DIR
   376      fi
   377  
   378      if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then
   379          start_auto_restart_daemon
   380      else
   381          start_node
   382      fi
   383  }
   384  
   385  stop() {
   386      if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then
   387          stop_auto_restart_daemon
   388      fi
   389      stop_node
   390  }
   391  
   392  case "$1" in
   393      start)
   394          start
   395          ;;
   396      stop)
   397          stop
   398          ;;
   399      status)
   400          status -p ${pidfile} -l ${lockfile} $kspn
   401          ;;
   402      restart)
   403          stop
   404          start
   405          ;;
   406      *)
   407          echo "Usage: $prog {start|stop|restart|status}"
   408          exit 1
   409          ;;
   410  esac
   411  exit $?