init.sh


Overview

`init.sh` is a shell script designed to initialize and manage the lifecycle of the AvalancheGo node within a container or server environment. It ensures the necessary dependencies are installed, starts the AvalancheGo process with specific runtime parameters, and gracefully handles termination signals to cleanly stop the node.

This script is typically used as an entrypoint in Docker containers or startup scripts in server deployments, enabling automated management of the Avalanche node lifecycle.


Detailed Explanation

Script Behavior


Functions

start()

**Purpose:** Launch the AvalancheGo executable with predefined parameters and run it in the background.

**Function Definition:**

start() {
  /avalanchego/build/avalanchego \
    --data-dir /data \
    --http-host 0.0.0.0 \
    --http-allowed-hosts "*" \
    --staking-ephemeral-cert-enabled=true \
    --chain-config-dir=/configs/chains &
  PID="$!"
}

**Parameters:**

**What it does:**

**Return Value:**

**Usage Example:**

start
echo "AvalancheGo started with PID $PID"

stop()

**Purpose:** Terminates the running AvalancheGo process gracefully upon receiving termination signals.

**Function Definition:**

stop() {
  echo "Catching signal and sending to PID: $PID" && kill $PID
  while $(kill -0 $PID 2>/dev/null); do sleep 1; done
}

**Parameters:**

**What it does:**

**Return Value:**

**Usage Example:** This function is not normally called manually; it's triggered automatically by signal traps.


Signal Handling

trap 'stop' TERM INT

Main Execution Flow

start
wait $PID

Important Implementation Details and Algorithms


Interaction with Other System Components


Usage Example in Deployment

FROM ubuntu:20.04

COPY avalanchego /avalanchego/build/avalanchego
COPY configs /configs
COPY init.sh /init.sh
RUN chmod +x /init.sh

ENTRYPOINT ["/init.sh"]

This example Dockerfile illustrates how `init.sh` might be used as an entrypoint script to start AvalancheGo inside a container.


Mermaid Flowchart Diagram

flowchart TD
    A[Start Script] --> B[Install curl & jq]
    B --> C[start() Function]
    C --> D[AvalancheGo process runs in background]
    D --> E[Store PID]
    E --> F[Wait for AvalancheGo process]
    F --> G{Receive SIGTERM or SIGINT?}
    G -- Yes --> H[Invoke stop() Function]
    H --> I[Send SIGTERM to PID]
    I --> J[Wait until process exits]
    J --> K[Script exits]
    G -- No --> F

Summary

This script plays a crucial role in ensuring the Avalanche node runs reliably and responds correctly to system signals in a production or containerized deployment.