#!/usr/bin/env bash # # setup/bootstrap script # Script to idempotently validate that Python, PyEnv, Poetry, PyInvoke, venv, # Git, Docker, ssh-client, etc are installed on Linux and OSX # # Can be run with: `curl -sL git.io/elgalu | bash` # # This script is idempotent, it will not re-install things but only make sure nothing is missing. # If you want to force this script to install most things again run `rm -rf ~/.pyenv .venv/` # # Tested on: # - Linux: Ubuntu 20.04.2 LTS # - OSX..: TODO # # :source: https://github.com/elgalu/ensure # :copyright: (c) 2021 by Leo Gallucci. # :license: MIT # set -e: exit asap if a command exits with a non-zero status set -e # set -o pipefail: Don't mask errors from a command piped into another command set -o pipefail # set -x: prints all lines before running debug (debugging) [ -n "${DEBUG}" ] && set -x [ -n "${PYENV_DEBUG}" ] && set -x #-------------------------# #--- Program Variables ---# #-------------------------# [ -z "$PYENV_ROOT" ] && export PYENV_ROOT="${HOME}/.pyenv" GITHUB="https://github.com" #------------------------# #--- Helper Functions ---# #------------------------# ## Output to standard error function util.log.error() { printf "ERROR $(${_date} "+%H:%M:%S:%N") %s\n" "$*" >&2 } ## Output a warning to standard output function util.log.warn() { printf "WARN $(${_date} "+%H:%M:%S:%N") %s\n" "$*" >&2 } ## Output an info standard output function util.log.info() { printf "INFO $(${_date} "+%H:%M:%S:%N") %s\n" "$*" >&1 } ## Output a new line break to stdout function util.log.newline() { printf "\n" >&1 } ## Print an error and exit, failing function util.die() { util.log.error "$1" # if $2 is defined AND NOT EMPTY, use $2; otherwise, set the exit code to: 150 errnum=${2-150} exit "${errnum}" } #----------------------------# #--- OSX vs Linux tooling ---# #----------------------------# if [ "$(uname)" == 'Darwin' ]; then export _tee='gtee' export _cut='gcut' export _sed='gsed' export _tail='gtail' export _date='gdate' export _timeout='gtimeout' if ! command -v gdate 1>/dev/null 2>&1; then util.die "GNU date is not installed, run: brew install coreutils" fi elif [ "$(uname)" == 'Linux' ]; then export _tee='tee' export _cut='cut' export _sed='sed' export _tail='tail' export _date='date' export _timeout='timeout' else util.die "Unsupported operating system: $(uname)" fi #-----------# #--- Git ---# #-----------# ## Git clone each pyenv dependency repo. function checkout() { [ -d "$2" ] || git clone --depth 1 "$1" "$2" || util.die "Failed to git clone $1" } ## Ensure git client is installed. function ensure_git() { if ! command -v git 1>/dev/null 2>&1; then util.die "Git is not installed, can't continue. git command not found in path" fi } ensure_git #----------------------# #--- Git Submodules ---# #----------------------# ## Ensure git client is installed. function ensure_git_submodules() { if [ -f ".gitmodules" ]; then git submodule update --init fi } ensure_git_submodules #--------------# #--- Docker ---# #--------------# ## Ensure docker client is installed and that doesn't require sudo. function ensure_docker() { if ! command -v docker --version 1>/dev/null 2>&1; then util.log.error "it seems docker is not installed, please install it first." docker --version fi if ! command -v docker ps --last=1 1>/dev/null 2>&1; then util.log.error "it seems docker is installed but it requires sudo, fix that first." docker ps --last=1 fi } ensure_docker #------------------------------------# #--- SSH client and other tooling ---# #------------------------------------# ## Ensure ssh client is installed. function ensure_ssh_client() { if ! command which ssh 1>/dev/null 2>&1; then util.die "it seems ssh is not installed, please install a tool like openssh-client." fi if ! command which ssh-keygen 1>/dev/null 2>&1; then util.die "it seems ssh-keygen is not installed, please install a tool like openssh-client." fi if ! command which ssh-copy-id 1>/dev/null 2>&1; then util.die "it seems ssh-copy-id is not installed, please install a tool like openssh-client." fi if ! command which sftp 1>/dev/null 2>&1; then util.die "it seems sftp is not installed." fi } ensure_ssh_client #-------------------------------------# #--- Ensure we have grep, awk, etc ---# #-------------------------------------# function ensure_system_helpers(