#!/bin/bash # Setup namespace station - the infrastructure service for a namespace # Handles service registry and shared configuration set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/lib.sh" # Parameters NAS_SERVER="${1:-}" TIN_NAMESPACE="${TIN_NAMESPACE:-dynamicalsystem}" # Setup logging functions using shared lib log() { log_with_prefix "Station Setup" "$@" } error() { error_with_prefix "Station Setup" "$@" } warn() { warn_with_prefix "Station Setup" "$@" } # Discover existing services by scanning system UIDs discover_existing_services() { local namespace_num=$(get_namespace_number "$TIN_NAMESPACE") local services=() log "Discovering existing services in namespace '$TIN_NAMESPACE'..." # Scan for users matching our UID pattern # Pattern: 1${namespace_num}${service_num}${env_num}0 for service_num in {1..9}; do for env_num in 0 1; do local uid="1${namespace_num}${service_num}${env_num}0" if getent passwd "$uid" >/dev/null 2>&1; then local username=$(getent passwd "$uid" | cut -d: -f1) local env_name=$([[ "$env_num" == "0" ]] && echo "prod" || echo "test") # Extract service name from username pattern: service-env if [[ "$username" =~ ^([^-]+)-${env_name}$ ]]; then local service_name="${BASH_REMATCH[1]}" log "Found: $service_name ($env_name) - UID $uid (user: $username)" services+=("$service_name=$service_num") else log "Found UID $uid but couldn't parse service name from username: $username" fi fi done done # Return unique service mappings printf '%s\n' "${services[@]}" | sort -u } # Create service registry from discovered services create_service_registry() { local mount_point="/mnt/station-prod" local registry_file="$mount_point/state/service-registry" # Create state directory sudo -u station-prod mkdir -p "$mount_point/state" # Discover services local services=$(discover_existing_services) # Create registry file cat << EOF | sudo -u station-prod tee "$registry_file" > /dev/null # Service Registry for $TIN_NAMESPACE namespace # Format: service_name=service_number station=0 EOF } # Setup station user and mount using shared mount_nas.sh setup_station_mount() { # Station mount setup delegated to mount_nas.sh - no additional logging needed if ! "$SCRIPT_DIR/mount_nas.sh" "station" "prod" "$NAS_SERVER"; then error "Failed to setup station NFS mount" fi } # Main setup flow main() { if [[ -z "$NAS_SERVER" ]]; then echo "Usage: $0 " echo "" echo "Sets up the namespace station for service registry and shared config" exit 1 fi log "Setting up station:" log " Namespace: $TIN_NAMESPACE" log " NFS mount: /mnt/station-prod" log " User: station-prod (UID: 11000)" # Setup station mount and user (mount_nas.sh handles existence check and creation guidance) setup_station_mount # Step 3: Create service registry create_service_registry log "Station setup complete!" log " Registry: /mnt/station-prod/state/service-registry" } main "$@"