diff --git a/pm-task b/pm-task index 4ae6e98..9daa00e 100755 --- a/pm-task +++ b/pm-task @@ -67,17 +67,21 @@ build_containers() { 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 + reload_nginx else echo "Error: Cannot build (neither compose.yaml, docker-compose.yml nor docker-compose.yaml is found)." return 1 fi } +# Function to reload nginx +reload_nginx() { + 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" +} + # Function to show menu and select directory show_menu() { echo "" @@ -85,13 +89,13 @@ show_menu() { echo "Workspace: $WORKSPACE" echo "" - # Validate workspace directory exists + # 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) + # 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 @@ -107,7 +111,7 @@ show_menu() { read -p "Select a project (1-${#dirs[@]}): " choice - # If user pressed Enter without selection, show workspace contents + # If user pressed Enter without selection, show workspace contents if [ -z "$choice" ]; then echo "Entering Docker/Podman Projects Workspace" cd "$WORKSPACE" || return 1 @@ -124,7 +128,8 @@ show_menu() { echo "" echo "=== Contents of $(basename "$selected_dir") ===" - ls -lah "$selected_dir" + cd "$selected_dir" || return 1 + ls -lah } # Function to restart containers @@ -194,6 +199,18 @@ run_shell() { docker exec -it "$(basename $PWD)" bash } +# Function to upgrade containers +upgrade_containers() { + check_compose_file || return 1 + + echo "Pulling latest images..." + docker compose pull + + echo "Upgrading containers..." + docker compose up -d --remove-orphans --force-recreate + echo "Done!" +} + # Main script main() { load_config @@ -206,13 +223,15 @@ main() { 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 " start/up - Start container(s)" + echo " stop/down - 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" + echo " build/rebuild - Build containers and reload nginx" + echo " reload - Reload nginx service" + echo " upgrade - Pull latest images and upgrade containers" return 0 fi @@ -220,10 +239,10 @@ main() { restart) restart_containers ;; - start) + start|up) start_containers ;; - stop) + stop|down) stop_containers ;; ps) @@ -241,7 +260,13 @@ main() { sh|bash|shell) run_shell ;; - "") + reload) + reload_nginx + ;; + upgrade) + upgrade_containers + ;; + "") show_menu ;; *) diff --git a/pm-task.md b/pm-task.md index 0656946..28bc0f8 100644 --- a/pm-task.md +++ b/pm-task.md @@ -16,11 +16,13 @@ Tasks are defined in the first argument. - `(none)` : Shows a numbered list menu of all subdirectories under the workspace directory, navigates to the selected directory, and runs `ls -lah` - `restart` : Runs `docker compose down`, `sleep 1`, and `docker compose up` commands -- `start` : Runs `docker compose up` command -- `stop` : Runs `docker compose down` command +- `start` / `up` : Runs `docker compose up` command +- `stop` / `down` : Runs `docker compose down` command - `ps` : Runs `watch docker ps` to monitor running containers - `stats` : Runs `docker stats` to monitor container resource usage - `logs` / `log` : Runs `docker logs -f ` to follow container logs - `sh` / `bash` / `shell` : Runs `docker exec -it bash` to access container shell +- `reload` : Reloads the nginx service if the container is running - `build` / `rebuild` : Builds containers with `docker compose build`, optionally restarts containers, and reloads nginx +- `upgrade` : Pulls latest images and upgrades containers with `docker compose up -d --remove-orphans --force-recreate` - `--help` : Displays available tasks and usage information \ No newline at end of file