homelab infrastructure services
at main 79 lines 3.0 kB view raw
1#!/bin/bash 2# Docker installation validation for tinsnip 3 4set -euo pipefail 5 6# Get script directory and source libraries 7SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 8TINSNIP_ROOT="$(dirname "$SCRIPT_DIR")" 9source "$TINSNIP_ROOT/lib/core.sh" 10 11# Verify Docker installation for a service user 12verify_docker_installation() { 13 local username="$1" 14 15 log_with_prefix "Docker Validation" "Verifying Docker installation for $username..." 16 17 # Use the service .env file (source of truth) 18 local service_env_file="/mnt/$username/.env" 19 20 # Docker context already set up in configure_docker() 21 22 # Debug verification process 23 log_with_prefix "Docker Validation" "Debugging verification process..." 24 log_with_prefix "Docker Validation" " Service env file: $service_env_file" 25 26 if [[ -f "$service_env_file" ]]; then 27 log_with_prefix "Docker Validation" " Environment variables in service .env:" 28 sudo -u "$username" grep "DOCKER\|XDG_RUNTIME" "$service_env_file" | while read line; do 29 log_with_prefix "Docker Validation" " $line" 30 done 31 else 32 log_with_prefix "Docker Validation" " WARNING: Service .env file not found!" 33 fi 34 35 # Test Docker verification with detailed output 36 log_with_prefix "Docker Validation" " Testing Docker command with environment..." 37 if sudo -u "$username" bash -c "source '$service_env_file' 2>/dev/null && docker version"; then 38 log_with_prefix "Docker Validation" "Docker verification successful!" 39 local docker_version 40 docker_version=$(sudo -u "$username" bash -c "source '$service_env_file' && docker --version") 41 log_with_prefix "Docker Validation" "Installed: $docker_version" 42 else 43 log_with_prefix "Docker Validation" "Docker verification failed - showing detailed error:" 44 sudo -u "$username" bash -c "source '$service_env_file' 2>/dev/null && docker version" 2>&1 | while read line; do 45 log_with_prefix "Docker Validation" " ERROR: $line" 46 done 47 error_with_prefix "Docker Validation" "Docker verification failed for $username" 48 return 1 49 fi 50 51 log_with_prefix "Docker Validation" "Service available for user: $username" 52 log_with_prefix "Docker Validation" "Privileged ports: enabled" 53 return 0 54} 55 56# Main validation entry point 57main() { 58 local service_user="${1:-}" 59 60 if [[ -z "$service_user" ]]; then 61 echo "Usage: $0 <service-user>" >&2 62 echo "Example: $0 testservice-prod" >&2 63 exit 1 64 fi 65 66 # Check if user exists 67 if ! id "$service_user" &>/dev/null; then 68 error_with_prefix "Docker Validation" "User $service_user does not exist" 69 exit 1 70 fi 71 72 verify_docker_installation "$service_user" 73 log_with_prefix "Docker Validation" "✅ All Docker validation checks passed" 74} 75 76# Run main if called directly 77if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then 78 main "$@" 79fi