From 396a7c2ad22b6abc61288bac7e1573d07fcd4564 Mon Sep 17 00:00:00 2001 From: Eric Engestrom Date: Sat, 15 Dec 2018 15:32:49 +0000 Subject: [PATCH] install.sh script fixes (#263) * install.sh: allow user to customize install location Fixes #262 * install.sh: verify that the install location can be used * install.sh: don't invoke sudo if it isn't needed * install.sh: make sure `lab` is executable If /tmp is mounted with the noexec option, the execution bit is dropped. Let's make sure to add it back in the install script. * install.sh: error out if an unset variable is used * install.sh: error out if part of a pipe chain fails * install.sh: allow exact location of binary to be specified By passing it as the first argument to the script. Not that the argument overrides the environment, in case both are set. * install.sh: replace `cp+chmod` with `install` --- install.sh | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/install.sh b/install.sh index ea848d7c..6a724182 100755 --- a/install.sh +++ b/install.sh @@ -1,16 +1,35 @@ #!/usr/bin/env bash -set -e +set -eu -o pipefail -if [[ ! -z $DEBUG ]]; then +if [[ ! -z ${DEBUG-} ]]; then set -x fi -if [[ $EUID != 0 ]]; then +: ${PREFIX:=/usr/local} +BINDIR="$PREFIX/bin" + +if [[ $# -gt 0 ]]; then + BINDIR=$1 +fi + +_can_install() { + if [[ ! -d "$BINDIR" ]]; then + mkdir -p "$BINDIR" 2> /dev/null + fi + [[ -d "$BINDIR" && -w "$BINDIR" ]] +} + +if ! _can_install && [[ $EUID != 0 ]]; then sudo "$0" "$@" exit "$?" fi +if ! _can_install; then + echo "Can't install to $BINDIR" + exit 1 +fi + case "$(uname -m)" in x86_64) machine="amd64" @@ -39,5 +58,5 @@ esac latest="$(curl -sL 'https://api.github.com/repos/zaquestion/lab/releases/latest' | grep 'tag_name' | grep --only 'v[0-9\.]\+' | cut -c 2-)" curl -sL "https://github.com/zaquestion/lab/releases/download/v${latest}/lab_${latest}_${os}_${machine}.tar.gz" | tar -C /tmp/ -xzf - -cp /tmp/lab /usr/local/bin/lab -echo "Successfully installed lab into /usr/local/bin/" +install -m755 /tmp/lab $BINDIR/lab +echo "Successfully installed lab into $BINDIR/"