133 lines
5.3 KiB
Bash
133 lines
5.3 KiB
Bash
#!/usr/bin/env bash
|
|
# =============================================================================
|
|
# OPAX.SH — Authoritative OSVauco OPAX Environment Bootstrapper
|
|
#
|
|
# Idempotent script to set up the OPAX operator environment.
|
|
# Can be sourced multiple times without side-effects.
|
|
#
|
|
# Usage: source ~/OSVauco/scripts/opax.sh
|
|
# Alias: opax
|
|
# =============================================================================
|
|
|
|
# --- Pre-computation and Environment ---
|
|
OSVAUCO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
export GOOGLE_CLOUD_PROJECT="propane-will-491900-m5"
|
|
export GOOGLE_CLOUD_LOCATION="us-central1"
|
|
export GEMINI_MODEL="gemini-2.5-pro"
|
|
|
|
# --- UI Colors and Icons ---
|
|
GREEN="$(tput setaf 2 2>/dev/null || printf '\033[0;32m')"
|
|
RED="$(tput setaf 1 2>/dev/null || printf '\033[0;31m')"
|
|
YELLOW="$(tput setaf 3 2>/dev/null || printf '\033[1;33m')"
|
|
CYAN="$(tput setaf 6 2>/dev/null || printf '\033[0;36m')"
|
|
WHITE="$(tput setaf 7 2>/dev/null || printf '\033[1;37m')"
|
|
DIM="$(tput dim 2>/dev/null || printf '\033[2m')"
|
|
BOLD="$(tput bold 2>/dev/null || printf '\033[1m')"
|
|
NC="$(tput sgr0 2>/dev/null || printf '\033[0m')"
|
|
|
|
OK="✅"
|
|
FAIL="❌"
|
|
|
|
if [[ -z "${OPAX_LOADED-}" ]]; then
|
|
clear
|
|
printf "${CYAN}"
|
|
echo " ___ _____ _ _ ___ _ _ ___ _____ "
|
|
echo " / _ \| ___| | | | |/ _ \| | | |/ __|| _ | "
|
|
echo " | | | | |___ | | | / /_\ \ | | | | | | | | "
|
|
echo " | | | |___ | | | | | _ | | | | | | | | | "
|
|
echo " | |_| |___| | \ \_/ / | | | |_| | |__ \ \_/ / "
|
|
echo " \___/\____/ \___/\_| |_/\___/ \___| \___/ "
|
|
printf "${NC}"
|
|
printf "${DIM} -------------------------------------------------${NC}\n"
|
|
printf " ${CYAN}OPAX${NC} ${DIM}Operator Platform // ${GOOGLE_CLOUD_PROJECT}${NC}\n"
|
|
printf "${DIM} -------------------------------------------------${NC}\n\n"
|
|
|
|
printf "${BOLD}${WHITE} Preflight Checks...${NC}\n"
|
|
|
|
run_check() {
|
|
local description="$1"
|
|
local command_to_run="$2"
|
|
local error_message="$3"
|
|
local is_critical="${4:-false}"
|
|
|
|
printf " %-40s" "$description"
|
|
if eval "$command_to_run" &>/dev/null; then
|
|
printf "${GREEN}${OK}${NC}\n"
|
|
else
|
|
printf "${RED}${FAIL}${NC}\n"
|
|
if [[ "$is_critical" == "true" ]]; then
|
|
printf "\n ${RED}${BOLD}CRITICAL:${NC} ${error_message}\n"
|
|
printf " ${YELLOW}Aborting OPAX startup.${NC}\n\n"
|
|
return 1
|
|
else
|
|
printf " ${YELLOW}⚠${NC} ${error_message}\n"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
run_check "gcloud user authenticated" \
|
|
"gcloud auth list --filter=status:ACTIVE --format='value(account)' | grep -q @" \
|
|
"Run: gcloud auth login" \
|
|
true
|
|
|
|
run_check "Application Default Credentials (ADC)" \
|
|
"gcloud auth application-default print-access-token --quiet" \
|
|
"Run: gcloud auth application-default login" \
|
|
true
|
|
|
|
run_check "gcloud project = $GOOGLE_CLOUD_PROJECT" \
|
|
"[[ \$(gcloud config get-value project 2>/dev/null) == '$GOOGLE_CLOUD_PROJECT' ]]" \
|
|
"Run: gcloud config set project $GOOGLE_CLOUD_PROJECT" \
|
|
true
|
|
|
|
run_check "GitHub remote reachable" \
|
|
"git -C '$OSVAUCO_DIR' ls-remote --exit-code origin" \
|
|
"Cannot reach git remote origin" \
|
|
true
|
|
|
|
run_check "Secret Manager readable" \
|
|
"gcloud secrets versions access latest --secret=github-token --project=$GOOGLE_CLOUD_PROJECT" \
|
|
"Check IAM permissions for Secret Manager"
|
|
|
|
run_check "Git working tree clean" \
|
|
"git -C '$OSVAUCO_DIR' diff --quiet && git -C '$OSVAUCO_DIR' diff --cached --quiet" \
|
|
"Uncommitted changes present"
|
|
|
|
# Cloud Run health — bruk osvauco-agent (ikke opax-mcp-core)
|
|
SERVICE_URL=$(gcloud run services describe osvauco-agent \
|
|
--region "$GOOGLE_CLOUD_LOCATION" \
|
|
--project "$GOOGLE_CLOUD_PROJECT" \
|
|
--format="value(status.url)" 2>/dev/null || echo "")
|
|
|
|
if [[ -n "$SERVICE_URL" ]]; then
|
|
TOKEN=$(gcloud auth print-identity-token 2>/dev/null || echo "")
|
|
run_check "Cloud Run osvauco-agent healthy" \
|
|
"curl -sf -H 'Authorization: Bearer ${TOKEN}' '${SERVICE_URL}/health'" \
|
|
"Service ikke tilgjengelig — sjekk Cloud Run"
|
|
else
|
|
printf " %-40s${YELLOW}SKIP${NC}\n" "Cloud Run osvauco-agent healthy"
|
|
printf " ${DIM}Service ikke funnet i $GOOGLE_CLOUD_LOCATION${NC}\n"
|
|
fi
|
|
|
|
printf "\n${GREEN}${BOLD}✅ OPAX klar.${NC}\n\n"
|
|
fi
|
|
|
|
# --- Miljø og prompt ---
|
|
cd "$OSVAUCO_DIR" 2>/dev/null || true
|
|
|
|
export PS1="\[${GREEN}\]┌──(\[${CYAN}\]OPAX:\[${BOLD}\]\w\[${NC}\])\[${NC}\]\n\[${GREEN}\]└─▪\[${NC}\] "
|
|
|
|
export OPAX_LOADED=true
|
|
|
|
if [[ -z "${OPAX_COMMANDS_SHOWN-}" ]]; then
|
|
printf "${BOLD}${WHITE}QUICK COMMANDS${NC}\n"
|
|
printf " ${GREEN}%-8s${NC} %s\n" "ca" "cost-audit"
|
|
printf " ${GREEN}%-8s${NC} %s\n" "cl" "cost-lens"
|
|
printf " ${GREEN}%-8s${NC} %s\n" "st" "smoke-test"
|
|
printf " ${GREEN}%-8s${NC} %s\n" "hc" "health-check"
|
|
printf " ${GREEN}%-8s${NC} %s\n" "g" "gemini"
|
|
printf " ${GREEN}%-8s${NC} %s\n" "sol" "avslutt"
|
|
printf "\n"
|
|
export OPAX_COMMANDS_SHOWN=true
|
|
fi
|