Skip to content

Commit 2176d35

Browse files
Refactor logging in dappnode_install.sh to use log function for consistency
1 parent 00ea62e commit 2176d35

1 file changed

Lines changed: 87 additions & 82 deletions

File tree

scripts/dappnode_install.sh

Lines changed: 87 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,12 @@
44
# thanks to the shebang), but users sometimes invoke it as `zsh ./script.sh` or `source ./script.sh`.
55
# - If sourced, bail out (sourcing would pollute the current shell and can break it).
66
# - If invoked by a non-bash shell, re-exec with bash before hitting bash-specific builtins.
7-
if (return 0 2>/dev/null); then
8-
echo "This script must be executed, not sourced. Run: bash $0"
9-
return 1
10-
fi
11-
12-
if [ -z "${BASH_VERSION:-}" ]; then
13-
exec /usr/bin/env bash "$0" "$@"
14-
fi
15-
16-
set -Eeuo pipefail
17-
18-
# Optional env inputs (avoid unbound-variable errors under `set -u`)
19-
: "${UPDATE:=false}"
20-
: "${STATIC_IP:=}"
21-
: "${LOCAL_PROFILE_PATH:=}"
22-
: "${MINIMAL:=false}"
23-
: "${LITE:=false}"
24-
: "${PACKAGES:=}"
25-
26-
# Enable alias expansion in non-interactive bash scripts.
27-
# Required so commands like `dappnode_wireguard` (defined as aliases in `.dappnode_profile`) work.
28-
shopt -s expand_aliases
29-
30-
# Ensure array is always defined (avoid `set -u` edge cases)
31-
DNCORE_COMPOSE_ARGS=()
327

338
##############################
349
# Logging / Errors #
3510
##############################
11+
# Note: LOGFILE and LOGS_DIR are set during bootstrap_filesystem based on DAPPNODE_DIR
12+
# Early definition allows these functions to be used throughout the script.
3613

3714
log() {
3815
# LOGFILE is created after dir bootstrap; until then we just print to stdout.
@@ -71,6 +48,35 @@ die() {
7148
exit 1
7249
}
7350

51+
##############################
52+
# Script Guards #
53+
##############################
54+
55+
if (return 0 2>/dev/null); then
56+
die "This script must be executed, not sourced. Run: bash $0"
57+
fi
58+
59+
if [ -z "${BASH_VERSION:-}" ]; then
60+
exec /usr/bin/env bash "$0" "$@"
61+
fi
62+
63+
set -Eeuo pipefail
64+
65+
# Optional env inputs (avoid unbound-variable errors under `set -u`)
66+
: "${UPDATE:=false}"
67+
: "${STATIC_IP:=}"
68+
: "${LOCAL_PROFILE_PATH:=}"
69+
: "${MINIMAL:=false}"
70+
: "${LITE:=false}"
71+
: "${PACKAGES:=}"
72+
73+
# Enable alias expansion in non-interactive bash scripts.
74+
# Required so commands like `dappnode_wireguard` (defined as aliases in `.dappnode_profile`) work.
75+
shopt -s expand_aliases
76+
77+
# Ensure array is always defined (avoid `set -u` edge cases)
78+
DNCORE_COMPOSE_ARGS=()
79+
7480
usage() {
7581
cat <<'EOF'
7682
Usage: dappnode_install.sh [options]
@@ -200,7 +206,7 @@ wait_for_internal_ip() {
200206
local internal_ip_url="http://127.0.0.1/global-envs/INTERNAL_IP"
201207
local hostname_url="http://127.0.0.1/global-envs/HOSTNAME"
202208

203-
echo "Waiting for dappmanager to publish INTERNAL_IP and HOSTNAME..."
209+
log "Waiting for dappmanager to publish INTERNAL_IP and HOSTNAME..."
204210
sleep "$initial_sleep_seconds"
205211

206212
local start_seconds internal_http_code internal_value internal_result
@@ -235,12 +241,12 @@ wait_for_internal_ip() {
235241

236242
if [[ "$internal_http_code" == "200" && -n "$internal_value" && "$internal_value" != "null" && "$hostname_http_code" == "200" && -n "$hostname_value" && "$hostname_value" != "null" ]]; then
237243
sleep "$final_sleep_seconds" # Extra buffer to ensure values are fully propagated before we proceed
238-
echo "INTERNAL_IP is ready: $internal_value"
239-
echo "HOSTNAME is ready: $hostname_value"
244+
log "INTERNAL_IP is ready: $internal_value"
245+
log "HOSTNAME is ready: $hostname_value"
240246
return 0
241247
fi
242248

243-
echo "INTERNAL_IP/HOSTNAME not ready yet (INTERNAL_IP code=${internal_http_code:-?}, HOSTNAME code=${hostname_http_code:-?}). Retrying..."
249+
log "INTERNAL_IP/HOSTNAME not ready yet (INTERNAL_IP code=${internal_http_code:-?}, HOSTNAME code=${hostname_http_code:-?}). Retrying..."
244250
sleep 2
245251
done
246252
}
@@ -266,43 +272,42 @@ print_vpn_access_credentials() {
266272
done
267273

268274
if [[ "$has_wireguard" != "true" && "$has_vpn" != "true" ]]; then
269-
echo ""
270-
echo "No VPN package selected (VPN/WIREGUARD). Skipping credentials output."
275+
log "No VPN package selected (VPN/WIREGUARD). Skipping credentials output."
271276
return 0
272277
fi
273278

274-
echo ""
275-
echo "Waiting for VPN initialization..."
279+
log ""
280+
log "Waiting for VPN initialization..."
276281
wait_for_internal_ip "DAppNodeCore-dappmanager.dnp.dappnode.eth" 120 20 10
277282

278-
echo ""
279-
echo "##############################################"
280-
echo "# DAppNode VPN Access Credentials #"
281-
echo "##############################################"
282-
echo ""
283-
echo "Your DAppNode is ready! Connect using your preferred VPN client."
284-
echo "Choose either Wireguard (recommended) or OpenVPN and import the"
285-
echo "credentials below into your VPN app to access your DAppNode."
286-
echo ""
283+
log ""
284+
log "##############################################"
285+
log "# DAppNode VPN Access Credentials #"
286+
log "##############################################"
287+
log ""
288+
log "Your DAppNode is ready! Connect using your preferred VPN client."
289+
log "Choose either Wireguard (recommended) or OpenVPN and import the"
290+
log "credentials below into your VPN app to access your DAppNode."
291+
log ""
287292

288293
if [[ "$has_wireguard" == "true" ]]; then
289-
echo "--- Wireguard ---"
294+
log "--- Wireguard ---"
290295
docker exec -i DAppNodeCore-api.wireguard.dnp.dappnode.eth getWireguardCredentials "${localhost_flag[@]}" 2>&1 || \
291-
echo "Wireguard credentials not yet available. Try later with: dappnode_wireguard${localhost_flag:+ ${localhost_flag[*]}}"
296+
log "Wireguard credentials not yet available. Try later with: dappnode_wireguard${localhost_flag:+ ${localhost_flag[*]}}"
292297
fi
293298

294299
if [[ "$has_wireguard" == "true" && "$has_vpn" == "true" ]]; then
295-
echo ""
300+
log ""
296301
fi
297302

298303
if [[ "$has_vpn" == "true" ]]; then
299-
echo "--- OpenVPN ---"
304+
log "--- OpenVPN ---"
300305
docker exec -i DAppNodeCore-vpn.dnp.dappnode.eth vpncli get dappnode_admin "${localhost_flag[@]}" 2>&1 || \
301-
echo "OpenVPN credentials not yet available. Try later with: dappnode_openvpn_get dappnode_admin${localhost_flag:+ ${localhost_flag[*]}}"
306+
log "OpenVPN credentials not yet available. Try later with: dappnode_openvpn_get dappnode_admin${localhost_flag:+ ${localhost_flag[*]}}"
302307
fi
303308

304-
echo ""
305-
echo "Import the configuration above into your VPN client of choice to access your DAppNode at http://my.dappnode"
309+
log ""
310+
log "Import the configuration above into your VPN client of choice to access your DAppNode at http://my.dappnode"
306311
}
307312

308313
# Build docker compose "-f <file>" args from downloaded compose files.
@@ -421,8 +426,8 @@ normalize_ipfs_version_ref() {
421426
local manifest
422427
manifest="$(download_stdout "$manifest_url" 2>/dev/null || true)"
423428
if [[ -z "$manifest" ]]; then
424-
echo "[ERROR] Could not fetch IPFS manifest for ${comp} from: $manifest_url" 1>&2
425-
echo "[ERROR] Provide ${comp}_VERSION as /ipfs/<cid>:<version> (example: /ipfs/Qm...:0.2.11)" 1>&2
429+
error "Could not fetch IPFS manifest for ${comp} from: $manifest_url"
430+
error "Provide ${comp}_VERSION as /ipfs/<cid>:<version> (example: /ipfs/Qm...:0.2.11)"
426431
return 1
427432
fi
428433

@@ -435,8 +440,8 @@ normalize_ipfs_version_ref() {
435440
)"
436441

437442
if [[ -z "$inferred_version" || "$inferred_version" == "$manifest" ]]; then
438-
echo "[ERROR] Could not infer version for ${comp} from IPFS manifest: $manifest_url" 1>&2
439-
echo "[ERROR] Provide ${comp}_VERSION as /ipfs/<cid>:<version>" 1>&2
443+
error "Could not infer version for ${comp} from IPFS manifest: $manifest_url"
444+
error "Provide ${comp}_VERSION as /ipfs/<cid>:<version>"
440445
return 1
441446
fi
442447

@@ -520,7 +525,7 @@ patch_dappmanager_compose_for_macos() {
520525
bootstrap_filesystem() {
521526
# Clean if update
522527
if [[ "${UPDATE}" == "true" ]]; then
523-
echo "Cleaning for update..."
528+
log "Cleaning for update..."
524529
rm -f "${LOGFILE}" || true
525530
rm -f "${DAPPNODE_CORE_DIR}"/docker-compose-*.yml || true
526531
rm -f "${DAPPNODE_CORE_DIR}"/dappnode_package-*.json || true
@@ -805,17 +810,17 @@ dappnode_core_build() {
805810
ver="${comp}_VERSION"
806811
if [[ ${!ver} == dev:* ]]; then
807812
if $IS_MACOS; then
808-
echo "Development builds (dev:*) are not supported on macOS."
813+
error "Development builds (dev:*) are not supported on macOS."
809814
exit 1
810815
fi
811-
echo "Cloning & building DNP_${comp}..."
816+
log "Cloning & building DNP_${comp}..."
812817
if ! dpkg -s git >/dev/null 2>&1; then
813818
apt-get install -y git
814819
fi
815820
local tmpdir
816821
tmpdir="$(mktemp -d)"
817822
pushd "$tmpdir" >/dev/null || {
818-
echo "Error on pushd"
823+
error "Error on pushd"
819824
exit 1
820825
}
821826
git clone -b "${!ver##*:}" https://github.com/dappnode/DNP_"${comp}"
@@ -829,7 +834,7 @@ dappnode_core_build() {
829834
cp "./DNP_${comp}/dappnode_package.json" "${DAPPNODE_CORE_DIR}/dappnode_package-${comp_lower}.json"
830835
rm -rf "./DNP_${comp}"
831836
popd >/dev/null || {
832-
echo "Error on popd"
837+
error "Error on popd"
833838
exit 1
834839
}
835840
rm -rf "$tmpdir"
@@ -849,13 +854,13 @@ dappnode_core_download() {
849854
local manifest_var="${comp}_MANIFEST"
850855

851856
# Download DAppNode Core Images if needed
852-
echo "Downloading ${comp} tar..."
857+
log "Downloading ${comp} tar..."
853858
[ -f "${!file_var}" ] || download_file "${!file_var}" "${!url_var}" || exit 1
854859
# Download DAppNode Core docker-compose yml files if needed
855-
echo "Downloading ${comp} yml..."
860+
log "Downloading ${comp} yml..."
856861
[ -f "${!yml_file_var}" ] || download_file "${!yml_file_var}" "${!yml_var}" || exit 1
857862
# Download DAppNode Core manifest files if needed
858-
echo "Downloading ${comp} manifest..."
863+
log "Downloading ${comp} manifest..."
859864
[ -f "${!manifest_file_var}" ] || download_file "${!manifest_file_var}" "${!manifest_var}" || exit 1
860865

861866
# macOS: patch compose files for Docker Desktop compatibility
@@ -941,7 +946,7 @@ addSwap() {
941946

942947
# if not then create it
943948
if [ "$IS_SWAP" -eq 0 ]; then
944-
echo 'Swap not found. Adding swapfile.'
949+
log 'Swap not found. Adding swapfile.'
945950
#RAM=$(awk '/MemTotal/ {print $2}' /proc/meminfo)
946951
#SWAP=$(($RAM * 2))
947952
SWAP=8388608
@@ -951,7 +956,7 @@ addSwap() {
951956
swapon /swapfile
952957
echo '/swapfile none swap defaults 0 0' >>/etc/fstab
953958
else
954-
echo 'Swap found. No changes made.'
959+
log 'Swap found. No changes made.'
955960
fi
956961
}
957962

@@ -1000,15 +1005,15 @@ add_profile_to_shell() {
10001005
}
10011006

10021007
dappnode_core_start() {
1003-
echo "DAppNode starting..." 2>&1 | tee -a "$LOGFILE"
1008+
log "DAppNode starting..."
10041009

10051010
if [[ ${#DNCORE_COMPOSE_ARGS[@]} -eq 0 ]]; then
10061011
build_dncore_compose_args
10071012
fi
10081013
[[ ${#DNCORE_COMPOSE_ARGS[@]} -gt 0 ]] || die "No docker-compose-*.yml files found in ${DAPPNODE_CORE_DIR}"
10091014

10101015
docker compose "${DNCORE_COMPOSE_ARGS[@]}" up -d 2>&1 | tee -a "$LOGFILE"
1011-
echo "DAppNode started" 2>&1 | tee -a "$LOGFILE"
1016+
log "DAppNode started"
10121017

10131018
# Add profile sourcing to user's shell configuration
10141019
add_profile_to_shell
@@ -1027,7 +1032,7 @@ dappnode_core_start() {
10271032
fi
10281033

10291034
# Display help message to the user
1030-
echo "Execute dappnode_help to see a full list with commands available"
1035+
log "Execute dappnode_help to see a full list with commands available"
10311036
}
10321037

10331038
grabContentHashes() {
@@ -1100,58 +1105,58 @@ main() {
11001105
resolve_packages
11011106

11021107
echo "" 2>&1 | tee -a "$LOGFILE"
1103-
echo "##############################################" 2>&1 | tee -a "$LOGFILE"
1104-
echo "#### DAPPNODE INSTALLER ####" 2>&1 | tee -a "$LOGFILE"
1105-
echo "##############################################" 2>&1 | tee -a "$LOGFILE"
1108+
log "##############################################"
1109+
log "#### DAPPNODE INSTALLER ####"
1110+
log "##############################################"
11061111

11071112
# --- Linux-only setup steps ---
11081113
if $IS_LINUX; then
11091114
if [[ "${MINIMAL}" != "true" && "${LITE}" != "true" ]]; then
1110-
echo "Creating swap memory..." 2>&1 | tee -a "$LOGFILE"
1115+
log "Creating swap memory..."
11111116
addSwap
11121117

1113-
echo "Customizing login..." 2>&1 | tee -a "$LOGFILE"
1118+
log "Customizing login..."
11141119
customMotd
11151120

1116-
echo "Installing extra packages..." 2>&1 | tee -a "$LOGFILE"
1121+
log "Installing extra packages..."
11171122
installExtraDpkg
11181123

1119-
echo "Grabbing latest content hashes..." 2>&1 | tee -a "$LOGFILE"
1124+
log "Grabbing latest content hashes..."
11201125
grabContentHashes
11211126

11221127
if [ "$ARCH" == "amd64" ]; then
1123-
echo "Installing SGX modules..." 2>&1 | tee -a "$LOGFILE"
1128+
log "Installing SGX modules..."
11241129
installSgx
11251130

1126-
echo "Installing extra packages..." 2>&1 | tee -a "$LOGFILE"
1131+
log "Installing extra packages..."
11271132
installExtraDpkg # TODO: Why is this being called twice?
11281133
fi
11291134
fi
11301135

1131-
echo "Adding user to docker group..." 2>&1 | tee -a "$LOGFILE"
1136+
log "Adding user to docker group..."
11321137
addUserToDockerGroup
11331138
fi
11341139

11351140
# --- Common steps (Linux and macOS) ---
1136-
echo "Creating dncore_network if needed..." 2>&1 | tee -a "$LOGFILE"
1141+
log "Creating dncore_network if needed..."
11371142
docker network create --driver bridge --subnet 172.33.0.0/16 dncore_network 2>&1 | tee -a "$LOGFILE" || true
11381143

1139-
echo "Building DAppNode Core if needed..." 2>&1 | tee -a "$LOGFILE"
1144+
log "Building DAppNode Core if needed..."
11401145
dappnode_core_build
11411146

1142-
echo "Downloading DAppNode Core..." 2>&1 | tee -a "$LOGFILE"
1147+
log "Downloading DAppNode Core..."
11431148
dappnode_core_download
11441149

11451150
# Build compose args now that compose files exist
11461151
build_dncore_compose_args
11471152

1148-
echo "Loading DAppNode Core..." 2>&1 | tee -a "$LOGFILE"
1153+
log "Loading DAppNode Core..."
11491154
dappnode_core_load
11501155

11511156
# --- Start DAppNode ---
11521157
if $IS_LINUX; then
11531158
if [ ! -f "${DAPPNODE_DIR}/.firstboot" ]; then
1154-
echo "DAppNode installed" 2>&1 | tee -a "$LOGFILE"
1159+
log "DAppNode installed"
11551160
dappnode_core_start
11561161
print_vpn_access_credentials
11571162
fi
@@ -1166,7 +1171,7 @@ main() {
11661171
fi
11671172

11681173
if $IS_MACOS; then
1169-
echo "DAppNode installed" 2>&1 | tee -a "$LOGFILE"
1174+
log "DAppNode installed"
11701175
dappnode_core_start
11711176
print_vpn_access_credentials
11721177
fi

0 commit comments

Comments
 (0)