6ad8a6e04e
La regex del loader de .env solo aceptaba [A-Z_] en nombres de variables, lo que excluía vars como MATRIX_TOKEN_ASISTENTE2 (con dígito). Cambiado ^[A-Z_]+=.+ → ^[A-Z_][A-Z0-9_]*=.+ para soportar dígitos después del primer carácter. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
142 lines
4.9 KiB
Bash
Executable File
142 lines
4.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# _common.sh — sourced by all dev-scripts. Do not run directly.
|
|
|
|
set -euo pipefail
|
|
|
|
# ── Colores ────────────────────────────────────────────────────────────────
|
|
RED='\033[0;31m'
|
|
GRN='\033[0;32m'
|
|
YLW='\033[0;33m'
|
|
BLU='\033[0;34m'
|
|
DIM='\033[2m'
|
|
RST='\033[0m'
|
|
|
|
ok() { echo -e "${GRN}✓${RST} $*"; }
|
|
info() { echo -e "${BLU}→${RST} $*"; }
|
|
warn() { echo -e "${YLW}!${RST} $*"; }
|
|
fail() { echo -e "${RED}✗${RST} $*" >&2; exit 1; }
|
|
dim() { echo -e "${DIM}$*${RST}"; }
|
|
|
|
# ── Repo root ──────────────────────────────────────────────────────────────
|
|
# Scripts can be called from any directory; we always operate from repo root.
|
|
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
cd "$REPO_ROOT"
|
|
|
|
# ── .env loader ───────────────────────────────────────────────────────────
|
|
load_env() {
|
|
local env_file="${1:-.env}"
|
|
[[ -f "$env_file" ]] || fail ".env not found at $REPO_ROOT/$env_file — copy .env.example and fill it in"
|
|
# Export only lines that are KEY=VALUE (skip comments and blanks)
|
|
set -o allexport
|
|
# shellcheck disable=SC1090
|
|
source <(grep -E '^[A-Z_][A-Z0-9_]*=.+' "$env_file")
|
|
set +o allexport
|
|
}
|
|
|
|
# ── Go tooling ─────────────────────────────────────────────────────────────
|
|
GO=${GO_BIN:-go}
|
|
export PATH="$PATH:/usr/local/go/bin"
|
|
command -v "$GO" &>/dev/null || fail "go not found — install Go or set GO_BIN"
|
|
|
|
# ── Process helpers ────────────────────────────────────────────────────────
|
|
RUN_DIR="$REPO_ROOT/run"
|
|
mkdir -p "$RUN_DIR"
|
|
|
|
pid_file() { echo "$RUN_DIR/$1.pid"; }
|
|
log_file() { echo "$RUN_DIR/$1.log"; }
|
|
|
|
read_pid() {
|
|
local f; f="$(pid_file "$1")"
|
|
[[ -f "$f" ]] && cat "$f" || echo 0
|
|
}
|
|
|
|
# Map agent ID to its config path by scanning agent directories.
|
|
config_path_for() {
|
|
local target_id="$1"
|
|
for cfg in agents/*/config.yaml; do
|
|
[[ -f "$cfg" ]] || continue
|
|
local id
|
|
id=$(grep -m1 '^ id:' "$cfg" | awk '{print $2}')
|
|
if [[ "$id" == "$target_id" ]]; then
|
|
echo "$cfg"
|
|
return
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Find all PIDs of launcher processes for a given agent ID.
|
|
# Searches for the actual config path in the process command line.
|
|
# Returns newline-separated PIDs (may be empty).
|
|
find_agent_pids() {
|
|
local id="$1"
|
|
local cfg; cfg="$(config_path_for "$id")"
|
|
if [[ -z "$cfg" ]]; then
|
|
return
|
|
fi
|
|
pgrep -f "launcher.*-c.*${cfg}" 2>/dev/null || true
|
|
}
|
|
|
|
is_running() {
|
|
local id="$1"
|
|
|
|
# First check PID file
|
|
local pid; pid="$(read_pid "$id")"
|
|
if [[ "$pid" -gt 0 ]] && kill -0 "$pid" 2>/dev/null; then
|
|
return 0
|
|
fi
|
|
|
|
# PID file is stale or missing — search for actual processes
|
|
local pids; pids="$(find_agent_pids "$id")"
|
|
if [[ -n "$pids" ]]; then
|
|
# Update PID file with the first found process
|
|
local first_pid; first_pid="$(echo "$pids" | head -1)"
|
|
echo "$first_pid" > "$(pid_file "$id")"
|
|
return 0
|
|
fi
|
|
|
|
# Truly not running — clean up stale PID file
|
|
[[ "$pid" -gt 0 ]] && rm -f "$(pid_file "$id")"
|
|
return 1
|
|
}
|
|
|
|
# Count how many instances of an agent are running.
|
|
count_instances() {
|
|
local id="$1"
|
|
local pids; pids="$(find_agent_pids "$id")"
|
|
if [[ -z "$pids" ]]; then
|
|
echo 0
|
|
else
|
|
echo "$pids" | wc -l
|
|
fi
|
|
}
|
|
|
|
agent_status() {
|
|
local id="$1" enabled="$2"
|
|
if [[ "$enabled" != "true" ]]; then
|
|
echo "disabled"
|
|
elif is_running "$id"; then
|
|
echo "running"
|
|
else
|
|
echo "stopped"
|
|
fi
|
|
}
|
|
|
|
# ── Agent discovery ────────────────────────────────────────────────────────
|
|
# Prints: id|version|enabled|description (one line per agent)
|
|
list_agents_raw() {
|
|
for cfg in agents/*/config.yaml; do
|
|
[[ -f "$cfg" ]] || continue
|
|
local id version enabled desc
|
|
id=$(grep -m1 '^ id:' "$cfg" | awk '{print $2}')
|
|
version=$(grep -m1 '^ version:' "$cfg" | awk '{print $2}' | tr -d '"')
|
|
enabled=$(grep -m1 '^ enabled:' "$cfg" | awk '{print $2}')
|
|
desc=$(grep -m1 '^ description:' "$cfg" | cut -d'"' -f2)
|
|
[[ -n "$id" ]] && echo "${id}|${version}|${enabled}|${desc}|${cfg}"
|
|
done
|
|
}
|
|
|
|
# ── Usage helper ──────────────────────────────────────────────────────────
|
|
need_arg() {
|
|
[[ -n "${1:-}" ]] || { echo "Usage: $0 <agent-id>"; exit 1; }
|
|
}
|