#!/bin/bash # Docker installation validation for tinsnip set -euo pipefail # Get script directory and source libraries SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" TINSNIP_ROOT="$(dirname "$SCRIPT_DIR")" source "$TINSNIP_ROOT/lib/core.sh" # Verify Docker installation for a service user verify_docker_installation() { local username="$1" log_with_prefix "Docker Validation" "Verifying Docker installation for $username..." # Use the service .env file (source of truth) local service_env_file="/mnt/$username/.env" # Docker context already set up in configure_docker() # Debug verification process log_with_prefix "Docker Validation" "Debugging verification process..." log_with_prefix "Docker Validation" " Service env file: $service_env_file" if [[ -f "$service_env_file" ]]; then log_with_prefix "Docker Validation" " Environment variables in service .env:" sudo -u "$username" grep "DOCKER\|XDG_RUNTIME" "$service_env_file" | while read line; do log_with_prefix "Docker Validation" " $line" done else log_with_prefix "Docker Validation" " WARNING: Service .env file not found!" fi # Test Docker verification with detailed output log_with_prefix "Docker Validation" " Testing Docker command with environment..." if sudo -u "$username" bash -c "source '$service_env_file' 2>/dev/null && docker version"; then log_with_prefix "Docker Validation" "Docker verification successful!" local docker_version docker_version=$(sudo -u "$username" bash -c "source '$service_env_file' && docker --version") log_with_prefix "Docker Validation" "Installed: $docker_version" else log_with_prefix "Docker Validation" "Docker verification failed - showing detailed error:" sudo -u "$username" bash -c "source '$service_env_file' 2>/dev/null && docker version" 2>&1 | while read line; do log_with_prefix "Docker Validation" " ERROR: $line" done error_with_prefix "Docker Validation" "Docker verification failed for $username" return 1 fi log_with_prefix "Docker Validation" "Service available for user: $username" log_with_prefix "Docker Validation" "Privileged ports: enabled" return 0 } # Main validation entry point main() { local service_user="${1:-}" if [[ -z "$service_user" ]]; then echo "Usage: $0 " >&2 echo "Example: $0 testservice-prod" >&2 exit 1 fi # Check if user exists if ! id "$service_user" &>/dev/null; then error_with_prefix "Docker Validation" "User $service_user does not exist" exit 1 fi verify_docker_installation "$service_user" log_with_prefix "Docker Validation" "✅ All Docker validation checks passed" } # Run main if called directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi