diff --git a/scripts/opax.sh b/scripts/opax.sh index ec7b8e3..a07e455 100644 --- a/scripts/opax.sh +++ b/scripts/opax.sh @@ -9,10 +9,6 @@ # Alias: opax # ============================================================================= -# --- General Setup --- -# Exit on error, unset variable, or pipe failure. -# set -euo pipefail # REMOVED: kills shell when sourced - # --- Pre-computation and Environment --- OSVAUCO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" export GOOGLE_CLOUD_PROJECT="propane-will-491900-m5" @@ -20,23 +16,19 @@ export GOOGLE_CLOUD_LOCATION="us-central1" export GEMINI_MODEL="gemini-2.5-pro" # --- UI Colors and Icons --- -# Use tput for wider compatibility, with fallbacks. -GREEN="$(tput setaf 2 || echo -e '\033[0;32m')" -RED="$(tput setaf 1 || echo -e '\033[0;31m')" -YELLOW="$(tput setaf 3 || echo -e '\033[1;33m')" -CYAN="$(tput setaf 6 || echo -e '\033[0;36m')" -WHITE="$(tput setaf 7 || echo -e '\033[1;37m')" -DIM="$(tput dim || echo -e '\033[2m')" -BOLD="$(tput bold || echo -e '\033[1m')" -NC="$(tput sgr0 || echo -e '\033[0m')" # No Color +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')" -# Checkmarks OK="✅" FAIL="❌" -# Internal state to prevent re-printing banner on multiple sources if [[ -z "${OPAX_LOADED-}" ]]; then - # --- Banner Display --- clear printf "${CYAN}" echo " ___ _____ _ _ ___ _ _ ___ _____ " @@ -46,128 +38,95 @@ if [[ -z "${OPAX_LOADED-}" ]]; then echo " | |_| |___| | \ \_/ / | | | |_| | |__ \ \_/ / " echo " \___/\____/ \___/\_| |_/\___/ \___| \___/ " printf "${NC}" - printf "${DIM} -------------------------------------------------${NC} -" - printf " ${CYAN}OPAX${NC} ${DIM}Operator Platform // ${GOOGLE_CLOUD_PROJECT}${NC} -" - printf "${DIM} -------------------------------------------------${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" - # --- Health & Auth Checks --- - printf "${BOLD}${WHITE} Preflight Checks...${NC} -" - - # Helper function for checks run_check() { local description="$1" local command_to_run="$2" local error_message="$3" - local is_critical="${4:-false}" # Default to not critical + local is_critical="${4:-false}" - printf " %-35s" "$description" + printf " %-40s" "$description" if eval "$command_to_run" &>/dev/null; then - printf "${GREEN}${OK}${NC} -" + printf "${GREEN}${OK}${NC}\n" else - printf "${RED}${FAIL}${NC} -" + printf "${RED}${FAIL}${NC}\n" if [[ "$is_critical" == "true" ]]; then - printf " - ${RED}${BOLD}CRITICAL FAILURE:${NC} ${error_message} -" - printf " ${YELLOW}Aborting OPAX startup.${NC} - -" - # Clean up and exit the subshell this script is running in + printf "\n ${RED}${BOLD}CRITICAL:${NC} ${error_message}\n" + printf " ${YELLOW}Aborting OPAX startup.${NC}\n\n" return 1 else - printf " ${YELLOW}Warning:${NC} ${error_message} -" + printf " ${YELLOW}⚠${NC} ${error_message}\n" fi fi } - # 1. Critical: gcloud authentication - run_check "gcloud user authenticated" - "gcloud auth list --filter=status:ACTIVE --format='value(account)' | grep -q .@" - "Not logged into gcloud. Run 'gcloud auth login'." + run_check "gcloud user authenticated" \ + "gcloud auth list --filter=status:ACTIVE --format='value(account)' | grep -q @" \ + "Run: gcloud auth login" \ true - # 2. Critical: Application Default Credentials (ADC) - run_check "Application Default Credentials (ADC)" - "gcloud auth application-default print-access-token --quiet" - "ADC not configured. Run 'gcloud auth application-default login'." + run_check "Application Default Credentials (ADC)" \ + "gcloud auth application-default print-access-token --quiet" \ + "Run: gcloud auth application-default login" \ true - # 3. Critical: gcloud project configuration - run_check "gcloud project set to '$GOOGLE_CLOUD_PROJECT'" - "[[ \$(gcloud config get-value project) == '$GOOGLE_CLOUD_PROJECT' ]]" - "Wrong project. Run 'gcloud config set project $GOOGLE_CLOUD_PROJECT'." - true - - # 4. Critical: GitHub remote connectivity - run_check "GitHub remote reachable" - "git ls-remote --exit-code origin >/dev/null 2>&1" - "Cannot connect to git remote 'origin'." + 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 - # 5. Non-critical: Secret Manager access - run_check "Secret Manager access (test read)" - "gcloud secrets versions access latest --secret=github-token --project=$GOOGLE_CLOUD_PROJECT >/dev/null" - "Could not access secrets. Check IAM permissions for Secret Manager." + 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 "") - # 6. Non-critical: Git repository is clean - run_check "Git status is clean" - "git diff --quiet && git diff --cached --quiet" - "You have uncommitted changes in your repository." - - # 7. Non-critical: Cloud Run service health - SERVICE_URL=$(gcloud run services describe opax-mcp-core --region "$GOOGLE_CLOUD_LOCATION" --project "$GOOGLE_CLOUD_PROJECT" --format="value(status.url)" 2>/dev/null || echo "") if [[ -n "$SERVICE_URL" ]]; then - run_check "Cloud Run service 'opax-mcp-core' healthy" - "curl -s -f -H "Authorization: Bearer \$(gcloud auth print-identity-token)" "${SERVICE_URL}/health"" - "The 'opax-mcp-core' service is not responding correctly." + 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 " %-35s" "Cloud Run service 'opax-mcp-core' healthy" - printf "${YELLOW}SKIP${NC} -" - printf " ${DIM}Warning: Service 'opax-mcp-core' not found or URL could not be retrieved.${NC} -" + printf " %-40s${YELLOW}SKIP${NC}\n" "Cloud Run osvauco-agent healthy" + printf " ${DIM}Service ikke funnet i $GOOGLE_CLOUD_LOCATION${NC}\n" fi - printf " -${GREEN}All critical checks passed. OPAX environment is ready.${NC} - -" + printf "\n${GREEN}${BOLD}✅ OPAX klar.${NC}\n\n" fi +# --- Miljø og prompt --- +cd "$OSVAUCO_DIR" 2>/dev/null || true -# --- Set Environment --- -# Ensure we are in the project directory -cd "$OSVAUCO_DIR" || return +export PS1="\[${GREEN}\]┌──(\[${CYAN}\]OPAX:\[${BOLD}\]\w\[${NC}\])\[${NC}\]\n\[${GREEN}\]└─▪\[${NC}\] " -# Set a custom, identifiable prompt -export PS1="\[${GREEN}\]┌──(${CYAN}OPAX:${BOLD}\w${NC})\[${NC}\] -\[${GREEN}\]└─▪\[${NC}\] " - -# --- Finalization --- -# Mark as loaded to prevent re-running the banner and checks export OPAX_LOADED=true -# Display quick commands only on the first load in a session if [[ -z "${OPAX_COMMANDS_SHOWN-}" ]]; then - printf "${BOLD}${WHITE}QUICK COMMANDS${NC} -" - printf " ${GREEN}%-15s${NC} %s -" "opax" "Reload this environment script" - printf " ${GREEN}%-15s${NC} %s -" "cost-audit" "Run cost analysis" - printf " ${GREEN}%-15s${NC} %s -" "smoke-test" "Run API smoke tests" - printf " ${GREEN}%-15s${NC} %s -" "g" "Start the Gemini CLI" - printf " -" + 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