Skip to content

Commit beaf111

Browse files
committed
feat: add dedicated AMI build script
Replaces fragile 'head -n -12' line-stripping of the prerequisites script with a self-contained build script that: - Installs Docker, wireguard, lsof, iptables - Sets up /etc/rc.local for first-boot DAppNode installation - Skips network connectivity checks (not needed during AMI build) - Handles Ubuntu 24 LTS properly
1 parent 1dbea0d commit beaf111

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

scripts/dappnode_ami_build.sh

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/bin/bash
2+
# DAppNode AMI Build Script
3+
# Purpose: Install prerequisites and set up first-boot installer for EC2 Image Builder.
4+
# This script is self-contained and does NOT depend on line-counting hacks
5+
# against the upstream prerequisites script (which changes over time).
6+
7+
set -euo pipefail
8+
9+
DAPPNODE_DIR="/usr/src/dappnode"
10+
LOGS_DIR="$DAPPNODE_DIR/logs"
11+
LOG_FILE="$LOGS_DIR/ami_build.log"
12+
13+
mkdir -p "$DAPPNODE_DIR/scripts" "$LOGS_DIR"
14+
touch "$LOG_FILE"
15+
16+
log() { echo "[AMI-BUILD] $*" | tee -a "$LOG_FILE"; }
17+
18+
lsb_dist="$(. /etc/os-release && echo "$ID")"
19+
log "Detected OS: $lsb_dist"
20+
21+
# ─── Docker ───────────────────────────────────────────────────────────────────
22+
install_docker() {
23+
log "Installing Docker..."
24+
apt-get update -y
25+
apt-get remove -y docker docker-engine docker.io containerd runc || true
26+
27+
apt-get install -y ca-certificates curl lsb-release
28+
install -m 0755 -d /etc/apt/keyrings
29+
curl -fsSL "https://download.docker.com/linux/${lsb_dist}/gpg" -o /etc/apt/keyrings/docker.asc
30+
chmod a+r /etc/apt/keyrings/docker.asc
31+
32+
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/$lsb_dist $(lsb_release -cs) stable" \
33+
| tee /etc/apt/sources.list.d/docker.list >/dev/null
34+
35+
apt-get update -y
36+
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
37+
[ -f "/usr/bin/xz" ] || apt-get install -y xz-utils
38+
log "Docker installed successfully"
39+
}
40+
41+
# ─── Docker Compose alias (legacy compatibility) ──────────────────────────────
42+
install_compose_alias() {
43+
cat >/usr/local/bin/docker-compose <<'EOL'
44+
#!/bin/bash
45+
docker compose "$@"
46+
EOL
47+
chmod +x /usr/local/bin/docker-compose
48+
}
49+
50+
# ─── Wireguard ────────────────────────────────────────────────────────────────
51+
install_wireguard() {
52+
log "Installing wireguard..."
53+
apt-get update -y
54+
apt-get install -y wireguard-dkms || apt-get install -y wireguard-tools || true
55+
if modprobe wireguard >/dev/null 2>&1; then
56+
log "Wireguard module loaded"
57+
else
58+
log "WARNING: wireguard kernel module not available (may be built-in on this kernel)"
59+
fi
60+
}
61+
62+
# ─── Lsof ─────────────────────────────────────────────────────────────────────
63+
install_lsof() {
64+
log "Installing lsof..."
65+
apt-get install -y lsof
66+
}
67+
68+
# ─── Iptables ─────────────────────────────────────────────────────────────────
69+
install_iptables() {
70+
log "Installing iptables..."
71+
apt-get install -y iptables
72+
}
73+
74+
# ─── Main prerequisites installation ─────────────────────────────────────────
75+
log "=== Starting DAppNode AMI prerequisites ==="
76+
77+
apt-get update -y | tee -a "$LOG_FILE"
78+
79+
if ! docker -v >/dev/null 2>&1; then
80+
install_docker 2>&1 | tee -a "$LOG_FILE"
81+
else
82+
log "Docker already installed"
83+
fi
84+
85+
install_compose_alias
86+
87+
if ! modprobe wireguard >/dev/null 2>&1; then
88+
install_wireguard 2>&1 | tee -a "$LOG_FILE"
89+
else
90+
log "Wireguard already available"
91+
fi
92+
93+
if ! lsof -v >/dev/null 2>&1; then
94+
install_lsof 2>&1 | tee -a "$LOG_FILE"
95+
else
96+
log "lsof already installed"
97+
fi
98+
99+
if ! iptables -V >/dev/null 2>&1; then
100+
install_iptables 2>&1 | tee -a "$LOG_FILE"
101+
else
102+
log "iptables already installed"
103+
fi
104+
105+
# ─── Set up first-boot installer ─────────────────────────────────────────────
106+
log "=== Setting up first-boot installer ==="
107+
108+
wget -O "$DAPPNODE_DIR/scripts/dappnode_install.sh" https://installer.dappnode.io
109+
chmod +x "$DAPPNODE_DIR/scripts/dappnode_install.sh"
110+
111+
cat > /etc/rc.local << 'RC'
112+
#!/bin/sh -e
113+
/usr/src/dappnode/scripts/dappnode_install.sh
114+
exit 0
115+
RC
116+
chmod +x /etc/rc.local
117+
118+
log "=== AMI build complete ==="

0 commit comments

Comments
 (0)