homelab infrastructure services
1#!/bin/bash
2
3# Setup namespace station - the infrastructure service for a namespace
4# Handles service registry and shared configuration
5
6set -euo pipefail
7
8SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9source "$SCRIPT_DIR/lib.sh"
10
11# Parameters
12NAS_SERVER="${1:-}"
13TIN_NAMESPACE="${TIN_NAMESPACE:-dynamicalsystem}"
14
15# Setup logging functions using shared lib
16log() {
17 log_with_prefix "Station Setup" "$@"
18}
19
20error() {
21 error_with_prefix "Station Setup" "$@"
22}
23
24warn() {
25 warn_with_prefix "Station Setup" "$@"
26}
27
28
29# Discover existing services by scanning system UIDs
30discover_existing_services() {
31 local namespace_num=$(get_namespace_number "$TIN_NAMESPACE")
32 local services=()
33
34 log "Discovering existing services in namespace '$TIN_NAMESPACE'..."
35
36 # Scan for users matching our UID pattern
37 # Pattern: 1${namespace_num}${service_num}${env_num}0
38 for service_num in {1..9}; do
39 for env_num in 0 1; do
40 local uid="1${namespace_num}${service_num}${env_num}0"
41
42 if getent passwd "$uid" >/dev/null 2>&1; then
43 local username=$(getent passwd "$uid" | cut -d: -f1)
44 local env_name=$([[ "$env_num" == "0" ]] && echo "prod" || echo "test")
45
46 # Extract service name from username pattern: service-env
47 if [[ "$username" =~ ^([^-]+)-${env_name}$ ]]; then
48 local service_name="${BASH_REMATCH[1]}"
49 log "Found: $service_name ($env_name) - UID $uid (user: $username)"
50 services+=("$service_name=$service_num")
51 else
52 log "Found UID $uid but couldn't parse service name from username: $username"
53 fi
54 fi
55 done
56 done
57
58 # Return unique service mappings
59 printf '%s\n' "${services[@]}" | sort -u
60}
61
62# Create service registry from discovered services
63create_service_registry() {
64 local mount_point="/mnt/station-prod"
65 local registry_file="$mount_point/state/service-registry"
66
67 # Create state directory
68 sudo -u station-prod mkdir -p "$mount_point/state"
69
70 # Discover services
71 local services=$(discover_existing_services)
72
73 # Create registry file
74 cat << EOF | sudo -u station-prod tee "$registry_file" > /dev/null
75# Service Registry for $TIN_NAMESPACE namespace
76# Format: service_name=service_number
77
78station=0
79EOF
80}
81
82# Setup station user and mount using shared mount_nas.sh
83setup_station_mount() {
84 # Station mount setup delegated to mount_nas.sh - no additional logging needed
85 if ! "$SCRIPT_DIR/mount_nas.sh" "station" "prod" "$NAS_SERVER"; then
86 error "Failed to setup station NFS mount"
87 fi
88}
89
90# Main setup flow
91main() {
92 if [[ -z "$NAS_SERVER" ]]; then
93 echo "Usage: $0 <nas_server>"
94 echo ""
95 echo "Sets up the namespace station for service registry and shared config"
96 exit 1
97 fi
98
99 log "Setting up station:"
100 log " Namespace: $TIN_NAMESPACE"
101 log " NFS mount: /mnt/station-prod"
102 log " User: station-prod (UID: 11000)"
103
104 # Setup station mount and user (mount_nas.sh handles existence check and creation guidance)
105 setup_station_mount
106
107 # Step 3: Create service registry
108 create_service_registry
109
110 log "Station setup complete!"
111 log " Registry: /mnt/station-prod/state/service-registry"
112}
113
114main "$@"