diff --git a/.drone.yml b/.drone.yml index acb08bdb..8288031a 100644 --- a/.drone.yml +++ b/.drone.yml @@ -3,21 +3,29 @@ kind: pipeline name: linters steps: - name: run shellcheck - image: koalaman/shellcheck-alpine:v0.7.1 + image: koalaman/shellcheck-alpine commands: - shellcheck abra - shellcheck bin/*.sh + - shellcheck deploy/install.abra.coopcloud.tech/installer - name: run flake8 - image: alpine/flake8:3.9.0 + image: alpine/flake8 commands: - - flake8 --max-line-length 100 bin/app-json.py + - flake8 --max-line-length 100 bin/*.py - name: run unit tests image: decentral1se/docker-dind-bats-kcov commands: - bats tests + - name: test installation script + image: debian:buster + commands: + - apt update && apt install -yqq sudo lsb-release + - deploy/install.abra.coopcloud.tech/installer --no-prompt + - ~/.local/bin/abra version + - name: publish image image: plugins/docker settings: @@ -31,6 +39,7 @@ steps: - run shellcheck - run flake8 - run unit tests + - test installation script when: event: exclude: @@ -49,6 +58,7 @@ steps: - run shellcheck - run flake8 - run unit tests + - test installation script - publish image when: event: @@ -67,6 +77,7 @@ steps: - run shellcheck - run flake8 - run unit tests + - test installation script - publish image - trigger downstream builds when: diff --git a/CHANGELOG.md b/CHANGELOG.md index 951d6db0..1572f3c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - Fix logging for chaos deploys and recipe selection logic ([#185](https://git.autonomic.zone/coop-cloud/abra/issues/185)) - Improve reliability of selectig when to download a new `apps.json` ([#170](https://git.autonomic.zone/coop-cloud/abra/issues/170)) - Remove `pwgen`/`pwqgen` as password generator requirements ([#167](https://git.autonomic.zone/coop-cloud/abra/issues/167)) +- `abra` installer script will now try to install system requirements ([#196](https://git.autonomic.zone/coop-cloud/abra/issues/196)) # abra 9.0.0 (2021-06-10) diff --git a/README.md b/README.md index d1500790..2c73b8ab 100644 --- a/README.md +++ b/README.md @@ -39,13 +39,18 @@ Install the latest stable release: curl https://install.abra.coopcloud.tech | bash ``` -or the bleeding-edge development version: +The source for this script is [here](./deploy/install.abra.coopcloud.tech/installer). + +You can pass options to the script like so (e.g. install the bleeding edge development version): ```sh curl https://install.abra.coopcloud.tech | bash -s -- --dev ``` -The source for this script is [here](./deploy/install.abra.coopcloud.tech/installer). +Other options available are as follows: + +- **--no-prompt**: non-interactive installation +- **--no-deps**: do not attempt to install [requirements](#requirements) ## Container diff --git a/abra b/abra index c20ee127..f8e42f03 100755 --- a/abra +++ b/abra @@ -2628,7 +2628,7 @@ abra() { # Use abra__command_ in case `command` is provided (i.e. `volume` or `stack`) CMD="sub_${abra__command_}" if type "$CMD" > /dev/null 2>&1; then - # shellcheck disable=SC2086 + # shellcheck disable=SC2086,SC2048 "$CMD" ${abra__args_[*]} else docopt_exit diff --git a/deploy/install.abra.coopcloud.tech/installer b/deploy/install.abra.coopcloud.tech/installer index 02043e09..e3ef7ed1 100755 --- a/deploy/install.abra.coopcloud.tech/installer +++ b/deploy/install.abra.coopcloud.tech/installer @@ -1,10 +1,180 @@ -#!/bin/bash +#!/usr/bin/env bash + +# shellcheck disable=SC2154,SC2034 ABRA_VERSION="9.0.0" GIT_URL="https://git.autonomic.zone/coop-cloud/abra" ABRA_SRC="$GIT_URL/raw/tag/$ABRA_VERSION/abra" ABRA_DIR="${ABRA_DIR:-$HOME/.abra}" +DOC=" +abra command-line installer script + +Usage: + installer [options] + +Options: + -h, --help Show this message and exit + -d, --dev Install bleeding edge development version + -n, --no-prompt Don't prompt for input and run non-interactively + -p, --no-deps Don't attempt to install system dependencies +" + +# docopt parser below, refresh this parser with `docopt.sh installer` +# shellcheck disable=2016,1075 +docopt() { parse() { if ${DOCOPT_DOC_CHECK:-true}; then local doc_hash +if doc_hash=$(printf "%s" "$DOC" | (sha256sum 2>/dev/null || shasum -a 256)); then +if [[ ${doc_hash:0:5} != "$digest" ]]; then +stderr "The current usage doc (${doc_hash:0:5}) does not match \ +what the parser was generated with (${digest}) +Run \`docopt.sh\` to refresh the parser."; _return 70; fi; fi; fi +local root_idx=$1; shift; argv=("$@"); parsed_params=(); parsed_values=() +left=(); testdepth=0; local arg; while [[ ${#argv[@]} -gt 0 ]]; do +if [[ ${argv[0]} = "--" ]]; then for arg in "${argv[@]}"; do +parsed_params+=('a'); parsed_values+=("$arg"); done; break +elif [[ ${argv[0]} = --* ]]; then parse_long +elif [[ ${argv[0]} = -* && ${argv[0]} != "-" ]]; then parse_shorts +elif ${DOCOPT_OPTIONS_FIRST:-false}; then for arg in "${argv[@]}"; do +parsed_params+=('a'); parsed_values+=("$arg"); done; break; else +parsed_params+=('a'); parsed_values+=("${argv[0]}"); argv=("${argv[@]:1}"); fi +done; local idx; if ${DOCOPT_ADD_HELP:-true}; then +for idx in "${parsed_params[@]}"; do [[ $idx = 'a' ]] && continue +if [[ ${shorts[$idx]} = "-h" || ${longs[$idx]} = "--help" ]]; then +stdout "$trimmed_doc"; _return 0; fi; done; fi +if [[ ${DOCOPT_PROGRAM_VERSION:-false} != 'false' ]]; then +for idx in "${parsed_params[@]}"; do [[ $idx = 'a' ]] && continue +if [[ ${longs[$idx]} = "--version" ]]; then stdout "$DOCOPT_PROGRAM_VERSION" +_return 0; fi; done; fi; local i=0; while [[ $i -lt ${#parsed_params[@]} ]]; do +left+=("$i"); ((i++)) || true; done +if ! required "$root_idx" || [ ${#left[@]} -gt 0 ]; then error; fi; return 0; } +parse_shorts() { local token=${argv[0]}; local value; argv=("${argv[@]:1}") +[[ $token = -* && $token != --* ]] || _return 88; local remaining=${token#-} +while [[ -n $remaining ]]; do local short="-${remaining:0:1}" +remaining="${remaining:1}"; local i=0; local similar=(); local match=false +for o in "${shorts[@]}"; do if [[ $o = "$short" ]]; then similar+=("$short") +[[ $match = false ]] && match=$i; fi; ((i++)) || true; done +if [[ ${#similar[@]} -gt 1 ]]; then +error "${short} is specified ambiguously ${#similar[@]} times" +elif [[ ${#similar[@]} -lt 1 ]]; then match=${#shorts[@]}; value=true +shorts+=("$short"); longs+=(''); argcounts+=(0); else value=false +if [[ ${argcounts[$match]} -ne 0 ]]; then if [[ $remaining = '' ]]; then +if [[ ${#argv[@]} -eq 0 || ${argv[0]} = '--' ]]; then +error "${short} requires argument"; fi; value=${argv[0]}; argv=("${argv[@]:1}") +else value=$remaining; remaining=''; fi; fi; if [[ $value = false ]]; then +value=true; fi; fi; parsed_params+=("$match"); parsed_values+=("$value"); done +}; parse_long() { local token=${argv[0]}; local long=${token%%=*} +local value=${token#*=}; local argcount; argv=("${argv[@]:1}") +[[ $token = --* ]] || _return 88; if [[ $token = *=* ]]; then eq='='; else eq='' +value=false; fi; local i=0; local similar=(); local match=false +for o in "${longs[@]}"; do if [[ $o = "$long" ]]; then similar+=("$long") +[[ $match = false ]] && match=$i; fi; ((i++)) || true; done +if [[ $match = false ]]; then i=0; for o in "${longs[@]}"; do +if [[ $o = $long* ]]; then similar+=("$long"); [[ $match = false ]] && match=$i +fi; ((i++)) || true; done; fi; if [[ ${#similar[@]} -gt 1 ]]; then +error "${long} is not a unique prefix: ${similar[*]}?" +elif [[ ${#similar[@]} -lt 1 ]]; then +[[ $eq = '=' ]] && argcount=1 || argcount=0; match=${#shorts[@]} +[[ $argcount -eq 0 ]] && value=true; shorts+=(''); longs+=("$long") +argcounts+=("$argcount"); else if [[ ${argcounts[$match]} -eq 0 ]]; then +if [[ $value != false ]]; then +error "${longs[$match]} must not have an argument"; fi +elif [[ $value = false ]]; then +if [[ ${#argv[@]} -eq 0 || ${argv[0]} = '--' ]]; then +error "${long} requires argument"; fi; value=${argv[0]}; argv=("${argv[@]:1}") +fi; if [[ $value = false ]]; then value=true; fi; fi; parsed_params+=("$match") +parsed_values+=("$value"); }; required() { local initial_left=("${left[@]}") +local node_idx; ((testdepth++)) || true; for node_idx in "$@"; do +if ! "node_$node_idx"; then left=("${initial_left[@]}"); ((testdepth--)) || true +return 1; fi; done; if [[ $((--testdepth)) -eq 0 ]]; then +left=("${initial_left[@]}"); for node_idx in "$@"; do "node_$node_idx"; done; fi +return 0; }; optional() { local node_idx; for node_idx in "$@"; do +"node_$node_idx"; done; return 0; }; switch() { local i +for i in "${!left[@]}"; do local l=${left[$i]} +if [[ ${parsed_params[$l]} = "$2" ]]; then +left=("${left[@]:0:$i}" "${left[@]:((i+1))}") +[[ $testdepth -gt 0 ]] && return 0; if [[ $3 = true ]]; then +eval "((var_$1++))" || true; else eval "var_$1=true"; fi; return 0; fi; done +return 1; }; stdout() { printf -- "cat <<'EOM'\n%s\nEOM\n" "$1"; }; stderr() { +printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() { +[[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() { +printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:333} +usage=${DOC:37:28}; digest=36916; shorts=(-h -d -n -p) +longs=(--help --dev --no-prompt --no-deps); argcounts=(0 0 0 0); node_0(){ +switch __help 0; }; node_1(){ switch __dev 1; }; node_2(){ switch __no_prompt 2 +}; node_3(){ switch __no_deps 3; }; node_4(){ optional 0 1 2 3; }; node_5(){ +optional 4; }; node_6(){ required 5; }; node_7(){ required 6; } +cat <<<' docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2 +printf "%s\n" "${DOC:37:28}" >&2; exit 1; }'; unset var___help var___dev \ +var___no_prompt var___no_deps; parse 7 "$@"; local prefix=${DOCOPT_PREFIX:-''} +unset "${prefix}__help" "${prefix}__dev" "${prefix}__no_prompt" \ +"${prefix}__no_deps"; eval "${prefix}"'__help=${var___help:-false}' +eval "${prefix}"'__dev=${var___dev:-false}' +eval "${prefix}"'__no_prompt=${var___no_prompt:-false}' +eval "${prefix}"'__no_deps=${var___no_deps:-false}'; local docopt_i=1 +[[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2; for ((;docopt_i>0;docopt_i--)); do +declare -p "${prefix}__help" "${prefix}__dev" "${prefix}__no_prompt" \ +"${prefix}__no_deps"; done; } +# docopt parser above, complete command for generating this parser is `docopt.sh installer` + +function prompt_confirm { + if [ "$no_prompt" == "true" ]; then + return + fi + + read -rp "Continue? [y/N]? " choice + + case "$choice" in + y|Y ) return ;; + * ) exit;; + esac +} + +function show_banner { + echo "" + echo " ____ ____ _ _ " + echo " / ___|___ ___ _ __ / ___| | ___ _ _ __| |" + echo " | | / _ \ _____ / _ \| '_ \ | | | |/ _ \| | | |/ _' |" + echo " | |__| (_) |_____| (_) | |_) | | |___| | (_) | |_| | (_| |" + echo " \____\___/ \___/| .__/ \____|_|\___/ \__,_|\__,_|" + echo " |_|" + echo "" +} + +function install_docker { + sudo apt-get remove docker docker-engine docker.io containerd runc + sudo apt-get install -yq \ + apt-transport-https \ + ca-certificates \ + curl \ + gnupg \ + lsb-release + curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg + echo \ + "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \ + $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null + sudo apt-get update + sudo apt-get install -yq docker-ce docker-ce-cli containerd.io +} + +function install_requirements { + if [ -f "/etc/debian_version" ]; then + echo "Detected Debian based distribution, attempting to install system requirements..." + + sudo apt update && sudo apt install -y \ + curl \ + passwdqc \ + pwgen + + echo "Install Docker (https://docs.docker.com/engine/install/debian/)?" + prompt_confirm + install_docker + else + echo "Sorry, we only support Debian based distributions at the moment" + echo "You'll have to install the requirements manually for your distribution" + echo "See https://git.autonomic.zone/coop-cloud/abra#requirements for more" + fi +} + function install_abra_release { mkdir -p "$HOME/.local/bin" curl "$ABRA_SRC" > "$HOME/.local/bin/abra" @@ -24,7 +194,25 @@ function install_abra_dev { } function run_installation { - if [ "$1" = "--dev" ]; then + show_banner + + DOCOPT_PREFIX=installer_ + DOCOPT_ADD_HELP=false + eval "$(docopt "$@")" + + dev="$installer___dev" + no_prompt="$installer___no_prompt" + no_deps="$installer___no_deps" + + if [ "$no_deps" == "false" ]; then + install_requirements + fi + + if ! type curl > /dev/null 2>&1; then + error "'curl' program is not installed, cannot continue..." + fi + + if [ "$dev" == "true" ]; then install_abra_dev else install_abra_release diff --git a/makefile b/makefile index fc557f97..d8446f89 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ -.PHONY: test shellcheck docopt release-installer build push +.PHONY: test shellcheck docopt release-installer build push deploy-docopt symlink test: @sudo DOCKER_CONTEXT=default docker run \ @@ -21,8 +21,9 @@ shellcheck: --rm \ -v $$(pwd):/workdir \ koalaman/shellcheck-alpine \ - shellcheck /workdir/abra && \ - shellcheck /workdir/bin/*.sh + sh -c "shellcheck /workdir/abra && \ + shellcheck /workdir/bin/*.sh && \ + shellcheck /workdir/deploy/install.abra.coopcloud.tech/installer" docopt: @if [ ! -d ".venv" ]; then \ @@ -32,6 +33,14 @@ docopt: fi .venv/bin/docopt.sh abra +deploy-docopt: + @if [ ! -d ".venv" ]; then \ + python3 -m venv .venv && \ + .venv/bin/pip install -U pip setuptools wheel && \ + .venv/bin/pip install docopt-sh; \ + fi + .venv/bin/docopt.sh deploy/install.abra.coopcloud.tech/installer + release-installer: @DOCKER_CONTEXT=swarm.autonomic.zone \ docker stack rm abra-installer-script && \