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
The script is written in Bash and uses the
set -eoption, which causes the script to exit immediately if any command returns a non-zero status. This is a safety feature to prevent the container or environment from running in an inconsistent state.It installs essential utilities (
curlandjq) required potentially for health checks, configuration parsing, or further scripting.It defines two main functions:
startandstop.Uses Bash's
trapmechanism to catch termination signals (SIGTERMandSIGINT) and execute thestopfunction to ensure graceful shutdown.
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:**
None (all options are hardcoded within the function).
**What it does:**
Runs the AvalancheGo binary located at
/avalanchego/build/avalanchego.Uses
/dataas the data directory for blockchain data.Configures the HTTP server to listen on all interfaces (
0.0.0.0), allowing access from any host.Whitelists all hosts (
*) for HTTP API access.Enables ephemeral staking certificates for simplified staking setup.
Points to chain configuration files in
/configs/chains.Runs the process in the background (
&).Captures the process ID (
PID) of the background process for later control.
**Return Value:**
None explicitly returned; however, the global variable
PIDis set to the AvalancheGo process ID.
**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:**
None.
**What it does:**
Prints a message indicating signal handling.
Sends the
SIGTERMsignal to the AvalancheGo process using itsPID.Enters a loop that repeatedly checks if the process still exists (
kill -0 $PIDis a non-destructive check).Sleeps 1 second between checks until the process exits, ensuring a clean shutdown before the script itself exits.
**Return Value:**
None.
**Usage Example:** This function is not normally called manually; it's triggered automatically by signal traps.
Signal Handling
trap 'stop' TERM INT
This line sets up a trap to invoke the
stopfunction whenever the script receives aSIGTERMorSIGINTsignal.This is important for container environments (e.g., Docker), where receiving such signals indicates the container is stopping.
Main Execution Flow
start
wait $PID
Starts the AvalancheGo node in the background.
wait $PIDcauses the script to pause and wait for the AvalancheGo process to terminate.If the process ends, the script exits.
If a termination signal is caught, the
stopfunction is called to kill the process and wait for it to finish.
Important Implementation Details and Algorithms
Graceful Shutdown: The use of
trapcombined withkilland the polling loop (kill -0) ensures that the AvalancheGo process has time to terminate cleanly, preventing data corruption or abrupt termination issues.Background Process Management: Launching AvalancheGo in the background and storing its PID allows the script to maintain control over the process lifecycle.
Dependency Installation: The script installs
curlandjqon startup. These tools are common for HTTP requests and JSON processing, indicating that this script (or other scripts in the environment) may rely on these utilities for configuration or health checks.Use of Hardcoded Paths:
/avalanchego/build/avalanchego- location of the binary/data- data directory for node storage/configs/chains- directory for chain configurations
These paths imply a containerized or pre-configured environment where these directories are mounted or baked into the image.
Interaction with Other System Components
AvalancheGo Node: This script directly manages the AvalancheGo process lifecycle, serving as the entrypoint/startup script.
Filesystem:
/datadirectory likely mounted as a persistent volume or directory where blockchain data is stored./configs/chainscontains JSON or YAML chain configuration files used by AvalancheGo.
Container Orchestration: In Kubernetes or Docker environments, this script enables clean startup and shutdown of the Avalanche node container.
Other Services:
Since
curlandjqare installed, other scripts or health checks running alongside may use these tools to query the AvalancheGo HTTP API.
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
init.shis a lightweight bootstrap script for running the AvalancheGo node.It installs dependencies, starts the node with specific flags, and manages graceful shutdown on termination signals.
It is designed for containerized or automated environments where controlled lifecycle management is essential.
The script assumes a specific directory structure and environment setup.
Signal trapping and process management ensure clean node operations and avoid data loss.
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.