mirror of
https://github.com/johndoe6345789/docker-swarm-termina.git
synced 2026-04-24 13:45:01 +00:00
Split monolithic 631-line app.py into focused modules: **Structure:** - config.py - Configuration and shared state - utils/ - Utility functions (1-2 functions per file) - auth.py - Authentication helpers - docker_client.py - Docker client getter - exec_helpers.py - Command execution helpers - formatters.py - Data formatting utilities - terminal_helpers.py - Terminal operation helpers - diagnostics/docker_env.py - Docker diagnostics - routes/ - HTTP endpoints (1 endpoint per file) - login.py, logout.py, health.py - containers/list.py, exec.py, start.py, stop.py, restart.py, remove.py - handlers/ - WebSocket handlers (1 handler per file) - terminal/connect.py, disconnect.py, start.py, input.py, resize.py, register.py **Improvements:** - Reduced function complexity (from 21 locals to 18 max) - Fixed all pylint import order issues - Removed unused imports (select, timedelta, stat) - Applied lazy logging formatting throughout - Added comprehensive docstrings - Each file has focused responsibility - Easier to test, maintain, and extend **Pylint score improvement:** - Before: 25 problems (15 errors, 10 warnings) - After: Only duplicate code warnings (expected for similar routes) https://claude.ai/code/session_011PzvkCnVrsatoxbY3HbGXz
39 lines
1.5 KiB
Python
39 lines
1.5 KiB
Python
"""Docker client getter."""
|
|
import docker
|
|
from config import logger
|
|
from utils.diagnostics.docker_env import diagnose_docker_environment
|
|
|
|
|
|
def get_docker_client():
|
|
"""Get Docker client with enhanced error reporting."""
|
|
try:
|
|
logger.info("Attempting to connect to Docker...")
|
|
|
|
# Try default connection first
|
|
try:
|
|
client = docker.from_env()
|
|
client.ping()
|
|
logger.info("✓ Successfully connected to Docker using docker.from_env()")
|
|
return client
|
|
except Exception as e: # pylint: disable=broad-exception-caught
|
|
logger.warning("docker.from_env() failed: %s", e)
|
|
|
|
# Try explicit Unix socket connection
|
|
try:
|
|
logger.info("Trying explicit Unix socket connection...")
|
|
client = docker.DockerClient(base_url='unix:///var/run/docker.sock')
|
|
client.ping()
|
|
logger.info("✓ Successfully connected to Docker using Unix socket")
|
|
return client
|
|
except Exception as e: # pylint: disable=broad-exception-caught
|
|
logger.warning("Unix socket connection failed: %s", e)
|
|
|
|
# If all fails, run diagnostics and return None
|
|
logger.error("All Docker connection attempts failed!")
|
|
diagnose_docker_environment()
|
|
return None
|
|
|
|
except Exception as e: # pylint: disable=broad-exception-caught
|
|
logger.error("Unexpected error in get_docker_client: %s", e, exc_info=True)
|
|
return None
|