-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinstall-local.sh
More file actions
executable file
·336 lines (285 loc) · 11.6 KB
/
install-local.sh
File metadata and controls
executable file
·336 lines (285 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/bin/bash
set -Eeo pipefail
# local Installation Script for Mist
# lhis script copies the local directory to /opt/mist instead of cloning from GitHub
# lseful for local development and testing
LOG_FILE="/tmp/mist-install-local.log"
sudo rm -f "$LOG_FILE" 2>/dev/null || true
: > "$LOG_FILE"
REAL_USER="${SUDO_USER:-$USER}"
REAL_HOME="$(getent passwd "$REAL_USER" | cut -d: -f6)"
APP_NAME="mist"
INSTALL_DIR="/opt/mist"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GO_BACKEND_DIR="server"
GO_BINARY_NAME="mist"
PORT=8080
MIST_FILE="/var/lib/mist/mist.db"
SERVICE_FILE="/etc/systemd/system/$APP_NAME.service"
SPINNER_PID=""
SUDO_KEEPALIVE_PID=""
export PATH="/usr/local/go/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$PATH"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log() { echo -e "${GREEN}[INFO]${NC} $1" | tee -a "$LOG_FILE"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1" | tee -a "$LOG_FILE"; }
error() { echo -e "${RED}[ERROR]${NC} $1" | tee -a "$LOG_FILE"; }
debug() { echo -e "${BLUE}[DEBUG]${NC} $1" | tee -a "$LOG_FILE"; }
spinner() {
local i=0 chars='|/-\'
while :; do
i=$(( (i + 1) % 4 ))
printf "\r⏳ %c" "${chars:$i:1}"
sleep 0.1
done
}
run_step() {
printf "\n▶ $1\n"
spinner & SPINNER_PID=$!
if bash -c "$2" >>"$LOG_FILE" 2>&1; then
kill "$SPINNER_PID" 2>/dev/null || true
wait "$SPINNER_PID" 2>/dev/null || true
printf "\r\033[K✔ Done\n"
return 0
else
kill "$SPINNER_PID" 2>/dev/null || true
wait "$SPINNER_PID" 2>/dev/null || true
printf "\r\033[K✘ Failed\n"
return 1
fi
}
cleanup() {
kill "$SPINNER_PID" 2>/dev/null || true
kill "$SUDO_KEEPALIVE_PID" 2>/dev/null || true
}
trap cleanup EXIT
rollback() {
error "Installation failed! Cleaning up..."
if [ -f "$SERVICE_FILE" ]; then
sudo systemctl stop "$APP_NAME" 2>>"$LOG_FILE" || true
sudo systemctl disable "$APP_NAME" 2>>"$LOG_FILE" || true
sudo rm -f "$SERVICE_FILE" 2>>"$LOG_FILE" || true
fi
error "Check logs: $LOG_FILE"
tail -30 "$LOG_FILE" 2>/dev/null
cleanup
exit 1
}
trap - ERR
echo
echo "╔════════════════════════════════════════════╗"
echo "║ 🚀 Mist Local Installation ║"
echo "║ (Development Mode) ║"
echo "╚════════════════════════════════════════════╝"
echo
log "Starting Mist local installation from: $SCRIPT_DIR"
if [ "$EUID" -ne 0 ] && [ -z "${SUDO_USER:-}" ]; then
error "This script requires sudo. Run: sudo bash install-local.sh"
exit 1
fi
# Verify we're in a valid Mist directory
if [ ! -f "$SCRIPT_DIR/traefik-static.yml" ] || [ ! -d "$SCRIPT_DIR/server" ]; then
error "This doesn't look like a Mist directory!"
error "Expected to find traefik-static.yml and server/ directory"
exit 1
fi
echo "🔐 Verifying sudo access..."
sudo -v || exit 1
(while true; do sleep 60; sudo -n true || exit; done) 2>/dev/null &
SUDO_KEEPALIVE_PID=$!
log "Checking disk space..."
AVAILABLE=$(df /opt 2>/dev/null | tail -1 | awk '{print $4}' || echo "0")
if [ "$AVAILABLE" -lt 2000000 ]; then
error "Need at least 2GB free in /opt"
exit 1
fi
trap rollback ERR
log "Installing dependencies..."
if command -v apt >/dev/null 2>&1; then
run_step "Installing packages (apt)" "sudo DEBIAN_FRONTEND=noninteractive apt update && sudo DEBIAN_FRONTEND=noninteractive apt install -y git curl build-essential wget unzip rsync" || exit 1
elif command -v dnf >/dev/null 2>&1; then
run_step "Installing packages (dnf)" "sudo dnf install -y git curl gcc make wget unzip rsync" || exit 1
elif command -v yum >/dev/null 2>&1; then
run_step "Installing packages (yum)" "sudo yum install -y git curl gcc make wget unzip rsync" || exit 1
elif command -v pacman >/dev/null 2>&1; then
run_step "Installing packages (pacman)" "sudo pacman -Sy --noconfirm git curl base-devel wget unzip rsync" || exit 1
else
error "Unsupported package manager"
exit 1
fi
if ! command -v docker >/dev/null 2>&1; then
error "Docker not found. Install from: https://docs.docker.com/engine/install/"
exit 1
fi
if ! docker ps >/dev/null 2>&1; then
error "Docker not running or no permissions"
exit 1
fi
log "Docker verified"
if ! command -v go >/dev/null 2>&1; then
ARCH=$(uname -m)
case "$ARCH" in
x86_64) GO_ARCH="amd64";;
aarch64|arm64) GO_ARCH="arm64";;
armv7l) GO_ARCH="armv6l";;
*) error "Unsupported arch: $ARCH"; exit 1;;
esac
run_step "Downloading Go" "wget -q https://go.dev/dl/go1.22.11.linux-${GO_ARCH}.tar.gz -O /tmp/go.tar.gz" || exit 1
run_step "Installing Go" "sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf /tmp/go.tar.gz && rm -f /tmp/go.tar.gz" || exit 1
grep -q '/usr/local/go/bin' "$REAL_HOME/.bashrc" 2>/dev/null || echo 'export PATH=$PATH:/usr/local/go/bin' >>"$REAL_HOME/.bashrc"
export PATH="$PATH:/usr/local/go/bin"
fi
go version >>"$LOG_FILE" 2>&1 || { error "Go not working"; exit 1; }
log "Go ready"
# ---------------- Local Copy ----------------
log "Copying local files to $INSTALL_DIR..."
debug "Source: $SCRIPT_DIR"
debug "Destination: $INSTALL_DIR"
# Stop existing service if running
if sudo systemctl is-active --quiet "$APP_NAME" 2>/dev/null; then
log "Stopping existing service..."
sudo systemctl stop "$APP_NAME" || true
fi
# Stop Traefik if running
if docker ps --format '{{.Names}}' | grep -q "^traefik$"; then
log "Stopping Traefik container..."
docker compose -f "$INSTALL_DIR/traefik-compose.yml" down 2>/dev/null || docker stop traefik 2>/dev/null || true
fi
# Create install directory
run_step "Creating install directory" "sudo mkdir -p '$INSTALL_DIR'" || exit 1
# Copy files using rsync (excludes .git, node_modules, build artifacts)
run_step "Copying files" "sudo rsync -av --delete \
--exclude='.git' \
--exclude='node_modules' \
--exclude='dist' \
--exclude='build' \
--exclude='*.log' \
--exclude='mist.db' \
--exclude='letsencrypt' \
--exclude='server/mist' \
--exclude='cli/mist-cli' \
--exclude='.env' \
'$SCRIPT_DIR/' '$INSTALL_DIR/'" || exit 1
[ -d "$INSTALL_DIR/$GO_BACKEND_DIR" ] || { error "Server directory missing"; exit 1; }
run_step "Setting ownership" "sudo chown -R root:root '$INSTALL_DIR'" || exit 1
log "Local files copied successfully"
run_step "Creating data directories" "sudo mkdir -p /var/lib/mist/{traefik,logs,backups} && sudo touch '$MIST_FILE' && sudo chown -R root:root /var/lib/mist && sudo chmod -R 755 /var/lib/mist" || exit 1
run_step "Creating Traefik dynamic config" "sudo tee /var/lib/mist/traefik/dynamic.yml >/dev/null <<'EOF'
http:
routers: {}
services: {}
EOF
" || exit 1
cd "$INSTALL_DIR/$GO_BACKEND_DIR"
[ -f "go.mod" ] || { error "go.mod missing"; exit 1; }
run_step "Downloading dependencies" "cd '$INSTALL_DIR/$GO_BACKEND_DIR' && go mod download && go mod tidy" || exit 1
run_step "Building backend" "cd '$INSTALL_DIR/$GO_BACKEND_DIR' && go build -v -o '$GO_BINARY_NAME'" || exit 1
[ -f "$GO_BINARY_NAME" ] || { error "Binary not created"; exit 1; }
chmod +x "$GO_BINARY_NAME"
log "Build complete"
# ---------------- Dashboard Build ----------------
if [ -d "$INSTALL_DIR/dash" ]; then
if ! command -v node >/dev/null 2>&1; then
error "Node.js not found. Install from: https://nodejs.org/"
exit 1
fi
if ! command -v bun>/dev/null 2>&1; then
error "bun not found"
exit 1
fi
run_step "Installing dashboard dependencies" "cd '$INSTALL_DIR/dash' && bun install" || exit 1
run_step "Building dashboard" "cd '$INSTALL_DIR/dash' && bun run build" || exit 1
# Move build output to server/static
DASH_BUILD_DIR="$INSTALL_DIR/dash/dist"
STATIC_DIR="$INSTALL_DIR/server/static"
if [ -d "$DASH_BUILD_DIR" ]; then
run_step "Moving dashboard build to static folder" "sudo rm -rf '$STATIC_DIR' && sudo mv '$DASH_BUILD_DIR' '$STATIC_DIR'" || exit 1
log "Dashboard built and deployed to server/static"
else
error "Dashboard build output not found"
exit 1
fi
else
warn "Dashboard directory not found, skipping dashboard build"
fi
# ---------------- CLI Tool ----------------
if [ -d "$INSTALL_DIR/cli" ]; then
if run_step "Building CLI tool" "cd '$INSTALL_DIR/cli' && go mod tidy && go build -o mist-cli"; then
if run_step "Installing CLI tool" "sudo cp '$INSTALL_DIR/cli/mist-cli' /usr/local/bin/mist-cli && sudo chmod +x /usr/local/bin/mist-cli"; then
log "CLI tool installed: mist-cli"
else
warn "Failed to install CLI tool, but continuing..."
fi
else
warn "Failed to build CLI tool, but continuing..."
fi
fi
run_step "Creating systemd service" "sudo tee '$SERVICE_FILE' >/dev/null <<'EOF'
[Unit]
Description=Mist Service (Local Dev)
After=network.target docker.service
Requires=docker.service
[Service]
WorkingDirectory=/opt/mist/server
ExecStart=/opt/mist/server/mist
Restart=always
RestartSec=5
User=root
Environment=PORT=8080
Environment=PATH=/usr/local/go/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
[Install]
WantedBy=multi-user.target
EOF
" || exit 1
run_step "Starting service" "sudo systemctl daemon-reload && sudo systemctl enable '$APP_NAME' && sudo systemctl start '$APP_NAME'" || exit 1
sleep 3
sudo systemctl is-active --quiet "$APP_NAME" || { error "Service failed to start"; sudo journalctl -u "$APP_NAME" -n 20; exit 1; }
log "Service running"
[ -f "$INSTALL_DIR/traefik-compose.yml" ] || { error "traefik-compose.yml missing"; exit 1; }
run_step "Creating Docker network" "docker network inspect traefik-net >/dev/null 2>&1 || docker network create traefik-net" || warn "Network creation failed"
run_step "Starting Traefik" "docker compose -f '$INSTALL_DIR/traefik-compose.yml' up -d" || warn "Traefik failed"
if command -v ufw >/dev/null 2>&1; then
sudo ufw allow $PORT/tcp 2>&1 || true
elif command -v firewall-cmd >/dev/null 2>&1; then
sudo firewall-cmd --permanent --add-port=${PORT}/tcp 2>&1 || true
sudo firewall-cmd --reload 2>&1 || true
fi
log "Running health check..."
sleep 5
HTTP_OK=false
for i in {1..10}; do
if curl -f -s -o /dev/null "http://localhost:$PORT/api/health" 2>>"$LOG_FILE"; then
HTTP_OK=true
break
fi
sleep 2
done
[ "$HTTP_OK" = true ] && log "HTTP check passed" || warn "HTTP check failed (may still be initializing)"
SERVER_IP=$(hostname -i 2>/dev/null | awk '{print $1}' || ip route get 1 2>/dev/null | awk '{print $7; exit}' || echo "localhost")
CLI_INSTALLED=""
if [ -f "/usr/local/bin/mist-cli" ]; then
CLI_INSTALLED="║ 💻 CLI Tool: mist-cli --help ║"
fi
echo
echo "╔════════════════════════════════════════════╗"
echo "║ 🎉 Mist local installation complete ║"
printf "║ 👉 %-40s║\n" "http://$SERVER_IP:$PORT"
printf "║ 👉 %-40s║\n" "http://localhost:$PORT"
if [ -n "$CLI_INSTALLED" ]; then
printf "║ %-42s ║\n" "$CLI_INSTALLED"
fi
echo "╚════════════════════════════════════════════╝"
echo
echo "📄 Logs: $LOG_FILE"
echo "🔧 Service: sudo systemctl status $APP_NAME"
echo "📋 Logs: sudo journalctl -u $APP_NAME -f"
if [ -f "/usr/local/bin/mist-cli" ]; then
echo "💻 CLI Tool: mist-cli --help"
fi
echo
echo "💡 To reinstall after changes:"
echo " sudo bash install-local.sh"
echo