|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Enable strict mode |
| 4 | +set +x |
| 5 | +set -o errexit |
| 6 | +set -o pipefail |
| 7 | +set -o nounset |
| 8 | +set -o functrace |
| 9 | +set -o errtrace |
| 10 | +set -o monitor |
| 11 | +set -o posix |
| 12 | +shopt -s dotglob |
| 13 | + |
| 14 | +echo ">>> Entering entrypoint script..." |
| 15 | +# Verify the storage path exists |
| 16 | +if [[ ! -d "$STORAGE_PATH" ]]; then |
| 17 | + echo "ERROR: storage path does not exist at: $STORAGE_PATH"; |
| 18 | + echo ">>> Aborting..." |
| 19 | + exit 1 |
| 20 | +fi |
| 21 | +# Verify config is present |
| 22 | +if [[ ! -f "$NODE_CONFIG_PATH" ]]; then |
| 23 | + echo "ERROR: node configuration is absent at: $NODE_CONFIG_PATH" |
| 24 | + echo ">>> Aborting..." |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | +# Verify genesis block is present |
| 28 | +if [[ ! -f "$GENESIS_PATH" ]]; then |
| 29 | + echo "ERROR: genesis block is absent at: $GENESIS_PATH" |
| 30 | + echo ">>> Aborting..." |
| 31 | + exit 1 |
| 32 | +fi |
| 33 | +# Allow overriding jormungandr binary |
| 34 | +if [[ ! -f "$BIN_PATH" ]]; then |
| 35 | + echo "ERROR: path to jormungandr binary is absent at: $BIN_PATH" |
| 36 | + echo ">>> Aborting..." |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | +echo ">>> Using the following parameters:" |
| 40 | +echo "Storage path: $STORAGE_PATH" |
| 41 | +echo "Node config: $NODE_CONFIG_PATH" |
| 42 | +echo "Genesis block: $GENESIS_PATH" |
| 43 | +echo "Binary path: $BIN_PATH" |
| 44 | +args=() |
| 45 | +args+=("--storage" "$STORAGE_PATH") |
| 46 | +args+=("--config" "$NODE_CONFIG_PATH") |
| 47 | +args+=("--genesis-block" "$GENESIS_PATH") |
| 48 | +if [[ -n "${LEADER:-}" ]]; then |
| 49 | + echo ">>> Configuring node as leader..." |
| 50 | + # shellcheck disable=SC2153 |
| 51 | + if [[ ! -f "$BFT_PATH" ]]; then |
| 52 | + echo "ERROR: BFT is absent at: $BFT_PATH" |
| 53 | + echo ">>> Aborting..." |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + echo ">>> Using BFT at: $BFT_PATH" |
| 57 | + args+=("--secret" "$BFT_PATH") |
| 58 | +fi |
| 59 | +# Nodes will fail to start if they cannot resolve the domain names of |
| 60 | +# their respective peers. If domains are used for peers, it's necessary |
| 61 | +# to wait for them to resolve first before starting the node. |
| 62 | +if [[ -n "${DNS_PEERS:-}" ]]; then |
| 63 | + for PEER in $DNS_PEERS |
| 64 | + do |
| 65 | + while ! nslookup "$PEER"; do |
| 66 | + echo ">>> Waiting for $PEER to be resolvable..." |
| 67 | + sleep 1 |
| 68 | + done |
| 69 | + echo "Successfully resolved $PEER" |
| 70 | + done |
| 71 | +fi |
| 72 | +# Allows resetting our footprint in persistent storage |
| 73 | +if [[ -f "$STORAGE_PATH/reset" ]]; then |
| 74 | + echo ">>> Reset file detected at $STORAGE_PATH/reset" |
| 75 | + rm -rf "$STORAGE_PATH/reset" |
| 76 | + if [[ -d "$STORAGE_PATH/fragments" ]]; then |
| 77 | + echo ">>> Deleting $STORAGE_PATH/fragments" |
| 78 | + rm -rf "$STORAGE_PATH/fragments" |
| 79 | + fi |
| 80 | + if [[ -d "$STORAGE_PATH/permanent" ]]; then |
| 81 | + echo ">>> Deleting $STORAGE_PATH/permanent" |
| 82 | + rm -rf "$STORAGE_PATH/permanent" |
| 83 | + fi |
| 84 | + if [[ -d "$STORAGE_PATH/volatile" ]]; then |
| 85 | + echo ">>> Deleting $STORAGE_PATH/volatile" |
| 86 | + rm -rf "$STORAGE_PATH/volatile" |
| 87 | + fi |
| 88 | + echo ">>> Reset complete" |
| 89 | +fi |
| 90 | + |
| 91 | +# Define the command to be executed |
| 92 | +ARGS="${args[*]}" |
| 93 | +EXTRA_ARGS=$* |
| 94 | +CMD="$BIN_PATH $ARGS $EXTRA_ARGS" |
| 95 | +echo ">>> Executing command: $CMD" |
| 96 | + |
| 97 | +# Wait for DEBUG_SLEEP seconds if the DEBUG_SLEEP environment variable is set |
| 98 | +if [ -n "${DEBUG_SLEEP:-}" ]; then |
| 99 | + echo "DEBUG_SLEEP is set to $DEBUG_SLEEP. Sleeping..." |
| 100 | + sleep "$DEBUG_SLEEP" |
| 101 | +fi |
| 102 | + |
| 103 | +echo "Starting node..." |
| 104 | +# Expand the command with arguments and capture the exit code |
| 105 | +set +e |
| 106 | +eval "$CMD" |
| 107 | +EXIT_CODE=$? |
| 108 | +set -e |
| 109 | + |
| 110 | +# If the exit code is 0, the executable returned successfully |
| 111 | +if [ $EXIT_CODE -ne 0 ]; then |
| 112 | + echo "Error: jormungandr returned with exit code $EXIT_CODE" |
| 113 | + exit 1 |
| 114 | +fi |
0 commit comments