From 51b89631b283fff07de4ce1acbce1b3196bc145e Mon Sep 17 00:00:00 2001 From: Marco Martins Date: Mon, 20 Oct 2025 17:09:52 +0100 Subject: [PATCH] feat: use environment variable for midnight container name management --- .envrc.qanet | 1 + .envrc.testnet-02 | 1 + compose.yml | 2 +- midnight-node.sh | 6 +++--- midnight-shell.sh | 19 ++++++++----------- reset-midnight.sh | 2 +- test.sh | 8 ++++---- 7 files changed, 19 insertions(+), 20 deletions(-) diff --git a/.envrc.qanet b/.envrc.qanet index 4e493aa..14137e1 100644 --- a/.envrc.qanet +++ b/.envrc.qanet @@ -1,5 +1,6 @@ # overrides for qanet export MIDNIGHT_NODE_IMAGE="ghcr.io/midnight-ntwrk/midnight-node:0.12.0-rc.3" +export MIDNIGHT_CONTAINER_NAME="midnight-node" # Well known addresses of network that allow discovery of all other nodes. export BOOTNODES="/dns/boot-node-01.qanet.dev.midnight.network/tcp/30333/ws/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp \ diff --git a/.envrc.testnet-02 b/.envrc.testnet-02 index 2d5e64a..d55a9b7 100644 --- a/.envrc.testnet-02 +++ b/.envrc.testnet-02 @@ -1,5 +1,6 @@ # overrides for testnet-02 export MIDNIGHT_NODE_IMAGE="midnightnetwork/midnight-node:0.12.0" +export MIDNIGHT_CONTAINER_NAME="midnight-node" # These are well known addresses of a network that allow you to discover all the other nodes. export BOOTNODES="/dns/boot-node-01.testnet-02.midnight.network/tcp/30333/ws/p2p/12D3KooWMjUq13USCvQR9Y6yFzYNYgTQBLNAcmc8psAuPx2UUdnB \ diff --git a/compose.yml b/compose.yml index af88c53..d88e7e9 100644 --- a/compose.yml +++ b/compose.yml @@ -18,7 +18,7 @@ volumes: services: midnight-node-testnet: - container_name: midnight-node + container_name: ${MIDNIGHT_CONTAINER_NAME} restart: unless-stopped image: ${MIDNIGHT_NODE_IMAGE} ports: diff --git a/midnight-node.sh b/midnight-node.sh index cbcfda6..d82917c 100755 --- a/midnight-node.sh +++ b/midnight-node.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash -if [ -z "$MIDNIGHT_NODE_IMAGE" ]; then - echo "Error: Env var MIDNIGHT_NODE_IMAGE is not set or is empty" +if [ -z "$MIDNIGHT_CONTAINER_NAME" ]; then + echo "Error: Env var MIDNIGHT_CONTAINER_NAME is not set or is empty" echo "Please install direnv and run 'direnv allow' to activate it." exit 1 fi -docker exec -t midnight /midnight-node "$@" \ No newline at end of file +docker exec -t ${MIDNIGHT_CONTAINER_NAME} /midnight-node "$@" diff --git a/midnight-shell.sh b/midnight-shell.sh index 3f07a14..2369a0e 100755 --- a/midnight-shell.sh +++ b/midnight-shell.sh @@ -1,15 +1,12 @@ #!/usr/bin/env bash -# Define container name -CONTAINER_NAME="midnight" - # Check if the container already exists -if [ "$(docker ps -a -f name=^${CONTAINER_NAME}$ --format '{{.Names}}' | grep -c -w "${CONTAINER_NAME}")" -eq 0 ]; then - echo "Container '${CONTAINER_NAME}' does not exist. Creating and starting it..." +if [ "$(docker ps -a -f name=^${MIDNIGHT_CONTAINER_NAME}$ --format '{{.Names}}' | grep -c -w "${MIDNIGHT_CONTAINER_NAME}")" -eq 0 ]; then + echo "Container '${MIDNIGHT_CONTAINER_NAME}' does not exist. Creating and starting it..." # Run the container with the specified configuration docker run -it \ - --name ${CONTAINER_NAME} \ + --name ${MIDNIGHT_CONTAINER_NAME} \ -e CFG_PRESET="${CFG_PRESET}" \ -e DB_SYNC_POSTGRES_CONNECTION_STRING="${DB_SYNC_POSTGRES_CONNECTION_STRING}" \ -v ./data:/data \ @@ -17,14 +14,14 @@ if [ "$(docker ps -a -f name=^${CONTAINER_NAME}$ --format '{{.Names}}' | grep -c --entrypoint bash \ "${MIDNIGHT_NODE_IMAGE}" else - echo "Container '${CONTAINER_NAME}' already exists. Opening an interactive shell..." + echo "Container '${MIDNIGHT_CONTAINER_NAME}' already exists. Opening an interactive shell..." # Check if the container is running, if not, start it - if [ "$(docker ps -f name=^${CONTAINER_NAME}$ --format '{{.Names}}' | grep -c -w "${CONTAINER_NAME}")" -eq 0 ]; then - echo "Starting container '${CONTAINER_NAME}'..." - docker start ${CONTAINER_NAME} + if [ "$(docker ps -f name=^${MIDNIGHT_CONTAINER_NAME}$ --format '{{.Names}}' | grep -c -w "${MIDNIGHT_CONTAINER_NAME}")" -eq 0 ]; then + echo "Starting container '${MIDNIGHT_CONTAINER_NAME}'..." + docker start ${MIDNIGHT_CONTAINER_NAME} fi # Open an interactive shell in the container - docker exec -it ${CONTAINER_NAME} /bin/bash + docker exec -it ${MIDNIGHT_CONTAINER_NAME} /bin/bash fi diff --git a/reset-midnight.sh b/reset-midnight.sh index ff57ec0..fe1fb9a 100755 --- a/reset-midnight.sh +++ b/reset-midnight.sh @@ -3,6 +3,6 @@ # This script will reset the midnight node's data back to nothing. # -docker rm -f midnight +docker rm -f ${MIDNIGHT_CONTAINER_NAME} rm -R ./data docker volume rm midnight-node-docker_midnight-data-testnet diff --git a/test.sh b/test.sh index 0d61b34..b57f0b5 100755 --- a/test.sh +++ b/test.sh @@ -117,9 +117,9 @@ fi # # Check midnight-node has peers: # -midnight=$(docker ps --filter "name=midnight") -if [[ "$midnight" != *"midnight"* ]]; then - echo "❌ midnight container not running, please run docker compose -d" +container_status=$(docker ps --filter "name=${MIDNIGHT_CONTAINER_NAME}" --format "{{.Names}}") +if [[ "$container_status" != *"$MIDNIGHT_CONTAINER_NAME"* ]]; then + echo "❌ ${MIDNIGHT_CONTAINER_NAME} container not running, please run: docker compose up -d" exit 1 fi @@ -136,7 +136,7 @@ if [[ "$SHOULD_HAVE_PEERS" == "true" && "$PEERS" -lt 1 ]]; then echo "$HEALTH" - docker logs midnight 2>&1 | grep "Genesis mismatch" + docker logs ${MIDNIGHT_CONTAINER_NAME} 2>&1 | grep "Genesis mismatch" echo "For genesis mismatch, check that CFG_PRESET=$CFG_PRESET is aligned with BOOTNODES=$BOOTNODES" echo "If they're aligned then run ./reset-midnight.sh and restart" exit 1