This commit is contained in:
pika 2025-04-21 14:48:12 +02:00
parent 2bd2592970
commit 61d7de93d1

179
.zshrc
View file

@ -173,81 +173,124 @@ __docker__() {
alias dcs="docker compose ps -a --format 'table {{.Name}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'"
alias dl="docker compose logs -f"
alias dc="docker compose"
check_for_updates() {
# check_for_updates() {
# if ! command -v jq &>/dev/null; then
# echo -e "${RED}Error: jq is required but not installed. Please install jq.${NC}"
# return 1
# fi
#
# if ! docker compose version &>/dev/null; then
# echo -e "${RED}Error: docker compose is not available.${NC}"
# return 1
# fi
#
# local updated=false
# local images
# images=$(docker compose config --format json | jq -r '.services[] | .image' 2>/dev/null)
#
# if [[ -z "$images" ]]; then
# echo -e "${RED}Error: No Docker images found in the compose configuration.${NC}"
# return 1
# fi
#
# for image in $images; do
# echo -e "${CYAN}Checking for updates for image: ${YELLOW}$image${NC}"
#
# # Get local image digest
# local local_digest
# local_digest=$(docker image inspect "$image" --format '{{index .RepoDigests 0}}') # 2>/dev/null
#
# # If no local digest exists, force check
# if [[ -z "$local_digest" ]]; then
# echo -e "${YELLOW}Image not present locally. Update needed.${NC}"
# updated=true
# continue
# fi
#
# # Get remote digest using pull --dry-run
# local remote_digest
# remote_digest=$(docker pull --quiet "$image" 2>/dev/null | awk '/Digest:/{print $2}')
#
# # Fallback to manifest inspect if dry-run fails
# if [[ -z "$remote_digest" ]]; then
# remote_digest=$(DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect -v "$image" | jq -r '.Descriptor.digest')
# fi
#
# if [[ -z "$remote_digest" ]]; then
# echo -e "${RED}Failed to retrieve remote digest. Performing forced check...${NC}"
# # Fallback method: Pull image and compare before/after digests
# local pre_pull_digest=$(docker image inspect "$image" --format '{{index .RepoDigests 0}}')
# docker pull --quiet "$image" >/dev/null
# local post_pull_digest=$(docker image inspect "$image" --format '{{index .RepoDigests 0}}')
#
# if [[ "$pre_pull_digest" != "$post_pull_digest" ]]; then
# echo -e "${GREEN}Update found during forced pull${NC}"
# updated=true
# fi
# continue
# fi
#
# # Compare digests
# if [[ "$local_digest" != "$remote_digest" ]]; then
# echo -e "${GREEN}Update available for $image${NC}"
# updated=true
# else
# echo -e "${YELLOW}No update available for $image${NC}"
# fi
# done
#
# if $updated; then
# echo -e "${CYAN}Pulling updates and recreating containers...${NC}"
# docker compose pull --quiet && docker compose up -d --force-recreate
# else
# echo -e "${GREEN}All images are up to date. No action needed.${NC}"
# fi
# }
# Check for required dependencies
if ! command -v jq &>/dev/null; then
echo -e "${RED}Error: jq is required but not installed. Please install jq.${NC}"
return 1
fi
if ! docker compose version &>/dev/null; then
echo -e "${RED}Error: docker compose is not available.${NC}"
return 1
fi
local updated=false
local images
images=$(docker compose config --format json | jq -r '.services[] | .image' 2>/dev/null)
if [[ -z "$images" ]]; then
echo -e "${RED}Error: No Docker images found in the compose configuration.${NC}"
return 1
fi
for image in $images; do
echo -e "${CYAN}Checking for updates for image: ${YELLOW}$image${NC}"
# Get local image digest
local local_digest
local_digest=$(docker image inspect "$image" --format '{{index .RepoDigests 0}}') # 2>/dev/null
# If no local digest exists, force check
if [[ -z "$local_digest" ]]; then
echo -e "${YELLOW}Image not present locally. Update needed.${NC}"
updated=true
continue
fi
# Get remote digest using pull --dry-run
local remote_digest
remote_digest=$(docker pull --quiet "$image" 2>/dev/null | awk '/Digest:/{print $2}')
# Fallback to manifest inspect if dry-run fails
if [[ -z "$remote_digest" ]]; then
remote_digest=$(DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect -v "$image" | jq -r '.Descriptor.digest')
fi
if [[ -z "$remote_digest" ]]; then
echo -e "${RED}Failed to retrieve remote digest. Performing forced check...${NC}"
# Fallback method: Pull image and compare before/after digests
local pre_pull_digest=$(docker image inspect "$image" --format '{{index .RepoDigests 0}}')
docker pull --quiet "$image" >/dev/null
local post_pull_digest=$(docker image inspect "$image" --format '{{index .RepoDigests 0}}')
if [[ "$pre_pull_digest" != "$post_pull_digest" ]]; then
echo -e "${GREEN}Update found during forced pull${NC}"
updated=true
fi
continue
fi
# Compare digests
if [[ "$local_digest" != "$remote_digest" ]]; then
echo -e "${GREEN}Update available for $image${NC}"
updated=true
check_for_updates() {
local compose_cmd
# Determine available compose command
if command -v docker-compose &>/dev/null; then
compose_cmd="docker-compose"
elif docker compose version &>/dev/null; then
compose_cmd="docker compose"
else
echo -e "${YELLOW}No update available for $image${NC}"
echo "Error: docker-compose or docker compose not found."
return 1
fi
done
if $updated; then
echo -e "${CYAN}Pulling updates and recreating containers...${NC}"
docker compose pull --quiet && docker compose up -d --force-recreate
# Check for compose file
if [ ! -f docker-compose.yml ] && [ ! -f docker-compose.yaml ] && [ ! -f compose.yml ]; then
echo "Error: No docker-compose.yml/yaml found in current directory."
return 1
fi
# Pull images and capture output
local pull_output
if ! pull_output=$(LC_ALL=C $compose_cmd pull 2>&1); then
echo "Error pulling images:"
echo "$pull_output"
return 1
fi
# Check for updated images
local updated=0
if echo "$pull_output" | grep -q "Downloaded newer image"; then
updated=1
fi
# Update containers if needed
if [ $updated -eq 1 ]; then
echo "Updates found. Updating containers..."
$compose_cmd up -d
echo "Cleaning up old images..."
docker system prune -f
echo "Update completed."
else
echo -e "${GREEN}All images are up to date. No action needed.${NC}"
echo "All containers are up to date. Current versions:"
$compose_cmd images | awk '{if(NR>1) print $2, $3, $4}'
fi
}
alias appupdate="check_for_updates"
if ! command_exists gmd; then