Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amend linux cleanup script with full minikube, docker and kvm cleanup #9726

Merged
merged 4 commits into from
Dec 11, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 72 additions & 6 deletions hack/jenkins/cron/cleanup_and_reboot_Linux.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

# Copyright 2019 The Kubernetes Authors All rights reserved.
# Copyright 2020 The Kubernetes Authors All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,11 +36,77 @@ logger "cleanup_and_reboot is happening!"
# kill jenkins to avoid an incoming request
killall java

# clean minikube left overs
echo -e "\ncleanup minikube..."
killall minikube >/dev/null 2>&1 || true
priyawadhwa marked this conversation as resolved.
Show resolved Hide resolved
USERS="$(lslogins --user-accs --noheadings --output=USER)"
for user in $USERS; do
if sudo su - $user -c "minikube delete --all --purge" >/dev/null 2>&1; then
echo "successfully cleaned up minikube for $user user"
fi
done

# clean docker left overs
docker rm -f -v $(docker ps -aq) >/dev/null 2>&1 || true
docker volume prune -f || true
docker volume ls || true
docker system df || true
echo -e "\ncleanup docker..."
docker kill $(docker ps -aq) >/dev/null 2>&1 || true
docker system prune --volumes --force || true

# clean KVM left overs
echo -e "\ncleanup kvm..."
overview() {
echo -e "\n - KVM domains:"
sudo virsh list --all || true
echo " - KVM pools:"
sudo virsh pool-list --all || true
echo " - KVM networks:"
sudo virsh net-list --all || true
echo " - host networks:"
sudo ip link show || true
}
echo -e "\nbefore the cleanup:"
overview
for DOM in $( sudo virsh list --all --name ); do
if sudo virsh destroy "${DOM}"; then
if sudo virsh undefine "${DOM}"; then
echo "successfully deleted KVM domain:" "${DOM}"
continue
fi
echo "unable to delete KVM domain:" "${DOM}"
fi
done
#for POOL in $( sudo virsh pool-list --all --name ); do # better, but flag '--name' is not supported for 'virsh pool-list' command on older libvirt versions
for POOL in $( sudo virsh pool-list --all | awk 'NR>2 {print $1}' ); do
for VOL in $( sudo virsh vol-list "${POOL}" ); do
if sudo virsh vol-delete --pool "${POOL}" "${VOLUME}"; then # flag '--delete-snapshots': "delete snapshots associated with volume (must be supported by storage driver)"
echo "successfully deleted KVM pool/volume:" "${POOL}"/"${VOL}"
continue
fi
echo "unable to delete KVM pool/volume:" "${POOL}"/"${VOL}"
done
done
for NET in $( sudo virsh net-list --all --name ); do
if [ "${NET}" != "default" ]; then
if sudo virsh net-destroy "${NET}"; then
if sudo virsh net-undefine "${NET}"; then
echo "successfully deleted KVM network" "${NET}"
continue
fi
fi
echo "unable to delete KVM network" "${NET}"
fi
done
# DEFAULT_BRIDGE is a bridge connected to the 'default' KVM network
DEFAULT_BRIDGE=$( sudo virsh net-info default | awk '{ if ($1 == "Bridge:") print $2 }' )
echo "bridge connected to the 'default' KVM network to leave alone:" "${DEFAULT_BRIDGE}"
for VIF in $( sudo ip link show | awk -v defvbr="${DEFAULT_BRIDGE}.*" -F': ' '$2 !~ defvbr { if ($2 ~ /virbr.*/ || $2 ~ /vnet.*/) print $2 }' ); do
if sudo ip link delete "${VIF}"; then
echo "successfully deleted KVM interface" "${VIF}"
continue
fi
echo "unable to delete KVM interface" "${VIF}"
done
echo -e "\nafter the cleanup:"
overview

# Linux-specific cleanup

Expand All @@ -51,4 +117,4 @@ systemctl list-unit-files --state=enabled \
| xargs systemctl disable

# update and reboot
apt update -y && apt upgrade -y && reboot
apt update -y && apt upgrade -y && apt-get autoclean && reboot