#!/bin/bash # tin machine create - Create machine environment set -euo pipefail # Get tinsnip root and source libraries SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" TINSNIP_ROOT="$(dirname "$(dirname "$SCRIPT_DIR")")" source "$TINSNIP_ROOT/lib/core.sh" source "$TINSNIP_ROOT/lib/uid.sh" source "$TINSNIP_ROOT/lib/registry.sh" # Create machine environment create_machine() { local service="$1" local environment="$2" local nas_server="${3:-}" log_with_prefix "Machine Create" "Creating machine environment: $service-$environment" echo # Show current sheet local sheet=$(get_sheet) log_with_prefix "Machine Create" "Sheet: $sheet" # Validate sheet is registered (unless it's topsheet) if [[ "$sheet" != "topsheet" ]]; then if ! get_sheet_number "$sheet" >/dev/null 2>&1; then error_with_prefix "Machine Create" "Sheet '$sheet' not registered" echo "Register with: tin sheet create $sheet" >&2 exit 1 fi fi # Calculate expected UID local service_uid if service_uid=$(calculate_service_uid "$service" "$environment"); then log_with_prefix "Machine Create" "Service UID: $service_uid" else error_with_prefix "Machine Create" "Failed to calculate service UID" exit 1 fi # Auto-discover NAS if not provided if [[ -z "$nas_server" ]]; then log_with_prefix "Machine Create" "Auto-discovering NAS server..." if nas_server=$(discover_nas_for_sheet 2>/dev/null); then log_with_prefix "Machine Create" "Discovered NAS server: $nas_server" else error_with_prefix "Machine Create" "No NAS server found for sheet '$sheet'" echo "Either:" >&2 echo "1. Specify NAS server: tin machine create $service $environment " >&2 echo "2. Register default NAS: tin registry nas add default " >&2 echo "3. Register for sheet: tin registry nas add $sheet " >&2 exit 1 fi else log_with_prefix "Machine Create" "Using provided NAS server: $nas_server" fi echo echo "Configuration:" echo " Service: $service" echo " Environment: $environment" echo " Sheet: $sheet" echo " UID: $service_uid" echo " NAS: $nas_server" echo " Mount: /mnt/$service-$environment" echo # Confirm creation read -p "Create this machine environment? [Y/n]: " confirm case "${confirm:-y}" in [Yy]*|"") log_with_prefix "Machine Create" "Creating machine environment..." # Delegate to existing machine setup (pass UID to avoid duplicate warning) if [[ "$service" == "station" ]]; then exec "$TINSNIP_ROOT/machine/setup_station.sh" "$nas_server" else exec "$TINSNIP_ROOT/machine/setup_service.sh" "$service" "$environment" "$nas_server" "$service_uid" fi ;; [Nn]*) log_with_prefix "Machine Create" "Machine creation cancelled" exit 1 ;; *) error_with_prefix "Machine Create" "Invalid input. Machine creation cancelled" exit 1 ;; esac } show_help() { cat << EOF tin machine create - Create machine environment USAGE: tin machine create [nas-server] tin machine # Shorthand DESCRIPTION: Create a complete machine environment for a service including: - Service user with SMEP UID - NFS mount for persistent storage - Rootless Docker installation - XDG directory integration ARGUMENTS: Service name (lowercase alphanumeric with hyphens) Environment name (prod, test, dev, staging, etc.) [nas-server] NAS server hostname or IP (optional if registered) EXAMPLES: tin machine create gazette prod DS412plus.local tin machine create lldap test 192.168.1.100 tin machine gazette prod # Use registered NAS SUPPORTED ENVIRONMENTS: prod, test, dev, staging, demo, qa, uat, preview, canary, local EOF } # Handle help flags case "${1:-}" in --help|-h|help) show_help exit 0 ;; esac # Main execution if [[ $# -lt 2 ]]; then error_with_prefix "Machine Create" "Service and environment required" echo "Usage: tin machine create [nas-server]" >&2 exit 1 fi service="$1" environment="$2" nas_server="${3:-}" # Validate service name if [[ ! "$service" =~ ^[a-z0-9-]+$ ]]; then error_with_prefix "Machine Create" "Service name must be lowercase alphanumeric with hyphens only" fi # Validate environment name case "$environment" in prod|test|dev|staging|demo|qa|uat|preview|canary|local) # Valid environment ;; *) error_with_prefix "Machine Create" "Invalid environment: $environment" echo "Supported: prod, test, dev, staging, demo, qa, uat, preview, canary, local" >&2 exit 1 ;; esac create_machine "$service" "$environment" "$nas_server"