homelab infrastructure services
at main 162 lines 5.2 kB view raw
1#!/bin/bash 2# tin machine create - Create machine environment 3 4set -euo pipefail 5 6# Get tinsnip root and source libraries 7SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 8TINSNIP_ROOT="$(dirname "$(dirname "$SCRIPT_DIR")")" 9source "$TINSNIP_ROOT/lib/core.sh" 10source "$TINSNIP_ROOT/lib/uid.sh" 11source "$TINSNIP_ROOT/lib/registry.sh" 12 13# Create machine environment 14create_machine() { 15 local service="$1" 16 local environment="$2" 17 local nas_server="${3:-}" 18 19 log_with_prefix "Machine Create" "Creating machine environment: $service-$environment" 20 echo 21 22 # Show current sheet 23 local sheet=$(get_sheet) 24 log_with_prefix "Machine Create" "Sheet: $sheet" 25 26 # Validate sheet is registered (unless it's topsheet) 27 if [[ "$sheet" != "topsheet" ]]; then 28 if ! get_sheet_number "$sheet" >/dev/null 2>&1; then 29 error_with_prefix "Machine Create" "Sheet '$sheet' not registered" 30 echo "Register with: tin sheet create $sheet" >&2 31 exit 1 32 fi 33 fi 34 35 # Calculate expected UID 36 local service_uid 37 if service_uid=$(calculate_service_uid "$service" "$environment"); then 38 log_with_prefix "Machine Create" "Service UID: $service_uid" 39 else 40 error_with_prefix "Machine Create" "Failed to calculate service UID" 41 exit 1 42 fi 43 44 # Auto-discover NAS if not provided 45 if [[ -z "$nas_server" ]]; then 46 log_with_prefix "Machine Create" "Auto-discovering NAS server..." 47 if nas_server=$(discover_nas_for_sheet 2>/dev/null); then 48 log_with_prefix "Machine Create" "Discovered NAS server: $nas_server" 49 else 50 error_with_prefix "Machine Create" "No NAS server found for sheet '$sheet'" 51 echo "Either:" >&2 52 echo "1. Specify NAS server: tin machine create $service $environment <nas-server>" >&2 53 echo "2. Register default NAS: tin registry nas add default <nas-server>" >&2 54 echo "3. Register for sheet: tin registry nas add $sheet <nas-server>" >&2 55 exit 1 56 fi 57 else 58 log_with_prefix "Machine Create" "Using provided NAS server: $nas_server" 59 fi 60 61 echo 62 echo "Configuration:" 63 echo " Service: $service" 64 echo " Environment: $environment" 65 echo " Sheet: $sheet" 66 echo " UID: $service_uid" 67 echo " NAS: $nas_server" 68 echo " Mount: /mnt/$service-$environment" 69 echo 70 71 # Confirm creation 72 read -p "Create this machine environment? [Y/n]: " confirm 73 case "${confirm:-y}" in 74 [Yy]*|"") 75 log_with_prefix "Machine Create" "Creating machine environment..." 76 77 # Delegate to existing machine setup (pass UID to avoid duplicate warning) 78 if [[ "$service" == "station" ]]; then 79 exec "$TINSNIP_ROOT/machine/setup_station.sh" "$nas_server" 80 else 81 exec "$TINSNIP_ROOT/machine/setup_service.sh" "$service" "$environment" "$nas_server" "$service_uid" 82 fi 83 ;; 84 [Nn]*) 85 log_with_prefix "Machine Create" "Machine creation cancelled" 86 exit 1 87 ;; 88 *) 89 error_with_prefix "Machine Create" "Invalid input. Machine creation cancelled" 90 exit 1 91 ;; 92 esac 93} 94 95show_help() { 96 cat << EOF 97tin machine create - Create machine environment 98 99USAGE: 100 tin machine create <service> <environment> [nas-server] 101 tin machine <service> <environment> # Shorthand 102 103DESCRIPTION: 104 Create a complete machine environment for a service including: 105 - Service user with SMEP UID 106 - NFS mount for persistent storage 107 - Rootless Docker installation 108 - XDG directory integration 109 110ARGUMENTS: 111 <service> Service name (lowercase alphanumeric with hyphens) 112 <environment> Environment name (prod, test, dev, staging, etc.) 113 [nas-server] NAS server hostname or IP (optional if registered) 114 115EXAMPLES: 116 tin machine create gazette prod DS412plus.local 117 tin machine create lldap test 192.168.1.100 118 tin machine gazette prod # Use registered NAS 119 120SUPPORTED ENVIRONMENTS: 121 prod, test, dev, staging, demo, qa, uat, preview, canary, local 122 123EOF 124} 125 126# Handle help flags 127case "${1:-}" in 128 --help|-h|help) 129 show_help 130 exit 0 131 ;; 132esac 133 134# Main execution 135if [[ $# -lt 2 ]]; then 136 error_with_prefix "Machine Create" "Service and environment required" 137 echo "Usage: tin machine create <service> <environment> [nas-server]" >&2 138 exit 1 139fi 140 141service="$1" 142environment="$2" 143nas_server="${3:-}" 144 145# Validate service name 146if [[ ! "$service" =~ ^[a-z0-9-]+$ ]]; then 147 error_with_prefix "Machine Create" "Service name must be lowercase alphanumeric with hyphens only" 148fi 149 150# Validate environment name 151case "$environment" in 152 prod|test|dev|staging|demo|qa|uat|preview|canary|local) 153 # Valid environment 154 ;; 155 *) 156 error_with_prefix "Machine Create" "Invalid environment: $environment" 157 echo "Supported: prod, test, dev, staging, demo, qa, uat, preview, canary, local" >&2 158 exit 1 159 ;; 160esac 161 162create_machine "$service" "$environment" "$nas_server"