From 81c9131fcc4c5eef8963ae1602057060c9fc9e98 Mon Sep 17 00:00:00 2001 From: Joshua Gilman Date: Fri, 5 Jan 2024 16:48:53 -0800 Subject: [PATCH] refactor: refactors jormungandr entrypoint script --- Earthfile | 8 ++- config.yaml | 42 +++++++++++++ entrypoint.sh | 128 ++++++++++++++++++++++++++++++++++++++ jormungandr/entrypoint.sh | 91 --------------------------- 4 files changed, 177 insertions(+), 92 deletions(-) create mode 100644 config.yaml create mode 100755 entrypoint.sh delete mode 100755 jormungandr/entrypoint.sh diff --git a/Earthfile b/Earthfile index 54111591aa..7e1f3c2d2b 100644 --- a/Earthfile +++ b/Earthfile @@ -76,16 +76,22 @@ publish: WORKDIR /app ARG tag=latest + ARG fetcher_version=2.1.1 # Install build dependencies RUN apt-get update && \ apt-get install -y --no-install-recommends \ + ca-certificates \ libssl-dev \ libpq-dev \ libsqlite3-dev + # Install fetcher + IMPORT github.com/input-output-hk/catalyst-ci/tools/fetcher:v${fetcher_version} AS fetcher + COPY fetcher+build/fetcher /usr/local/bin/fetcher + COPY +build/jormungandr . - COPY jormungandr/entrypoint.sh . + COPY entrypoint.sh . RUN chmod +x entrypoint.sh ENTRYPOINT ["/app/entrypoint.sh"] diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000000..cd3a6cdc2f --- /dev/null +++ b/config.yaml @@ -0,0 +1,42 @@ +bootstrap_from_trusted_peers: true +leadership: + logs_capacity: 1024 +log: + format: plain + level: debug + output: stdout +mempool: + log_max_entries: 100000 + pool_max_entries: 400000 +p2p: + allow_private_addresses: true + layers: + preferred_list: + peers: + - address: /ip4/172.20.200.1/tcp/30700 + - address: /ip4/172.20.200.2/tcp/30700 + - address: /ip4/172.20.200.3/tcp/30700 + view_max: 20 + topics_of_interest: + messages: high + blocks: high + listen: "0.0.0.0:30700" + max_bootstrap_attempts: 3 + max_client_connections: 192 + max_connections: 256 + policy: + quarantine_duration: 5s + quarantine_whitelist: + - /ip4/172.20.200.1/tcp/30700 + - /ip4/172.20.200.2/tcp/30700 + - /ip4/172.20.200.3/tcp/30700 + public_address: /ip4/172.20.200.0/tcp/30700 + trusted_peers: + - address: /ip4/172.20.200.1/tcp/30700 + - address: /ip4/172.20.200.2/tcp/30700 + - address: /ip4/172.20.200.3/tcp/30700 +prometheus: + enabled: true +rest: + listen: "0.0.0.0:8080" +skip_bootstrap: true diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000000..0237b11d23 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,128 @@ +#!/bin/bash + +# --------------------------------------------------------------- +# Entrypoint script for voting-node container +# --------------------------------------------------------------- +# +# This script serves as the entrypoint for the jormungandr node. +# +# It expects the following environment variables to be set except where noted: +# +# STORAGE_PATH - The path where the node's data will be stored +# NODE_CONFIG_PATH - The path to the node's configuration file +# GENESIS_PATH (optional) - The path to the genesis block +# GENESIS_BUCKET (optional) - The S3 bucket where the genesis block is stored. +# GENESIS_ENV (optional) - The target environment. Used for fetching the genesis block from S3. +# GENESIS_FUND (optional) - The fund name. Used for fetching the genesis block from S3. +# GENESIS_VERSION (optional) - The genesis version. Used for fetching the genesis block from S3. If not set, the "default" version will be used. +# BFT_PATH (optional) - The path to the BFT file. Only used by leader nodes. +# LEADER (optional) - If set, the node will be configured as a leader node. +# DEBUG_SLEEP (optional) - If set, the script will sleep for the specified number of seconds before starting the node. +# --------------------------------------------------------------- + +# Enable strict mode +set +x +set -o errexit +set -o pipefail +set -o nounset +set -o functrace +set -o errtrace +set -o monitor +set -o posix +shopt -s dotglob + +check_env_vars() { + local env_vars=("$@") + + # Iterate over the array and check if each variable is set + for var in "${env_vars[@]}"; do + echo "Checking $var" + if [ -z "${!var+x}" ]; then + echo ">>> Error: $var is required and not set." + exit 1 + fi + done +} + +debug_sleep() { + if [ -n "${DEBUG_SLEEP:-}" ]; then + echo "DEBUG_SLEEP is set. Sleeping for ${DEBUG_SLEEP} seconds..." + sleep "${DEBUG_SLEEP}" + fi +} + +echo ">>> Starting entrypoint script..." + +REQUIRED_ENV=( + "STORAGE_PATH" + "NODE_CONFIG_PATH" +) +echo ">>> Checking required env vars..." +check_env_vars "${REQUIRED_ENV[@]}" + +# Verify the storage path exists +if [[ ! -d "$STORAGE_PATH" ]]; then + echo "ERROR: storage path does not exist at: $STORAGE_PATH" + echo ">>> Aborting..." + exit 1 +fi + +# Verify config is present +if [[ ! -f "$NODE_CONFIG_PATH" ]]; then + echo "ERROR: node configuration is absent at: $NODE_CONFIG_PATH" + echo ">>> Aborting..." + exit 1 +fi + +# Verify genesis block is present or attempt to fetch from S3 +if [[ -z "${GENESIS_PATH:=}" || ! -f "$GENESIS_PATH" ]]; then + echo ">>> No genesis block provided. Attempting to fetch from S3..." + + REQUIRED_ENV=( + "GENESIS_BUCKET" + "GENESIS_ENV" + "GENESIS_FUND" + ) + echo ">>> Checking required env vars for fetching from S3..." + check_env_vars "${REQUIRED_ENV[@]}" + + echo ">>> Fetching genesis block from S3 using the following parameters..." + echo "Bucket: $GENESIS_BUCKET" + echo "Environment: $GENESIS_ENV" + echo "Fund: $GENESIS_FUND" + echo "Version: ${GENESIS_VERSION:=}" + + GENESIS_PATH="$STORAGE_PATH/artifacts/block0.bin" + mkdir -p "$(dirname "$GENESIS_PATH")" + fetcher --bucket "$GENESIS_BUCKET" artifact -e "$GENESIS_ENV" -f "$GENESIS_FUND" -t "genesis" -v "${GENESIS_VERSION:=}" "$GENESIS_PATH" +fi + +echo ">>> Using the following parameters:" +echo "Storage path: $STORAGE_PATH" +echo "Node config: $NODE_CONFIG_PATH" +echo "Genesis block: $GENESIS_PATH" + +args+=() +args+=("--storage" "$STORAGE_PATH") +args+=("--config" "$NODE_CONFIG_PATH") +args+=("--genesis-block" "$GENESIS_PATH") + +if [[ -n "${LEADER:=}" ]]; then + echo ">>> Configuring node as leader..." + + # shellcheck disable=SC2153 + if [[ ! -f "$BFT_PATH" ]]; then + echo "ERROR: BFT is absent at: $BFT_PATH" + echo ">>> Aborting..." + exit 1 + fi + + echo ">>> Using BFT at: $BFT_PATH" + args+=("--secret" "$BFT_PATH") +fi + +# Sleep if DEBUG_SLEEP is set +debug_sleep + +echo "Starting node..." +exec "/app/jormungandr" "${args[@]}" diff --git a/jormungandr/entrypoint.sh b/jormungandr/entrypoint.sh deleted file mode 100755 index 2144379d75..0000000000 --- a/jormungandr/entrypoint.sh +++ /dev/null @@ -1,91 +0,0 @@ -#!/usr/bin/bash - -echo ">>> Entering entrypoint script..." - -# Verify the storage path exists -if [[ ! -d "$STORAGE_PATH" ]]; then - echo "ERROR: storage path does not exist at: $STORAGE_PATH" - echo ">>> Aborting..." - exit 1 -fi - -# Verify config is present -if [[ ! -f "$NODE_CONFIG_PATH" ]]; then - echo "ERROR: node configuration is absent at: $NODE_CONFIG_PATH" - echo ">>> Aborting..." - exit 1 -fi - -# Verify genesis block is present -if [[ ! -f "$GENESIS_PATH" ]]; then - echo "ERROR: genesis block is absent at: $GENESIS_PATH" - echo ">>> Aborting..." - exit 1 -fi - -# Allow overriding jormungandr binary -BIN_PATH=''${BIN_PATH:=/app/jormungandr} - -echo ">>> Using the following parameters:" -echo "Storage path: $STORAGE_PATH" -echo "Node config: $NODE_CONFIG_PATH" -echo "Genesis block: $GENESIS_PATH" -echo "Binary path: $BIN_PATH" - -args+=() -args+=("--storage" "$STORAGE_PATH") -args+=("--config" "$NODE_CONFIG_PATH") -args+=("--genesis-block" "$GENESIS_PATH") - -if [[ -n "${LEADER:=}" ]]; then - echo ">>> Configuring node as leader..." - - # shellcheck disable=SC2153 - if [[ ! -f "$BFT_PATH" ]]; then - echo "ERROR: BFT is absent at: $BFT_PATH" - echo ">>> Aborting..." - exit 1 - fi - - echo ">>> Using BFT at: $BFT_PATH" - args+=("--secret" "$BFT_PATH") -fi - -# Nodes will fail to start if they cannot resolve the domain names of -# their respective peers. If domains are used for peers, it's necessary -# to wait for them to resolve first before starting the node. -if [[ -n "${DNS_PEERS:=}" ]]; then - for PEER in $DNS_PEERS; do - while ! nslookup "$PEER"; do - echo ">>> Waiting for $PEER to be resolvable..." - sleep 1 - done - echo "Successfully resolved $PEER" - done -fi - -# Allows resetting our footprint in persistent storage -if [[ -f "$STORAGE_PATH/reset" ]]; then - echo ">>> Reset file detected at $STORAGE_PATH/reset" - rm -rf "$STORAGE_PATH/reset" - - if [[ -d "$STORAGE_PATH/fragments" ]]; then - echo ">>> Deleting $STORAGE_PATH/fragments" - rm -rf "$STORAGE_PATH/fragments" - fi - - if [[ -d "$STORAGE_PATH/permanent" ]]; then - echo ">>> Deleting $STORAGE_PATH/permanent" - rm -rf "$STORAGE_PATH/permanent" - fi - - if [[ -d "$STORAGE_PATH/volatile" ]]; then - echo ">>> Deleting $STORAGE_PATH/volatile" - rm -rf "$STORAGE_PATH/volatile" - fi - - echo ">>> Reset complete" -fi - -echo "Starting node..." -exec "$BIN_PATH" "${args[@]}"