From 98a8f86f61844e581b9af75f35e4f3eec388c4d2 Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 12 Dec 2025 14:01:28 +0800 Subject: [PATCH] Added pm-task --- README.md | 7 +- pm-task | 191 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 pm-task diff --git a/README.md b/README.md index cee5f8b..36ed628 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,12 @@ **PUBLIC REPOSITORY** -Just some common scripts for all every Linux shells. +Just some everyday scripts for all my Linux shells. ## Installation -To `/usr/local/shared-scripts`: (reecommended) +To `/usr/local/shared-scripts`: (recommended) ```bash git clone https://githive.duckdns.org/shared/scripts ~/shared-scripts && \ sudo mv ~/shared-scripts /usr/local/ && \ @@ -16,10 +16,9 @@ echo -e "\n# Shared Scripts:\n. /usr/local/shared-scripts/startup.sh \"/usr/loca Auto update (via Cron): ``` -*/15 * * * * * git clone https://githive.duckdns.org/shared/scripts /usr/local/shared-scripts > /dev/null 2>&1 +*/15 * * * * cd /usr/local/shared-scripts && git pull origin main > /dev/null 2>&1 ``` - To `$HOME/shared-scripts`: ```bash git clone https://githive.duckdns.org/shared/scripts ~/shared-scripts && \ diff --git a/pm-task b/pm-task new file mode 100644 index 0000000..ea49f2d --- /dev/null +++ b/pm-task @@ -0,0 +1,191 @@ +#!/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 +} + +function subGotoWorkspace() { + case $HOSTNAME in + Eighty) + if [ $1 ]; then + cd $HOME/docker/$1 + else + cd $HOME/docker + fi + ;; + HiveDC) + if [ $1 ]; then + cd /mnt/workspace/docker/$1 + else + cd /mnt/workspace/docker + fi + ;; + HivePM) + if [ $1 ]; then + cd /mnt/workspace/podman/$1 + else + cd /mnt/workspace/podman + fi + ;; + libpodman) + if [ $1 ]; then + cd /podman/$1 + else + cd /podman + fi + ;; + *) + echo Unrecognized host: $HOSTNAME + ;; + esac +} + +# 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 1: pm-task [build|up|down|logs|sh|start|stop|resatart] [...] --help" + echo "Usage 2: pm-task - Go to the project directory." + echo "Usage 3: pm-task (no arguments) - Go to the workspace directory." +} + +# 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 + ;; + --help) + subHelp + ;; + *) + subGotoWorkspace $TASK + esac + +# Go to the workspace when no argument is provided. +else + subGotoWorkspace +fi