Explorar o código

[update.sh] Do not clean up dangling items, remove old image tags (not removed by dangling=true)

andryyy %!s(int64=8) %!d(string=hai) anos
pai
achega
bdfaef09c6
Modificáronse 1 ficheiros con 48 adicións e 8 borrados
  1. 48 8
      update.sh

+ 48 - 8
update.sh

@@ -109,6 +109,17 @@ elif [[ ${MERGE_RETURN} != 0 ]]; then
 	exit 1
 fi
 
+
+echo -e "\e[32mFetching new docker-compose version...\e[0m"
+sleep 2
+if [[ $(curl -sL -w "%{http_code}" https://www.servercow.de/docker-compose/latest.php -o /dev/null) == "200" ]]; then
+	LATEST_COMPOSE=$(curl -#L https://www.servercow.de/docker-compose/latest.php)
+	curl -#L https://github.com/docker/compose/releases/download/${LATEST_COMPOSE}/docker-compose-$(uname -s)-$(uname -m) > $(which docker-compose)
+	chmod +x $(which docker-compose)
+else
+	echo -e "\e[33mCannot determine latest docker-compose version, skipping...\e[0m"
+fi
+
 echo -e "\e[32mFetching new images, if any...\e[0m"
 sleep 2
 docker-compose pull --parallel
@@ -117,19 +128,48 @@ docker-compose pull --parallel
 [[ ! -d data/assets/ssl ]] && mkdir -p data/assets/ssl
 cp -n data/assets/ssl-example/*.pem data/assets/ssl/
 
-echo -e "\e[32mFetching new docker-compose version...\e[0m"
-sleep 2
-curl -L https://github.com/docker/compose/releases/download/$(curl -Ls https://www.servercow.de/docker-compose/latest.php)/docker-compose-$(uname -s)-$(uname -m) > $(which docker-compose)
-chmod +x $(which docker-compose)
-
 echo -e "\e[32mStarting mailcow...\e[0m"
 sleep 2
 docker-compose up -d --remove-orphans
-#echo -e "\e[32mCleaning up dangling Docker objects...\e[0m"
-if [[ ! -z $(docker images -qf "dangling=true") ]]; then
-	docker rmi -f $(docker images -qf "dangling=true" -q)
+
+IMGS_TO_DELETE=()
+for container in $(grep -oP "image: \Kmailcow.+" docker-compose.yml); do
+	REPOSITORY=${container/:*}
+	TAG=${container/*:}
+	V_MAIN=${container/*.}
+	V_SUB=${container/*.}
+
+	EXISTING_TAGS=$(docker images | grep ${REPOSITORY} | awk '{ print $2 }')
+	for existing_tag in ${EXISTING_TAGS[@]}; do
+		V_MAIN_EXISTING=${existing_tag/*.}
+		V_SUB_EXISTING=${existing_tag/*.}
+		if [[ $V_MAIN_EXISTING == "latest" ]]; then
+			echo "Found deprecated label \"latest\" for repository $REPOSITORY, it should be deleted."
+			IMGS_TO_DELETE+=($REPOSITORY:$existing_tag)
+		elif [[ $V_MAIN_EXISTING -lt $V_MAIN ]]; then
+			echo "Found tag $existing_tag for $REPOSITORY, which is older than the current tag $TAG and should be deleted."
+			IMGS_TO_DELETE+=($REPOSITORY:$existing_tag)
+		elif [[ $V_SUB_EXISTING -lt $V_SUB ]]; then
+			echo "Found tag $existing_tag for $REPOSITORY, which is older than the current tag $TAG and should be deleted."
+			IMGS_TO_DELETE+=($REPOSITORY:$existing_tag)
+		fi
+	done
+done
+if [[ ! -z ${IMGS_TO_DELETE[*]} ]]; then
+	echo "Run the following command to delete unused image tags:"
+	echo
+	echo "    docker rmi ${IMGS_TO_DELETE[*]}"
+	echo
+	read -r -p "Do you want to delete old image tags right now? [Y/n] " response
+	if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
+		docker rmi ${IMGS_TO_DELETE[*]}
+	else
+		"OK, skipped."
+	fi
 fi
 
+echo -e "\e[32mIf you want to cleanup further garbage collected by Docker, please make sure all containers are up and running before cleaning your system by executing \"docker system prune\".\e[0m"
+
 #echo "In case you encounter any problem, hard-reset to a state before updating mailcow:"
 #echo
 #git reflog --color=always | grep "Before update on "