netgescon-day0/scripts/ops/netgescon-client-kb-bootstrap.sh

174 lines
4.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
HOST="192.168.0.53"
SSH_USER="michele"
SSH_KEY="$HOME/.ssh/id_ed25519_netgescon_machine"
KB_URL="http://192.168.0.53:3838"
MOUNT_ROOT="$HOME/mnt/netgescon-ssh"
WORKSPACE_ROOT="$HOME/netgescon-sshfs-workspace"
CLIENT_ID="200"
INSTALL_ORCHESTRATOR=0
ENV_FILE="${NETGESCON_KB_ENV_FILE:-$HOME/.config/netgescon/kb.env}"
usage() {
cat <<'EOF'
Usage: scripts/ops/netgescon-client-kb-bootstrap.sh [options]
Options:
--host <host> SSH and KB host. Default: 192.168.0.53
--ssh-user <user> SSH user. Default: michele
--ssh-key <path> Machine SSH key path.
--kb-url <url> KB base URL. Default: http://192.168.0.53:3838
--mount-root <path> SSHFS mount root. Default: ~/mnt/netgescon-ssh
--workspace-root <path> Local workspace root. Default: ~/netgescon-sshfs-workspace
--client-id <id> Scambio client id. Default: 200
--install-orchestrator Install agent-orchestrator into ~/netgescon-orchestrator/.venv
--env-file <path> Optional shell env file to source before checks.
--help Show this message.
Environment:
NETGESCON_KB_API_KEY Required for KB search validation.
NETGESCON_KB_ENV_FILE Optional env file path. Default: ~/.config/netgescon/kb.env
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--host)
HOST="$2"
shift 2
;;
--ssh-user)
SSH_USER="$2"
shift 2
;;
--ssh-key)
SSH_KEY="$2"
shift 2
;;
--kb-url)
KB_URL="$2"
shift 2
;;
--mount-root)
MOUNT_ROOT="$2"
shift 2
;;
--workspace-root)
WORKSPACE_ROOT="$2"
shift 2
;;
--client-id)
CLIENT_ID="$2"
shift 2
;;
--install-orchestrator)
INSTALL_ORCHESTRATOR=1
shift
;;
--env-file)
ENV_FILE="$2"
shift 2
;;
--help)
usage
exit 0
;;
*)
echo "Unknown option: $1"
usage
exit 1
;;
esac
done
require_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "Missing required command: $1"
exit 1
fi
}
mount_share() {
local remote_path="$1"
local local_path="$2"
mkdir -p "$local_path"
if ! mountpoint -q "$local_path"; then
sshfs \
-o IdentityFile="$SSH_KEY" \
-o StrictHostKeyChecking=accept-new \
-o reconnect \
-o ServerAliveInterval=15 \
"$SSH_USER@$HOST:$remote_path" \
"$local_path"
fi
}
require_cmd ssh
require_cmd sshfs
require_cmd curl
require_cmd python3
require_cmd mountpoint
if [[ -f "$ENV_FILE" ]]; then
# shellcheck disable=SC1090
source "$ENV_FILE"
fi
if [[ ! -f "$SSH_KEY" ]]; then
echo "SSH key not found: $SSH_KEY"
exit 1
fi
echo "== Trusted SSH Test =="
ssh -i "$SSH_KEY" -o BatchMode=yes -o ConnectTimeout=8 "$SSH_USER@$HOST" 'hostname && whoami'
echo
echo "== SSHFS Mounts =="
mount_share "/home/michele/netgescon-git/Documenti condivisi" "$MOUNT_ROOT/documenti"
mount_share "/home/michele/netgescon-git/Progetti" "$MOUNT_ROOT/progetti"
mount_share "/home/michele/netgescon-git/Scambio" "$MOUNT_ROOT/scambio"
mount | grep "$MOUNT_ROOT" || true
echo
echo "== Workspace Links =="
mkdir -p "$WORKSPACE_ROOT"
ln -sfn "$MOUNT_ROOT/documenti" "$WORKSPACE_ROOT/documenti"
ln -sfn "$MOUNT_ROOT/progetti" "$WORKSPACE_ROOT/progetti"
ln -sfn "$MOUNT_ROOT/scambio/$CLIENT_ID" "$WORKSPACE_ROOT/scambio-$CLIENT_ID"
ls -la "$WORKSPACE_ROOT"
echo
echo "== Shared Content Check =="
find "$MOUNT_ROOT/scambio/$CLIENT_ID" -maxdepth 1 -type f | sort | sed -n '1,20p'
find "$MOUNT_ROOT/progetti" -maxdepth 1 | sort | sed -n '1,20p'
echo
echo "== KB Health =="
curl -fsS "$KB_URL/api/v1/health"
echo
if [[ -n "${NETGESCON_KB_API_KEY:-}" ]]; then
echo "== KB Search =="
curl -fsS -H "X-API-Key: $NETGESCON_KB_API_KEY" \
--get \
--data-urlencode "q=anagrafica unica" \
"$KB_URL/api/v1/search" | head -c 1200
echo
else
echo "NETGESCON_KB_API_KEY not set: skipping KB search validation"
echo "Hint: create $ENV_FILE with NETGESCON_KB_API_KEY=... to enable authenticated KB search checks"
fi
if [[ "$INSTALL_ORCHESTRATOR" -eq 1 ]]; then
echo
echo "== Agent Orchestrator =="
mkdir -p "$HOME/netgescon-orchestrator"
if [[ ! -d "$HOME/netgescon-orchestrator/.venv" ]]; then
python3 -m venv "$HOME/netgescon-orchestrator/.venv"
fi
"$HOME/netgescon-orchestrator/.venv/bin/pip" install --upgrade pip setuptools wheel
"$HOME/netgescon-orchestrator/.venv/bin/pip" install agent-orchestrator
"$HOME/netgescon-orchestrator/.venv/bin/agent-orchestrator" --version
fi