Last active
August 15, 2018 18:03
-
-
Save akutz/e33c674fe785c6e8891d2019d10dc985 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/sh | |
# posix complaint | |
# verified by https://www.shellcheck.net | |
# | |
# usage: get-k8s.sh FLAGS [stable|latest|ci|<version>] [client|node|server|src|test|<file>] | |
# | |
# FLAGS | |
# -s Enable streaming. Instead of inflating the K8s tarball, the | |
# script redirects the curl process into stdout, making it possible | |
# to pipe this script into a custom tar command. | |
# | |
# Parse the flags. | |
while getopts "s" opt; do | |
case "${opt}" in | |
s) | |
streaming=true | |
shift | |
;; | |
*) | |
;; | |
esac | |
done | |
# Determine the version to fetch. | |
K8S_VERSION=${1:-${K8S_VERSION}} | |
if [ -z "${K8S_VERSION}" ]; then K8S_VERSION=release/stable.txt; fi | |
case ${K8S_VERSION} in | |
stable) | |
K8S_VERSION=release/stable.txt | |
;; | |
latest) | |
K8S_VERSION=release/latest.txt | |
;; | |
ci) | |
K8S_VERSION=ci/latest.txt | |
;; | |
esac | |
# Determine which bits to fetch. | |
K8S_TYPE=${2:-${K8S_TYPE}} | |
if [ -z "${K8S_TYPE}" ]; then K8S_TYPE=server; fi | |
case ${K8S_TYPE} in | |
client) | |
K8S_TGZ=kubernetes-client-linux-amd64.tar.gz | |
;; | |
node) | |
K8S_TGZ=kubernetes-node-linux-amd64.tar.gz | |
;; | |
server) | |
K8S_TGZ=kubernetes-server-linux-amd64.tar.gz | |
;; | |
src) | |
K8S_TGZ=kubernetes-src.tar.gz | |
;; | |
test) | |
K8S_TGZ=kubernetes-test.tar.gz | |
;; | |
*) | |
K8S_TGZ=${K8S_TYPE} | |
;; | |
esac | |
# This is the base URL for getting the K8s bits. | |
K8S_URL=https://storage.googleapis.com/kubernetes-release | |
# If the version does *not* begin with release/ then it's a dev version | |
if ! echo "${K8S_VERSION}" | grep '^release/' >/dev/null 2>&1; then | |
K8S_URL=${K8S_URL}-dev | |
fi | |
# If the version points to a .txt file then its *that* file that contains | |
# the actual version information. | |
if echo "${K8S_VERSION}" | grep '\.txt$' >/dev/null 2>&1; then | |
K8S_REAL_VERSION=$(curl --retry-max-time 120 -sL ${K8S_URL}/${K8S_VERSION}) | |
K8S_VERSION_PREFIX=$(echo "${K8S_VERSION}" | awk -F/ '{print $1}') | |
K8S_VERSION=${K8S_VERSION_PREFIX}/${K8S_REAL_VERSION} | |
fi | |
# Build the actual artifact URL. | |
K8S_ARTIFACT=${K8S_URL}/${K8S_VERSION}/${K8S_TGZ} | |
# Get the bits and inflate them to the current directory. | |
printf 'VERSION\t%s\nURL\t%s\n' "${K8S_VERSION}" "${K8S_ARTIFACT}" >/dev/stderr | |
# Create a temp file to store information about the artifact. | |
temp_file=$(mktemp) | |
curl -sSIL "${K8S_ARTIFACT}" > "${temp_file}" | |
# If the response wasn't 200 then the artifact does not exist. | |
if [ -z "$(grep '^HTTP.\{0,\}200[[:space:]]\{0,\}$' "${temp_file}" | \ | |
tail -n1)" ]; then | |
printf '\nThe artifact at the specified URL does not exist\n' >/dev/stderr | |
exit 1 | |
fi | |
# Print the size of the artifact that will be downloaded. | |
bytes="$(grep -i '^content-length:' "${temp_file}" | \ | |
tail -n1 | awk '{print $2}' | tr -d '\r')" | |
if [ -n "${bytes}" ]; then | |
# bytes | |
if [ "${bytes}" -lt "1024" ]; then | |
printf 'SIZE\t%sb\n' "${bytes}" >/dev/stderr | |
# kilobytes | |
elif [ "${bytes}" -lt "1048576" ]; then | |
printf 'SIZE\t%sKiB\n' "$((bytes/1024))" >/dev/stderr | |
# megabytes | |
elif [ "${bytes}" -lt "1073741824" ]; then | |
printf 'SIZE\t%sMiB\n' "$((bytes/1024/1024))" >/dev/stderr | |
# gigabytes | |
else | |
printf 'SIZE\t%dkGiB\n' "$((bytes/1024/1024))" >/dev/stderr | |
fi | |
echo >/dev/stderr | |
fi | |
# Remove the temp file. | |
rm -f "${temp_file}" | |
# Fetch the tarball and inflate it into the current directory. | |
if [ "${streaming}" = "true" ]; then | |
curl --retry-max-time 120 -L "${K8S_ARTIFACT}" | |
else | |
curl --retry-max-time 120 -L "${K8S_ARTIFACT}" | tar -xz | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment