From a33816e140b60d436c173bcea4548171e9a43ea7 Mon Sep 17 00:00:00 2001 From: Floris van der Grinten Date: Wed, 29 Apr 2020 16:43:45 +0200 Subject: [PATCH 1/5] Use install script from get.secrethub.io --- src/commands/install.yml | 76 ++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/src/commands/install.yml b/src/commands/install.yml index 771c766..501d21d 100755 --- a/src/commands/install.yml +++ b/src/commands/install.yml @@ -20,37 +20,59 @@ steps: shell: << parameters.shell >> command: | - # If the version is not pinned then check if SecretHub is already installed. - if [ "<< parameters.version >>" = "latest" ] && command -v secrethub >/dev/null 2>&1; then - exit 0 - fi + set -e + + # Colors + NO_COLOR="\033[0m" + OK_COLOR="\033[32;01m" + ERROR_COLOR="\033[31;01m" + WARN_COLOR="\033[33;01m" - # Get the latest version. - VERSION="<< parameters.version >>" - if [ "$VERSION" = "latest" ]; then - VERSION=$(curl -sSfL "https://api.github.com/repos/secrethub/secrethub-cli/releases/latest" | grep tag_name | awk -F\" '{ print substr($4,2) }') + # Detect Architecture + ARCH=amd64 + if [ $(getconf LONG_BIT) == 32 ]; then + ARCH=386 fi - # Get the current platform. - if uname -a | grep Darwin > /dev/null 2>&1; then - PLATFORM=darwin + # Detect OS + UNAME=$(uname) + if [[ "$UNAME" == "Darwin" ]]; then + OS=darwin + elif [[ $UNAME == "Linux" ]]; then + OS=linux else - PLATFORM=linux + echo -e "${ERROR_COLOR}Cannot determine OS type. Exiting...${NO_COLOR}" + exit; fi - # If the CLI is not installed or the current version of the CLI does not match the specified one, download the provided version. - if ! command -v secrethub >/dev/null 2>&1 || [[ $(secrethub --version 2>&1 | cut -d "," -f 1) != *"$VERSION" ]]; then - mkdir secrethub - curl -sSfL https://github.com/secrethub/secrethub-cli/releases/download/v$VERSION/secrethub-v$VERSION-$PLATFORM-amd64.tar.gz | tar zxf - -C ./secrethub - - # check if we can install the CLI without sudo - if ! mv ./secrethub/bin/secrethub << parameters.path >> 2>/dev/null ; then - sudo mv ./secrethub/bin/secrethub << parameters.path >> - fi - rm -rf ./secrethub - - # Add executable to path if it is not there already. - if ! echo ":$PATH:" | grep -q ":<>:"; then - echo 'export PATH="$PATH:<>"' >> $BASH_ENV; - fi + # Make sure we have root priviliges. + SUDO="" + if [[ $(id -u) -ne 0 ]]; then + if ! [[ $(command -v sudo) ]]; then + echo -e "${ERROR_COLOR}Installer requires root privileges. Please run this script as root.${NO_COLOR}" + exit; + fi + + if ! sudo -n true 2>/dev/null; then + echo -e "${OK_COLOR}==> I need root privileges to continue, please type in your password.${NO_COLOR}" + sudo -v + fi + + SUDO="sudo" fi + + echo -e "${OK_COLOR}==> Creating directories${NO_COLOR}" + $SUDO mkdir -p /usr/local/secrethub/bin + + # Retrieve latest version + echo -e "${OK_COLOR}==> Retrieving latest version${NO_COLOR}" + VERSION=$(curl --silent "https://api.github.com/repos/secrethub/secrethub-cli/releases/latest" | grep tag_name | awk -F\" '{ print $4 }') + + echo -e "${OK_COLOR}==> Downloading latest version${NO_COLOR}" + ARCHIVE_NAME=secrethub-$VERSION-$OS-$ARCH + LINK_TAR=https://github.com/secrethub/secrethub-cli/releases/download/$VERSION/$ARCHIVE_NAME.tar.gz + + curl -fsSL $LINK_TAR | $SUDO tar -xz -C /usr/local/secrethub; + + # symlink in the PATH + $SUDO ln -sf /usr/local/secrethub/bin/secrethub /usr/local/bin/secrethub From 417f3baf18ea995d595f53f008503c5ebc71e51c Mon Sep 17 00:00:00 2001 From: Floris van der Grinten Date: Wed, 29 Apr 2020 17:57:49 +0200 Subject: [PATCH 2/5] Remove password prompt --- src/commands/install.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/commands/install.yml b/src/commands/install.yml index 501d21d..3ce443a 100755 --- a/src/commands/install.yml +++ b/src/commands/install.yml @@ -53,11 +53,6 @@ steps: exit; fi - if ! sudo -n true 2>/dev/null; then - echo -e "${OK_COLOR}==> I need root privileges to continue, please type in your password.${NO_COLOR}" - sudo -v - fi - SUDO="sudo" fi From b22e49b899e837ec27cb92535efa3b14b81a2cbb Mon Sep 17 00:00:00 2001 From: Floris van der Grinten Date: Wed, 29 Apr 2020 18:29:28 +0200 Subject: [PATCH 3/5] Install passed in version number --- src/commands/install.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/commands/install.yml b/src/commands/install.yml index 3ce443a..d384160 100755 --- a/src/commands/install.yml +++ b/src/commands/install.yml @@ -18,6 +18,8 @@ steps: - run: name: "Install SecretHub CLI" shell: << parameters.shell >> + environment: + SECRETHUB_CLI_VERSION: << parameters.version >> command: | set -e @@ -60,10 +62,14 @@ steps: $SUDO mkdir -p /usr/local/secrethub/bin # Retrieve latest version - echo -e "${OK_COLOR}==> Retrieving latest version${NO_COLOR}" - VERSION=$(curl --silent "https://api.github.com/repos/secrethub/secrethub-cli/releases/latest" | grep tag_name | awk -F\" '{ print $4 }') + if [[ -n "${SECRETHUB_CLI_VERSION}" ]]; then + VERSION=v${SECRETHUB_CLI_VERSION} + else + echo -e "${OK_COLOR}==> Retrieving latest version${NO_COLOR}" + VERSION=$(curl --silent "https://api.github.com/repos/secrethub/secrethub-cli/releases/latest" | grep tag_name | awk -F\" '{ print $4 }') + fi - echo -e "${OK_COLOR}==> Downloading latest version${NO_COLOR}" + echo -e "${OK_COLOR}==> Downloading version ${VERSION}${NO_COLOR}" ARCHIVE_NAME=secrethub-$VERSION-$OS-$ARCH LINK_TAR=https://github.com/secrethub/secrethub-cli/releases/download/$VERSION/$ARCHIVE_NAME.tar.gz From a4b68a8a187193ed00b1321d01b5d0f0fc81a548 Mon Sep 17 00:00:00 2001 From: Floris van der Grinten Date: Wed, 29 Apr 2020 18:35:25 +0200 Subject: [PATCH 4/5] Support bash and sh --- src/commands/install.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/commands/install.yml b/src/commands/install.yml index d384160..c6c8b3e 100755 --- a/src/commands/install.yml +++ b/src/commands/install.yml @@ -32,15 +32,15 @@ steps: # Detect Architecture ARCH=amd64 - if [ $(getconf LONG_BIT) == 32 ]; then + if [ $(getconf LONG_BIT) = 32 ]; then ARCH=386 fi # Detect OS UNAME=$(uname) - if [[ "$UNAME" == "Darwin" ]]; then + if [ "$UNAME" = "Darwin" ]; then OS=darwin - elif [[ $UNAME == "Linux" ]]; then + elif [ "$UNAME" = "Linux" ]; then OS=linux else echo -e "${ERROR_COLOR}Cannot determine OS type. Exiting...${NO_COLOR}" @@ -49,8 +49,8 @@ steps: # Make sure we have root priviliges. SUDO="" - if [[ $(id -u) -ne 0 ]]; then - if ! [[ $(command -v sudo) ]]; then + if [ $(id -u) -ne 0 ]; then + if ! [ $(command -v sudo) ]; then echo -e "${ERROR_COLOR}Installer requires root privileges. Please run this script as root.${NO_COLOR}" exit; fi @@ -62,7 +62,7 @@ steps: $SUDO mkdir -p /usr/local/secrethub/bin # Retrieve latest version - if [[ -n "${SECRETHUB_CLI_VERSION}" ]]; then + if [ -n "${SECRETHUB_CLI_VERSION}" ]; then VERSION=v${SECRETHUB_CLI_VERSION} else echo -e "${OK_COLOR}==> Retrieving latest version${NO_COLOR}" @@ -73,7 +73,7 @@ steps: ARCHIVE_NAME=secrethub-$VERSION-$OS-$ARCH LINK_TAR=https://github.com/secrethub/secrethub-cli/releases/download/$VERSION/$ARCHIVE_NAME.tar.gz - curl -fsSL $LINK_TAR | $SUDO tar -xz -C /usr/local/secrethub; + curl -fsSL $LINK_TAR | $SUDO tar -xz -C /usr/local/secrethub; # symlink in the PATH $SUDO ln -sf /usr/local/secrethub/bin/secrethub /usr/local/bin/secrethub From b483e8215fbce8cf68c50d53b680d2da12ac1ddd Mon Sep 17 00:00:00 2001 From: Floris van der Grinten Date: Thu, 30 Apr 2020 16:28:32 +0200 Subject: [PATCH 5/5] Check if version is already installed --- src/commands/install.yml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/commands/install.yml b/src/commands/install.yml index c6c8b3e..2839da3 100755 --- a/src/commands/install.yml +++ b/src/commands/install.yml @@ -61,14 +61,20 @@ steps: echo -e "${OK_COLOR}==> Creating directories${NO_COLOR}" $SUDO mkdir -p /usr/local/secrethub/bin - # Retrieve latest version - if [ -n "${SECRETHUB_CLI_VERSION}" ]; then + if [ "${SECRETHUB_CLI_VERSION:-latest}" != "latest" ]; then VERSION=v${SECRETHUB_CLI_VERSION} else + # Retrieve latest version echo -e "${OK_COLOR}==> Retrieving latest version${NO_COLOR}" VERSION=$(curl --silent "https://api.github.com/repos/secrethub/secrethub-cli/releases/latest" | grep tag_name | awk -F\" '{ print $4 }') fi + # Exit if version is already installed + if command -v secrethub >/dev/null 2>&1 && secrethub --version 2>&1 | cut -d "," -f 1 | grep -q "$(echo $VERSION | cut -c 2-)$"; then + echo -e "${OK_COLOR}==> Version ${VERSION} is already installed${NO_COLOR}" + exit 0 + fi + echo -e "${OK_COLOR}==> Downloading version ${VERSION}${NO_COLOR}" ARCHIVE_NAME=secrethub-$VERSION-$OS-$ARCH LINK_TAR=https://github.com/secrethub/secrethub-cli/releases/download/$VERSION/$ARCHIVE_NAME.tar.gz