Skip to content

Commit b5aaf88

Browse files
committed
Merge pull request kubernetes#13095 from kelcecil/docker-machine
Refactor to use docker-machine or boot2docker
2 parents fcee09a + 6090586 commit b5aaf88

File tree

1 file changed

+91
-60
lines changed

1 file changed

+91
-60
lines changed

build/common.sh

Lines changed: 91 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -126,66 +126,11 @@ readonly KUBE_ADDON_PATHS=(
126126
function kube::build::verify_prereqs() {
127127
kube::log::status "Verifying Prerequisites...."
128128
kube::build::ensure_tar || return 1
129-
130-
if [[ "${1-}" != "clean" ]]; then
131-
if [[ -z "$(which docker)" ]]; then
132-
echo "Can't find 'docker' in PATH, please fix and retry." >&2
133-
echo "See https://docs.docker.com/installation/#installation for installation instructions." >&2
134-
exit 1
135-
fi
136-
137-
if kube::build::is_osx; then
138-
if [[ -z "$DOCKER_NATIVE" ]];then
139-
if [[ -z "$(which boot2docker)" ]]; then
140-
echo "It looks like you are running on Mac OS X and boot2docker can't be found." >&2
141-
echo "See: https://docs.docker.com/installation/mac/" >&2
142-
exit 1
143-
fi
144-
if [[ $(boot2docker status) != "running" ]]; then
145-
echo "boot2docker VM isn't started. Please run 'boot2docker start'" >&2
146-
exit 1
147-
else
148-
# Reach over and set the clock. After sleep/resume the clock will skew.
149-
kube::log::status "Setting boot2docker clock"
150-
boot2docker ssh sudo date -u -D "%Y%m%d%H%M.%S" --set "$(date -u +%Y%m%d%H%M.%S)" >/dev/null
151-
if [[ -z "$DOCKER_HOST" ]]; then
152-
kube::log::status "Setting boot2docker env variables"
153-
$(boot2docker shellinit)
154-
fi
155-
fi
156-
fi
157-
fi
158-
159-
if ! "${DOCKER[@]}" info > /dev/null 2>&1 ; then
160-
{
161-
echo "Can't connect to 'docker' daemon. please fix and retry."
162-
echo
163-
echo "Possible causes:"
164-
echo " - On Mac OS X, boot2docker VM isn't installed or started"
165-
echo " - On Mac OS X, docker env variable isn't set appropriately. Run:"
166-
echo " \$(boot2docker shellinit)"
167-
echo " - On Linux, user isn't in 'docker' group. Add and relogin."
168-
echo " - Something like 'sudo usermod -a -G docker ${USER-user}'"
169-
echo " - RHEL7 bug and workaround: https://bugzilla.redhat.com/show_bug.cgi?id=1119282#c8"
170-
echo " - On Linux, Docker daemon hasn't been started or has crashed"
171-
} >&2
172-
exit 1
173-
fi
174-
else
175-
176-
# On OS X, set boot2docker env vars for the 'clean' target if boot2docker is running
177-
if kube::build::is_osx && kube::build::has_docker ; then
178-
if [[ ! -z "$(which boot2docker)" ]]; then
179-
if [[ $(boot2docker status) == "running" ]]; then
180-
if [[ -z "$DOCKER_HOST" ]]; then
181-
kube::log::status "Setting boot2docker env variables"
182-
$(boot2docker shellinit)
183-
fi
184-
fi
185-
fi
186-
fi
187-
129+
kube::build::ensure_docker_in_path || return 1
130+
if kube::build::is_osx; then
131+
kube::build::docker_available_on_osx || return 1
188132
fi
133+
kube::build::ensure_docker_daemon_connectivity || return 1
189134

190135
KUBE_ROOT_HASH=$(kube::build::short_hash "$KUBE_ROOT")
191136
KUBE_BUILD_IMAGE_TAG="build-${KUBE_ROOT_HASH}"
@@ -198,10 +143,96 @@ function kube::build::verify_prereqs() {
198143
# ---------------------------------------------------------------------------
199144
# Utility functions
200145

146+
function kube::build::docker_available_on_osx() {
147+
if [[ -z "${DOCKER_HOST}" ]]; then
148+
kube::log::status "No docker host is set. Checking options for setting one..."
149+
150+
if [[ -z "$(which docker-machine)" && -z "$(which boot2docker)" ]]; then
151+
kube::log::status "It looks like you're running Mac OS X, and neither docker-machine or boot2docker are nowhere to be found."
152+
kube::log::status "See: https://docs.docker.com/machine/ for installation instructions."
153+
return 1
154+
elif [[ -n "$(which docker-machine)" ]]; then
155+
kube::build::prepare_docker_machine
156+
elif [[ -n "$(which boot2docker)" ]]; then
157+
kube::build::prepare_boot2docker
158+
fi
159+
fi
160+
}
161+
162+
function kube::build::prepare_docker_machine() {
163+
kube::log::status "docker-machine was found."
164+
docker-machine inspect kube-dev >/dev/null || {
165+
kube::log::status "Creating a machine to build Kubernetes"
166+
docker-machine create -d virtualbox kube-dev > /dev/null || {
167+
kube::log::error "Something went wrong creating a machine."
168+
kube::log::error "Try the following: "
169+
kube::log::error "docker-machine create -d <provider> kube-dev"
170+
return 1
171+
}
172+
}
173+
docker-machine start kube-dev > /dev/null
174+
eval $(docker-machine env kube-dev)
175+
kube::log::status "A Docker host using docker-machine named kube-dev is ready to go!"
176+
return 0
177+
}
178+
179+
function kube::build::prepare_boot2docker() {
180+
kube::log::status "boot2docker cli has been deprecated in favor of docker-machine."
181+
kube::log::status "See: https://github.com/boot2docker/boot2docker-cli for more details."
182+
if [[ $(boot2docker status) != "running" ]]; then
183+
kube::log::status "boot2docker isn't running. We'll try to start it."
184+
boot2docker up || {
185+
kube::log::error "Can't start boot2docker."
186+
kube::log::error "You may need to 'boot2docker init' to create your VM."
187+
return 1
188+
}
189+
fi
190+
191+
# Reach over and set the clock. After sleep/resume the clock will skew.
192+
kube::log::status "Setting boot2docker clock"
193+
boot2docker ssh sudo date -u -D "%Y%m%d%H%M.%S" --set "$(date -u +%Y%m%d%H%M.%S)" >/dev/null
194+
195+
kube::log::status "Setting boot2docker env variables"
196+
$(boot2docker shellinit)
197+
kube::log::status "boot2docker-vm has been successfully started."
198+
199+
return 0
200+
}
201+
201202
function kube::build::is_osx() {
202203
[[ "$(uname)" == "Darwin" ]]
203204
}
204205

206+
function kube::build::ensure_docker_in_path() {
207+
if [[ -z "$(which docker)" ]]; then
208+
kube::log::error "Can't find 'docker' in PATH, please fix and retry."
209+
kube::log::error "See https://docs.docker.com/installation/#installation for installation instructions."
210+
return 1
211+
fi
212+
}
213+
214+
function kube::build::ensure_docker_daemon_connectivity {
215+
if ! "${DOCKER[@]}" info > /dev/null 2>&1 ; then
216+
{
217+
echo "Can't connect to 'docker' daemon. please fix and retry."
218+
echo
219+
echo "Possible causes:"
220+
echo " - On Mac OS X, DOCKER_HOST hasn't been set. You may need to: "
221+
echo " - Create and start your VM using docker-machine or boot2docker: "
222+
echo " - docker-machine create -f <provider> kube-dev"
223+
echo " - boot2docker init && boot2docker start"
224+
echo " - Set your environment variables using: "
225+
echo " - eval \$(docker-machine env kube-dev)"
226+
echo " - \$(boot2docker shellinit)"
227+
echo " - On Linux, user isn't in 'docker' group. Add and relogin."
228+
echo " - Something like 'sudo usermod -a -G docker ${USER-user}'"
229+
echo " - RHEL7 bug and workaround: https://bugzilla.redhat.com/show_bug.cgi?id=1119282#c8"
230+
echo " - On Linux, Docker daemon hasn't been started or has crashed."
231+
} >&2
232+
return 1
233+
fi
234+
}
235+
205236
function kube::build::ensure_tar() {
206237
if [[ -n "${TAR:-}" ]]; then
207238
return
@@ -615,7 +646,7 @@ function kube::release::package_server_tarballs() {
615646
# KUBE_SERVER_BINARIES array.
616647
cp "${KUBE_SERVER_BINARIES[@]/#/${LOCAL_OUTPUT_BINPATH}/${platform}/}" \
617648
"${release_stage}/server/bin/"
618-
649+
619650
kube::release::create_docker_images_for_server "${release_stage}/server/bin";
620651
kube::release::write_addon_docker_images_for_server "${release_stage}/addons"
621652

0 commit comments

Comments
 (0)