#!/usr/bin/env bash # # LWSS installer — spin up a private, self-hosted web-search MCP server. # # curl -fsSL https://git.neilbantoc.dev/pub/local-web-search/raw/branch/main/install.sh | bash # # Idempotent: re-running updates the checkout and restarts the stack, preserving # the generated .env (and thus your API key). Supports Linux and macOS. # # Environment overrides: # LWSS_DIR install dir (default: ~/local-web-search) # LWSS_REPO git clone URL (default: the canonical repo) # LWSS_BRANCH branch to check out (default: main) # LWSS_PORT host port (default: 8000) # LWSS_BIND host bind address (default: 0.0.0.0) # LWSS_NO_START set to 1 to configure but not `compose up` # set -euo pipefail REPO="${LWSS_REPO:-https://git.neilbantoc.dev/pub/local-web-search.git}" BRANCH="${LWSS_BRANCH:-main}" DIR="${LWSS_DIR:-$HOME/local-web-search}" PORT="${LWSS_PORT:-8000}" BIND="${LWSS_BIND:-0.0.0.0}" c_blue='\033[1;34m'; c_grn='\033[1;32m'; c_yel='\033[1;33m'; c_red='\033[1;31m'; c_off='\033[0m' info() { printf "${c_blue}==>${c_off} %s\n" "$*"; } ok() { printf "${c_grn}✓${c_off} %s\n" "$*"; } warn() { printf "${c_yel}!${c_off} %s\n" "$*"; } die() { printf "${c_red}✗ %s${c_off}\n" "$*" >&2; exit 1; } need_cmd() { command -v "$1" >/dev/null 2>&1; } # ---- 1. prerequisites -------------------------------------------------------- OS="$(uname -s)" info "Detected OS: $OS" if ! need_cmd git; then die "git is required but not installed. Install git and re-run." fi install_docker_linux() { warn "Docker not found. Installing via get.docker.com (needs sudo)…" need_cmd curl || die "curl is required to install Docker." curl -fsSL https://get.docker.com | sh if need_cmd systemctl; then sudo systemctl enable --now docker || true; fi # Add invoking user to the docker group for socket access (effective next login). if [ "${SUDO_USER:-$USER}" != "root" ]; then sudo usermod -aG docker "${SUDO_USER:-$USER}" || true warn "Added ${SUDO_USER:-$USER} to the 'docker' group — log out/in for it to take effect." warn "This run will use sudo for docker commands." fi } if ! need_cmd docker; then case "$OS" in Linux) install_docker_linux ;; Darwin) die "Docker not found. Install Docker Desktop for Mac (https://docs.docker.com/desktop/) and re-run." ;; *) die "Unsupported OS: $OS" ;; esac fi # Decide how to call docker (plain, or sudo if the socket isn't accessible yet). DOCKER="docker" if ! docker info >/dev/null 2>&1; then if sudo -n docker info >/dev/null 2>&1 || sudo docker info >/dev/null 2>&1; then DOCKER="sudo docker" warn "Using 'sudo docker' (current user lacks docker socket access this session)." else die "Docker is installed but the daemon isn't reachable. Start it and re-run." fi fi if $DOCKER compose version >/dev/null 2>&1; then COMPOSE="$DOCKER compose" elif need_cmd docker-compose; then COMPOSE="docker-compose" else die "Docker Compose v2 plugin not found. Install it and re-run." fi ok "Docker and Compose are available." # ---- 2. fetch / update the repo --------------------------------------------- if [ -d "$DIR/.git" ]; then info "Updating existing checkout at $DIR" git -C "$DIR" fetch --quiet origin "$BRANCH" git -C "$DIR" checkout --quiet "$BRANCH" git -C "$DIR" pull --quiet --ff-only origin "$BRANCH" else info "Cloning $REPO -> $DIR" git clone --quiet --branch "$BRANCH" "$REPO" "$DIR" fi cd "$DIR" # ---- 3. generate .env with strong secrets (preserved on re-run) ------------- gen_secret() { if need_cmd openssl; then openssl rand -hex 32 else head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n'; fi } if [ ! -f .env ]; then info "Generating .env with fresh secrets" cp .env.example .env API_KEY="$(gen_secret)" SEARXNG_SECRET="$(gen_secret)" # Portable in-place edit (BSD vs GNU sed). sed -i.bak "s|^LWSS_API_KEY=.*|LWSS_API_KEY=${API_KEY}|" .env sed -i.bak "s|^SEARXNG_SECRET=.*|SEARXNG_SECRET=${SEARXNG_SECRET}|" .env sed -i.bak "s|^ORCHESTRATOR_PORT=.*|ORCHESTRATOR_PORT=${PORT}|" .env sed -i.bak "s|^ORCHESTRATOR_BIND=.*|ORCHESTRATOR_BIND=${BIND}|" .env rm -f .env.bak ok "Wrote .env (API key generated)" else warn ".env already exists — keeping it (secrets preserved)." fi API_KEY="$(grep '^LWSS_API_KEY=' .env | cut -d= -f2-)" # ---- 4. bring the stack up --------------------------------------------------- if [ "${LWSS_NO_START:-0}" = "1" ]; then warn "LWSS_NO_START=1 — skipping 'compose up'. Run: $COMPOSE up --build -d" else info "Building and starting the stack (first run pulls images + builds; be patient)…" $COMPOSE up --build -d info "Waiting for the orchestrator to become healthy…" for _ in $(seq 1 60); do if curl -fsS "http://localhost:${PORT}/health" >/dev/null 2>&1; then break; fi sleep 2 done if curl -fsS "http://localhost:${PORT}/health" >/dev/null 2>&1; then ok "Stack is up." else warn "Health check didn't pass yet. Inspect with: $COMPOSE logs -f orchestrator" fi fi # ---- 5. next steps ----------------------------------------------------------- HOSTIP="$(hostname -I 2>/dev/null | awk '{print $1}' || echo localhost)" cat <