You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
2.9 KiB
120 lines
2.9 KiB
#!/bin/sh |
|
set -e |
|
|
|
# utun_update.sh - Remote uTun update script |
|
# Usage: ./tools/utun_update.sh [fast|full] [workdir] |
|
# fast: git pull + make (incremental build) |
|
# full: git pull + make distclean + autoreconf + configure + make |
|
# Returns: |
|
# 0 - success |
|
# 1 - invalid arguments or not a git repo |
|
# 2 - git pull failed |
|
# 3 - build failed |
|
# 4 - start failed |
|
|
|
# Determine project root from script location |
|
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) |
|
PROJECT_ROOT=$(cd "$SCRIPT_DIR/.." && pwd) |
|
|
|
# Parse arguments |
|
MODE="${1:-fast}" |
|
WORKDIR="${2:-$PROJECT_ROOT}" |
|
|
|
# Validate mode |
|
if [ "$MODE" != "fast" ] && [ "$MODE" != "full" ]; then |
|
echo "ERROR: Invalid mode '$MODE'. Use 'fast' or 'full'" >&2 |
|
exit 1 |
|
fi |
|
|
|
# Change to workdir |
|
cd "$WORKDIR" || { echo "ERROR: Cannot cd to $WORKDIR" >&2; exit 1; } |
|
|
|
# Validate git repository |
|
if [ ! -d ".git" ]; then |
|
echo "ERROR: Not a git repository: $WORKDIR" >&2 |
|
exit 1 |
|
fi |
|
|
|
PIDFILE="/var/run/utun.pid" |
|
UTUN_BIN="./utun" |
|
|
|
echo "=== uTun Update ===" |
|
echo "Mode: $MODE" |
|
echo "Workdir: $(pwd)" |
|
|
|
# Stop utun if running via pidfile |
|
if [ -f "$PIDFILE" ]; then |
|
PID=$(cat "$PIDFILE") |
|
if kill -0 "$PID" 2>/dev/null; then |
|
echo "Stopping utun (PID $PID)..." |
|
kill -TERM "$PID" 2>/dev/null || true |
|
# Wait graceful shutdown (up to 10s) |
|
for i in 1 2 3 4 5 6 7 8 9 10; do |
|
if ! kill -0 "$PID" 2>/dev/null; then |
|
break |
|
fi |
|
sleep 1 |
|
done |
|
# Force kill if still running |
|
if kill -0 "$PID" 2>/dev/null; then |
|
echo "Force killing utun (PID $PID)..." |
|
kill -9 "$PID" 2>/dev/null || true |
|
sleep 1 |
|
fi |
|
fi |
|
rm -f "$PIDFILE" |
|
fi |
|
|
|
# Git pull |
|
echo "Pulling latest changes..." |
|
if ! git pull; then |
|
echo "ERROR: git pull failed" >&2 |
|
exit 2 |
|
fi |
|
|
|
# Detect number of CPUs (Linux or BSD) |
|
if command -v nproc >/dev/null 2>&1; then |
|
JOBS=$(nproc) |
|
else |
|
JOBS=$(sysctl -n hw.ncpu 2>/dev/null || echo 1) |
|
fi |
|
|
|
# Build |
|
if [ "$MODE" = "full" ]; then |
|
echo "Full rebuild (autotools, clean, configure)..." |
|
make distclean 2>/dev/null || true |
|
autoreconf -fi 2>&1 | tee autoreconf.log |
|
./configure --prefix=/usr/local 2>&1 | tee configure.log |
|
make clean |
|
else |
|
echo "Fast rebuild (incremental)..." |
|
fi |
|
|
|
echo "Building with $JOBS parallel jobs..." |
|
if ! make -j"$JOBS" 2>&1 | tee build.log; then |
|
echo "ERROR: Build failed" >&2 |
|
exit 3 |
|
fi |
|
|
|
# Verify binary exists |
|
if [ ! -x "$UTUN_BIN" ]; then |
|
echo "ERROR: Binary not found or not executable: $UTUN_BIN" >&2 |
|
exit 3 |
|
fi |
|
|
|
echo "Build successful: $(ls -la $UTUN_BIN)" |
|
|
|
# Start utun |
|
echo "Starting utun..." |
|
nohup "$UTUN_BIN" > utun.log 2> utun_err.log & |
|
NEW_PID=$! |
|
sleep 2 |
|
|
|
# Verify process is running |
|
if kill -0 "$NEW_PID" 2>/dev/null; then |
|
echo "uTun started successfully (PID $NEW_PID)" |
|
exit 0 |
|
else |
|
echo "ERROR: Failed to start utun (PID $NEW_PID died)" >&2 |
|
exit 4 |
|
fi
|
|
|