Files
scripts/pm-task
2026-04-09 11:40:03 +08:00

262 lines
6.6 KiB
Bash
Executable File

#!/bin/bash
#
# Pod Management Task Script for Docker or Podman.
#
# Do not run `./pm-task` directly, since the `cd` command will not work in this
# way. Instead, run in `source pm-task` pattern. There is already an alias for
# this in the `startup.sh` script, after running that script, simply run 'pm' to
# use this script.
#
# To work with Podman, simply make `docker` an aliase to the `podman` command.
# Image building with docker compose.
function subBuild() {
if [ -f compose.yaml ] || [ -f docker-compose.yml ] || [ -f container-compose.yml ]; then
ls -lah
export RANDOM_UUID=`uuid`
docker compose build
read -ei Y -p "Restart container(s)? " confirm
if [[ $confirm =~ ^[Yy]$ ]]; then
docker compose down && docker compose up -d
fi
# read -e -p "Restart nginx service in container? " confirm
# if [[ $confirm =~ ^[Yy]$ ]]; then
# docker exec -d nginx service nginx reload
# fi
# Assume nginx is always in place.
docker exec -d nginx service nginx reload
else
echo 'Cannot build (niether compose.yaml, docker-compose.yml nor container-compose.yml is found).'
fi
}
# List available projects in a workspace and let user choose one.
# Arguments: $1 = workspace path (optional, defaults to current host's workspace)
# Returns: Sets WORKSPACE_CHOICE var to selected project name or empty string
function subListProjects() {
local workspace="$1"
local projects=()
# Get list of subdirectories (excluding special dirs)
if [ -d "$workspace" ]; then
while IFS= read -r -d '' dir; do
local basename=$(basename "$dir")
# Skip hidden dirs and common non-project dirs
if [[ ! "$basename" =~ ^\. ]] && [[ "$basename" != "README"* ]]; then
projects+=("$basename")
fi
done < <(find "$workspace" -mindepth 1 -maxdepth 1 -type d -print0 2>/dev/null)
fi
# Store projects in a global array
PROJECTS=("${projects[@]}")
}
function subGotoWorkspace() {
local workspace=""
# Determine workspace path based on hostname
case $HOSTNAME in
Eighty)
workspace="$HOME/docker"
;;
HiveDC)
workspace="/mnt/workspace/docker"
;;
hivegcp)
workspace="$HOME/docker"
;;
Podman)
workspace="/mnt/workspace/podman"
;;
HivePM)
workspace="/mnt/workspace/podman"
;;
libpodman)
workspace="/podman"
;;
*)
echo "Unrecognized host: $HOSTNAME"
return 1
;;
esac
# If no argument provided, list projects and let user choose
if [ -z "$1" ]; then
PROJECTS=()
subListProjects "$workspace"
if [ ${#PROJECTS[@]} -eq 0 ]; then
echo "No projects found in $workspace"
cd "$workspace"
return 0
fi
echo "Available projects in $workspace:"
local i=1
for project in "${PROJECTS[@]}"; do
echo " [$i] $project"
((i++))
done
echo " [0] Cancel"
read -ei 1 -p "Choose a project [1-${#PROJECTS[@]}]: " choice
if [[ ! "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 0 ] || [ "$choice" -gt ${#PROJECTS[@]} ]; then
echo "Invalid choice. Canceling."
cd "$workspace"
return 0
fi
if [ "$choice" -eq 0 ]; then
echo "Canceled."
cd "$workspace"
return 0
fi
workspace="$workspace/${PROJECTS[$((choice-1))]}"
else
workspace="$workspace/$1"
fi
cd "$workspace"
}
# Application logs streaming (single log).
function subLogs() {
docker logs -f $POD
# # $1 is $2 of the script.
# if [ $1 ]; then
# # ELABORATE
# docker exec -it $1 tail -s 5 -f /var/www/app/storage/logs/laravel.log
# else
# docker exec -it $POD tail -s 5 -f /var/www/app/storage/logs/laravel.log
# fi
}
# Application logs streaming (daily log).
function subLogsDaily() {
# $1 is $2 of the script.
if [ $1 ]; then
# ELABORATE
docker exec -it $1 tail -s 5 -f /var/www/app/storage/logs/laravel-$TODAY.log
else
docker exec -it $POD tail -s 5 -f /var/www/app/storage/logs/laravel-$TODAY.log
fi
}
# Reload service in container (hard-coded, limited usage).
function subReload() {
case $POD in
nginx)
docker exec -it $POD service nginx reload
;;
*)
# docker exec -it $POD service apache2 reload # No longer using Apache.
docker exec -it $POD service nginx reload
;;
esac
}
function subRestart() {
docker compose down && docker compose up -d
}
# Container shell access.
function subShell() {
# $1 is $2 of the script.
if [ $1 ]; then
docker exec -it $1 bash
else
docker exec -it $POD bash
fi
}
# Help messages (keep at bottom). ----------------------------------------------
function subHelp() {
echo "Usage: pm-task <command> [<argument>...]"
echo "Commands:"
echo " build, rebuild Build the Docker Compose project."
echo " daily, today Stream daily logs for the current project."
echo " down, stop Stop and remove containers."
echo " logs, log Stream logs for the current project."
echo " ps Watch container stats."
echo " reload Reload the service in the current project."
echo " restart Restart the Docker Compose project."
echo " stats Display container resource usage statistics."
echo " up, start Start the Docker Compose project."
echo " bash|rsh|sh Access a shell in the running container."
echo " (no command) List available projects and choose one to navigate to."
}
# Task
if [ $1 ]; then
# Assume the host is running podman when alias docket='podman' exists.
if [ $(type -t docker) == "alias" ]; then
COMMAND='podman'
fi
# Assumed container is named after the directory name.
TASK=$1
TODAY=$(date +%Y-%m-%d)
POD=${PWD##*/}
case $TASK in
bash|rsh|sh)
subShell $2
;;
build|rebuild)
subBuild
;;
daily|today)
subLogsDaily $2
;;
down|stop)
docker compose down
;;
logs|log)
subLogs $2
;;
ps)
# Because `watch` has no idea of the alias.
eval "watch -n 10 $COMMAND ps"
;;
reload)
subReload
;;
restart)
if [ $2 ]; then
subGotoWorkspace $2
fi
subRestart $2
;;
stats)
docker stats
;;
up|start)
docker compose up -d
;;
upgrade)
docker compose pull && docker compose up -d --remove-orphans --force-recreate
;;
--help)
subHelp
;;
*)
subGotoWorkspace $TASK
esac
# Go to the workspace when no argument is provided.
else
echo "Usage: pm-task <command> [<argument>...]"
echo "Run 'pm-task --help' for more information."
subGotoWorkspace
fi