Skip to content

Commit

Permalink
[#2801] Allow docker container configuration through env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
tdiesler committed Jun 8, 2021
1 parent 61035e2 commit 11a446f
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 22 deletions.
18 changes: 18 additions & 0 deletions configuration/cardano/mainnet-alonzo-genesis.json
@@ -0,0 +1,18 @@
{
"adaPerUTxOWord": 0,
"executionPrices": {
"prMem": 1,
"prSteps": 1
},
"maxTxExUnits": {
"exUnitsMem": 1,
"exUnitsSteps": 1
},
"maxBlockExUnits": {
"exUnitsMem": 1,
"exUnitsSteps": 1
},
"maxValueSize": 1000,
"collateralPercentage": 100,
"maxCollateralInputs": 1
}
16 changes: 15 additions & 1 deletion configuration/cardano/mainnet-config.json
@@ -1,10 +1,12 @@
{
"AlonzoGenesisFile": "mainnet-alonzo-genesis.json",
"AlonzoGenesisHash": "06cc024b823b6d20f5dde2faf8de2d895f47983ab584db38ea62111b61038e35",
"ApplicationName": "cardano-sl",
"ApplicationVersion": 1,
"ByronGenesisFile": "mainnet-byron-genesis.json",
"ByronGenesisHash": "5f20df933584822601f9e3f8c024eb5eb252fe8cefb24d1317dc3d432e940ebb",
"LastKnownBlockVersion-Alt": 0,
"LastKnownBlockVersion-Major": 2,
"LastKnownBlockVersion-Major": 3,
"LastKnownBlockVersion-Minor": 0,
"MaxKnownMajorProtocolVersion": 2,
"Protocol": "Cardano",
Expand All @@ -21,19 +23,28 @@
"TraceChainSyncClient": false,
"TraceChainSyncHeaderServer": false,
"TraceChainSyncProtocol": false,
"TraceConnectionManager": true,
"TraceDNSResolver": true,
"TraceDNSSubscription": true,
"TraceDiffusionInitialization": true,
"TraceErrorPolicy": true,
"TraceForge": true,
"TraceHandshake": false,
"TraceInboundGovernor": true,
"TraceIpSubscription": true,
"TraceLedgerPeers": true,
"TraceLocalChainSyncProtocol": false,
"TraceLocalErrorPolicy": true,
"TraceLocalHandshake": false,
"TraceLocalRootPeers": true,
"TraceLocalTxSubmissionProtocol": false,
"TraceLocalTxSubmissionServer": false,
"TraceMempool": true,
"TraceMux": false,
"TracePeerSelection": true,
"TracePeerSelectionActions": true,
"TracePublicRootPeers": true,
"TraceServer": true,
"TraceTxInbound": false,
"TraceTxOutbound": false,
"TraceTxSubmissionProtocol": false,
Expand All @@ -59,6 +70,9 @@
"mapBackends": {
"cardano.node.metrics": [
"EKGViewBK"
],
"cardano.node.resources": [
"EKGViewBK"
]
},
"mapSubtrace": {
Expand Down
101 changes: 101 additions & 0 deletions nix/docker/README.md
@@ -0,0 +1,101 @@

## Build Cardano Node + Image

https://docs.cardano.org/projects/cardano-node/en/latest/getting-started/building-the-node-using-nix.html

```
# Build + Install the cardano node
nix-build -A scripts.mainnet.node -o ~/bin/cardano-node
# Build + Install the cardano Docker image
docker load -i $(nix-build -A dockerImage --no-out-link) \
&& GITHASH=`git log -n1 --pretty='%H'` \
&& docker tag inputoutput/cardano-node:$GITHASH inputoutput/cardano-node:dev \
&& docker rmi inputoutput/cardano-node:$GITHASH
GITTAG=`git describe --exact-match --tags $GITHASH`
if [ $? -eq 0 ]; then
echo "Current tag: $GITTAG"
docker tag inputoutput/cardano-node:dev inputoutput/cardano-node:$GITTAG
fi
# Bash into the node to look around
docker run --rm -it --entrypoint=bash \
-v node-data:/opt/cardano/data \
inputoutput/cardano-node:dev
cardano-node run \
--config /opt/cardano/config/mainnet-config.json \
--topology /opt/cardano/config/mainnet-topology.json \
--socket-path /opt/cardano/ipc/socket \
--database-path /opt/cardano/data \
--host-addr 0.0.0.0 \
--port 3001
```

## Manual Testing

1. Run -e NETWORK=mainnet and check graceful shutdown SIGINT with -it
2. Run -e NETWORK=mainnet and check graceful shutdown SIGTERM with --detach
3. Run without -e NETWORK and check graceful shutdown SIGINT with -it
4. Run without -e NETWORK and check graceful shutdown SIGTERM with --detach
5. Check cardano-cli access

### Run with NETWORK

Run -e NETWORK=mainnet and check graceful shutdown SIGINT with -it

```
docker run --rm -it \
-p 3001:3001 \
-e NETWORK=mainnet \
-v node-data:/data/db \
inputoutput/cardano-node:dev
```

Run -e NETWORK=mainnet and check graceful shutdown SIGTERM with --detach

```
docker rm -f relay
docker run --detach \
--name=relay \
-p 3001:3001 \
-e NETWORK=mainnet \
-v node-data:/data/db \
inputoutput/cardano-node:dev
docker logs -f relay
```

### Run without NETWORK

Check graceful shutdown SIGINT with -it

```
docker run --rm -it \
-p 3001:3001 \
-v node-data:/opt/cardano/data \
inputoutput/cardano-node:dev run
```

Check graceful shutdown SIGTERM with --detach

```
docker rm -f relay
docker run --detach \
--name=relay \
-p 3001:3001 \
-v node-data:/opt/cardano/data \
-v node-ipc:/opt/cardano/ipc \
inputoutput/cardano-node:dev run
docker logs -f relay
```

### Check cardano-cli

```
docker run --rm -it \
-v node-ipc:/opt/cardano/ipc \
inputoutput/cardano-node:dev cli query tip --mainnet
```
20 changes: 20 additions & 0 deletions nix/docker/context/bin/entrypoint
@@ -0,0 +1,20 @@
#!/bin/bash

if [[ -n $NETWORK ]]; then

exec /usr/local/bin/run-network $@

elif [[ $1 == "run" ]]; then

exec /usr/local/bin/run-node $@

elif [[ $1 == "cli" ]]; then

exec /usr/local/bin/run-client $@

else

echo "Nothing to do! Perhaps try [run|cli]"
exit 1

fi
10 changes: 10 additions & 0 deletions nix/docker/context/bin/run-client
@@ -0,0 +1,10 @@
#!/bin/bash

# Shift the first option by one index
shift

if [[ -z $CARDANO_NODE_SOCKET_PATH ]]; then
export CARDANO_NODE_SOCKET_PATH="/opt/cardano/ipc/socket"
fi

/usr/local/bin/cardano-cli $@
119 changes: 119 additions & 0 deletions nix/docker/context/bin/run-node
@@ -0,0 +1,119 @@
#!/bin/bash

# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail
set -eo pipefail

echo "Running the cardano node ..."

# Define a few defaults
CARDANO_CONFIG_BASE="/opt/cardano/config"

if [[ -z $CARDANO_CONFIG ]]; then
CARDANO_CONFIG="$CARDANO_CONFIG_BASE/mainnet-config.json"
fi

if [[ -z $CARDANO_TOPOLOGY ]]; then
CARDANO_TOPOLOGY="$CARDANO_CONFIG_BASE/mainnet-topology.json"
fi

if [[ -z $CARDANO_DATABASE_PATH ]]; then
CARDANO_DATABASE_PATH="/opt/cardano/data"
fi

if [[ -z $CARDANO_SOCKET_PATH ]]; then
CARDANO_SOCKET_PATH="/opt/cardano/ipc/socket"
fi

if [[ -z $CARDANO_BIND_ADDR ]]; then
CARDANO_BIND_ADDR="0.0.0.0"
fi

if [[ -z $CARDANO_PORT ]]; then
CARDANO_PORT=3001
fi

if [ -v $CARDANO_BLOCK_PRODUCER ]; then
CARDANO_BLOCK_PRODUCER=false
fi

#####################################################################
#
# Print run environment
#
printRunEnv () {

echo "CARDANO_BIND_ADDR=$CARDANO_BIND_ADDR"
echo "CARDANO_BLOCK_PRODUCER=$CARDANO_BLOCK_PRODUCER"
echo "CARDANO_CONFIG=$CARDANO_CONFIG"
echo "CARDANO_DATABASE_PATH=$CARDANO_DATABASE_PATH"
echo "CARDANO_PORT=$CARDANO_PORT"
echo "CARDANO_SOCKET_PATH=$CARDANO_SOCKET_PATH"
echo "CARDANO_TOPOLOGY=$CARDANO_TOPOLOGY"

if [ "$CARDANO_BLOCK_PRODUCER" = true ]; then

if [ -v $CARDANO_SHELLEY_KES_KEY ]; then
CARDANO_SHELLEY_KES_KEY="$CARDANO_CONFIG_BASE/keys/kes.skey"
fi

if [ -v $CARDANO_SHELLEY_VRF_KEY ]; then
CARDANO_SHELLEY_VRF_KEY="$CARDANO_CONFIG_BASE/keys/vrf.skey"
fi

if [ -v $CARDANO_SHELLEY_OPERATIONAL_CERTIFICATE ]; then
CARDANO_SHELLEY_OPERATIONAL_CERTIFICATE="$CARDANO_CONFIG_BASE/keys/node.cert"
fi

echo "CARDANO_SHELLEY_KES_KEY=$CARDANO_SHELLEY_KES_KEY"
echo "CARDANO_SHELLEY_VRF_KEY=$CARDANO_SHELLEY_VRF_KEY"
echo "CARDANO_SHELLEY_OPERATIONAL_CERTIFICATE=$CARDANO_SHELLEY_OPERATIONAL_CERTIFICATE"
fi
}

# Shift the first option by one index
shift

# Override default values with explicit options

options=($@)

for i in "${!options[@]}"
do
j=$((i + 1))
key=${options[i]}
val=${options[j]}
found=false

# echo "$i/$j: ${key} ${val}"

case ${key} in
--config) CARDANO_CONFIG=${val}; found=true;;
--topology) CARDANO_TOPOLOGY=${val}; found=true;;
--database-path) CARDANO_DATABASE_PATH=${val}; found=true;;
--socket-path) CARDANO_SOCKET_PATH=${val}; found=true;;
--host-addr) CARDANO_BIND_ADDR=${val}; found=true;;
--port) CARDANO_PORT=${val}; found=true;;
esac

if [[ $found == true ]]; then
options[i]="";
options[j]="";
fi
done

printRunEnv

effopts=(--config $CARDANO_CONFIG \
--topology $CARDANO_TOPOLOGY \
--database-path $CARDANO_DATABASE_PATH \
--socket-path $CARDANO_SOCKET_PATH \
--host-addr $CARDANO_BIND_ADDR \
--port $CARDANO_PORT)

effopts+=(${options[@]})

# The IPC socket dir is not created on demand
mkdir -p `dirname $CARDANO_SOCKET_PATH`

echo "/usr/local/bin/cardano-node run ${effopts[@]}"
exec /usr/local/bin/cardano-node run ${effopts[@]}

0 comments on commit 11a446f

Please sign in to comment.