293 lines
7.7 KiB
Bash
Executable File
293 lines
7.7 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Docker Dashboard Setup Script
|
|
# This script sets up and launches the Docker Dashboard as a background process
|
|
|
|
set -e
|
|
|
|
echo "🚀 Docker Dashboard Setup Script"
|
|
echo "================================"
|
|
|
|
# Get script directory
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
cd "$SCRIPT_DIR"
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Function to print colored output
|
|
print_status() {
|
|
echo -e "${GREEN}[INFO]${NC} $1"
|
|
}
|
|
|
|
print_warning() {
|
|
echo -e "${YELLOW}[WARN]${NC} $1"
|
|
}
|
|
|
|
print_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# Check if running as root for Docker access
|
|
check_permissions() {
|
|
if [[ $EUID -ne 0 ]]; then
|
|
print_warning "This script needs to run with sudo for Docker access"
|
|
print_warning "Rerunning with sudo..."
|
|
exec sudo "$0" "$@"
|
|
fi
|
|
}
|
|
|
|
# Check if Docker is running
|
|
check_docker() {
|
|
if ! systemctl is-active --quiet docker; then
|
|
print_warning "Docker service is not running. Starting Docker..."
|
|
systemctl start docker
|
|
fi
|
|
|
|
if ! docker info >/dev/null 2>&1; then
|
|
print_error "Docker is not accessible. Please check Docker installation"
|
|
exit 1
|
|
fi
|
|
|
|
print_status "Docker is running and accessible"
|
|
}
|
|
|
|
# Install Python dependencies
|
|
install_dependencies() {
|
|
print_status "Installing Python dependencies..."
|
|
|
|
# Detect Python environment
|
|
PYTHON_CMD="python3"
|
|
PIP_CMD="pip3"
|
|
|
|
# Check if we're in a virtual environment
|
|
if [[ -n "$VIRTUAL_ENV" ]]; then
|
|
print_status "Detected virtual environment: $VIRTUAL_ENV"
|
|
PIP_CMD="pip"
|
|
elif [[ -f "venv/bin/activate" ]]; then
|
|
print_status "Detected local virtual environment"
|
|
source venv/bin/activate
|
|
PIP_CMD="pip"
|
|
fi
|
|
|
|
# Check if pip is available
|
|
if ! command -v $PIP_CMD &> /dev/null; then
|
|
print_error "pip is not available. Please install pip first"
|
|
exit 1
|
|
fi
|
|
|
|
# Install requirements with appropriate flags
|
|
if [[ -f "requirements.txt" ]]; then
|
|
$PIP_CMD install -r requirements.txt --break-system-packages || $PIP_CMD install -r requirements.txt
|
|
else
|
|
$PIP_CMD install flask flask-cors docker requests python-dotenv --break-system-packages || \
|
|
$PIP_CMD install flask flask-cors docker requests python-dotenv
|
|
fi
|
|
|
|
print_status "Dependencies installed successfully"
|
|
}
|
|
|
|
# Setup environment file
|
|
setup_environment() {
|
|
if [[ ! -f ".env" ]]; then
|
|
print_status "Creating .env file from template..."
|
|
cp .env.example .env
|
|
print_status "Please edit .env file with your specific configuration"
|
|
else
|
|
print_status ".env file already exists"
|
|
fi
|
|
|
|
# Source the environment file
|
|
source .env
|
|
|
|
# Set defaults if not provided
|
|
export PORT=${PORT:-5000}
|
|
export HOST=${HOST:-0.0.0.0}
|
|
export DEBUG=${DEBUG:-False}
|
|
export REFRESH_INTERVAL=${REFRESH_INTERVAL:-30}
|
|
|
|
print_status "Environment configured"
|
|
}
|
|
|
|
# Initialize database
|
|
initialize_database() {
|
|
print_status "Initializing database..."
|
|
|
|
# Detect Python command
|
|
PYTHON_CMD="python3"
|
|
if [[ -n "$VIRTUAL_ENV" ]] || [[ -f "venv/bin/activate" ]]; then
|
|
if [[ -f "venv/bin/activate" ]]; then
|
|
source venv/bin/activate
|
|
PYTHON_CMD="python"
|
|
else
|
|
PYTHON_CMD="python"
|
|
fi
|
|
fi
|
|
|
|
# Create database if it doesn't exist
|
|
if [[ ! -f "dashboard.db" ]]; then
|
|
$PYTHON_CMD -c "
|
|
import database
|
|
db = database.DatabaseManager()
|
|
db.init_database()
|
|
print('Database initialized successfully')
|
|
"
|
|
else
|
|
print_status "Database already exists"
|
|
fi
|
|
}
|
|
|
|
# Kill existing processes
|
|
kill_existing_processes() {
|
|
print_status "Checking for existing processes..."
|
|
|
|
# Kill existing Python processes on the port
|
|
PID=$(lsof -t -i :${PORT} 2>/dev/null || echo "")
|
|
if [[ -n "$PID" ]]; then
|
|
print_warning "Killing existing process on port ${PORT} (PID: $PID)"
|
|
kill -TERM "$PID" 2>/dev/null || true
|
|
sleep 2
|
|
kill -KILL "$PID" 2>/dev/null || true
|
|
fi
|
|
}
|
|
|
|
# Launch application
|
|
launch_application() {
|
|
print_status "Launching Docker Dashboard..."
|
|
|
|
# Detect Python command
|
|
PYTHON_CMD="python3"
|
|
if [[ -n "$VIRTUAL_ENV" ]] || [[ -f "venv/bin/activate" ]]; then
|
|
if [[ -f "venv/bin/activate" ]]; then
|
|
source venv/bin/activate
|
|
PYTHON_CMD="python"
|
|
else
|
|
PYTHON_CMD="python"
|
|
fi
|
|
fi
|
|
|
|
# Create logs directory
|
|
mkdir -p logs
|
|
|
|
# Launch the application in background
|
|
nohup $PYTHON_CMD api.py > logs/dashboard.log 2>&1 &
|
|
|
|
# Get the process ID
|
|
APP_PID=$!
|
|
|
|
# Wait a moment for the app to start
|
|
sleep 3
|
|
|
|
# Check if the process is still running
|
|
if kill -0 "$APP_PID" 2>/dev/null; then
|
|
print_status "Application started successfully (PID: $APP_PID)"
|
|
print_status "Dashboard available at: http://localhost:${PORT}"
|
|
print_status "Logs available at: logs/dashboard.log"
|
|
|
|
# Save PID to file
|
|
echo "$APP_PID" > dashboard.pid
|
|
|
|
print_status ""
|
|
print_status "To stop the application, run:"
|
|
print_status " sudo ./stop.sh"
|
|
print_status ""
|
|
print_status "To view logs:"
|
|
print_status " tail -f logs/dashboard.log"
|
|
else
|
|
print_error "Failed to start application. Check logs/dashboard.log for details"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Create stop script
|
|
create_stop_script() {
|
|
cat > stop.sh << 'EOF'
|
|
#!/bin/bash
|
|
|
|
# Stop Docker Dashboard Script
|
|
|
|
if [[ -f "dashboard.pid" ]]; then
|
|
PID=$(cat dashboard.pid)
|
|
if kill -0 "$PID" 2>/dev/null; then
|
|
echo "Stopping Docker Dashboard (PID: $PID)..."
|
|
kill -TERM "$PID"
|
|
sleep 2
|
|
|
|
if kill -0 "$PID" 2>/dev/null; then
|
|
echo "Force stopping..."
|
|
kill -KILL "$PID"
|
|
fi
|
|
|
|
rm dashboard.pid
|
|
echo "Application stopped successfully"
|
|
else
|
|
echo "Application is not running"
|
|
rm -f dashboard.pid
|
|
fi
|
|
else
|
|
echo "PID file not found. Attempting to kill by port..."
|
|
PID=$(lsof -t -i :5000 2>/dev/null || echo "")
|
|
if [[ -n "$PID" ]]; then
|
|
echo "Stopping process on port 5000 (PID: $PID)..."
|
|
kill -TERM "$PID"
|
|
sleep 2
|
|
|
|
if kill -0 "$PID" 2>/dev/null; then
|
|
echo "Force stopping..."
|
|
kill -KILL "$PID"
|
|
fi
|
|
|
|
echo "Application stopped successfully"
|
|
else
|
|
echo "No application found running on port 5000"
|
|
fi
|
|
fi
|
|
EOF
|
|
|
|
chmod +x stop.sh
|
|
print_status "Stop script created: stop.sh"
|
|
}
|
|
|
|
# Main execution
|
|
main() {
|
|
print_status "Starting Docker Dashboard setup..."
|
|
|
|
check_permissions
|
|
check_docker
|
|
install_dependencies
|
|
setup_environment
|
|
initialize_database
|
|
kill_existing_processes
|
|
create_stop_script
|
|
launch_application
|
|
|
|
print_status ""
|
|
print_status "🎉 Docker Dashboard setup complete!"
|
|
print_status "Dashboard is running at: http://localhost:${PORT}"
|
|
print_status ""
|
|
print_status "Next steps:"
|
|
print_status "1. Open your browser to http://localhost:${PORT}"
|
|
print_status "2. Click 'Sync Apps' to load your Docker containers"
|
|
print_status "3. Edit tiles by clicking the 'Edit' button on each tile"
|
|
print_status "4. Use ./stop.sh to stop the application"
|
|
}
|
|
|
|
# Handle command line arguments
|
|
case "${1:-}" in
|
|
"--help"|"-h")
|
|
echo "Usage: $0 [--help]"
|
|
echo ""
|
|
echo "This script sets up and launches the Docker Dashboard"
|
|
echo ""
|
|
echo "Options:"
|
|
echo " --help, -h Show this help message"
|
|
exit 0
|
|
;;
|
|
*)
|
|
main
|
|
;;
|
|
esac
|