aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAkhila Kishore <akhila.kishore@intel.com>2020-01-28 10:25:44 -0800
committerAkhila Kishore <akhila.kishore@intel.com>2020-02-20 14:21:23 -0800
commita0decec906ac96d6cdfa084fc9894c7c23688817 (patch)
tree8a5aa0fde70099f2eeb2df8126307b17a2a501d9
parentfc960539db54415255c710f593db4513c5e49412 (diff)
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 <akhila.kishore@intel.com> Issue-ID: MULTICLOUD-981 Change-Id: Ia6484ebb039daf0b1cb8e698fa7bfa758eeaa568
-rwxr-xr-xkud/hosting_providers/vagrant/cleanup.sh87
1 files changed, 87 insertions, 0 deletions
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 </tmp/docker-list.txt
+ rm /tmp/docker-list.txt
+ else
+ # Remove all images which are not used by existing container
+ docker image prune -a || echo "No untagged images to delete."
+fi
+
+}
+
+function _clean_ansible {
+if [ "$1" == "--nuclear" ]; then
+version=$(grep "ansible_version" ${kud_playbooks}/kud-vars.yml | \
+awk -F ': ' '{print $2}')
+sudo pip uninstall ansible==$version
+fi
+}
+
+#Defining config path
+INSTALLER_DIR="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")"
+kud_folder=${INSTALLER_DIR}
+kud_infra_folder=$kud_folder/../../deployment_infra
+kud_playbooks=$kud_infra_folder/playbooks
+
+_clean_docker $@
+_clean_ansible $1