Added pm-task

This commit is contained in:
Ali
2025-12-12 14:01:28 +08:00
parent 49b92b4aa6
commit 98a8f86f61
2 changed files with 194 additions and 4 deletions

View File

@@ -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 && \

191
pm-task Normal file
View File

@@ -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] [<argument>...] --help"
echo "Usage 2: pm-task <project-name> - 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