From 974c8f2c9652fff23659668078751b60b25a185c Mon Sep 17 00:00:00 2001 From: Bruno21 Date: Sat, 6 Jan 2024 19:48:21 +0100 Subject: [PATCH] Support Docker --- cleanup.sh | 100 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 89 insertions(+), 11 deletions(-) diff --git a/cleanup.sh b/cleanup.sh index 515bf63..355bfb5 100755 --- a/cleanup.sh +++ b/cleanup.sh @@ -18,18 +18,19 @@ cleanup() { #reset="\033[0m" opt_cache=true -opt_log=true -opt_adobe=true +opt_log=false +opt_adobe=false opt_chrome=false # -opt_ios_app=true -opt_ios_device=true -opt_xcode=true +opt_ios_app=false +opt_ios_device=false +opt_xcode=false opt_xcrun=true opt_brew=false opt_gem=false # ok opt_npm=false # ok +opt_docker=true opt_go=false # ok -opt_teams=true +opt_teams=false opt_dns=true opt_memory=true opt_periodic=true @@ -224,6 +225,7 @@ echo -e "${RED}System cache: $count_section${NOFORMAT}" else msg "${BLUE}\nApprox $(bytesToHuman $count_section) of space will be cleaned up${NOFORMAT}\n" unset path_list + totalFree="$((totalFree+count_section))" fi echo "totalFree $totalFree" @@ -253,6 +255,7 @@ echo -e "${RED}System log: $count_section${NOFORMAT}" else msg "${BLUE}\nApprox $(bytesToHuman $count_section) of space will be cleaned up${NOFORMAT}\n" unset path_list + totalFree="$((totalFree+count_section))" fi echo "totalFree $totalFree" @@ -274,6 +277,7 @@ echo -e "${RED}Adobe: $count_section${NOFORMAT}" else msg "${BLUE}\nApprox $(bytesToHuman $count_section) of space will be cleaned up${NOFORMAT}\n" unset path_list + totalFree="$((totalFree+count_section))" fi fi @@ -297,6 +301,7 @@ echo -e "${RED}Chrome: $count_section${NOFORMAT}" else msg "${BLUE}\nApprox $(bytesToHuman $count_section) of space will be cleaned up${NOFORMAT}\n" unset path_list + totalFree="$((totalFree+count_section))" fi fi fi @@ -318,6 +323,7 @@ echo -e "${RED}iOS app: $count_section${NOFORMAT}" else msg "${BLUE}\nApprox $(bytesToHuman $count_section) of space will be cleaned up${NOFORMAT}\n" unset path_list + totalFree="$((totalFree+count_section))" fi fi @@ -341,6 +347,7 @@ echo -e "${RED}iOS device: $count_section${NOFORMAT}" else msg "${BLUE}\nApprox $(bytesToHuman $count_section) of space will be cleaned up${NOFORMAT}\n" unset path_list + totalFree="$((totalFree+count_section))" fi echo "totalFree $totalFree" @@ -367,6 +374,7 @@ echo -e "${RED}xcode: $count_section${NOFORMAT}" else msg "${BLUE}\nApprox $(bytesToHuman $count_section) of space will be cleaned up${NOFORMAT}\n" unset path_list + totalFree="$((totalFree+count_section))" fi fi @@ -402,10 +410,13 @@ if [[ $opt_xcrun = true ]]; then collect_paths /Users/wah/Library/Developer/CoreSimulator/Devices/*/data/Library/!(PreferencesCaches|Caches|AddressBook) collect_paths ~/Library/Developer/CoreSimulator/Devices/*/data/Library/Caches/* collect_paths ~/Library/Developer/CoreSimulator/Devices/*/data/Library/AddressBook/AddressBook* - echo ${path_list[@]} + count_section=$(count_dry) + [ -z $count_section ] && count_section=0 + echo -e "${RED}xcrcun: $count_section${NOFORMAT}" unset path_list + totalFree="$((totalFree+count_section))" fi fi fi @@ -451,6 +462,8 @@ if [[ $opt_brew = true ]]; then brew autoremove --dry-run + # totalFree="$((totalFree+count_section))" # free + fi unset path_list fi @@ -518,7 +531,68 @@ echo -e "${RED}TOTAL: $totalFree${NOFORMAT}" fi -# Docker + +### Docker ### + +#docker system prune: +# remove: +# - all stopped containers +# - all networks not used by at least one container +# - all dangling images +# - all dangling build cache +# +# -a Remove +# - all stopped containers +# - all networks not used by at least one container +# - all images without at least one container associated to them +# - all build cache +# -f Do not prompt for confirmation + + +#docker volume prune: +# remove anonymous local volumes not used by at least one container. +# -a remove all local volumes not used by at least one container. +# -f Do not prompt for confirmation + +# docker container prune +# remove all stopped containers. +# -f Do not prompt for confirmation + +# ~/Library/Containers/com.docker.docker/Data/vms/0/data/Docker.raw => 60Go + +# https://docs.docker.com/desktop/faqs/macfaqs/ + +if [[ $opt_docker = true ]]; then + + if type "docker" &>/dev/null; then + msg "\n${BOLD}Cleaning Docker...${NOFORMAT}" + + vm_size=$(($(/usr/bin/stat -f%z ~/Library/Containers/com.docker.docker/Data/vms/0/data/Docker.raw) / 1000)) # 1000->38.14 GiB; 1024->37.25 GiB + # 40 000 028 672 octets (2,53 Go sur disque) + + msg "\nOn macOS, Docker Desktop stores Linux containers and images in a single, large disk image file in the Mac filesystem (located à ~/Library/Containers/com.docker.docker/Data/vms/0/data/Docker.raw)(size=$(bytesToHuman $vm_size))." + msg "\nhttps://docs.docker.com/desktop/faqs/macfaqs/" + msg "\n${RED}Delete ou prune some images and container will free up space on this large disk image, not on the Mac filesystem.${NOFORMAT}" + + if [ -z "$dry_run" ]; then + if ! docker ps >/dev/null 2>&1; then + close_docker=true + echo "Docker closed" + open --background -a Docker + fi + msg '\nCleaning up Docker' + echo -e "Running ${ITALIC}docker system prune -af${NOFORMAT}" + docker_prune=$(docker system prune -af) # &>/dev/null + docker_free=$(echo "$docker_prune" | grep 'Total reclaimed space' | awk -F"=" '{print $2}' | xargs) + msg "${BLUE}\nApprox $docker_free of space has been cleaned up${NOFORMAT}." + + if [ "$close_docker" = true ]; then + killall Docker + fi + fi + fi +fi + # Pyenv @@ -638,9 +712,10 @@ echo -e "${RED}teams: $count_section${NOFORMAT}" else msg "${BLUE}\nApprox $(bytesToHuman $count_section) of space will be cleaned up${NOFORMAT}" unset path_list + totalFree="$((totalFree+count_section))" fi - totalFree="$((totalFree+count_section))" + echo -e "${RED}TOTAL: $totalFree${NOFORMAT}" fi fi @@ -681,7 +756,10 @@ fi # End msg "${GREEN}\nSuccess!${NOFORMAT}" -msg "${BLUE}\nApprox $(bytesToHuman $totalFree) of space has been cleaned up${NOFORMAT}" - +if [ -z "$dry_run" ]; then + msg "${BLUE}\nApprox $(bytesToHuman $totalFree) of space has been cleaned up${NOFORMAT}" +else + msg "${BLUE}\nApprox $(bytesToHuman $totalFree) of space will be cleaned up${NOFORMAT}" +fi newAvailable=$(df / | tail -1 | awk '{print $4}') msg "${BLUE}$(bytesToHuman $newAvailable) available on /${NOFORMAT}"