update_docker_stacks.sh
· 2.0 KiB · Bash
原始文件
#!/bin/bash
set -euo pipefail
# Regex for valid docker compose file names
COMPOSE_REGEX='^(docker-compose|compose)\.ya?ml$'
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Dry-run mode
DRY_RUN=0
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--dry-run)
DRY_RUN=1
shift
;;
*)
ROOT_DIR="$1"
shift
;;
esac
done
# Prompt for directory if not provided
if [[ -z "${ROOT_DIR:-}" ]]; then
echo -e "${YELLOW}No root directory provided.${NC}"
read -e -p "Enter the root directory to search: " ROOT_DIR
fi
# Validate directory
if [[ ! -d "$ROOT_DIR" ]]; then
echo -e "${RED}Error: '$ROOT_DIR' is not a valid directory.${NC}"
exit 1
fi
echo -e "${GREEN}Searching for docker-compose files in: $ROOT_DIR${NC}"
processed=()
skipped=()
find "$ROOT_DIR" -maxdepth 1 -type d -print0 | while IFS= read -r -d $'\0' dir; do
# Skip the root directory itself if you want only subdirs
# [[ "$dir" == "$ROOT_DIR" ]] && continue
compose_file=""
for file in "$dir"/*; do
filename=$(basename "$file")
if [[ $filename =~ $COMPOSE_REGEX ]]; then
compose_file="$file"
break
fi
done
if [[ -n "$compose_file" ]]; then
echo -e "${GREEN}Processing: $dir ($compose_file)${NC}"
processed+=("$dir")
if [[ $DRY_RUN -eq 0 ]]; then
(
cd "$dir" || exit 1
docker compose -f "$compose_file" pull && docker compose -f "$compose_file" up -d
)
else
echo -e "${YELLOW}[Dry-run] Would run: docker compose -f \"$compose_file\" pull && docker compose -f \"$compose_file\" up -d${NC}"
fi
else
echo -e "${YELLOW}No compose file found in: $dir${NC}"
skipped+=("$dir")
fi
done
echo -e "\n${GREEN}Completed processing all subdirectories.${NC}"
echo -e "${GREEN}Processed directories:${NC}"
for d in "${processed[@]}"; do
echo " $d"
done
if [[ ${#skipped[@]} -gt 0 ]]; then
echo -e "${YELLOW}Skipped directories (no compose file found):${NC}"
for d in "${skipped[@]}"; do
echo " $d"
done
fi
| 1 | #!/bin/bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | |
| 5 | # Regex for valid docker compose file names |
| 6 | COMPOSE_REGEX='^(docker-compose|compose)\.ya?ml$' |
| 7 | |
| 8 | # Colors for output |
| 9 | RED='\033[0;31m' |
| 10 | GREEN='\033[0;32m' |
| 11 | YELLOW='\033[1;33m' |
| 12 | NC='\033[0m' # No Color |
| 13 | |
| 14 | # Dry-run mode |
| 15 | DRY_RUN=0 |
| 16 | |
| 17 | # Parse arguments |
| 18 | while [[ $# -gt 0 ]]; do |
| 19 | case "$1" in |
| 20 | --dry-run) |
| 21 | DRY_RUN=1 |
| 22 | shift |
| 23 | ;; |
| 24 | *) |
| 25 | ROOT_DIR="$1" |
| 26 | shift |
| 27 | ;; |
| 28 | esac |
| 29 | done |
| 30 | |
| 31 | # Prompt for directory if not provided |
| 32 | if [[ -z "${ROOT_DIR:-}" ]]; then |
| 33 | echo -e "${YELLOW}No root directory provided.${NC}" |
| 34 | read -e -p "Enter the root directory to search: " ROOT_DIR |
| 35 | fi |
| 36 | |
| 37 | # Validate directory |
| 38 | if [[ ! -d "$ROOT_DIR" ]]; then |
| 39 | echo -e "${RED}Error: '$ROOT_DIR' is not a valid directory.${NC}" |
| 40 | exit 1 |
| 41 | fi |
| 42 | |
| 43 | echo -e "${GREEN}Searching for docker-compose files in: $ROOT_DIR${NC}" |
| 44 | |
| 45 | processed=() |
| 46 | skipped=() |
| 47 | |
| 48 | find "$ROOT_DIR" -maxdepth 1 -type d -print0 | while IFS= read -r -d $'\0' dir; do |
| 49 | # Skip the root directory itself if you want only subdirs |
| 50 | # [[ "$dir" == "$ROOT_DIR" ]] && continue |
| 51 | |
| 52 | compose_file="" |
| 53 | for file in "$dir"/*; do |
| 54 | filename=$(basename "$file") |
| 55 | if [[ $filename =~ $COMPOSE_REGEX ]]; then |
| 56 | compose_file="$file" |
| 57 | break |
| 58 | fi |
| 59 | done |
| 60 | |
| 61 | if [[ -n "$compose_file" ]]; then |
| 62 | echo -e "${GREEN}Processing: $dir ($compose_file)${NC}" |
| 63 | processed+=("$dir") |
| 64 | if [[ $DRY_RUN -eq 0 ]]; then |
| 65 | ( |
| 66 | cd "$dir" || exit 1 |
| 67 | docker compose -f "$compose_file" pull && docker compose -f "$compose_file" up -d |
| 68 | ) |
| 69 | else |
| 70 | echo -e "${YELLOW}[Dry-run] Would run: docker compose -f \"$compose_file\" pull && docker compose -f \"$compose_file\" up -d${NC}" |
| 71 | fi |
| 72 | else |
| 73 | echo -e "${YELLOW}No compose file found in: $dir${NC}" |
| 74 | skipped+=("$dir") |
| 75 | fi |
| 76 | done |
| 77 | |
| 78 | echo -e "\n${GREEN}Completed processing all subdirectories.${NC}" |
| 79 | echo -e "${GREEN}Processed directories:${NC}" |
| 80 | for d in "${processed[@]}"; do |
| 81 | echo " $d" |
| 82 | done |
| 83 | |
| 84 | if [[ ${#skipped[@]} -gt 0 ]]; then |
| 85 | echo -e "${YELLOW}Skipped directories (no compose file found):${NC}" |
| 86 | for d in "${skipped[@]}"; do |
| 87 | echo " $d" |
| 88 | done |
| 89 | fi |