Skip to content

Commit

Permalink
Update script
Browse files Browse the repository at this point in the history
Signed-off-by: larzie <[email protected]>
  • Loading branch information
larziwau authored Dec 14, 2024
1 parent 90dbd0a commit 8bcc2fe
Showing 1 changed file with 48 additions and 55 deletions.
103 changes: 48 additions & 55 deletions src/ci/free-disk-space.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
#!/bin/bash

set -e

fast_rmdir() {
cd "$@" || exit
pwd
ls
perl -e 'Unlink for glob "*"'
}

list_installed_dpkg() {
dpkg --get-selections "$@" | grep -v deinstall | awk '{print $1}'
}

printSeparationLine() {
str=${1:=}
num=${2:-80}
counter=1
output=""
while [ $counter -le "$num" ]; do
output="${output}${str}"
counter=$((counter + 1))
counter=$((counter+1))
done
echo "${output}"
}

getAvailableSpace() {
echo $(df -a "$1" | awk 'NR > 1 {avail+=$4} END {print avail}')
getAvailableSpace() {
echo $(df -a $1 | awk 'NR > 1 {avail+=$4} END {print avail}')
}

formatByteCount() {
echo $(numfmt --to=iec-i --suffix=B --padding=7 "$1"000)
formatByteCount() {
echo $(numfmt --to=iec-i --suffix=B --padding=7 $1'000')
}

printSavedSpace() {
Expand Down Expand Up @@ -74,93 +61,98 @@ echo ""

BEFORE=$(getAvailableSpace)

fast_rmdir "$AGENT_TOOLSDIRECTORY" || true
sudo rm -rf "$AGENT_TOOLSDIRECTORY" || true

AFTER=$(getAvailableSpace)
SAVED=$((AFTER - BEFORE))
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Agent tools"

BEFORE=$(getAvailableSpace)
fast_rmdir /usr/local/share/powershell || true
sudo rm -rf /usr/local/share/powershell || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER - BEFORE))
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Powershell"

BEFORE=$(getAvailableSpace)
fast_rmdir /usr/local/share/chromium || true
sudo rm -rf /usr/local/share/chromium || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER - BEFORE))
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Chromium"

BEFORE=$(getAvailableSpace)
fast_rmdir /usr/local/lib/node_modules || true
sudo rm -rf /usr/local/lib/node_modules || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER - BEFORE))
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Node modules"

BEFORE=$(getAvailableSpace)
fast_rmdir /usr/share/swift || true
sudo rm -rf /usr/share/swift || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER - BEFORE))
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Swift"

BEFORE=$(getAvailableSpace)
fast_rmdir /usr/local/lib/android || true
sudo rm -rf /usr/local/lib/android || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER - BEFORE))
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Android library"

BEFORE=$(getAvailableSpace)
fast_rmdir /usr/share/dotnet || true
sudo rm -rf /usr/share/dotnet || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER - BEFORE))
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED ".NET runtime"

BEFORE=$(getAvailableSpace)
fast_rmdir /opt/ghc || true
fast_rmdir /usr/local/.ghcup || true
sudo rm -rf /opt/ghc || true
sudo rm -rf /usr/local/.ghcup || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER - BEFORE))
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Haskell runtime"

BEFORE=$(getAvailableSpace)

pkgs=$(list_installed_dpkg 'microsoft-edge-*' 'snapd-*' 'aspnetcore-*' 'dotnet-*' 'llvm-*' '*php*' 'mongodb-*' 'mysql-*' azure-cli google-chrome-stable firefox powershell mono-devel libgl1-mesa-dri 'google-cloud-*' 'gcloud-*' || true)

gcloud_prerm='#!/bin/sh
echo $0
if [ -d "/usr/lib/google-cloud-sdk" ]; then
echo "Cleaning Google Cloud CLI files..."
find /usr/lib/google-cloud-sdk -type f -delete -print | wc -l
rm -rf /usr/lib/google-cloud-sdk
echo "Cleaning Google Cloud CLI manuals..."
find /usr/share/man -type f -name "gcloud*" -delete -print | wc -l
fi'
echo "$gcloud_prerm" | sudo tee /var/lib/dpkg/info/google-cloud-cli-anthoscli.prerm >/dev/null
echo "$gcloud_prerm" | sudo tee /var/lib/dpkg/info/google-cloud-cli.prerm >/dev/null
sudo apt-get remove --autoremove -y $pkgs || echo "::warning::The command [sudo apt-get remove -y] failed to complete successfully. Proceeding..."
sudo apt-get remove -y '^aspnetcore-.*' || echo "::warning::The command [sudo apt-get remove -y '^aspnetcore-.*'] failed."
sudo apt-get remove -y '^dotnet-.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y '^dotnet-.*' --fix-missing] failed."
sudo apt-get remove -y '^llvm-.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y '^llvm-.*' --fix-missing] failed."
sudo apt-get remove -y 'php.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y 'php.*' --fix-missing] failed."
sudo apt-get remove -y '^mongodb-.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y '^mongodb-.*' --fix-missing] failed."
sudo apt-get remove -y '^mysql-.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y '^mysql-.*' --fix-missing] failed."
sudo apt-get remove -y azure-cli --fix-missing || echo "::warning::The command [sudo apt-get remove -y azure-cli --fix-missing] failed."
sudo apt-get remove -y google-cloud-sdk --fix-missing || echo "::warning::The command [sudo apt-get remove -y google-chrome-stable --fix-missing] failed."
sudo apt-get remove -y firefox --fix-missing || echo "::warning::The command [sudo apt-get remove -y firefox --fix-missing] failed."
sudo apt-get remove -y powershell --fix-missing || echo "::warning::The command [sudo apt-get remove -y powershell --fix-missing] failed."
sudo apt-get remove -y mono-devel --fix-missing || echo "::warning::The command [sudo apt-get remove -y mono-devel --fix-missing] failed."
sudo apt-get remove -y libgl1-mesa-dri --fix-missing || echo "::warning::The command [sudo apt-get remove -y libgl1-mesa-dri --fix-missing] failed."
sudo apt-get remove -y google-cloud-sdk --fix-missing || echo "::debug::The command [sudo apt-get remove -y google-cloud-sdk --fix-missing] failed."
sudo apt-get remove -y google-cloud-cli --fix-missing || echo "::debug::The command [sudo apt-get remove -y google-cloud-cli --fix-missing] failed."
sudo apt-get remove -y microsoft-edge-stable --fix-missing || echo "::debug::The command [sudo apt-get remove -y microsoft-edge-stable --fix-missing] failed."
sudo apt-get remove -y snapd --fix-missing || echo "::debug::The command [sudo apt-get remove -y snapd --fix-missing] failed."
sudo apt-get autoremove -y || echo "::warning::The command [sudo apt-get autoremove -y] failed."
sudo apt-get clean || echo "::warning::The command [sudo apt-get clean] failed."

AFTER=$(getAvailableSpace)
SAVED=$((AFTER - BEFORE))
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Large misc. packages"

BEFORE=$(getAvailableSpace)
sudo docker image prune --all --force || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER - BEFORE))
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Docker images"

BEFORE=$(getAvailableSpace)
sudo rm -rf "$AGENT_TOOLSDIRECTORY" || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER - BEFORE))
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Tool cache"

BEFORE=$(getAvailableSpace)
sudo swapoff -a || true
sudo rm -f /mnt/swapfile || true
sudo rm -rf /mnt/swapfile || true
free -h
AFTER=$(getAvailableSpace)
SAVED=$((AFTER - BEFORE))
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Swap storage"

AVAILABLE_END=$(getAvailableSpace)
Expand All @@ -169,7 +161,8 @@ AVAILABLE_ROOT_END=$(getAvailableSpace '/')
echo ""
printDH "After clean-up:"

echo ""
echo "/dev/root:"
printSavedSpace $((AVAILABLE_ROOT_END - AVAILABLE_ROOT_INITIAL))
echo "overall:"
echo "Overall:"
printSavedSpace $((AVAILABLE_END - AVAILABLE_INITIAL))

0 comments on commit 8bcc2fe

Please sign in to comment.