Created
July 10, 2015 20:00
-
-
Save pr3d4t0r/fc129c19bc509a97bb74 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# Copyright (c) 2014-2015 by Cosmify, Inc. All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without modification, | |
# are permitted provided that the following conditions are met: | |
# | |
# Redistributions of source code must retain the above copyright notice, this list | |
# of conditions and the following disclaimer. Redistributions in binary form must | |
# reproduce the above copyright notice, this list of conditions and the following | |
# disclaimer in the documentation and/or other materials provided with the | |
# distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND | |
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, | |
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY | |
# OF SUCH DAMAGE. | |
# *** Symbolic constants *** | |
B2D_APP="/Applications/boot2docker.app" | |
B2D_BIN="/usr/local/bin/boot2docker" | |
COSMIFY_DOCUMENTS_DIR="$HOME/Documents/cosmify-data" | |
# *** functions *** | |
function die { | |
printf "fatal: %s\n\n" "$1" 1>&2 | |
exit "$2" | |
} # die | |
function warn { | |
printf "warning: %s\n" "$1" 1>&2 | |
} # warn | |
function assertValidHostOS { | |
[[ "Linux" == $(uname) ]] && die "this process is intended only for OS X and Windows execution" 1 | |
[[ "Darwin" == $(uname) ]] || die "Windows support is not implemented yet" 4 | |
} # assertValidHostOS | |
function b2dDetection { | |
[[ -e "$B2D_BIN" ]] || die "$B2D_BIN not found; nothing to do" 2 | |
[[ -e "$B2D_APP" ]] || die "$B2D_APP not found; nothing to do" 3 | |
} # b2dDetection | |
function b2dMustBeRunning { | |
local status=$("$B2D_BIN" status) | |
if [[ $("$B2D_BIN" status) != "running" ]] | |
then | |
echo "$B2D_BIN must be running before nuking the environment to validate the set up." | |
"$B2D_BIN" up | |
fi | |
} # b2dMustBeRunning | |
function sudoNotification { | |
clear | |
cat << EOF | |
******************************* | |
* DANGER, DANGER WILL ROBINSON! * | |
******************************* | |
+=============================================+ | |
| nukeb2d - Nuke boot2docker from your system | | |
| | | |
| (c) Copyright 2015 by Cosmify, Inc. | | |
+=============================================+ | |
This process will remove boot2docker, the docker tools, and any docker | |
images that might be installed in your home directory. | |
The system will ask for an administrator password after it starts. | |
This is normal. Please provide the password when prompted. | |
All running containers will terminate before deletion to prevent loss | |
of data stored in this host. | |
All images in the local cache will be removed. This may take several | |
minutes. | |
EOF | |
local answer="" | |
read -p "Do you want to continue yes/no? " answer | |
echo "" | |
if [[ "yes" != "$answer" ]] | |
then | |
echo "continue = $answer - $0 ended without errors, no files" | |
echo "were deleted from your system." | |
exit 0 | |
fi | |
} # sudoNotification | |
function nukeEnvironmentVariables { | |
# Variables may be defined in .bash_profile or in an include | |
# file .boot2docker_cosmify_profile | |
local profile="$HOME/.bash_profile" | |
local tempFile="/tmp/$(whoami)_profile" | |
if [[ -e "$profile" ]] | |
then | |
awk -v "cosmicDockerProfile=$includeProfile" '!/ DOCKER_/' "$profile" > "$tempFile" | |
mv "$tempFile" "$profile" | |
fi | |
} # nukeEnvironmentVariables | |
function boot2dockerIsRunning { | |
echo $(boot2docker status) | |
} # boot2dockerIsRunning | |
function nukeRunningContainers { | |
echo "Removing all existing and/or running containers:" | |
docker ps -a | awk 'NR > 1 { system(sprintf("docker rm -f %s", $NF)); }' | |
[[ "$?" == "0" ]] || die "Error removing containers" 5 | |
echo "All containers removed" | |
} # nukeRunningContainers | |
function nukeAllImages { | |
echo "Removing all existing images:" | |
docker images -a | awk 'NR > 1 { system(sprintf("docker rmi -f %s", $3)); }' | |
[[ "$?" == "0" ]] || die "Error removing images" 6 | |
echo "All images removed" | |
} # nukeAllImages | |
function nukeVMImage { | |
echo "removing boot2docker VirtualBox image" | |
boot2docker delete | |
echo "boot2docker VirtualBox image removed" | |
} # nukeVMImage | |
function nukeCosmifyDocuments { | |
if [[ -d "$COSMIFY_DOCUMENTS_DIR" ]] | |
then | |
echo "removing the contents of $COSMIFY_DOCUMENTS_DIR" | |
rm -Rfv "$COSMIFY_DOCUMENTS_DIR" | |
echo "$COSMIFY_DOCUMENTS_DIR contents removed" | |
fi | |
} # nukeCosmifyDocuments | |
function nukeExecutables { | |
local dockerExecutable="/usr/local/bin/docker" | |
echo "Nuking executables (be ready to provide the Admin password)" | |
sudo rm -vf "$dockerExecutable" | |
sudo rm -vf "$B2D_BIN" | |
sudo rm -Rvf "/usr/local/share/boot2docker" | |
sudo rm -Rvf "$B2D_APP" | |
rm -Rvf "$HOME/.boot2docker" | |
rm -vf $HOME/.ssh/id_boot2docker* | |
} # nukeExecutables | |
# *** main *** | |
export DOCKER_CERT_PATH=$HOME/.boot2docker/certs/boot2docker-vm | |
if [[ -d "$DOCKER_CERT_PATH" ]] | |
then | |
servicePort=2376 | |
else | |
servicePort=2375 | |
fi | |
export DOCKER_HOST=tcp://$(boot2docker ip 2> /dev/null):"$servicePort" | |
export DOCKER_TLS_VERIFY=1 | |
assertValidHostOS | |
b2dDetection | |
b2dMustBeRunning | |
sudoNotification | |
nukeEnvironmentVariables | |
if [[ "running" == $(boot2dockerIsRunning) ]] | |
then | |
nukeRunningContainers | |
nukeAllImages | |
boot2docker stop | |
fi | |
nukeCosmifyDocuments | |
nukeVMImage | |
nukeExecutables |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment