Skip to content

Commit

Permalink
update plugin installation
Browse files Browse the repository at this point in the history
  • Loading branch information
maorfr committed Nov 30, 2018
1 parent d21a789 commit e89cece
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 127 deletions.
153 changes: 29 additions & 124 deletions install-binary.sh
@@ -1,133 +1,38 @@
#!/usr/bin/env bash

# Combination of the Dep and Helm scripts, with technosophos' own tweaks.
#!/bin/sh -e

PROJECT_NAME="helm-backup"
PROJECT_GH="maorfr/$PROJECT_NAME"
# Copied w/ love from the excellent hypnoglow/helm-s3

: ${HELM_PLUGIN_PATH:="$(helm home)/plugins/helm-backup"}

# Convert the HELM_PLUGIN_PATH to unix if cygpath is
# available. This is the case when using MSYS2 or Cygwin
# on Windows where helm returns a Windows path but we
# need a Unix path
if type cygpath > /dev/null; then
HELM_PLUGIN_PATH=$(cygpath -u $HELM_PLUGIN_PATH)
fi

if [[ $SKIP_BIN_INSTALL == "1" ]]; then
echo "Skipping binary install"
exit
if [ -n "${HELM_PUSH_PLUGIN_NO_INSTALL_HOOK}" ]; then
echo "Development mode: not downloading versioned release."
exit 0
fi

# initArch discovers the architecture for this system.
initArch() {
ARCH=$(uname -m)
case $ARCH in
armv5*) ARCH="armv5";;
armv6*) ARCH="armv6";;
armv7*) ARCH="armv7";;
aarch64) ARCH="arm64";;
x86) ARCH="386";;
x86_64) ARCH="amd64";;
i686) ARCH="386";;
i386) ARCH="386";;
esac
}

# initOS discovers the operating system for this system.
initOS() {
OS=$(echo `uname`|tr '[:upper:]' '[:lower:]')

case "$OS" in
# Msys support
msys*) OS='windows';;
# Minimalist GNU for Windows
mingw*) OS='windows';;
darwin) OS='macos';;
esac
}

# verifySupported checks that the os/arch combination is supported for
# binary builds.
verifySupported() {
local supported="linux-amd64\nmacos-amd64\nwindows-amd64"
if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then
echo "No prebuild binary for ${OS}-${ARCH}."
exit 1
fi
version="$(curl -s https://api.github.com/repos/maorfr/helm-backup/releases/latest | awk '/\"tag_name\":/{gsub( /[,\"]/,"", $2); print $2}')"
echo "Downloading and installing helm-backup ${version} ..."

if ! type "curl" > /dev/null && ! type "wget" > /dev/null; then
echo "Either curl or wget is required"
exit 1
fi
}

# getDownloadURL checks the latest available version.
getDownloadURL() {
# Use the GitHub API to find the latest version for this project.
local latest_url="https://api.github.com/repos/$PROJECT_GH/releases/latest"
if type "curl" > /dev/null; then
DOWNLOAD_URL=$(curl -s $latest_url | grep $OS | awk '/\"browser_download_url\":/{gsub( /[,\"]/,"", $2); print $2}')
elif type "wget" > /dev/null; then
DOWNLOAD_URL=$(wget -q -O - $latest_url | awk '/\"browser_download_url\":/{gsub( /[,\"]/,"", $2); print $2}')
fi
}

# downloadFile downloads the latest binary package and also the checksum
# for that binary.
downloadFile() {
PLUGIN_TMP_FILE="/tmp/${PROJECT_NAME}.tgz"
echo "Downloading $DOWNLOAD_URL"
if type "curl" > /dev/null; then
curl -L "$DOWNLOAD_URL" -o "$PLUGIN_TMP_FILE"
elif type "wget" > /dev/null; then
wget -q -O "$PLUGIN_TMP_FILE" "$DOWNLOAD_URL"
fi
}

# installFile verifies the SHA256 for the file, then unpacks and
# installs it.
installFile() {
HELM_TMP="/tmp/$PROJECT_NAME"
mkdir -p "$HELM_TMP"
tar xf "$PLUGIN_TMP_FILE" -C "$HELM_TMP"
HELM_TMP_BIN="$HELM_TMP/backup"
echo "Preparing to install into ${HELM_PLUGIN_PATH}"
# Use * to also copy the file withe the exe suffix on Windows
cp "$HELM_TMP_BIN"* "$HELM_PLUGIN_PATH"
}

# fail_trap is executed if an error occurs.
fail_trap() {
result=$?
if [ "$result" != "0" ]; then
echo "Failed to install $PROJECT_NAME"
echo "\tFor support, go to https://github.com/kubernetes/helm."
fi
exit $result
}
url=""
if [ "$(uname)" = "Darwin" ]; then
url="https://github.com/maorfr/helm-backup/releases/download/${version}/helm-backup-macos-${version}.tgz"
elif [ "$(uname)" = "Linux" ] ; then
url="https://github.com/maorfr/helm-backup/releases/download/${version}/helm-backup-linux-${version}.tgz"
else
url="https://github.com/maorfr/helm-backup/releases/download/${version}/helm-backup-windows-${version}.tgz"
fi

# testVersion tests the installed client to make sure it is working.
testVersion() {
set +e
echo "$PROJECT_NAME installed into $HELM_PLUGIN_PATH/$PROJECT_NAME"
# To avoid to keep track of the Windows suffix,
# call the plugin assuming it is in the PATH
PATH=$PATH:$HELM_PLUGIN_PATH
backup -h
set -e
}
echo $url

# Execution
cd $HELM_PLUGIN_DIR
mkdir -p "bin"
mkdir -p "releases/${version}"

#Stop execution on any error
trap "fail_trap" EXIT
set -e
initArch
initOS
verifySupported
getDownloadURL
downloadFile
installFile
testVersion
# Download with curl if possible.
if [ -x "$(which curl 2>/dev/null)" ]; then
curl -sSL "${url}" -o "releases/${version}.tgz"
else
wget -q "${url}" -O "releases/${version}.tgz"
fi
tar xzf "releases/${version}.tgz" -C "releases/${version}"
mv "releases/${version}/backup" "bin/backup" || \
mv "releases/${version}/backup.exe" "bin/backup"
rm -rf releases
6 changes: 3 additions & 3 deletions plugin.yaml
@@ -1,8 +1,8 @@
name: "backup"
# Version is the version of Helm plus the number of official builds for this plugin
version: "2.11.0+2"
version: "0.1.0"
usage: "backup/restore releases in a namespace to/from a file"
description: "backup/restore releases in a namespace to/from a file"
command: "$HELM_PLUGIN_DIR/backup"
command: "$HELM_PLUGIN_DIR/bin/backup"
hooks:
install: "$HELM_PLUGIN_DIR/install-binary.sh"
update: "$HELM_PLUGIN_DIR/install-binary.sh"

0 comments on commit e89cece

Please sign in to comment.