#!/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