Created
March 10, 2017 17:45
-
-
Save eriknelson/d409daf5f76746fe3c72bdc52753194a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
vagrantDir=/home/ernelson/cluster/centos-origin-cluster | |
vagrantHosts=(master node1 node2) | |
searchTerm=$1 | |
if [[ "$searchTerm" == "" ]]; then | |
echo "ERROR: Must provide search term." | |
exit 1 | |
fi | |
pushd $vagrantDir | |
for host in "${vagrantHosts[@]}"; do | |
echo "============================================================" | |
echo "Cleaning [ $host ]" | |
echo "============================================================" | |
containerCmd="\"docker ps -a | grep $searchTerm\"" | |
fullCmd="vagrant ssh $host --command $containerCmd" | |
readarray -t myArray < <(eval "$fullCmd" | awk '{print $1}') | |
rmCmd="docker rm" | |
originalCmd=$rmCmd | |
for line in "${myArray[@]}"; do | |
rmCmd="$rmCmd $line" | |
done | |
if ! [[ "$rmCmd" == "$originalCmd" ]]; then | |
echo "Removing old containers on $host..." | |
vagrant ssh $host -c "$rmCmd" | |
else | |
echo "No matching containers on $host, skipping..." | |
fi | |
readarray -t matchingImages < \ | |
<(vagrant ssh $host -c "docker images | grep $searchTerm" | awk '{print $3}') | |
for imageId in "${matchingImages[@]}"; do | |
echo "Image: $imageId" | |
done | |
rmCmd="docker rmi" | |
originalCmd=$rmCmd | |
for imgId in "${matchingImages[@]}"; do | |
rmCmd="$rmCmd $imgId" | |
done | |
if ! [[ "$rmCmd" == "$originalCmd" ]]; then | |
echo "Removing old images on $host..." | |
vagrant ssh $host -c "$rmCmd" | |
else | |
echo "No matching images on $host, skipping..." | |
fi | |
done | |
popd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clean_cluster.sh external # will clean any containers/images containing "external" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment