#!/bin/bash # =============================================================================== # INFINIXISM DEVICE CLAIMING SCRIPT v2.0 # =============================================================================== # Download and run: # curl -fsSL https://go.infinixism.com/install.sh > install.sh && sudo bash install.sh set -e # Exit immediately if a command exits with a non-zero status. # --- Read Passphrase from Argument --- PASSPHRASE="$1" # Read the passphrase from the first argument passed to bash -s # Check if passphrase was provided if [ -z "$PASSPHRASE" ]; then echo "Error: Activation passphrase was not provided as an argument." >&2 echo "Usage: curl -fsSL https://go.infinixism.com/install.sh > install.sh && sudo bash install.sh " >&2 # Prompt for passphrase if not provided echo "" echo "Please enter the activation passphrase shown on your dashboard:" read -r PASSPHRASE if [ -z "$PASSPHRASE" ]; then echo "Error: No passphrase entered. Installation aborted." >&2 exit 1 fi else echo "Passphrase received from argument." fi echo "Starting Infinixism Device Setup..." # --- Configuration --- PLATFORM_API_URL="http://209.74.79.9:3001/api/devices/claim-with-passphrase" # For development testing, you can use the test passphrase if [ "$PASSPHRASE" = "dev-test" ]; then PASSPHRASE="test-passphrase-123" echo "Development mode: Using test passphrase" fi # --- Functions --- # Function to generate a unique device ID (UUID) generate_device_id() { if command -v uuidgen &> /dev/null; then uuidgen elif [ -f /proc/sys/kernel/random/uuid ]; then cat /proc/sys/kernel/random/uuid else echo "Error: Cannot generate UUID. Please install uuidgen." >&2 exit 1 fi } # Function to call the platform API to claim the device call_claim_api() { local device_id="$1" local passphrase="$2" echo "Attempting to claim device ID: $device_id with the platform..." # Construct JSON payload local json_payload json_payload=$(printf '{"deviceId": "%s", "passphrase": "%s"}' "$device_id" "$passphrase") # Make the API call using curl local response response=$(curl -s -w "\n%{http_code}" -X POST "$PLATFORM_API_URL" \ -H "Content-Type: application/json" \ -d "$json_payload") # Extract HTTP status code and body local http_code http_code=$(echo "$response" | tail -n1) local response_body response_body=$(echo "$response" | sed '$d') # Remove last line (http_code) # Check response if [ "$http_code" -eq 200 ]; then echo "Device claimed successfully!" echo "Platform response: $response_body" # Create device config directory mkdir -p /etc/infinixism # Store device ID locally echo "$device_id" > /etc/infinixism/device_id chmod 600 /etc/infinixism/device_id return 0 else echo "Error: Failed to claim device. HTTP Status: $http_code" >&2 echo "Platform response: $response_body" >&2 if [ "$http_code" -eq 409 ]; then echo "This device ID has already been registered. Trying again with a new ID..." >&2 return 1 else echo "Please check your passphrase and try again." >&2 exit 1 fi fi } # --- Main Execution --- # Check for root privileges (implicitly handled by requiring 'sudo bash') if [ "$(id -u)" -ne 0 ]; then echo "Error: This script must be run as root (sudo)" >&2 exit 1 fi echo "Generating Device ID..." DEVICE_ID=$(generate_device_id) echo "Generated Device ID: $DEVICE_ID" echo "Calling platform API..." # Try up to 3 times in case of device ID collision MAX_ATTEMPTS=3 ATTEMPT=1 while [ $ATTEMPT -le $MAX_ATTEMPTS ]; do if call_claim_api "$DEVICE_ID" "$PASSPHRASE"; then break else ATTEMPT=$((ATTEMPT + 1)) if [ $ATTEMPT -le $MAX_ATTEMPTS ]; then echo "Retrying with a new device ID (attempt $ATTEMPT of $MAX_ATTEMPTS)..." DEVICE_ID=$(generate_device_id) echo "New Device ID: $DEVICE_ID" else echo "Error: Maximum number of attempts reached. Setup aborted." >&2 exit 1 fi fi done echo "Initial device registration completed successfully!" echo "Device ID: $DEVICE_ID" # --- Future Steps --- echo "Your device is now registered with the Infinixism platform." echo "Run the full installation script to set up your private cloud services:" echo "curl -fsSL https://go.infinixism.com/install.sh > install.sh && sudo bash install.sh" exit 0