c81abeda54
More portable than #!/bin/bash — resolves bash from PATH, which handles installations outside /bin (e.g. Homebrew on macOS).
413 lines
11 KiB
Bash
Executable File
413 lines
11 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# Author: Julien Gautier <jgautier.webdev@gmail.com>
|
|
# Organization: AdAstra
|
|
# Project: adastra_scripts
|
|
#
|
|
################################################################################
|
|
# MongoDB Database Utility Script (Standalone)
|
|
#
|
|
# This script manages a standalone MongoDB instance for local development.
|
|
# It runs independently and requires no configuration from the backend project.
|
|
#
|
|
# Usage: ./db-start.sh <command>
|
|
#
|
|
# Commands:
|
|
# up Start MongoDB and Mongo Express (interactive setup)
|
|
# down Stop MongoDB and Mongo Express
|
|
# logs Show database logs (follow mode)
|
|
# restart Restart MongoDB and Mongo Express
|
|
# status Show container status
|
|
# clean Remove database containers and volumes
|
|
# help Show this help message
|
|
#
|
|
# Configuration:
|
|
# The script will prompt for MongoDB credentials on first run.
|
|
# Configuration is stored in .env file.
|
|
#
|
|
# Examples:
|
|
# ./db-start.sh up
|
|
# ./db-start.sh logs
|
|
# ./db-start.sh down
|
|
#
|
|
################################################################################
|
|
|
|
set -e
|
|
|
|
# Configuration
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
COMPOSE_FILE="${SCRIPT_DIR}/docker-compose-mongodb.yml"
|
|
ENV_FILE="${SCRIPT_DIR}/.env"
|
|
PROJECT_NAME="dbutils"
|
|
|
|
# Colors
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
CYAN='\033[0;36m'
|
|
NC='\033[0m'
|
|
|
|
# Helper functions
|
|
print_info() {
|
|
echo -e "${BLUE}┣━${NC} ${CYAN}${1}${NC}"
|
|
}
|
|
|
|
print_success() {
|
|
echo -e "${GREEN}✓ ${1}${NC}"
|
|
}
|
|
|
|
print_error() {
|
|
echo -e "${RED}✗ ${1}${NC}"
|
|
}
|
|
|
|
print_warning() {
|
|
echo -e "${YELLOW}⚠ ${1}${NC}"
|
|
}
|
|
|
|
print_separator() {
|
|
echo -e "${BLUE}┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
|
}
|
|
|
|
print_separator_up() {
|
|
echo -e "${BLUE}┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
|
}
|
|
|
|
print_separator_down() {
|
|
echo -e "${BLUE}┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
|
}
|
|
|
|
check_docker() {
|
|
if ! command -v docker &> /dev/null; then
|
|
print_error "Docker is not installed or not in PATH"
|
|
exit 1
|
|
fi
|
|
print_success "Docker found: $(docker --version)"
|
|
}
|
|
|
|
check_docker_running() {
|
|
if ! docker info > /dev/null 2>&1; then
|
|
print_error "Docker daemon is not running"
|
|
exit 1
|
|
fi
|
|
print_success "Docker daemon is running"
|
|
}
|
|
|
|
check_docker_compose() {
|
|
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null 2>&1; then
|
|
print_error "Docker Compose is not installed or not in PATH"
|
|
exit 1
|
|
fi
|
|
print_success "Docker Compose is available"
|
|
}
|
|
|
|
check_compose_file() {
|
|
if [[ ! -f "$COMPOSE_FILE" ]]; then
|
|
print_error "Docker Compose file not found at $COMPOSE_FILE"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
get_compose_cmd() {
|
|
if command -v docker-compose &> /dev/null; then
|
|
echo "docker-compose"
|
|
else
|
|
echo "docker compose"
|
|
fi
|
|
}
|
|
|
|
setup_credentials() {
|
|
print_separator_up
|
|
print_info "MongoDB Configuration Setup"
|
|
print_separator_down
|
|
echo
|
|
|
|
# Read username
|
|
read -p "Enter MongoDB root username (default: admin): " mongo_username
|
|
mongo_username="${mongo_username:-admin}"
|
|
|
|
# Read password
|
|
read -sp "Enter MongoDB root password (default: password): " mongo_password
|
|
echo
|
|
mongo_password="${mongo_password:-password}"
|
|
|
|
# Read database name
|
|
read -p "Enter MongoDB database name (default: adastradb): " mongo_database
|
|
mongo_database="${mongo_database:-adastradb}"
|
|
|
|
echo
|
|
print_info "MongoDB port (default: 27017): "
|
|
read -p "Enter MongoDB port or press Enter for default: " mongo_port
|
|
mongo_port="${mongo_port:-27017}"
|
|
|
|
print_info "Mongo Express port (default: 8081): "
|
|
read -p "Enter Mongo Express port or press Enter for default: " mongo_express_port
|
|
mongo_express_port="${mongo_express_port:-8081}"
|
|
|
|
echo
|
|
|
|
# Create .env file
|
|
cat > "$ENV_FILE" << EOF
|
|
# MongoDB Configuration
|
|
MONGO_USERNAME=${mongo_username}
|
|
MONGO_PASSWORD=${mongo_password}
|
|
MONGO_DATABASE=${mongo_database}
|
|
MONGO_PORT=${mongo_port}
|
|
MONGO_EXPRESS_PORT=${mongo_express_port}
|
|
MONGO_EXPRESS_USERNAME=dev
|
|
MONGO_EXPRESS_PASSWORD=dev
|
|
EOF
|
|
|
|
print_success "Configuration saved to .env"
|
|
echo
|
|
}
|
|
|
|
load_credentials() {
|
|
if [[ -f "$ENV_FILE" ]]; then
|
|
print_info "Loading configuration from .env"
|
|
# shellcheck source=/dev/null
|
|
source "$ENV_FILE"
|
|
else
|
|
print_warning "No configuration found. Running setup..."
|
|
setup_credentials
|
|
# shellcheck source=/dev/null
|
|
source "$ENV_FILE"
|
|
fi
|
|
}
|
|
|
|
cmd_up() {
|
|
print_separator_up
|
|
print_info "Starting MongoDB Database..."
|
|
print_separator_down
|
|
echo
|
|
|
|
check_docker
|
|
check_docker_running
|
|
check_compose_file
|
|
check_docker_compose
|
|
|
|
load_credentials
|
|
|
|
COMPOSE_CMD=$(get_compose_cmd)
|
|
|
|
# Export variables for docker-compose
|
|
export MONGO_USERNAME
|
|
export MONGO_PASSWORD
|
|
export MONGO_DATABASE
|
|
export MONGO_PORT
|
|
export MONGO_EXPRESS_PORT
|
|
export MONGO_EXPRESS_USERNAME
|
|
export MONGO_EXPRESS_PASSWORD
|
|
|
|
$COMPOSE_CMD -f "$COMPOSE_FILE" -p "$PROJECT_NAME" up -d
|
|
|
|
print_success "MongoDB and Mongo Express have been started"
|
|
print_separator_up
|
|
print_info "MongoDB Connection Details:"
|
|
print_info " Host: localhost:${MONGO_PORT}"
|
|
print_info " Username: ${MONGO_USERNAME}"
|
|
print_info " Password: ${MONGO_PASSWORD}"
|
|
print_info " Database: ${MONGO_DATABASE}"
|
|
print_separator
|
|
print_info "Mongo Express (GUI) available at: http://localhost:${MONGO_EXPRESS_PORT}"
|
|
print_info " Username: ${MONGO_EXPRESS_USERNAME}"
|
|
print_info " Password: ${MONGO_EXPRESS_PASSWORD}"
|
|
print_separator_down
|
|
}
|
|
|
|
cmd_down() {
|
|
print_separator_up
|
|
print_info "Stopping MongoDB and Mongo Express..."
|
|
print_separator_down
|
|
|
|
check_docker
|
|
check_compose_file
|
|
check_docker_compose
|
|
|
|
COMPOSE_CMD=$(get_compose_cmd)
|
|
|
|
$COMPOSE_CMD -f "$COMPOSE_FILE" -p "$PROJECT_NAME" down
|
|
|
|
print_success "MongoDB and Mongo Express have been stopped"
|
|
}
|
|
|
|
cmd_logs() {
|
|
print_separator_up
|
|
print_info "Showing MongoDB logs..."
|
|
print_separator_down
|
|
|
|
check_docker
|
|
check_compose_file
|
|
check_docker_compose
|
|
|
|
COMPOSE_CMD=$(get_compose_cmd)
|
|
|
|
$COMPOSE_CMD -f "$COMPOSE_FILE" -p "$PROJECT_NAME" logs -f
|
|
}
|
|
|
|
cmd_restart() {
|
|
print_separator_up
|
|
print_info "Restarting MongoDB and Mongo Express..."
|
|
print_separator_down
|
|
|
|
check_docker
|
|
check_docker_running
|
|
check_compose_file
|
|
check_docker_compose
|
|
|
|
load_credentials
|
|
|
|
COMPOSE_CMD=$(get_compose_cmd)
|
|
|
|
# Export variables for docker-compose
|
|
export MONGO_USERNAME
|
|
export MONGO_PASSWORD
|
|
export MONGO_DATABASE
|
|
export MONGO_PORT
|
|
export MONGO_EXPRESS_PORT
|
|
export MONGO_EXPRESS_USERNAME
|
|
export MONGO_EXPRESS_PASSWORD
|
|
|
|
$COMPOSE_CMD -f "$COMPOSE_FILE" -p "$PROJECT_NAME" restart
|
|
|
|
print_success "MongoDB and Mongo Express have been restarted"
|
|
}
|
|
|
|
cmd_status() {
|
|
print_separator_up
|
|
print_info "Container Status"
|
|
print_separator_down
|
|
|
|
check_docker
|
|
check_compose_file
|
|
check_docker_compose
|
|
|
|
COMPOSE_CMD=$(get_compose_cmd)
|
|
|
|
$COMPOSE_CMD -f "$COMPOSE_FILE" -p "$PROJECT_NAME" ps
|
|
}
|
|
|
|
cmd_clean() {
|
|
print_separator_up
|
|
print_warning "This will remove database containers and volumes"
|
|
print_warning "All data will be PERMANENTLY LOST!"
|
|
echo
|
|
read -p "Are you sure? Type 'yes' to confirm: " response
|
|
echo
|
|
|
|
if [[ "$response" == "yes" ]]; then
|
|
print_separator
|
|
print_info "Removing containers and volumes..."
|
|
print_separator
|
|
|
|
check_docker
|
|
check_compose_file
|
|
check_docker_compose
|
|
|
|
COMPOSE_CMD=$(get_compose_cmd)
|
|
|
|
$COMPOSE_CMD -f "$COMPOSE_FILE" -p "$PROJECT_NAME" down -v --remove-orphans
|
|
|
|
print_success "All containers and volumes have been removed"
|
|
print_info "Configuration file (.env) has been preserved"
|
|
else
|
|
print_info "Clean operation cancelled"
|
|
fi
|
|
print_separator_down
|
|
}
|
|
|
|
cmd_reconfigure() {
|
|
print_separator_up
|
|
print_warning "This will reset the MongoDB configuration"
|
|
echo
|
|
read -p "Are you sure? Type 'yes' to confirm: " response
|
|
echo
|
|
|
|
if [[ "$response" == "yes" ]]; then
|
|
print_info "Stopping services before reconfiguration..."
|
|
check_docker 2>/dev/null || true
|
|
check_compose_file 2>/dev/null || true
|
|
|
|
COMPOSE_CMD=$(get_compose_cmd)
|
|
if command -v $COMPOSE_CMD &> /dev/null; then
|
|
$COMPOSE_CMD -f "$COMPOSE_FILE" -p "$PROJECT_NAME" down 2>/dev/null || true
|
|
fi
|
|
|
|
rm -f "$ENV_FILE"
|
|
print_success "Configuration cleared"
|
|
|
|
setup_credentials
|
|
print_info "You can now run './db-start.sh up' to start with the new configuration"
|
|
else
|
|
print_info "Reconfiguration cancelled"
|
|
fi
|
|
print_separator_down
|
|
}
|
|
|
|
cmd_help() {
|
|
cat << EOF
|
|
|
|
MongoDB Database Utility Script (Standalone)
|
|
|
|
Usage: ./db-start.sh <command>
|
|
|
|
Commands:
|
|
up Start MongoDB and Mongo Express (interactive setup on first run)
|
|
down Stop MongoDB and Mongo Express
|
|
logs Show database and container logs (follow mode)
|
|
restart Restart MongoDB and Mongo Express
|
|
status Show container status
|
|
reconfigure Reset and reconfigure MongoDB credentials
|
|
clean Remove all containers and volumes (DATA LOSS!)
|
|
help Show this help message
|
|
|
|
Examples:
|
|
./db-start.sh up
|
|
./db-start.sh logs
|
|
./db-start.sh down
|
|
./db-start.sh reconfigure
|
|
|
|
Configuration:
|
|
On first run, 'up' command will prompt for MongoDB credentials.
|
|
Configuration is stored in .env file (git-ignored).
|
|
You can reconfigure anytime with 'reconfigure' command.
|
|
|
|
EOF
|
|
}
|
|
|
|
# Main
|
|
COMMAND="${1:-help}"
|
|
|
|
case "$COMMAND" in
|
|
up|start)
|
|
cmd_up
|
|
;;
|
|
down|stop)
|
|
cmd_down
|
|
;;
|
|
logs)
|
|
cmd_logs
|
|
;;
|
|
restart)
|
|
cmd_restart
|
|
;;
|
|
status|ps)
|
|
cmd_status
|
|
;;
|
|
reconfigure)
|
|
cmd_reconfigure
|
|
;;
|
|
clean|remove)
|
|
cmd_clean
|
|
;;
|
|
help|--help|-h)
|
|
cmd_help
|
|
;;
|
|
*)
|
|
print_error "Unknown command: $COMMAND"
|
|
echo
|
|
cmd_help
|
|
exit 1
|
|
;;
|
|
esac
|