Repository Public
1#!/usr/bin/env bash2# =============================================================================3# SP BOUCHER | SOFTWARE & RESEARCH4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : SPIDER — Universal Web Intelligence Engine8# File : scripts/bootstrap.sh9# Module : Scripts10# Repository : https://git.spboucher.ai/spider11# Project Website : Not specified12# Created : 2026-12-0413# Last Modified : 2026-12-0414#15# Author : Simon-Pierre Boucher16# Position : Professor of Finance17# Institution : Université du Québec en Outaouais (UQO)18# Organization : SP Boucher19# Website : https://spboucher.ai20# Git : https://git.spboucher.ai21#22# Preferred Contact: contact@spboucher.ai23# Development : dev@spboucher.ai24# Support : support@spboucher.ai25# Security/Admin : admin@spboucher.ai26#27# Copyright (c) 202628# Simon-Pierre Boucher29# All rights reserved where applicable. This file is governed by the30# applicable project license, copyright notices and relevant law.31# Existing third-party rights and notices remain unaffected.32#33# License : ISC34#35# Do not embed credentials, private keys, access tokens,36# certificates or confidential personal information.37#38# =============================================================================39# END OF SP BOUCHER SOURCE HEADER40# =============================================================================4142# Idempotent installation of a SPIDER node: environment, browsers, OCR binary, database, first keys.43# Usage: scripts/bootstrap.sh [--role gateway|worker|all] [--no-browsers] [--vision]44# Never overwrites an existing env file or secret; never prints a key (keys go to the 0600 env file).45set -euo pipefail46cd "$(dirname "$0")/.."47export PATH="/opt/homebrew/bin:/usr/local/bin:$HOME/.local/bin:$PATH"48ROLE=all; BROWSERS=1; VISION=049while [ $# -gt 0 ]; do case "$1" in50 --role) ROLE="$2"; shift 2 ;; --no-browsers) BROWSERS=0; shift ;; --vision) VISION=1; shift ;;51 *) echo "unknown option $1" >&2; exit 2 ;; esac; done52CONF="${SPIDER_CONF_DIR:-$HOME/.config/spider}"; ENV_FILE="$CONF/env"53DATA="${SPIDER_DATA_DIR:-$HOME/spider-data}"; DB="${SPIDER_DB_NAME:-spider}"54mkdir -p "$CONF" "$DATA"; chmod 700 "$CONF"5556command -v uv >/dev/null || { echo "uv is required (https://docs.astral.sh/uv/)" >&2; exit 1; }57[ -d .venv ] || uv venv --python 3.13 -q .venv58uv pip install -q -e ".[browser]"59if [ "$BROWSERS" = 1 ] && [ "$ROLE" != gateway ]; then .venv/bin/playwright install chromium webkit >/dev/null; fi60if [ "$(uname)" = Darwin ] && [ "$ROLE" != gateway ] && command -v swift >/dev/null; then61 bash services/ocr-worker/build.sh >/dev/null && echo "ocr: spider-ocr built"62fi63if [ "$VISION" = 1 ] && [ "$(uname)" = Darwin ]; then64 [ -d "$HOME/.spider/venv-vision" ] || uv venv --python 3.13 -q "$HOME/.spider/venv-vision"65 uv pip install -q --python "$HOME/.spider/venv-vision/bin/python" mlx-vlm66 echo "vision: environment ready; the model must be present in the Hugging Face cache (see docs/ADMIN.md)"67fi6869if [ ! -f "$ENV_FILE" ]; then70 umask 17771 cat > "$ENV_FILE" <<ENV72SPIDER_ENV=production73SPIDER_DATABASE_URL=postgresql://localhost/$DB74SPIDER_DATA_DIR=$DATA75SPIDER_HOST=${SPIDER_HOST:-127.0.0.1}76SPIDER_PORT=${SPIDER_PORT:-8900}77SPIDER_PUBLIC_URL=${SPIDER_PUBLIC_URL:-http://127.0.0.1:8900}78SPIDER_SECRET_FILE=$CONF/secret.key79SPIDER_GATEWAY=${SPIDER_GATEWAY:-http://127.0.0.1:${SPIDER_PORT:-8900}}80SPIDER_CAPABILITIES=${SPIDER_CAPABILITIES:-http,browser,ocr}81SPIDER_CONCURRENCY=${SPIDER_CONCURRENCY:-2}82ENV83 umask 022; echo "env: created $ENV_FILE"84fi85set -a; . "$ENV_FILE"; set +a8687if [ "$ROLE" != worker ]; then88 command -v psql >/dev/null || { echo "PostgreSQL client not found" >&2; exit 1; }89 psql -d postgres -Atc "select 1 from pg_database where datname='$DB'" | grep -q 1 || createdb "$DB"90 .venv/bin/spider migrate >/dev/null && echo "db: migrations applied on $DB"91 if ! grep -q '^SPIDER_ADMIN_KEY=' "$ENV_FILE"; then92 TMP="$(mktemp)"; .venv/bin/spider bootstrap --tenant default --admin-key admin --worker-key "worker-$(hostname -s)" --write-env "$TMP" >/dev/null93 cat "$TMP" >> "$ENV_FILE"; rm -f "$TMP"; echo "keys: admin and worker keys written to $ENV_FILE (0600)"94 fi95fi96echo "bootstrap done (role=$ROLE)"97