257 lines
6.9 KiB
Bash
Executable File
257 lines
6.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Container Management Tasks Script (Docker/Podman)
|
|
# Usage: pm-task-v3 [task]
|
|
# Tasks: (none), restart, start, stop
|
|
|
|
CONFIG_FILE="$HOME/.config/pm-task.conf"
|
|
WORKSPACE=""
|
|
|
|
# Check if docker command exists
|
|
if ! command -v docker &> /dev/null; then
|
|
echo "Error: docker command not found."
|
|
echo "Please install Docker or configure podman as an alias (or install podman-docker)."
|
|
return 1
|
|
fi
|
|
|
|
# Function to load configuration
|
|
load_config() {
|
|
if [ ! -f "$CONFIG_FILE" ]; then
|
|
echo "Config file not found. Creating new config..."
|
|
mkdir -p "$(dirname "$CONFIG_FILE")"
|
|
echo "# Configuration file for pm-task" > "$CONFIG_FILE"
|
|
echo "# workspace=<path-to-your-docker-projects>" >> "$CONFIG_FILE"
|
|
fi
|
|
|
|
# Source the config file
|
|
if [ -f "$CONFIG_FILE" ]; then
|
|
# Extract workspace value if it exists
|
|
WORKSPACE=$(grep "^workspace=" "$CONFIG_FILE" | cut -d'=' -f2 | tr -d '"')
|
|
fi
|
|
|
|
# If workspace is not set, ask user
|
|
if [ -z "$WORKSPACE" ]; then
|
|
echo "Workspace directory not configured."
|
|
read -p "Enter your Docker/Podman projects workspace directory: " WORKSPACE
|
|
# Save to config file
|
|
echo "workspace=$WORKSPACE" >> "$CONFIG_FILE"
|
|
echo "Configuration saved to $CONFIG_FILE"
|
|
fi
|
|
}
|
|
|
|
# Function to check for compose file
|
|
check_compose_file() {
|
|
if [ -f "compose.yaml" ]; then
|
|
COMPOSE_FILE="compose.yaml"
|
|
elif [ -f "docker-compose.yml" ]; then
|
|
COMPOSE_FILE="docker-compose.yml"
|
|
elif [ -f "docker-compose.yaml" ]; then
|
|
COMPOSE_FILE="docker-compose.yaml"
|
|
else
|
|
echo "Error: No compose file found in $(basename $PWD)"
|
|
echo "Expected one of: compose.yaml, docker-compose.yml, docker-compose.yaml"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to build containers
|
|
build_containers() {
|
|
if [ -f "compose.yaml" ] || [ -f "docker-compose.yml" ] || [ -f "docker-compose.yaml" ]; then
|
|
ls -lah
|
|
|
|
docker compose build
|
|
|
|
read -e -i Y -p "Restart container(s)? " confirm
|
|
if [[ $confirm =~ ^[Yy]$ ]]; then
|
|
docker compose -f "$COMPOSE_FILE" down && docker compose -f "$COMPOSE_FILE" up -d
|
|
fi
|
|
|
|
# Check if nginx container is running before reloading
|
|
if docker ps --format '{{.Names}}' | grep -q '^nginx$'; then
|
|
docker exec -d nginx service nginx reload
|
|
else
|
|
echo "Warning: nginx container is not running, skipping reload"
|
|
fi
|
|
else
|
|
echo "Error: Cannot build (neither compose.yaml, docker-compose.yml nor docker-compose.yaml is found)."
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to show menu and select directory
|
|
show_menu() {
|
|
echo ""
|
|
echo "=== Container Management Tasks ==="
|
|
echo "Workspace: $WORKSPACE"
|
|
echo ""
|
|
|
|
# Validate workspace directory exists
|
|
if [ ! -d "$WORKSPACE" ]; then
|
|
echo "Error: Workspace directory '$WORKSPACE' does not exist."
|
|
return 1
|
|
fi
|
|
|
|
# Get list of subdirectories (excluding dot directories)
|
|
mapfile -t dirs < <(find "$WORKSPACE" -mindepth 1 -maxdepth 1 -type d ! -name '.*' 2>/dev/null | sort)
|
|
|
|
if [ ${#dirs[@]} -eq 0 ]; then
|
|
echo "No subdirectories found in $WORKSPACE"
|
|
return 1
|
|
fi
|
|
|
|
echo "Available projects:"
|
|
for i in "${!dirs[@]}"; do
|
|
echo " $((i+1)). $(basename "${dirs[$i]}")"
|
|
done
|
|
echo ""
|
|
|
|
read -p "Select a project (1-${#dirs[@]}): " choice
|
|
|
|
# If user pressed Enter without selection, show workspace contents
|
|
if [ -z "$choice" ]; then
|
|
echo "Entering Docker/Podman Projects Workspace"
|
|
cd "$WORKSPACE" || return 1
|
|
ls -lah "$WORKSPACE"
|
|
return 0
|
|
fi
|
|
|
|
selected_dir="${dirs[$((choice-1))]}"
|
|
|
|
if [ -z "$selected_dir" ] || [ ! -d "$selected_dir" ]; then
|
|
echo "Invalid selection"
|
|
return 1
|
|
fi
|
|
|
|
echo ""
|
|
echo "=== Contents of $(basename "$selected_dir") ==="
|
|
ls -lah "$selected_dir"
|
|
}
|
|
|
|
# Function to restart containers
|
|
restart_containers() {
|
|
echo "Restarting containers in $WORKSPACE..."
|
|
cd "$WORKSPACE" || return 1
|
|
|
|
check_compose_file || return 1
|
|
|
|
echo "Stopping containers..."
|
|
docker compose -f "$COMPOSE_FILE" down
|
|
|
|
echo "Waiting 1 second..."
|
|
sleep 1
|
|
|
|
echo "Starting containers..."
|
|
docker compose -f "$COMPOSE_FILE" up
|
|
|
|
echo "Done!"
|
|
}
|
|
|
|
# Function to start containers
|
|
start_containers() {
|
|
echo "Starting containers in $WORKSPACE..."
|
|
cd "$WORKSPACE" || return 1
|
|
|
|
check_compose_file || return 1
|
|
|
|
echo "Starting containers..."
|
|
docker compose -f "$COMPOSE_FILE" up
|
|
echo "Done!"
|
|
}
|
|
|
|
# Function to stop containers
|
|
stop_containers() {
|
|
echo "Stopping containers in $WORKSPACE..."
|
|
cd "$WORKSPACE" || return 1
|
|
|
|
check_compose_file || return 1
|
|
|
|
echo "Stopping containers..."
|
|
docker compose -f "$COMPOSE_FILE" down
|
|
echo "Done!"
|
|
}
|
|
|
|
# Function to run watch docker ps
|
|
watch_ps() {
|
|
echo "Running 'watch docker ps'..."
|
|
watch docker ps
|
|
}
|
|
|
|
# Function to run docker stats
|
|
run_stats() {
|
|
echo "Running 'docker stats'..."
|
|
docker stats
|
|
}
|
|
|
|
# Function to run docker logs
|
|
run_logs() {
|
|
echo "Running 'docker logs -f $(basename $PWD)'..."
|
|
docker logs -f "$(basename $PWD)"
|
|
}
|
|
|
|
# Function to run docker exec bash
|
|
run_shell() {
|
|
echo "Running 'docker exec -it $(basename $PWD) bash'..."
|
|
docker exec -it "$(basename $PWD)" bash
|
|
}
|
|
|
|
# Main script
|
|
main() {
|
|
load_config
|
|
|
|
task="${1:-}"
|
|
|
|
# Handle --help flag
|
|
if [ "$task" = "--help" ]; then
|
|
echo "Usage: $0 [task]"
|
|
echo "Tasks:"
|
|
echo " (none) - Show menu and list container projects"
|
|
echo " restart - Restart container(s)"
|
|
echo " start - Start container(s)"
|
|
echo " stop - Stop container(s)"
|
|
echo " ps - Watch running containers"
|
|
echo " stats - Container statistics"
|
|
echo " logs/log - Follow container logs"
|
|
echo " sh/bash/shell - Exec into container bash shell"
|
|
echo " build/rebuild - Build containers and reload nginx"
|
|
return 0
|
|
fi
|
|
|
|
case "$task" in
|
|
restart)
|
|
restart_containers
|
|
;;
|
|
start)
|
|
start_containers
|
|
;;
|
|
stop)
|
|
stop_containers
|
|
;;
|
|
ps)
|
|
watch_ps
|
|
;;
|
|
stats)
|
|
run_stats
|
|
;;
|
|
build|rebuild)
|
|
build_containers
|
|
;;
|
|
logs|log)
|
|
run_logs
|
|
;;
|
|
sh|bash|shell)
|
|
run_shell
|
|
;;
|
|
"")
|
|
show_menu
|
|
;;
|
|
*)
|
|
echo "Unknown task: $task"
|
|
echo "Usage: $0 [task]"
|
|
echo "Run '$0 --help' for available tasks."
|
|
return 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
main "$@"
|