diff --git a/hack/deploy/searchlight.sh b/hack/deploy/searchlight.sh index a83d46775..cd6f8e926 100755 --- a/hack/deploy/searchlight.sh +++ b/hack/deploy/searchlight.sh @@ -13,9 +13,60 @@ kubectl config current-context || { } echo "" +OS="" +ARCH="" +DOWNLOAD_URL="" +DOWNLOAD_DIR="" +TEMP_DIRS=() +ONESSL="" +ONESSL_VERSION=v0.13.1 + # http://redsymbol.net/articles/bash-exit-traps/ function cleanup() { - rm -rf $ONESSL ca.crt ca.key server.crt server.key + rm -rf ca.crt ca.key server.crt server.key + # remove temporary directories + for dir in "${TEMP_DIRS[@]}"; do + rm -rf "${dir}" + done +} + +# detect operating system +# ref: https://raw.githubusercontent.com/helm/helm/master/scripts/get +function detectOS() { + OS=$(echo `uname`|tr '[:upper:]' '[:lower:]') + + case "$OS" in + # Minimalist GNU for Windows + cygwin* | mingw* | msys*) OS='windows';; + esac +} + +# detect machine architecture +function detectArch() { + ARCH=$(uname -m) + case $ARCH in + armv7*) ARCH="arm";; + aarch64) ARCH="arm64";; + x86) ARCH="386";; + x86_64) ARCH="amd64";; + i686) ARCH="386";; + i386) ARCH="386";; + esac +} + +detectOS +detectArch + +# download file pointed by DOWNLOAD_URL variable +# store download file to the directory pointed by DOWNLOAD_DIR variable +# you have to sent the output file name as argument. i.e. downloadFile myfile.tar.gz +function downloadFile() { + if curl --output /dev/null --silent --head --fail "$DOWNLOAD_URL"; then + curl -fsSL ${DOWNLOAD_URL} -o $DOWNLOAD_DIR/$1 + else + echo "File does not exist" + exit 1 + fi } export APPSCODE_ENV=${APPSCODE_ENV:-prod} @@ -64,7 +115,7 @@ detect_tag() { onessl_found() { # https://stackoverflow.com/a/677212/244009 if [ -x "$(command -v onessl)" ]; then - onessl wait-until-has -h >/dev/null 2>&1 || { + onessl version --check=">=${ONESSL_VERSION}" >/dev/null 2>&1 || { # old version of onessl found echo "Found outdated onessl" return 1 @@ -75,31 +126,26 @@ onessl_found() { return 1 } +# download onessl if it does not exist onessl_found || { echo "Downloading onessl ..." - # ref: https://stackoverflow.com/a/27776822/244009 - case "$(uname -s)" in - Darwin) - curl -fsSL -o onessl https://github.com/kubepack/onessl/releases/download/0.10.0/onessl-darwin-amd64 - chmod +x onessl - export ONESSL=./onessl - ;; - - Linux) - curl -fsSL -o onessl https://github.com/kubepack/onessl/releases/download/0.10.0/onessl-linux-amd64 - chmod +x onessl - export ONESSL=./onessl - ;; - CYGWIN* | MINGW* | MSYS*) - curl -fsSL -o onessl.exe https://github.com/kubepack/onessl/releases/download/0.10.0/onessl-windows-amd64.exe - chmod +x onessl.exe - export ONESSL=./onessl.exe - ;; - *) - echo 'other OS' - ;; + ARTIFACT="https://github.com/kubepack/onessl/releases/download/${ONESSL_VERSION}" + ONESSL_BIN=onessl-${OS}-${ARCH} + case "$OS" in + cygwin* | mingw* | msys*) + ONESSL_BIN=${ONESSL_BIN}.exe + ;; esac + + DOWNLOAD_URL=${ARTIFACT}/${ONESSL_BIN} + DOWNLOAD_DIR="$(mktemp -dt onessl-XXXXXX)" + TEMP_DIRS+=($DOWNLOAD_DIR) # store DOWNLOAD_DIR to cleanup later + + downloadFile $ONESSL_BIN # downloaded file name will be saved as the value of ONESSL_BIN variable + + export ONESSL=${DOWNLOAD_DIR}/${ONESSL_BIN} + chmod +x $ONESSL } # ref: https://stackoverflow.com/a/7069755/244009