socktop-webterm/docker-quickstart.sh

394 lines
10 KiB
Bash
Raw Normal View History

#!/bin/bash
# Quick Start Script for socktop webterm Docker Deployment
# This script helps you set up and run the containerized application
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_info() {
echo -e "${BLUE} ${NC}$1"
}
print_success() {
echo -e "${GREEN}${NC} $1"
}
print_warning() {
echo -e "${YELLOW}${NC} $1"
}
print_error() {
echo -e "${RED}${NC} $1"
}
print_header() {
echo ""
echo -e "${BLUE}╔════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}${NC} $1"
echo -e "${BLUE}╚════════════════════════════════════════════════════════════╝${NC}"
echo ""
}
# Check if Docker is installed
check_docker() {
print_info "Checking Docker installation..."
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed. Please install Docker first."
echo " Visit: https://docs.docker.com/get-docker/"
exit 1
fi
print_success "Docker is installed: $(docker --version)"
if ! command -v docker-compose &> /dev/null; then
print_warning "docker-compose not found, checking for docker compose plugin..."
if ! docker compose version &> /dev/null; then
print_error "Docker Compose is not available. Please install Docker Compose."
exit 1
fi
print_success "Docker Compose plugin is available"
DOCKER_COMPOSE="docker compose"
else
print_success "Docker Compose is installed: $(docker-compose --version)"
DOCKER_COMPOSE="docker-compose"
fi
}
# Check if configuration files exist
check_config_files() {
print_info "Checking configuration files..."
local missing_files=()
# Check for required files
if [ ! -f "files/alacritty.toml" ]; then
missing_files+=("files/alacritty.toml")
fi
if [ ! -f "files/catppuccin-frappe.toml" ]; then
missing_files+=("files/catppuccin-frappe.toml")
fi
if [ ! -f "files/profiles.json" ]; then
missing_files+=("files/profiles.json")
fi
if [ ${#missing_files[@]} -gt 0 ]; then
print_warning "Some configuration files are missing:"
for file in "${missing_files[@]}"; do
echo " - $file"
done
echo ""
read -p "Would you like to create them from examples? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
create_config_from_examples
else
print_error "Cannot continue without configuration files."
exit 1
fi
else
print_success "All required configuration files found"
fi
}
# Create config files from examples
create_config_from_examples() {
print_info "Creating configuration files from examples..."
mkdir -p files
if [ ! -f "files/alacritty.toml" ] && [ -f "files/alacritty.toml.example" ]; then
cp files/alacritty.toml.example files/alacritty.toml
print_success "Created files/alacritty.toml"
fi
if [ ! -f "files/catppuccin-frappe.toml" ] && [ -f "files/catppuccin-frappe.toml.example" ]; then
cp files/catppuccin-frappe.toml.example files/catppuccin-frappe.toml
print_success "Created files/catppuccin-frappe.toml"
fi
if [ ! -f "files/profiles.json" ] && [ -f "files/profiles.json.example" ]; then
cp files/profiles.json.example files/profiles.json
print_success "Created files/profiles.json"
fi
print_warning "Note: You may need to customize these files for your environment"
}
# Check SSH keys
check_ssh_keys() {
print_info "Checking SSH keys..."
local key_files=("rpi-master.pem" "rpi-worker-1.pem" "rpi-worker-2.pem" "rpi-worker-3.pem")
local missing_keys=()
for key in "${key_files[@]}"; do
if [ ! -f "files/$key" ]; then
missing_keys+=("$key")
else
# Check permissions
if [ "$(stat -c %a "files/$key" 2>/dev/null || stat -f %A "files/$key" 2>/dev/null)" != "600" ]; then
print_warning "Fixing permissions for files/$key"
chmod 600 "files/$key"
fi
fi
done
if [ ${#missing_keys[@]} -gt 0 ]; then
print_warning "Some SSH key files are missing:"
for key in "${missing_keys[@]}"; do
echo " - files/$key"
done
echo ""
print_info "If you don't have SSH keys yet, the container will still start with local monitoring."
print_info "You can add keys later and restart the container."
else
print_success "All SSH key files found"
fi
}
# Build the Docker image
build_image() {
print_header "Building Docker Image"
print_info "This may take several minutes on first build..."
if $DOCKER_COMPOSE build; then
print_success "Docker image built successfully"
else
print_error "Failed to build Docker image"
exit 1
fi
}
# Start the container
start_container() {
print_header "Starting Container"
if $DOCKER_COMPOSE up -d; then
print_success "Container started successfully"
echo ""
print_info "Waiting for services to be ready..."
sleep 5
# Check if container is running
if docker ps | grep -q socktop-webterm; then
print_success "Container is running"
else
print_error "Container failed to start. Check logs with:"
echo " $DOCKER_COMPOSE logs"
exit 1
fi
else
print_error "Failed to start container"
exit 1
fi
}
# Show container status
show_status() {
print_header "Container Status"
# Show running containers
print_info "Running containers:"
docker ps --filter name=socktop-webterm --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
echo ""
# Show recent logs
print_info "Recent logs (last 20 lines):"
$DOCKER_COMPOSE logs --tail=20
}
# Show access information
show_access_info() {
print_header "Access Information"
echo -e "${GREEN}✓ socktop webterm is ready!${NC}"
echo ""
echo " 🌐 Web Interface: http://localhost:8082"
echo " 📊 Features:"
echo " - Beautiful Catppuccin Frappe theme"
echo " - Transparent terminal window"
echo " - Auto-running socktop -P local"
echo " - GitHub, Crates.io, APT links"
echo ""
echo " 📝 Useful commands:"
echo " View logs: $DOCKER_COMPOSE logs -f"
echo " Stop container: $DOCKER_COMPOSE down"
echo " Restart: $DOCKER_COMPOSE restart"
echo " Shell access: docker exec -it socktop-webterm bash"
echo ""
}
# Show help menu
show_help() {
echo "socktop webterm Docker Quick Start Script"
echo ""
echo "Usage: $0 [COMMAND]"
echo ""
echo "Commands:"
echo " start - Build and start the container (default)"
echo " stop - Stop the container"
echo " restart - Restart the container"
echo " rebuild - Rebuild the image from scratch"
echo " logs - Show container logs"
echo " shell - Open a shell in the container"
echo " status - Show container status"
echo " clean - Stop and remove container and volumes"
echo " help - Show this help message"
echo ""
}
# Stop container
stop_container() {
print_header "Stopping Container"
if $DOCKER_COMPOSE down; then
print_success "Container stopped"
else
print_error "Failed to stop container"
exit 1
fi
}
# Restart container
restart_container() {
print_header "Restarting Container"
if $DOCKER_COMPOSE restart; then
print_success "Container restarted"
else
print_error "Failed to restart container"
exit 1
fi
}
# Rebuild image
rebuild_image() {
print_header "Rebuilding Image"
print_info "Stopping container..."
$DOCKER_COMPOSE down
print_info "Removing old image..."
docker rmi socktop-webterm:latest 2>/dev/null || true
print_info "Building new image (no cache)..."
if $DOCKER_COMPOSE build --no-cache; then
print_success "Image rebuilt successfully"
read -p "Start the container now? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
start_container
show_access_info
fi
else
print_error "Failed to rebuild image"
exit 1
fi
}
# Show logs
show_logs() {
print_header "Container Logs"
print_info "Showing logs (Ctrl+C to exit)..."
$DOCKER_COMPOSE logs -f
}
# Open shell
open_shell() {
print_header "Container Shell"
print_info "Opening bash shell in container..."
docker exec -it socktop-webterm bash
}
# Clean everything
clean_all() {
print_header "Cleaning Up"
print_warning "This will remove the container and all volumes!"
read -p "Are you sure? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
print_info "Stopping and removing container..."
$DOCKER_COMPOSE down -v
print_info "Removing image..."
docker rmi socktop-webterm:latest 2>/dev/null || true
print_success "Cleanup complete"
else
print_info "Cleanup cancelled"
fi
}
# Main function
main() {
# Parse command
COMMAND=${1:-start}
case $COMMAND in
start)
print_header "socktop webterm - Quick Start"
check_docker
check_config_files
check_ssh_keys
build_image
start_container
show_status
show_access_info
;;
stop)
check_docker
stop_container
;;
restart)
check_docker
restart_container
;;
rebuild)
check_docker
rebuild_image
;;
logs)
check_docker
show_logs
;;
shell)
check_docker
open_shell
;;
status)
check_docker
show_status
;;
clean)
check_docker
clean_all
;;
help|--help|-h)
show_help
;;
*)
print_error "Unknown command: $COMMAND"
echo ""
show_help
exit 1
;;
esac
}
# Run main function
main "$@"