Skip to content

Commit 544c649

Browse files
committed
tracerOci: add context specific tracer entrypoint and support scripts
1 parent 6fc94d0 commit 544c649

File tree

4 files changed

+225
-14
lines changed

4 files changed

+225
-14
lines changed

nix/docker/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ To launch cardano-node with a custom configuration, "custom" mode, provide
6161
entrypoint args starting with `run` and:
6262
* Leave the `NETWORK` env variable unset
6363
* Optionally include additional cardano-node args to the entrypoint after `run`
64-
* Optionally include environment variables interpreted by [nix/docker/context/bin/run-node](context/bin/run-node),
64+
* Optionally include environment variables interpreted by [nix/docker/context/node/bin/run-node](context/node/bin/run-node),
6565
or `/usr/local/bin/run-node` in the container
6666

6767
For example, launch a custom cardano-node container using cardano-node args and
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/env bash
2+
3+
[[ -n $DEBUG ]] && set -x
4+
5+
# If the NETWORK env var is set to a valid cardano network, pre-defined
6+
# configuration will be used.
7+
if [[ -n $NETWORK ]]; then
8+
9+
# If CARDANO_CONFIG_JSON_MERGE env var is set, iohk-nix
10+
# pre-defined NETWORK configuration will be used as a starting point and
11+
# merged with custom configuration provided as json in the environment
12+
# variable(s).
13+
if [[ -n $CARDANO_CONFIG_JSON_MERGE ]]; then
14+
15+
CFG="/opt/cardano/config"
16+
if ! [[ -f $CFG/$NETWORK/tracer-config.json ]]; then
17+
echo "Network \"$NETWORK\" doesn't appear to have expected base configuration available at:"
18+
echo " $CFG/$NETWORK/tracer-config.json"
19+
echo
20+
echo "Please check that the NETWORK environment variable is set to a valid cardano network name."
21+
exit 1
22+
fi
23+
24+
# Do a recursive deep merge of iohk-nix NETWORK config and/or topology with
25+
# the supplied json merge environment variable(s).
26+
#
27+
# In a jq deep merge, arrays are replaced, primitive values in the second
28+
# object override the first, different types for the same key result in
29+
# full replacement and null values persist.
30+
if [[ -n $CARDANO_CONFIG_JSON_MERGE ]]; then
31+
jq -S \
32+
--argjson deepMerge "$CARDANO_CONFIG_JSON_MERGE" \
33+
'. * $deepMerge' \
34+
< "$CFG/$NETWORK/tracer-config.json" \
35+
> "$CFG/$NETWORK/tracer-config-merged.json"
36+
export CARDANO_CONFIG="$CFG/$NETWORK/tracer-config-merged.json"
37+
else
38+
export CARDANO_CONFIG="$CFG/$NETWORK/tracer-config.json"
39+
fi
40+
41+
# Run cardano-tracer using iohk-nix base config merged with provided custom
42+
# config for the requested NETWORK.
43+
unset NETWORK
44+
exec /usr/local/bin/run-tracer "$@"
45+
46+
else
47+
# Run cardano-tracer using "scripts" mode for the requested NETWORK.
48+
exec /usr/local/bin/run-network "$@"
49+
fi
50+
51+
elif [[ $1 == "run" ]]; then
52+
# Run cardano-tracer using "custom" mode.
53+
exec /usr/local/bin/run-tracer "$@"
54+
55+
else
56+
57+
echo "Nothing to do! Available modes of operation are:"
58+
echo
59+
echo "Scripts mode:"
60+
echo " * Set the NETWORK env var to a valid cardano network, such as mainnet to use default network config."
61+
echo
62+
echo "Custom mode:"
63+
echo " * Leave the NETWORK env var unset and provide entrypoint args starting with \"run\" and:"
64+
echo " * Optionally set environment variables interpreted by /usr/local/bin/run-tracer."
65+
echo " * Optionally include additional cardano-tracer args to the entrypoint after \"run\"."
66+
echo
67+
echo "Merge mode:"
68+
echo " * Set the NETWORK env var to a valid cardano network, such as mainnet, and"
69+
echo " set the CARDANO_CONFIG_JSON_MERGE env var with valid json to run cardano-tracer"
70+
echo " with deep merged base NETWORK and custom config."
71+
echo " * The extra environment variables and cardano-tracer args that can be used in custom mode"
72+
echo " are also available in merge mode."
73+
exit 1
74+
75+
fi
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/bin/env bash
2+
set -eo pipefail
3+
4+
echo "Running the cardano tracer ..."
5+
6+
[[ -n $DEBUG ]] && set -x
7+
8+
# Define a few defaults
9+
CARDANO_CONFIG_BASE="/opt/cardano/config"
10+
11+
if [[ -z $CARDANO_CONFIG ]]; then
12+
CARDANO_CONFIG="$CARDANO_CONFIG_BASE/mainnet/tracer-config.json"
13+
fi
14+
15+
#####################################################################
16+
#
17+
# Print run environment
18+
#
19+
printRunEnv () {
20+
21+
echo "CARDANO_CONFIG=$CARDANO_CONFIG"
22+
echo "CARDANO_MIN_LOG_SEVERITY=$CARDANO_MIN_LOG_SEVERITY"
23+
echo "CARDANO_STATE_DIR=$CARDANO_STATE_DIR"
24+
}
25+
26+
#####################################################################
27+
#
28+
# Write root env file
29+
#
30+
writeRootEnv () {
31+
32+
cat << EOF > /usr/local/bin/env
33+
#!/usr/bin/env bash
34+
35+
# Docker run ENV vars
36+
CARDANO_CONFIG="$CARDANO_CONFIG"
37+
CARDANO_MIN_LOG_SEVERITY="$CARDANO_MIN_LOG_SEVERITY"
38+
CARDANO_STATE_DIR="$CARDANO_STATE_DIR"
39+
EOF
40+
}
41+
42+
#####################################################################
43+
#
44+
# Run the relay node in the background
45+
#
46+
runTracer () {
47+
48+
effopts=(
49+
"--config" "$CARDANO_CONFIG"
50+
"--state-dir" "$CARDANO_STATE_DIR"
51+
)
52+
53+
effopts+=("${filteredOpts[@]}")
54+
55+
echo "cardano-tracer ${effopts[*]}"
56+
exec /usr/local/bin/cardano-tracer "${effopts[@]}"
57+
}
58+
59+
# Shift the first option by one index
60+
shift
61+
62+
# Override default values with explicit options
63+
64+
options=("$@")
65+
66+
for i in "${!options[@]}"
67+
do
68+
j=$((i + 1))
69+
key=${options[i]}
70+
val=${options[j]}
71+
found=false
72+
73+
# echo "$i/$j: ${key} ${val}"
74+
75+
case ${key} in
76+
--config) CARDANO_CONFIG=${val}; found=true;;
77+
--state-dir) CARDANO_STATE_DIR=${val}; found=true;;
78+
--min-log-severity) CARDANO_MIN_LOG_SEVERITY=${val}; found=true;;
79+
esac
80+
81+
if [[ $found == true ]]; then
82+
options[i]="";
83+
options[j]="";
84+
fi
85+
done
86+
87+
# Filter blank args from match removal above
88+
filteredOpts=(); for arg in "${options[@]}"; do [[ $arg ]] && filteredOpts+=("$arg"); done
89+
90+
printRunEnv
91+
writeRootEnv
92+
93+
runTracer

nix/docker/tracer.nix

+56-13
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
, jq
3535
, socat
3636
, utillinux
37-
, writeScriptBin
38-
, runtimeShell
3937
, lib
4038
, exe
4139
, script
@@ -82,16 +80,43 @@ let
8280
exec ${scriptBin}/bin/${scriptBin.name} $@
8381
'') scripts);
8482

85-
entry-point = writeScriptBin "entry-point" ''
86-
#!${runtimeShell}
83+
runNetwork = pkgs.writeShellScriptBin "run-network" ''
8784
if [[ -z "$NETWORK" ]]; then
88-
exec ${pkgs.${exe}}/bin/${exe} $@
85+
echo "[Error] Cannot obtain NETWORK env variable"
86+
exit 1
8987
${clusterStatements}
9088
else
91-
echo "Managed configuration for network "$NETWORK" does not exist"
89+
echo "[Error] Managed configuration for network "$NETWORK" does not exist"
90+
exit 1
9291
fi
9392
'';
9493

94+
# The docker context with static content
95+
context = ./context/tracer;
96+
97+
genCfgs = let
98+
environments' = lib.getAttrs [ "mainnet" "preprod" "preview" ] commonLib.environments;
99+
cardano-deployment = commonLib.mkConfigHtml environments';
100+
in
101+
pkgs.runCommand "cardano-html" {} ''
102+
mkdir "$out"
103+
104+
ENVS=(${lib.escapeShellArgs (builtins.attrNames environments')})
105+
for ENV in "''${ENVS[@]}"; do
106+
# Migrate each env from a flat dir to an ENV subdir
107+
mkdir -p "$out/config/$ENV"
108+
for i in $(find ${cardano-deployment} -type f -name "$ENV-tracer-config*" -printf "%f\n"); do
109+
cp -v "${cardano-deployment}/$i" "$out/config/$ENV/''${i#"$ENV-"}"
110+
111+
# Adjust from iohk-nix default config for the oci environment
112+
sed -i -r \
113+
-e 's|"contents": ".*"|"contents": "/ipc/cardano-tracer.socket"|g' \
114+
-e 's|"logRoot": ".*"|"logRoot": "/logs"|g' \
115+
"$out/config/$ENV/''${i#"$ENV-"}"
116+
done
117+
done
118+
'';
119+
95120
in
96121
dockerTools.buildImage {
97122
name = "${repoName}";
@@ -110,15 +135,33 @@ in
110135
111136
# Similarly, make a root level dir for logs:
112137
mkdir -p logs
113-
'';
114138
115-
copyToRoot = pkgs.buildEnv {
116-
name = "image-root";
117-
pathsToLink = ["/"];
118-
paths = [entry-point];
119-
};
139+
# The "custom" operation mode of this image, when the NETWORK env is
140+
# unset and "run" is provided as an entrypoint arg, will use the
141+
# following default directories.
142+
mkdir -p opt/cardano
143+
144+
# Setup bins
145+
mkdir -p usr/local/bin
146+
cp -v ${runNetwork}/bin/* usr/local/bin
147+
cp -v ${context}/bin/* usr/local/bin
148+
ln -sv ${cardano-tracer}/bin/cardano-tracer usr/local/bin/cardano-tracer
149+
ln -sv ${jq}/bin/jq usr/local/bin/jq
150+
151+
# Create iohk-nix network configs, organized by network directory.
152+
SRC="${genCfgs}"
153+
DST="opt/cardano"
154+
155+
# Make the directory structure with the iohk-nix configs mutable. This
156+
# enables creation of merge mode entrypoint configs in the respective
157+
# NETWORK directory. Keep config files as read-only copies from the nix
158+
# store instead of direct symlinks. This avoids volume mount failures
159+
# caused by broken symlinks as seen from the host.
160+
cp -R "$SRC"/* "$DST"
161+
find "$DST" -mindepth 1 -type d -exec bash -c "chmod 0755 {}" \;
162+
'';
120163

121164
config = {
122-
EntryPoint = [ "${entry-point}/bin/entry-point" ];
165+
EntryPoint = [ "entrypoint" ];
123166
};
124167
}

0 commit comments

Comments
 (0)