|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +if [ "$ALGORAND_DATA" != "/algod/data" ]; then |
| 6 | + echo "Do not override 'ALGORAND_DATA' environment variable." |
| 7 | + exit 1 |
| 8 | +fi |
| 9 | + |
| 10 | +if [ "$DEBUG" = "1" ]; then |
| 11 | + set -x |
| 12 | +fi |
| 13 | + |
| 14 | +# To allow mounting the data directory we need to change permissions |
| 15 | +# to our algorand user. The script is initially run as the root user |
| 16 | +# in order to change permissions, afterwards the script is re-launched |
| 17 | +# as the algorand user. |
| 18 | +if [ "$(id -u)" = '0' ]; then |
| 19 | + chown -R algorand:algorand $ALGORAND_DATA |
| 20 | + exec su -p -c "$(readlink -f $0) $@" algorand |
| 21 | +fi |
| 22 | + |
| 23 | +# Script to configure or resume a network. Based on environment settings the |
| 24 | +# node will be setup with a private network or connect to a public network. |
| 25 | +#################### |
| 26 | +# Helper functions # |
| 27 | +#################### |
| 28 | + |
| 29 | +function catchup() { |
| 30 | + sleep 5 |
| 31 | + goal node catchup --force --min 1000000 |
| 32 | +} |
| 33 | + |
| 34 | +function start_public_network() { |
| 35 | + cd "$ALGORAND_DATA" |
| 36 | + |
| 37 | + configure_data_dir |
| 38 | + start_kmd & |
| 39 | + |
| 40 | + if [ "$FAST_CATCHUP" = "1" ]; then |
| 41 | + catchup & |
| 42 | + fi |
| 43 | + |
| 44 | + if [ "$PEER_ADDRESS" != "" ]; then |
| 45 | + printf "$PEER_ADDRESS" |
| 46 | + algod -o -p $PEER_ADDRESS |
| 47 | + else |
| 48 | + # redirect output to stdout |
| 49 | + algod -o |
| 50 | + fi |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | +function configure_data_dir() { |
| 55 | + cd "$ALGORAND_DATA" |
| 56 | + |
| 57 | + # check for config file overrides. |
| 58 | + if [ -f "/etc/algorand/config.json" ]; then |
| 59 | + cp /etc/algorand/config.json config.json |
| 60 | + fi |
| 61 | + if [ -f "/etc/algorand/algod.token" ]; then |
| 62 | + cp /etc/algorand/algod.token algod.token |
| 63 | + fi |
| 64 | + if [ -f "/etc/algorand/algod.admin.token" ]; then |
| 65 | + cp /etc/algorand/algod.admin.token algod.admin.token |
| 66 | + fi |
| 67 | + if [ -f "/etc/algorand/logging.config" ]; then |
| 68 | + cp /etc/algorand/logging.config logging.config |
| 69 | + fi |
| 70 | + |
| 71 | + # initialize config with profile. |
| 72 | + if [ "$PROFILE" != "" ]; then |
| 73 | + algocfg profile set --yes -d "$ALGORAND_DATA" "$PROFILE" |
| 74 | + fi |
| 75 | + |
| 76 | + # set profile overrides |
| 77 | + if [ "$GOSSIP_PORT" != "" ]; then |
| 78 | + algocfg -d . set -p NetAddress -v "0.0.0.0:${GOSSIP_PORT}" |
| 79 | + algocfg -d . set -p DisableNetworking -v "false" |
| 80 | + algocfg -d . set -p IncomingConnectionsLimit -v "1000" |
| 81 | + fi |
| 82 | + |
| 83 | + algocfg -d . set -p EndpointAddress -v "0.0.0.0:${ALGOD_PORT}" |
| 84 | + algocfg -d . set -p NodeExporterPath -v "$(which node_exporter)" |
| 85 | + |
| 86 | + # set token overrides |
| 87 | + for dir in ${ALGORAND_DATA}/../*/; do |
| 88 | + if [ "$TOKEN" != "" ]; then |
| 89 | + echo "$TOKEN" > "$dir/algod.token" |
| 90 | + fi |
| 91 | + if [ "$ADMIN_TOKEN" != "" ]; then |
| 92 | + echo "$ADMIN_TOKEN" > "$dir/algod.admin.token" |
| 93 | + fi |
| 94 | + done |
| 95 | + |
| 96 | + # configure telemetry |
| 97 | + if [ "$TELEMETRY_NAME" != "" ]; then |
| 98 | + diagcfg telemetry name -n "$TELEMETRY_NAME" -d "$ALGORAND_DATA" |
| 99 | + diagcfg telemetry enable -d "$ALGORAND_DATA" |
| 100 | + elif ! [ -f "/etc/algorand/logging.config" ]; then |
| 101 | + diagcfg telemetry disable |
| 102 | + fi |
| 103 | + |
| 104 | + # start kmd |
| 105 | + if [ "$START_KMD" = "1" ]; then |
| 106 | + local KMD_DIR="kmd-v0.5" |
| 107 | + # on intial bootstrap, this directory won't exist. |
| 108 | + mkdir -p "$KMD_DIR" |
| 109 | + chmod 0700 "$KMD_DIR" |
| 110 | + cd "$KMD_DIR" |
| 111 | + if [ -f "/etc/algorand/kmd_config.json" ]; then |
| 112 | + cp /etc/algorand/kmd_config.json kmd_config.json |
| 113 | + else |
| 114 | + echo "{ \"address\":\"0.0.0.0:${KMD_PORT}\", \"allowed_origins\":[\"*\"] }" >kmd_config.json |
| 115 | + fi |
| 116 | + |
| 117 | + if [ "$KMD_TOKEN" != "" ]; then |
| 118 | + echo "$KMD_TOKEN" >kmd.token |
| 119 | + fi |
| 120 | + fi |
| 121 | + |
| 122 | +# touch "$ALGORAND_DATA/algod-err.log" |
| 123 | +# touch "$ALGORAND_DATA/algod-out.log" |
| 124 | +# chmod 000 "$ALGORAND_DATA/algod-err.log" |
| 125 | +# chmod 000 "$ALGORAND_DATA/algod-out.log" |
| 126 | +} |
| 127 | + |
| 128 | +function start_kmd() { |
| 129 | + if [ "$START_KMD" = "1" ]; then |
| 130 | + goal kmd start -d "$ALGORAND_DATA" |
| 131 | + fi |
| 132 | +} |
| 133 | + |
| 134 | +function start_new_public_network() { |
| 135 | + mkdir -p "$ALGORAND_DATA" |
| 136 | + cd "$ALGORAND_DATA" |
| 137 | + |
| 138 | + # initialize genesis.json |
| 139 | + if [ "$GENESIS_ADDRESS" != "" ]; then |
| 140 | + # download genesis file from peer |
| 141 | + echo "Attempting to download genesis file from $GENESIS_ADDRESS" |
| 142 | + curl "$GENESIS_ADDRESS/genesis" -o genesis.json |
| 143 | + elif [ -d "/node/run/genesis/${NETWORK}" ]; then |
| 144 | + echo "Installing genesis file for ${NETWORK}" |
| 145 | + cp "/node/run/genesis/${NETWORK}/genesis.json" genesis.json |
| 146 | + else |
| 147 | + echo "No genesis file for '$NETWORK' is available." |
| 148 | + exit 1 |
| 149 | + fi |
| 150 | + |
| 151 | + configure_data_dir |
| 152 | + |
| 153 | + # if the peer address is set, it will be used instead of the DNS bootstrap ID |
| 154 | + if [ "$PEER_ADDRESS" != "" ]; then |
| 155 | + local ID |
| 156 | + case $NETWORK in |
| 157 | + mainnet) ID="<network>.algorand.network" ;; |
| 158 | + testnet) ID="<network>.algorand.network" ;; |
| 159 | + betanet) ID="<network>.algodev.network" ;; |
| 160 | + alphanet) ID="<network>.algodev.network" ;; |
| 161 | + devnet) ID="<network>.algodev.network" ;; |
| 162 | + *) |
| 163 | + echo "Unknown network." |
| 164 | + exit 1 |
| 165 | + ;; |
| 166 | + esac |
| 167 | + |
| 168 | + set -p DNSBootstrapID -v "$ID" |
| 169 | + fi |
| 170 | + |
| 171 | + start_public_network |
| 172 | +} |
| 173 | + |
| 174 | +function start_private_network() { |
| 175 | + configure_data_dir |
| 176 | + start_kmd & |
| 177 | + |
| 178 | + # TODO: Is there a way to properly exec a private network? |
| 179 | + goal network start -r "${ALGORAND_DATA}/.." |
| 180 | + |
| 181 | + if [ -f "/algod/funded" ]; then |
| 182 | + echo "Account is funded" |
| 183 | + else |
| 184 | + goal account import -m "artefact exist coil life turtle edge edge inside punch glance recycle teach melody diet method pause slam dumb race interest amused side learn able heavy" |
| 185 | + touch "/algod/funded" |
| 186 | + fi |
| 187 | + |
| 188 | + # get rekd logs |
| 189 | + tail -f /proc/$(cat $ALGORAND_DATA/algod.pid)/fd/1 |
| 190 | +} |
| 191 | + |
| 192 | +function start_new_private_network() { |
| 193 | + local TEMPLATE="template.json" |
| 194 | + if [ -f "/etc/algorand/template.json" ]; then |
| 195 | + cp /etc/algorand/template.json "/node/run/$TEMPLATE" |
| 196 | + else |
| 197 | + if [ "$DEV_MODE" = "1" ]; then |
| 198 | + TEMPLATE="devmode_template.json" |
| 199 | + fi |
| 200 | + fi |
| 201 | + sed -i "s/NUM_ROUNDS/${NUM_ROUNDS:-30000}/" "/node/run/$TEMPLATE" |
| 202 | + |
| 203 | + # Check if keys are mounted, and if so, copy them over |
| 204 | + # Use pregen keys in network create command |
| 205 | + if [ -d "/etc/algorand/keys" ]; then |
| 206 | + cp -r /etc/algorand/keys /node/run/keys |
| 207 | + goal network create --noclean -n dockernet -r "${ALGORAND_DATA}/.." -t "/node/run/$TEMPLATE" -p "/node/run/keys" |
| 208 | + else |
| 209 | + goal network create --noclean -n dockernet -r "${ALGORAND_DATA}/.." -t "/node/run/$TEMPLATE" |
| 210 | + fi |
| 211 | + |
| 212 | + configure_data_dir |
| 213 | + start_private_network |
| 214 | +} |
| 215 | + |
| 216 | +############## |
| 217 | +# Entrypoint # |
| 218 | +############## |
| 219 | + |
| 220 | +echo "Starting Algod Docker Container" |
| 221 | +echo " ALGORAND_DATA: $ALGORAND_DATA" |
| 222 | +echo " NETWORK: $NETWORK" |
| 223 | +echo " PROFILE: $PROFILE" |
| 224 | +echo " DEV_MODE: $DEV_MODE" |
| 225 | +echo " START_KMD: ${START_KMD:-"Not Set"}" |
| 226 | +echo " FAST_CATCHUP: $FAST_CATCHUP" |
| 227 | +echo " TOKEN: ${TOKEN:-"Not Set"}" |
| 228 | +echo " ADMIN_TOKEN: ${ADMIN_TOKEN:-"Not Set"}" |
| 229 | +echo " KMD_TOKEN: ${KMD_TOKEN:-"Not Set"}" |
| 230 | +echo " TELEMETRY_NAME: $TELEMETRY_NAME" |
| 231 | +echo " NUM_ROUNDS: $NUM_ROUNDS" |
| 232 | +echo " GENESIS_ADDRESS: $GENESIS_ADDRESS" |
| 233 | +echo " PEER_ADDRESS: $PEER_ADDRESS" |
| 234 | +echo " GOSSIP_PORT: $GOSSIP_PORT" |
| 235 | +echo " ALGOD_PORT: $ALGOD_PORT" |
| 236 | + |
| 237 | +# If data directory is initialized, start existing environment. |
| 238 | +if [ -f "$ALGORAND_DATA/../network.json" ]; then |
| 239 | + start_private_network |
| 240 | + exit 1 |
| 241 | +elif [ -f "$ALGORAND_DATA/genesis.json" ]; then |
| 242 | + start_public_network |
| 243 | + exit 1 |
| 244 | +fi |
| 245 | + |
| 246 | +# Initialize and start network. |
| 247 | +if [ "$NETWORK" == "" ] && [ "$PEER_ADDRESS" == "" ]; then |
| 248 | + start_new_private_network |
| 249 | +else |
| 250 | + start_new_public_network |
| 251 | +fi |
0 commit comments