Files
skr/services.sh

429 lines
13 KiB
Bash
Raw Normal View History

#!/bin/bash
# Banking Application Services Manager
# Manages MongoDB and MinIO containers
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Configuration
MONGO_CONTAINER="banking-mongo"
MONGO_PORT=27017
MONGO_DATA_DIR="$(pwd)/.nogit/mongodata"
MONGO_USER="bankingadmin"
MONGO_PASS="banking123"
MONGO_VERSION="7.0"
MINIO_CONTAINER="banking-minio"
MINIO_PORT=9000
MINIO_CONSOLE_PORT=9001
MINIO_DATA_DIR="$(pwd)/.nogit/miniodata"
MINIO_USER="minioadmin"
MINIO_PASS="minioadmin"
# Function to print colored messages
print_message() {
echo -e "${2}${1}${NC}"
}
# Function to print header
print_header() {
echo
print_message "═══════════════════════════════════════════════════════════════" "$CYAN"
print_message " $1" "$CYAN"
print_message "═══════════════════════════════════════════════════════════════" "$CYAN"
echo
}
# Check Docker
check_docker() {
if ! command -v docker &> /dev/null; then
print_message "Error: Docker is not installed. Please install Docker first." "$RED"
exit 1
fi
}
# Check container status
check_status() {
local container=$1
if docker ps --format '{{.Names}}' | grep -q "^${container}$"; then
echo "running"
elif docker ps -a --format '{{.Names}}' | grep -q "^${container}$"; then
echo "stopped"
else
echo "not_exists"
fi
}
# Start MongoDB
start_mongodb() {
print_message "📦 MongoDB:" "$YELLOW"
# Create data directory if needed
[ ! -d "$MONGO_DATA_DIR" ] && mkdir -p "$MONGO_DATA_DIR"
local status=$(check_status "$MONGO_CONTAINER")
case $status in
"running")
print_message " Already running ✓" "$GREEN"
;;
"stopped")
docker start "$MONGO_CONTAINER" > /dev/null
print_message " Started ✓" "$GREEN"
;;
"not_exists")
print_message " Creating container..." "$YELLOW"
docker run -d \
--name "$MONGO_CONTAINER" \
-p "0.0.0.0:${MONGO_PORT}:${MONGO_PORT}" \
-v "$MONGO_DATA_DIR:/data/db" \
-e MONGO_INITDB_ROOT_USERNAME="$MONGO_USER" \
-e MONGO_INITDB_ROOT_PASSWORD="$MONGO_PASS" \
-e MONGO_INITDB_DATABASE=banking \
--restart unless-stopped \
"mongo:${MONGO_VERSION}" > /dev/null
print_message " Created and started ✓" "$GREEN"
;;
esac
print_message " URL: mongodb://$MONGO_USER:$MONGO_PASS@localhost:$MONGO_PORT/banking?authSource=admin" "$BLUE"
}
# Start MinIO
start_minio() {
print_message "📦 MinIO (S3 Storage):" "$YELLOW"
# Create data directory if needed
[ ! -d "$MINIO_DATA_DIR" ] && mkdir -p "$MINIO_DATA_DIR"
local status=$(check_status "$MINIO_CONTAINER")
case $status in
"running")
print_message " Already running ✓" "$GREEN"
;;
"stopped")
docker start "$MINIO_CONTAINER" > /dev/null
print_message " Started ✓" "$GREEN"
;;
"not_exists")
print_message " Creating container..." "$YELLOW"
docker run -d \
--name "$MINIO_CONTAINER" \
-p "${MINIO_PORT}:9000" \
-p "${MINIO_CONSOLE_PORT}:9001" \
-v "$MINIO_DATA_DIR:/data" \
-e MINIO_ROOT_USER="$MINIO_USER" \
-e MINIO_ROOT_PASSWORD="$MINIO_PASS" \
--restart unless-stopped \
minio/minio server /data --console-address ":9001" > /dev/null
# Wait for MinIO to start and create bucket
sleep 3
docker exec "$MINIO_CONTAINER" mc alias set local http://localhost:9000 "$MINIO_USER" "$MINIO_PASS" 2>/dev/null
docker exec "$MINIO_CONTAINER" mc mb local/banking-documents 2>/dev/null || true
print_message " Created and started ✓" "$GREEN"
print_message " Bucket 'banking-documents' created ✓" "$GREEN"
;;
esac
print_message " API: http://localhost:$MINIO_PORT" "$BLUE"
print_message " Console: http://localhost:$MINIO_CONSOLE_PORT (login: $MINIO_USER/$MINIO_PASS)" "$BLUE"
}
# Stop MongoDB
stop_mongodb() {
print_message "📦 MongoDB:" "$YELLOW"
local status=$(check_status "$MONGO_CONTAINER")
if [ "$status" = "running" ]; then
docker stop "$MONGO_CONTAINER" > /dev/null
print_message " Stopped ✓" "$GREEN"
else
print_message " Not running" "$YELLOW"
fi
}
# Stop MinIO
stop_minio() {
print_message "📦 MinIO:" "$YELLOW"
local status=$(check_status "$MINIO_CONTAINER")
if [ "$status" = "running" ]; then
docker stop "$MINIO_CONTAINER" > /dev/null
print_message " Stopped ✓" "$GREEN"
else
print_message " Not running" "$YELLOW"
fi
}
# Remove containers
remove_containers() {
local removed=false
if docker ps -a --format '{{.Names}}' | grep -q "^${MONGO_CONTAINER}$"; then
docker rm -f "$MONGO_CONTAINER" > /dev/null 2>&1
print_message " MongoDB container removed ✓" "$GREEN"
removed=true
fi
if docker ps -a --format '{{.Names}}' | grep -q "^${MINIO_CONTAINER}$"; then
docker rm -f "$MINIO_CONTAINER" > /dev/null 2>&1
print_message " MinIO container removed ✓" "$GREEN"
removed=true
fi
if [ "$removed" = false ]; then
print_message " No containers to remove" "$YELLOW"
fi
}
# Clean data
clean_data() {
local cleaned=false
if [ -d "$MONGO_DATA_DIR" ]; then
rm -rf "$MONGO_DATA_DIR"
print_message " MongoDB data removed ✓" "$GREEN"
cleaned=true
fi
if [ -d "$MINIO_DATA_DIR" ]; then
rm -rf "$MINIO_DATA_DIR"
print_message " MinIO data removed ✓" "$GREEN"
cleaned=true
fi
if [ "$cleaned" = false ]; then
print_message " No data to clean" "$YELLOW"
fi
}
# Show status
show_status() {
print_header "Service Status"
# MongoDB status
local mongo_status=$(check_status "$MONGO_CONTAINER")
case $mongo_status in
"running")
print_message "📦 MongoDB: 🟢 Running" "$GREEN"
print_message " └─ mongodb://$MONGO_USER:***@localhost:$MONGO_PORT/banking" "$CYAN"
;;
"stopped")
print_message "📦 MongoDB: 🟡 Stopped" "$YELLOW"
;;
"not_exists")
print_message "📦 MongoDB: ⚪ Not installed" "$MAGENTA"
;;
esac
# MinIO status
local minio_status=$(check_status "$MINIO_CONTAINER")
case $minio_status in
"running")
print_message "📦 MinIO: 🟢 Running" "$GREEN"
print_message " ├─ API: http://localhost:$MINIO_PORT" "$CYAN"
print_message " └─ Console: http://localhost:$MINIO_CONSOLE_PORT" "$CYAN"
;;
"stopped")
print_message "📦 MinIO: 🟡 Stopped" "$YELLOW"
;;
"not_exists")
print_message "📦 MinIO: ⚪ Not installed" "$MAGENTA"
;;
esac
# Show network access for MongoDB
if [ "$mongo_status" = "running" ]; then
echo
print_message "Network Access:" "$BLUE"
local ip=$(hostname -I | awk '{print $1}')
print_message " MongoDB Compass: mongodb://$MONGO_USER:$MONGO_PASS@$ip:$MONGO_PORT/banking?authSource=admin" "$CYAN"
fi
}
# Show logs
show_logs() {
local service=$1
local lines=${2:-20}
case $service in
"mongo"|"mongodb")
if docker ps --format '{{.Names}}' | grep -q "^${MONGO_CONTAINER}$"; then
print_header "MongoDB Logs (last $lines lines)"
docker logs --tail "$lines" "$MONGO_CONTAINER"
else
print_message "MongoDB container is not running" "$YELLOW"
fi
;;
"minio")
if docker ps --format '{{.Names}}' | grep -q "^${MINIO_CONTAINER}$"; then
print_header "MinIO Logs (last $lines lines)"
docker logs --tail "$lines" "$MINIO_CONTAINER"
else
print_message "MinIO container is not running" "$YELLOW"
fi
;;
"all")
show_logs "mongo" "$lines"
echo
show_logs "minio" "$lines"
;;
*)
print_message "Usage: $0 logs [mongo|minio|all] [lines]" "$YELLOW"
;;
esac
}
# Main menu
show_help() {
print_header "Banking Services Manager"
print_message "Usage: $0 [command] [options]" "$GREEN"
echo
print_message "Commands:" "$YELLOW"
print_message " start [service] Start services (mongo|minio|all)" "$NC"
print_message " stop [service] Stop services (mongo|minio|all)" "$NC"
print_message " restart [service] Restart services (mongo|minio|all)" "$NC"
print_message " status Show service status" "$NC"
print_message " logs [service] Show logs (mongo|minio|all) [lines]" "$NC"
print_message " remove Remove all containers" "$NC"
print_message " clean Remove all containers and data ⚠️" "$NC"
print_message " help Show this help message" "$NC"
echo
print_message "Examples:" "$YELLOW"
print_message " $0 start # Start all services" "$NC"
print_message " $0 start mongo # Start only MongoDB" "$NC"
print_message " $0 stop # Stop all services" "$NC"
print_message " $0 status # Check service status" "$NC"
print_message " $0 logs mongo 50 # Show last 50 lines of MongoDB logs" "$NC"
}
# Main script
check_docker
case ${1:-help} in
start)
print_header "Starting Services"
case ${2:-all} in
mongo|mongodb)
start_mongodb
;;
minio|s3)
start_minio
;;
all|"")
start_mongodb
echo
start_minio
;;
*)
print_message "Unknown service: $2" "$RED"
print_message "Use: mongo, minio, or all" "$YELLOW"
;;
esac
;;
stop)
print_header "Stopping Services"
case ${2:-all} in
mongo|mongodb)
stop_mongodb
;;
minio|s3)
stop_minio
;;
all|"")
stop_mongodb
echo
stop_minio
;;
*)
print_message "Unknown service: $2" "$RED"
print_message "Use: mongo, minio, or all" "$YELLOW"
;;
esac
;;
restart)
print_header "Restarting Services"
case ${2:-all} in
mongo|mongodb)
stop_mongodb
sleep 2
start_mongodb
;;
minio|s3)
stop_minio
sleep 2
start_minio
;;
all|"")
stop_mongodb
stop_minio
sleep 2
start_mongodb
echo
start_minio
;;
*)
print_message "Unknown service: $2" "$RED"
;;
esac
;;
status)
show_status
;;
logs)
show_logs "${2:-all}" "${3:-20}"
;;
remove)
print_header "Removing Containers"
print_message "⚠️ This will remove containers but preserve data" "$YELLOW"
read -p "Continue? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
remove_containers
else
print_message "Cancelled" "$YELLOW"
fi
;;
clean)
print_header "Clean All"
print_message "⚠️ WARNING: This will remove all containers and data!" "$RED"
print_message "This action cannot be undone!" "$RED"
read -p "Are you sure? Type 'yes' to confirm: " -r
if [ "$REPLY" = "yes" ]; then
remove_containers
echo
clean_data
print_message "All cleaned ✓" "$GREEN"
else
print_message "Cancelled" "$YELLOW"
fi
;;
help|--help|-h)
show_help
;;
*)
print_message "Unknown command: $1" "$RED"
show_help
exit 1
;;
esac
echo