143 lines
6 KiB
Bash
Executable file
143 lines
6 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# ── Config ─────────────────────────────────────────────
|
|
CONTAINER_ENGINE="${CONTAINER_ENGINE:-}"
|
|
IMAGE_NAME="${IMAGE_NAME:-license-activator}"
|
|
CONTAINER_NAME="${CONTAINER_NAME:-license-activator}"
|
|
HOST_PORT="${HOST_PORT:-8090}"
|
|
|
|
# ── Detect Docker / Podman ─────────────────────────────
|
|
if [ -z "$CONTAINER_ENGINE" ]; then
|
|
if command -v docker &>/dev/null; then
|
|
CONTAINER_ENGINE=docker
|
|
elif command -v podman &>/dev/null; then
|
|
CONTAINER_ENGINE=podman
|
|
else
|
|
echo "Error: neither docker nor podman found. Install one first." >&2
|
|
exit 1
|
|
fi
|
|
fi
|
|
echo "Using container engine: $CONTAINER_ENGINE"
|
|
|
|
# ── Check daemon is running ────────────────────────────
|
|
if ! $CONTAINER_ENGINE info >/dev/null 2>&1; then
|
|
echo "Error: $CONTAINER_ENGINE daemon is not running or you lack permissions." >&2
|
|
echo " Start the daemon or add your user to the docker/podman group." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# ── Stop & remove existing container ───────────────────
|
|
if $CONTAINER_ENGINE ps -a --format '{{.Names}}' | grep -Eq "^${CONTAINER_NAME}$"; then
|
|
echo ""
|
|
echo "Container '$CONTAINER_NAME' already exists."
|
|
echo " → Stopping..."
|
|
$CONTAINER_ENGINE stop "$CONTAINER_NAME" >/dev/null 2>&1 || true
|
|
echo " → Removing..."
|
|
$CONTAINER_ENGINE rm "$CONTAINER_NAME" >/dev/null 2>&1 || true
|
|
echo " → Old container cleaned up."
|
|
echo ""
|
|
else
|
|
echo "No existing container '$CONTAINER_NAME' found."
|
|
fi
|
|
|
|
# ── Check if host port is already in use (by something else) ──
|
|
check_port_in_use() {
|
|
local port="$1"
|
|
if command -v ss &>/dev/null; then
|
|
ss -tln | awk '{print $4}' | grep -Eq ":${port}$"
|
|
elif command -v netstat &>/dev/null; then
|
|
netstat -tln 2>/dev/null | awk '{print $4}' | grep -Eq ":${port}$"
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
if check_port_in_use "$HOST_PORT"; then
|
|
echo "Error: port $HOST_PORT is already listening on this host (another process)." >&2
|
|
echo " Use a different port: HOST_PORT=8080 ./install.sh"
|
|
exit 1
|
|
fi
|
|
|
|
# ── Locate main.go ─────────────────────────────────────
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
MAIN_GO="$SCRIPT_DIR/main.go"
|
|
|
|
if [ ! -f "$MAIN_GO" ]; then
|
|
echo "Error: main.go not found in $SCRIPT_DIR" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# ── Prepare build context ──────────────────────────────
|
|
BUILD_DIR=$(mktemp -d)
|
|
trap 'rm -rf "$BUILD_DIR"' EXIT
|
|
cp "$MAIN_GO" "$BUILD_DIR/"
|
|
|
|
cat > "$BUILD_DIR/Dockerfile" <<'EOF'
|
|
FROM golang:1.23-alpine AS builder
|
|
WORKDIR /build
|
|
COPY main.go .
|
|
RUN go mod init license-activator && go mod tidy
|
|
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o activator main.go
|
|
|
|
FROM alpine:latest
|
|
RUN apk add --no-cache ca-certificates
|
|
WORKDIR /app
|
|
COPY --from=builder /build/activator .
|
|
EXPOSE 8090
|
|
ENTRYPOINT ["./activator"]
|
|
EOF
|
|
|
|
# ── Build ──────────────────────────────────────────────
|
|
echo "Building image $IMAGE_NAME:latest..."
|
|
$CONTAINER_ENGINE build -t "$IMAGE_NAME:latest" "$BUILD_DIR"
|
|
echo ""
|
|
|
|
# ── Run ────────────────────────────────────────────────
|
|
echo "Starting container '$CONTAINER_NAME' on port $HOST_PORT..."
|
|
$CONTAINER_ENGINE run -d \
|
|
--name "$CONTAINER_NAME" \
|
|
-p "${HOST_PORT}:8090" \
|
|
--restart unless-stopped \
|
|
"$IMAGE_NAME:latest"
|
|
|
|
# ── Wait a moment for container to start ───────────────
|
|
sleep 1
|
|
|
|
# ── Show container status ──────────────────────────────
|
|
echo ""
|
|
echo "═══════════════════════════════════════════════════════"
|
|
echo " CONTAINER STATUS"
|
|
echo "═══════════════════════════════════════════════════════"
|
|
|
|
$CONTAINER_ENGINE ps --filter "name=^${CONTAINER_NAME}$" --format \
|
|
" Name: {{.Names}}
|
|
Image: {{.Image}}
|
|
Status: {{.Status}}
|
|
State: {{.State}}
|
|
Ports: {{.Ports}}
|
|
Created: {{.CreatedAt}}
|
|
Command: {{.Command}}"
|
|
|
|
echo ""
|
|
echo "═══════════════════════════════════════════════════════"
|
|
echo " HEALTH CHECK"
|
|
echo "═══════════════════════════════════════════════════════"
|
|
|
|
if curl -sf http://localhost:${HOST_PORT}/api/health >/dev/null 2>&1; then
|
|
echo " ✅ Health endpoint responding on http://localhost:${HOST_PORT}/api/health"
|
|
curl -s http://localhost:${HOST_PORT}/api/health | sed 's/^/ /'
|
|
else
|
|
echo " ⚠️ Health endpoint not responding yet (may need a few seconds)"
|
|
fi
|
|
|
|
echo ""
|
|
echo "═══════════════════════════════════════════════════════"
|
|
echo " QUICK COMMANDS"
|
|
echo "═══════════════════════════════════════════════════════"
|
|
echo " Open app: http://localhost:${HOST_PORT}"
|
|
echo " View logs: $CONTAINER_ENGINE logs -f $CONTAINER_NAME"
|
|
echo " Stop: $CONTAINER_ENGINE stop $CONTAINER_NAME"
|
|
echo " Remove: $CONTAINER_ENGINE rm -f $CONTAINER_NAME"
|
|
echo " Shell inside: $CONTAINER_ENGINE exec -it $CONTAINER_NAME sh"
|
|
echo "" |