Shell

Reusable shell functions for infrastructure automation and developer experience

Installation

Releases are viable via https://github.com/pellepelster/solidblocks/releases, for direct usage in shell-scripts the following helper function provides automatic installation

SOLIDBLOCKS_SHELL_VERSION="v0.5.3"
SOLIDBLOCKS_SHELL_CHECKSUM="431bd1252783711c5ee6009432a8665be9b04437db3030fff67d1753d10cb041"

# self contained function for initial Solidblocks bootstrapping
function bootstrap_solidblocks() {
  local default_dir="$(cd "$(dirname "$0")" ; pwd -P)"
  local install_dir="${1:-${default_dir}/.solidblocks-shell}"

  local temp_file="$(mktemp)"

  curl -L "${SOLIDBLOCKS_BASE_URL:-https://github.com}/pellepelster/solidblocks/releases/download/${SOLIDBLOCKS_SHELL_VERSION}/blcks-shell-${SOLIDBLOCKS_SHELL_VERSION}.zip" > "${temp_file}"
  echo "${SOLIDBLOCKS_SHELL_CHECKSUM}  ${temp_file}" | sha256sum -c

  mkdir -p "${install_dir}" || true
  (
      cd "${install_dir}"
      unzip -o -j "${temp_file}" -d "${install_dir}"
      rm -f "${temp_file}"
  )
}

After download and extraction the different components can be sourced in via

DIR="$(cd "$(dirname "$0")" ; pwd -P)"

source "${DIR}/.solidblocks-shell/software.sh"

software_ensure_terraform
Info

Note than when sourcing in the different files, all dependencies are also automatically loaded. E.g. when using the software helpers it will automatically also source in download.sh and file.sh because it needs functions from this file.

Functions

  • CI
  • Utilities for various CI/CD systems

  • Download
  • Secure and reliable data retrieval from remote servers

  • File
  • Utilities for local file operations

  • Log
  • Generic console and logging and helpers

  • Network
  • Networking Utilities

  • Python
  • Wrappers and helpers for Python

  • Software
  • Tooling setup for local development and continuous integration environments

  • Text
  • Constants for console text formatting

Kotlin Abstraction

Building up on the basic shell function, a Kotlin abstraction serves as type-safe interface to those functions. This is mainly intended as building blocks for non-interactive Cloud-Init shell scripts.

val script = ShellScript()

/**
inline sources are directly included in the rendered script
 */
script.addInlineSource(StorageLibrary)
script.addInlineSource(DockerLibrary)
script.addInlineSource(AptLibrary)

/**
library sources are written to ShellScript.LIB_SOURCES_PATH and sources from there
 */
script.addLibSources(PackageLibrary)

script.addCommand(PackageLibrary.UpdateRepositories())
script.addCommand(PackageLibrary.InstallPackage("jq"))
script.addCommand(DockerLibrary.InstallDebian())

val rawScript = script.render()

All functions are tested on the following distributions

  • Amazon Linux 2
  • Debian 10
  • Debian 11
  • Debian 12
  • Ubuntu 20.04
  • Ubuntu 22.04
  • Ubuntu 24.04

Kitchen Sink Usage Example

#!/usr/bin/env bash

set -eu -o pipefail

DIR="$(cd "$(dirname "$0")" ; pwd -P)"

SOLIDBLOCKS_SHELL_VERSION="v0.5.3"
SOLIDBLOCKS_SHELL_CHECKSUM="431bd1252783711c5ee6009432a8665be9b04437db3030fff67d1753d10cb041"

# self contained function for initial Solidblocks bootstrapping
function bootstrap_solidblocks() {
  local default_dir="$(cd "$(dirname "$0")" ; pwd -P)"
  local install_dir="${1:-${default_dir}/.solidblocks-shell}"

  local temp_file="$(mktemp)"

  curl -L "${SOLIDBLOCKS_BASE_URL:-https://github.com}/pellepelster/solidblocks/releases/download/${SOLIDBLOCKS_SHELL_VERSION}/blcks-shell-${SOLIDBLOCKS_SHELL_VERSION}.zip" > "${temp_file}"
  echo "${SOLIDBLOCKS_SHELL_CHECKSUM}  ${temp_file}" | sha256sum -c

  mkdir -p "${install_dir}" || true
  (
      cd "${install_dir}"
      unzip -o -j "${temp_file}" -d "${install_dir}"
      rm -f "${temp_file}"
  )
}


# makes sure all needed shell functions functions are available and all bootstrapped software is on the $PATH
function ensure_environment() {

  if [[ ! -d "${DIR}/.solidblocks-shell" ]]; then
    echo "environment is not bootstrapped, please run ./do bootstrap first"
    exit 1
  fi

  # included needed shell functions
  source "${DIR}/.solidblocks-shell/log.sh"
  source "${DIR}/.solidblocks-shell/text.sh"
  source "${DIR}/.solidblocks-shell/software.sh"

  # ensure $PATH contains all software downloaded via the `software_ensure_*` functions
  software_set_export_path
}

# bootstrap Solidblocks, and all other software needed using the software installer helpers from https://pellepelster.github.io/solidblocks/shell/software/
function task_bootstrap() {
  bootstrap_solidblocks
  ensure_environment
  software_ensure_terraform
}

# run the downloaded terraform version, ensure_environment ensures the downloaded versions takes precedence over any system binaries
function task_terraform {
  terraform -version
}

function task_log {
    log_info "info message"
    log_success "success message"
    log_warning "warning message"
    log_debug "debug message"
    log_error "error message"
}

function task_text {
    echo "${FORMAT_DIM}Dim${FORMAT_RESET}"
    echo "${FORMAT_UNDERLINE}Underline${FORMAT_RESET}"
    echo "${FORMAT_BOLD}Bold${FORMAT_RESET}"
    echo "${COLOR_RED}Red${COLOR_RESET}"
    echo "${COLOR_GREEN}Green${COLOR_RESET}"
    echo "${COLOR_YELLOW}Yellow${COLOR_RESET}"
    echo "${COLOR_BLACK}Black${COLOR_RESET}"
    echo "${COLOR_BLUE}Blue${COLOR_RESET}"
    echo "${COLOR_MAGENTA}Magenta${COLOR_RESET}"
    echo "${COLOR_CYAN}Cyan${COLOR_RESET}"
    echo "${COLOR_WHITE}White${COLOR_RESET}"
}

# provide some meaningful help using shell formatting from https://pellepelster.github.io/solidblocks/shell/text/
function task_usage {
  cat <<EOF
Usage: $0

  bootstrap             initialize the development environment
  terraform             run terraform
  log                   log some stuff
  text                  print soe fancy text formats
EOF
  exit 1
}

ARG=${1:-}
shift || true

# if we see the bootstrap command assume Solidshell is not yet initialized and skip environment setup
case "${ARG}" in
  bootstrap) ;;
  *) ensure_environment ;;
esac

case ${ARG} in
  bootstrap) task_bootstrap "$@" ;;
  terraform) task_terraform "$@" ;;
  log)       task_log "$@" ;;
  text)      task_text "$@" ;;
  *) task_usage ;;
esac