Updated pm-task.

This commit is contained in:
Ali
2026-04-09 15:29:24 +08:00
parent b1be74b662
commit be760cca0f
2 changed files with 44 additions and 17 deletions

55
pm-task
View File

@@ -67,17 +67,21 @@ build_containers() {
fi fi
# Check if nginx container is running before reloading # Check if nginx container is running before reloading
if docker ps --format '{{.Names}}' | grep -q '^nginx$'; then reload_nginx
docker exec -d nginx service nginx reload
else
echo "Warning: nginx container is not running, skipping reload"
fi
else else
echo "Error: Cannot build (neither compose.yaml, docker-compose.yml nor docker-compose.yaml is found)." echo "Error: Cannot build (neither compose.yaml, docker-compose.yml nor docker-compose.yaml is found)."
return 1 return 1
fi 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 # Function to show menu and select directory
show_menu() { show_menu() {
echo "" echo ""
@@ -85,13 +89,13 @@ show_menu() {
echo "Workspace: $WORKSPACE" echo "Workspace: $WORKSPACE"
echo "" echo ""
# Validate workspace directory exists # Validate workspace directory exists
if [ ! -d "$WORKSPACE" ]; then if [ ! -d "$WORKSPACE" ]; then
echo "Error: Workspace directory '$WORKSPACE' does not exist." echo "Error: Workspace directory '$WORKSPACE' does not exist."
return 1 return 1
fi 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) mapfile -t dirs < <(find "$WORKSPACE" -mindepth 1 -maxdepth 1 -type d ! -name '.*' 2>/dev/null | sort)
if [ ${#dirs[@]} -eq 0 ]; then if [ ${#dirs[@]} -eq 0 ]; then
@@ -107,7 +111,7 @@ show_menu() {
read -p "Select a project (1-${#dirs[@]}): " choice 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 if [ -z "$choice" ]; then
echo "Entering Docker/Podman Projects Workspace" echo "Entering Docker/Podman Projects Workspace"
cd "$WORKSPACE" || return 1 cd "$WORKSPACE" || return 1
@@ -124,7 +128,8 @@ show_menu() {
echo "" echo ""
echo "=== Contents of $(basename "$selected_dir") ===" echo "=== Contents of $(basename "$selected_dir") ==="
ls -lah "$selected_dir" cd "$selected_dir" || return 1
ls -lah
} }
# Function to restart containers # Function to restart containers
@@ -194,6 +199,18 @@ run_shell() {
docker exec -it "$(basename $PWD)" bash 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 script
main() { main() {
load_config load_config
@@ -206,13 +223,15 @@ main() {
echo "Tasks:" echo "Tasks:"
echo " (none) - Show menu and list container projects" echo " (none) - Show menu and list container projects"
echo " restart - Restart container(s)" echo " restart - Restart container(s)"
echo " start - Start container(s)" echo " start/up - Start container(s)"
echo " stop - Stop container(s)" echo " stop/down - Stop container(s)"
echo " ps - Watch running containers" echo " ps - Watch running containers"
echo " stats - Container statistics" echo " stats - Container statistics"
echo " logs/log - Follow container logs" echo " logs/log - Follow container logs"
echo " sh/bash/shell - Exec into container bash shell" 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 return 0
fi fi
@@ -220,10 +239,10 @@ main() {
restart) restart)
restart_containers restart_containers
;; ;;
start) start|up)
start_containers start_containers
;; ;;
stop) stop|down)
stop_containers stop_containers
;; ;;
ps) ps)
@@ -241,7 +260,13 @@ main() {
sh|bash|shell) sh|bash|shell)
run_shell run_shell
;; ;;
"") reload)
reload_nginx
;;
upgrade)
upgrade_containers
;;
"")
show_menu show_menu
;; ;;
*) *)

View File

@@ -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` - `(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 - `restart` : Runs `docker compose down`, `sleep 1`, and `docker compose up` commands
- `start` : Runs `docker compose up` command - `start` / `up` : Runs `docker compose up` command
- `stop` : Runs `docker compose down` command - `stop` / `down` : Runs `docker compose down` command
- `ps` : Runs `watch docker ps` to monitor running containers - `ps` : Runs `watch docker ps` to monitor running containers
- `stats` : Runs `docker stats` to monitor container resource usage - `stats` : Runs `docker stats` to monitor container resource usage
- `logs` / `log` : Runs `docker logs -f <container_name>` to follow container logs - `logs` / `log` : Runs `docker logs -f <container_name>` to follow container logs
- `sh` / `bash` / `shell` : Runs `docker exec -it <container_name> bash` to access container shell - `sh` / `bash` / `shell` : Runs `docker exec -it <container_name> 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 - `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 - `--help` : Displays available tasks and usage information