From db441f28b826304b8d6a4a7fcf58391f8d0d17f7 Mon Sep 17 00:00:00 2001 From: Joakim Roubert Date: Fri, 23 Aug 2019 14:39:20 +0200 Subject: [PATCH 1/3] install.sh: Add wget fallback if curl isn't available Reportedly, some systems don't have curl but wget. With this patch, install.sh will use wget instead of curl if the latter is not available on the target system. Change-Id: I0bc78feec6d8e1dbf7fbef7c2e10833b79bdbbdc Signed-off-by: Joakim Roubert --- install.sh | 56 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/install.sh b/install.sh index afd9e16e60..358be2c86a 100755 --- a/install.sh +++ b/install.sh @@ -67,6 +67,7 @@ set -e # if not specified. GITHUB_URL=https://github.com/rancher/k3s/releases +DOWNLOADER= # --- helper functions for logs --- info() @@ -260,11 +261,14 @@ setup_verify_arch() { esac } -# --- fatal if no curl --- -verify_curl() { - if [ -z `which curl || true` ]; then - fatal "Can not find curl for downloading files" - fi +# --- verify existence of network downloader executable --- +verify_downloader() { + # Return failure if it doesn't exist or is no executable + [ -x "$(which $1)" ] || return 1 + + # Set verified executable as our downloader program and return success + DOWNLOADER=$1 + return 0 } # --- create tempory directory and cleanup when done --- @@ -288,7 +292,17 @@ get_release_version() { VERSION_K3S="${INSTALL_K3S_VERSION}" else info "Finding latest release" - VERSION_K3S=`curl -w "%{url_effective}" -I -L -s -S ${GITHUB_URL}/latest -o /dev/null | sed -e 's|.*/||'` + case $DOWNLOADER in + curl) + VERSION_K3S=$(curl -w '%{url_effective}' -I -L -s -S ${GITHUB_URL}/latest -o /dev/null | sed -e 's|.*/||') + ;; + wget) + VERSION_K3S=$(wget -SqO /dev/null ${GITHUB_URL}/latest 2>&1 | grep Location | sed -e 's|.*/||') + ;; + *) + fatal "Incorrect downloader executable '$DOWNLOADER'" + ;; + esac fi info "Using ${VERSION_K3S} as release" } @@ -297,7 +311,19 @@ get_release_version() { download_hash() { HASH_URL=${GITHUB_URL}/download/${VERSION_K3S}/sha256sum-${ARCH}.txt info "Downloading hash ${HASH_URL}" - curl -o ${TMP_HASH} -sfL ${HASH_URL} || fatal "Hash download failed" + case $DOWNLOADER in + curl) + curl -o ${TMP_HASH} -sfL ${HASH_URL} + ;; + wget) + wget -qO ${TMP_HASH} ${HASH_URL} + ;; + *) + fatal "Incorrect executable '$DOWNLOADER'" + ;; + esac + # Abort if download command failed + [ $? -eq 0 ] || fatal 'Hash download failed' HASH_EXPECTED=`grep " k3s${SUFFIX}$" ${TMP_HASH} | awk '{print $1}'` } @@ -316,7 +342,19 @@ installed_hash_matches() { download_binary() { BIN_URL=${GITHUB_URL}/download/${VERSION_K3S}/k3s${SUFFIX} info "Downloading binary ${BIN_URL}" - curl -o ${TMP_BIN} -sfL ${BIN_URL} || fatal "Binary download failed" + case $DOWNLOADER in + curl) + curl -o ${TMP_BIN} -sfL ${BIN_URL} + ;; + wget) + wget -qO ${TMP_BIN} ${BIN_URL} + ;; + *) + fatal "Incorrect executable '$DOWNLOADER'" + ;; + esac + # Abort if download command failed + [ $? -eq 0 ] || fatal 'Binary download failed' } # --- verify downloaded binary hash --- @@ -355,7 +393,7 @@ download_and_verify() { fi setup_verify_arch - verify_curl + verify_downloader curl || verify_downloader wget || fatal 'Can not find curl or wget for downloading files' setup_tmp get_release_version download_hash From 0a67557c4adc6873860db75a684c68462dba4f37 Mon Sep 17 00:00:00 2001 From: Joakim Roubert Date: Tue, 3 Sep 2019 08:12:22 +0200 Subject: [PATCH 2/3] Update install.sh after review comments Change-Id: I8acf9f0fc0115dd8a45027b1d7610b47aa7462d9 --- install.sh | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/install.sh b/install.sh index bc13283426..fbdc4d9c6d 100755 --- a/install.sh +++ b/install.sh @@ -307,23 +307,31 @@ get_release_version() { info "Using ${VERSION_K3S} as release" } -# --- download hash from github url --- -download_hash() { - HASH_URL=${GITHUB_URL}/download/${VERSION_K3S}/sha256sum-${ARCH}.txt - info "Downloading hash ${HASH_URL}" +# --- download from github url --- +download() { + [ $# -eq 2 ] || fatal 'download needs exactly 2 arguments' + case $DOWNLOADER in curl) - curl -o ${TMP_HASH} -sfL ${HASH_URL} + curl -o $1 -sfL $2 ;; wget) - wget -qO ${TMP_HASH} ${HASH_URL} + wget -qO $1 $2 ;; *) fatal "Incorrect executable '$DOWNLOADER'" ;; esac + # Abort if download command failed [ $? -eq 0 ] || fatal 'Hash download failed' +} + +# --- download hash from github url --- +download_hash() { + HASH_URL=${GITHUB_URL}/download/${VERSION_K3S}/sha256sum-${ARCH}.txt + info "Downloading hash ${HASH_URL}" + download ${TMP_HASH} ${HASH_URL} HASH_EXPECTED=`grep " k3s${SUFFIX}$" ${TMP_HASH} | awk '{print $1}'` } @@ -342,19 +350,7 @@ installed_hash_matches() { download_binary() { BIN_URL=${GITHUB_URL}/download/${VERSION_K3S}/k3s${SUFFIX} info "Downloading binary ${BIN_URL}" - case $DOWNLOADER in - curl) - curl -o ${TMP_BIN} -sfL ${BIN_URL} - ;; - wget) - wget -qO ${TMP_BIN} ${BIN_URL} - ;; - *) - fatal "Incorrect executable '$DOWNLOADER'" - ;; - esac - # Abort if download command failed - [ $? -eq 0 ] || fatal 'Binary download failed' + download ${TMP_BIN} ${BIN_URL} } # --- verify downloaded binary hash --- From a0d2743868f955c95b918063d717e76177c081d7 Mon Sep 17 00:00:00 2001 From: Joakim Roubert Date: Tue, 3 Sep 2019 08:15:39 +0200 Subject: [PATCH 3/3] Update install.sh --- install.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/install.sh b/install.sh index fbdc4d9c6d..9b1d5a61dd 100755 --- a/install.sh +++ b/install.sh @@ -324,7 +324,7 @@ download() { esac # Abort if download command failed - [ $? -eq 0 ] || fatal 'Hash download failed' + [ $? -eq 0 ] || fatal 'Download failed' } # --- download hash from github url --- @@ -332,7 +332,7 @@ download_hash() { HASH_URL=${GITHUB_URL}/download/${VERSION_K3S}/sha256sum-${ARCH}.txt info "Downloading hash ${HASH_URL}" download ${TMP_HASH} ${HASH_URL} - HASH_EXPECTED=`grep " k3s${SUFFIX}$" ${TMP_HASH} | awk '{print $1}'` + HASH_EXPECTED=$(grep " k3s${SUFFIX}$" ${TMP_HASH} | awk '{print $1}') } # --- check hash against installed version ---