replica-omnisciente/.aurelio/mcp/setup.sh
Fábio Cunha 4fd171b0a4 chore(conscience): upgrade to v0.10.0 and align submodule paths
- Add savearth-workspace MCP server deployment to conscience state.
- Refactor .agent/ references to replica-omnisciente/ in operational
  scripts (setup.sh, savearth-mcp/server.py) and docs (READMEs,
  DIRECTORY_GUIDE.md, upgrade-conscience workflow).
- Document remaining legacy misspellings and stale .agent/ doc refs
  for follow-up cleanup.
- Record conscience upgrade report in data/conscience/ and
  .aurelio/memory/conscience_upgrade_report.md.

[skip ci]
2026-07-04 23:01:09 +01:00

232 lines
7.2 KiB
Bash

#!/usr/bin/env bash
# ──────────────────────────────────────────────────────────────────────
# savearth MCP Server — Proxmox LXC Setup
#
# Creates a Debian 13 LXC container on a Proxmox host, installs
# dependencies, deploys the MCP server, and registers with AWS SSM.
#
# Usage (from the Proxmox host):
# bash replica-omnisciente/.aurelio/mcp/setup.sh [VMID] [GITHUB_PAT]
#
# Defaults:
# VMID = 201
# Container name = savearth-mcp-ct201
# ──────────────────────────────────────────────────────────────────────
set -euo pipefail
VMID="${1:-201}"
CT_NAME="savearth-mcp-ct${VMID}"
GITHUB_PAT="${2:-}"
REPO_URL="https://github.com/SavearthTech/aws-iot-core-poc.git"
BRANCH="main"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log() { echo -e "${GREEN}[✓]${NC} $*"; }
warn() { echo -e "${YELLOW}[!]${NC} $*"; }
err() { echo -e "${RED}[✗]${NC} $*" >&2; }
info() { echo -e "${BLUE}[i]${NC} $*"; }
# ─── Detect if running on Proxmox host vs inside container ───────────
if command -v pveversion &>/dev/null; then
info "Running on Proxmox host ($(pveversion --verbose | head -1))"
ON_PROXMOX_HOST=true
else
ON_PROXMOX_HOST=false
fi
# ─── Phase 1: Create LXC (only on Proxmox host) ─────────────────────
if $ON_PROXMOX_HOST; then
if pct status "$VMID" &>/dev/null; then
warn "Container CT $VMID already exists. Skipping creation."
else
log "Creating LXC container CT $VMID ($CT_NAME)..."
# Use the latest Debian template
TEMPLATE=$(pveam available --section system | grep 'debian-13' | tail -1 | awk '{print $2}')
if [ -z "$TEMPLATE" ]; then
err "No Debian 13 template found. Downloading..."
pveam download local debian-13-standard_13.0-1_amd64.tar.zst
TEMPLATE="debian-13-standard_13.0-1_amd64.tar.zst"
fi
pct create "$VMID" "local:vztmpl/$TEMPLATE" \
--hostname "$CT_NAME" \
--cores 2 \
--memory 1024 \
--swap 512 \
--rootfs "local-lvm:8" \
--net0 "name=eth0,bridge=vmbr0,ip=dhcp" \
--unprivileged 1 \
--features nesting=1 \
--start 1 \
--onboot 1
log "Container CT $VMID created and started."
sleep 5 # Wait for network
fi
# Ensure container is running
if [ "$(pct status $VMID | awk '{print $2}')" != "running" ]; then
pct start "$VMID"
sleep 5
fi
# Push this script into the container and run Phase 2
info "Pushing setup script into container..."
SCRIPT_PATH=$(readlink -f "$0")
pct push "$VMID" "$SCRIPT_PATH" /tmp/setup_mcp_server.sh
pct exec "$VMID" -- bash /tmp/setup_mcp_server.sh "$VMID" "$GITHUB_PAT"
log "MCP server deployment complete on CT $VMID!"
echo ""
info "Connect to the container:"
echo " pct enter $VMID"
echo ""
info "MCP server status:"
echo " pct exec $VMID -- systemctl status savearth-mcp"
echo ""
info "MCP SSE endpoint (from host network):"
CT_IP=$(pct exec "$VMID" -- hostname -I | awk '{print $1}')
echo " http://${CT_IP}:8080/sse"
exit 0
fi
# ─── Phase 2: Inside-container setup ────────────────────────────────
log "Running inside container setup..."
# System packages
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get install -y -qq \
python3 python3-pip python3-venv \
git curl jq ca-certificates \
> /dev/null 2>&1
log "System packages installed."
# Create service user
if ! id -u savearth &>/dev/null; then
useradd --system --create-home --shell /bin/bash savearth
log "User 'savearth' created."
fi
# Clone or update repo
MCP_HOME="/opt/savearth-mcp"
if [ -d "$MCP_HOME/.git" ]; then
info "Repo exists, pulling latest..."
cd "$MCP_HOME"
git fetch origin
git checkout "$BRANCH"
git pull origin "$BRANCH"
else
log "Cloning repository..."
git clone --branch "$BRANCH" --depth 1 "$REPO_URL" "$MCP_HOME"
fi
# Initialize the central-brain submodule
log "Initializing replica-omnisciente submodule..."
cd "$MCP_HOME"
git submodule update --init --recursive replica-omnisciente
# Python venv + dependencies
log "Setting up Python environment..."
cd "$MCP_HOME/replica-omnisciente/.aurelio/mcp/savearth-mcp"
if [ ! -d ".venv" ]; then
python3 -m venv .venv
fi
source .venv/bin/activate
pip install --quiet --upgrade pip
pip install --quiet -e .
log "Python dependencies installed."
# Environment file
if [ ! -f .env ]; then
cp .env.example .env
warn ".env created from template — edit it with your InfluxDB token!"
fi
# Systemd service
cat > /etc/systemd/system/savearth-mcp.service << 'UNIT'
[Unit]
Description=savearth Unified MCP Server
After=network.target
Wants=network-online.target
[Service]
Type=simple
User=savearth
Group=savearth
WorkingDirectory=/opt/savearth-mcp/replica-omnisciente/.aurelio/mcp/savearth-mcp
EnvironmentFile=/opt/savearth-mcp/replica-omnisciente/.aurelio/mcp/savearth-mcp/.env
ExecStart=/opt/savearth-mcp/replica-omnisciente/.aurelio/mcp/savearth-mcp/.venv/bin/python3 server.py --transport sse --port 8080
Restart=on-failure
RestartSec=10
StandardOutput=journal
StandardError=journal
# Security hardening
NoNewPrivileges=true
ProtectSystem=strict
ReadWritePaths=/opt/savearth-mcp/logs
PrivateTmp=true
[Install]
WantedBy=multi-user.target
UNIT
# Set ownership
chown -R savearth:savearth "$MCP_HOME"
# Enable and start service
systemctl daemon-reload
systemctl enable savearth-mcp
systemctl restart savearth-mcp
log "systemd service installed and started."
# SSM registration (optional, if activation creds are available)
if [ -n "${SSM_ACTIVATION_ID:-}" ] && [ -n "${SSM_ACTIVATION_CODE:-}" ]; then
info "Registering with AWS SSM..."
if ! command -v amazon-ssm-agent &>/dev/null; then
# Install SSM agent
ARCH=$(dpkg --print-architecture)
SSM_URL="https://s3.eu-north-1.amazonaws.com/amazon-ssm-eu-north-1/latest/debian_${ARCH}/amazon-ssm-agent.deb"
curl -sLo /tmp/amazon-ssm-agent.deb "$SSM_URL"
dpkg -i /tmp/amazon-ssm-agent.deb
rm -f /tmp/amazon-ssm-agent.deb
fi
amazon-ssm-agent -register \
-code "$SSM_ACTIVATION_CODE" \
-id "$SSM_ACTIVATION_ID" \
-region eu-north-1 \
-y
systemctl enable amazon-ssm-agent
systemctl restart amazon-ssm-agent
log "SSM agent registered."
else
warn "SSM_ACTIVATION_ID/CODE not set — skipping SSM registration."
info "Register later with: amazon-ssm-agent -register -code <CODE> -id <ID> -region eu-north-1"
fi
# Final status
echo ""
log "=== Setup Complete ==="
systemctl status savearth-mcp --no-pager || true
echo ""
info "MCP SSE endpoint: http://$(hostname -I | awk '{print $1}'):8080/sse"
info "Edit credentials: nano $MCP_HOME/replica-omnisciente/.aurelio/mcp/savearth-mcp/.env"