From a0decec906ac96d6cdfa084fc9894c7c23688817 Mon Sep 17 00:00:00 2001 From: Akhila Kishore Date: Tue, 28 Jan 2020 10:25:44 -0800 Subject: KuD cleanup script. This script offers means to cleanup, Docker and Ansible and their configurations. Updated the script to use docker go templating as opposed to regex for docker operations. Further modified the script to include if/else for docker operations for better code readability. Signed-off-by: Akhila Kishore Issue-ID: MULTICLOUD-981 Change-Id: Ia6484ebb039daf0b1cb8e698fa7bfa758eeaa568 --- kud/hosting_providers/vagrant/cleanup.sh | 87 ++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100755 kud/hosting_providers/vagrant/cleanup.sh (limited to 'kud') diff --git a/kud/hosting_providers/vagrant/cleanup.sh b/kud/hosting_providers/vagrant/cleanup.sh new file mode 100755 index 00000000..75e6de11 --- /dev/null +++ b/kud/hosting_providers/vagrant/cleanup.sh @@ -0,0 +1,87 @@ +#!/bin/bash + +# options: +# remove stopped containers and untagged images +# $ ./cleanup +# remove all stopped|running containers and untagged images +# $ ./cleanup --reset +# remove containers|images| matching {repository:tag} +# pattern and untagged images +# $ ./cleanup --purge {image} +# everything +# $ ./cleanup --nuclear + +function _clean_docker { +local matchExp="" +if [ "$1" == "--reset" ]; then + # Remove all containers regardless of state + docker rm -vf $(docker ps -a -q) 2>/dev/null || \ + echo "No more containers to remove." + exit 0 +elif [ "$1" == "--purge" ]; then + if [ -z "$2" ]; then + echo "Cannot purge. Please provide image name to purge." + exit 0 + fi + matchExp=$2 + # Attempt to remove running containers that are using the images we're trying to purge first. + if [[ $(docker ps --filter "name=$matchExp") ]]; then + echo "Removing running containers using the \"$2\" container image" + docker rm -vf $(docker ps -a --format "{{.Image}} {{.Names}}" | \ + awk '$0 ~ matchExp {print $2}') 2>/dev/null + else + echo "No running containers using the \"$2\" container image" + fi + echo "Continue to purge container images..." + # Remove all images matching arg given after "--purge"` + docker images -q --format "{{.Repository}}:{{.Tag}}" | grep "$matchExp" + returnVal=$? + if [ $returnVal -ne 0 ]; then + echo "No \"$2\" container image found." + exit 0 + else + echo "Removing all the \"$2\" container images." + docker images --format "{{.Repository}}:{{.Tag}}" | grep "$matchExp" |\ + awk '$0 ~ matchExp {print $1}' | xargs docker rmi 2>/dev/null + exit 0 + fi +else + # This alternate only removes "stopped" containers + docker rm -vf $(docker ps -a | grep "Exited" | \ + awk '{print $2}') 2>/dev/null || echo "No stopped containers to remove." +fi + +if [ "$1" == "--nuclear" ]; then + docker rm -vf $(docker ps -a -q) 2>/dev/null || \ + echo "No more containers to remove." + docker rmi $(docker images -q) 2>/dev/null || \ + echo "No more images to remove." + echo "Preparing to uninstall docker ....." + dpkg -l | grep Docker | awk '{print $2}' > /tmp/docker-list.txt + while read list; do + sudo apt-get remove $list -y + done