Created
June 2, 2016 02:02
-
-
Save dimaspivak/1bf1e6795c7e76ac6b36b0d63a4764b1 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
#!/usr/bin/env bash | |
# | |
# Script which installs Docker on an Ubuntu 14.04 host and updates daemon configs. | |
# We hardcode which version of Docker to install to avoid potential breaking changes in new | |
# releases. | |
DOCKER_VERSION=1.11.1 | |
# Set bridge IP to avoid conflicts with Cloudera addresses. | |
BRIDGE_IP="192.168.123.1/24" | |
# DAEMON_OPTIONS will be an array of arguments to be added to /etc/default/docker. | |
DAEMON_OPTIONS+=("--bip=${BRIDGE_IP}") | |
# Keep track of the clusterdock Docker image name since we copy from it below. | |
CLUSTERDOCK_IMAGE=dimaspivak/hbase:clusterdock | |
usage() { | |
cat << __EOF | |
Usage: setup_docker [OPTIONS] | |
-g | --graph directory Set Docker's root directory (defaults to /var/lib/docker). | |
__EOF | |
} | |
parse_args() { | |
until [ -z "${1}" ]; do | |
case "${1}" in | |
-g | --graph) | |
GRAPH_DIRECTORY="${2}" | |
shift 2 ;; | |
*) | |
usage >&2 | |
exit 1 ;; | |
esac | |
done | |
} | |
install_docker() { | |
# If Docker is installed already, uninstall it. | |
if command -v docker >/dev/null; then | |
apt-get purge -y docker.io* lxc-docker* docker-engine* | |
fi | |
# Install Docker by following instructions from Docker's installation guide | |
# (https://docs.docker.com/engine/installation/ubuntulinux/). | |
# To use aufs on Ubuntu 14.04, we need to install the linux-image-extra package. | |
LINUX_IMAGE_EXTRA_PACKAGE="linux-image-extra-$(uname -r)" | |
if ! apt-get -y install "${LINUX_IMAGE_EXTRA_PACKAGE}"; then | |
echo "Failed to install required package ${LINUX_IMAGE_EXTRA_PACKAGE}. Exiting..." >&2 | |
exit 1 | |
fi | |
# Install Docker, exiting if any of the necessary steps (adding gpg key, updating apt sources, | |
# and then actually doing the installation) fail. | |
if ! apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 \ | |
--recv-keys 58118E89F3A912897C070ADBF76221572C52609D || \ | |
! echo "deb https://apt.dockerproject.org/repo ubuntu-trusty main" > \ | |
/etc/apt/sources.list.d/docker.list || \ | |
! apt-get update || \ | |
! apt-get -o Dpkg::Options::="--force-confnew" -y install \ | |
docker-engine=${DOCKER_VERSION}* ; then | |
echo "Docker installation failed. Exiting..." >&2 | |
exit 1 | |
fi | |
} | |
configure_docker() { | |
echo "Stopping Docker daemon..." | |
service docker stop | |
if [ -n "${GRAPH_DIRECTORY}" ]; then | |
DAEMON_OPTIONS+=("-g ${GRAPH_DIRECTORY}") | |
fi | |
echo "Overwriting /etc/default/docker to set DOCKER_OPTS=${DAEMON_OPTIONS[*]}."; | |
cat > /etc/default/docker <<-__EOF | |
# Overwritten by clusterdock's setup script. | |
DOCKER_OPTS="${DAEMON_OPTIONS[*]}" | |
__EOF | |
echo "Starting Docker daemon..." | |
service docker start | |
# Sleep for 5 seconds to make sure daemon is happy... | |
sleep 5 | |
} | |
write_file_from_image() { | |
local IMAGE=${1} | |
local FILE=${2} | |
local DESTINATION=${3} | |
local CONTAINER_ID=$(docker run -d "${IMAGE}") | |
if docker cp "${CONTAINER_ID}:${FILE}" "${DESTINATION}"; then | |
docker rm -f "${CONTAINER_ID}" | |
else | |
echo "Failed to copy ${FILE} from image ${IMAGE} (container ID ${CONTAINER_ID})." | |
return 1 | |
fi | |
} | |
configure_host() { | |
echo "Copying SSH key to /root/.ssh..." | |
local IMAGE="${CLUSTERDOCK_IMAGE}" | |
if ! write_file_from_image "${IMAGE}" \ | |
"/root/clusterdock/clusterdock/images/centos6.6_nodebase/ssh/id_rsa" \ | |
"/root/.ssh/id_rsa" || \ | |
! write_file_from_image "${IMAGE}" \ | |
"/root/clusterdock/clusterdock/images/centos6.6_nodebase/ssh/id_rsa.pub" \ | |
"/root/.ssh/id_rsa.pub" || \ | |
! chmod 400 /root/.ssh/id_rsa; then | |
echo "Failed to copy SSH key. Exiting..." >&2 | |
exit 1 | |
fi | |
if ! write_file_from_image "${IMAGE}" \ | |
"/root/clusterdock/clusterdock.sh" "/usr/local/bin/clusterdock.sh" || \ | |
! add_file_source_to_bashrc '/usr/local/bin/clusterdock.sh'; then | |
echo "Failed to add sourcing of clusterdock.sh to bashrc. Exiting..." >&2 | |
exit 1 | |
fi | |
# Source clusterdock so that a user running this file gets the functions in clusterdock.sh | |
# right away. | |
source /usr/local/bin/clusterdock.sh | |
} | |
add_file_source_to_bashrc() { | |
local FILE=${1} | |
local BASHRC="/etc/bash.bashrc" | |
# Only add the source if it's not already there. | |
if ! grep -q "\. ${FILE}" "${BASHRC}"; then | |
cat >> "${BASHRC}" << ____EOF | |
if [ -f ${FILE} ]; then | |
. ${FILE} | |
fi | |
____EOF | |
fi | |
} | |
main() { | |
# Make sure only root can run our script. | |
if [ $(id -u) -eq 0 ]; then | |
parse_args "$@" && \ | |
install_docker && \ | |
configure_docker && \ | |
configure_host && \ | |
echo "clusterdocker setup is complete. Have fun." | |
else | |
echo "This script must be run as root. Exiting..." >&2 | |
exit 1 | |
fi | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment