#!/usr/bin/env bash # shellcheck disable=SC2034 # shared helpers set variables not every script uses # Installs the latest k9s (terminal UI for Kubernetes), verified with its SHA-256 checksum. # Source: https://bianconi.tech/s/k9s # Author: Luis Fernando Bianconi (https://bianconi.tech) main() { SCRIPT_URL="https://bianconi.tech/s/k9s" SITE_URL="https://bianconi.tech" # ---- common helpers (shared by every script) ---- set -euo pipefail info() { printf '\033[1;34m==>\033[0m %s\n' "$*"; } ok() { printf '\033[1;32m✔\033[0m %s\n' "$*"; } warn() { printf '\033[1;33m!\033[0m %s\n' "$*" >&2; } fail() { printf '\033[1;31m✘\033[0m %s\n' "$*" >&2; exit 1; } require_root() { [ "$(id -u)" -eq 0 ] || fail "Run as root: curl -fsSL ${SCRIPT_URL} | sudo bash" } # Detects the package manager (apt or dnf) from /etc/os-release detect_os() { [ -r /etc/os-release ] || fail "Unsupported system: /etc/os-release not found" # shellcheck disable=SC1091 . /etc/os-release OS_ID="${ID:-unknown}" OS_VERSION="${VERSION_ID:-}" OS_CODENAME="${UBUNTU_CODENAME:-${VERSION_CODENAME:-}}" case "${ID:-} ${ID_LIKE:-}" in *debian*|*ubuntu*) PM=apt ;; *rhel*|*fedora*|*centos*) PM=dnf ;; *) fail "Unsupported distribution: ${PRETTY_NAME:-$OS_ID}. Supported: Ubuntu, Debian, RHEL, Rocky, AlmaLinux, Fedora" ;; esac info "Detected ${PRETTY_NAME:-$OS_ID}" } APT_UPDATED=0 pkg_install() { if [ "$PM" = apt ]; then if [ "$APT_UPDATED" -eq 0 ]; then apt-get update -qq; APT_UPDATED=1; fi DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "$@" >/dev/null else dnf install -y -q --allowerasing "$@" >/dev/null fi } # Forces the next pkg_install to refresh apt lists (after adding a repository) apt_refresh() { APT_UPDATED=0; } # EPEL provides extra packages (htop, fail2ban...) on RHEL-like systems enable_epel() { [ "$PM" = dnf ] || return 0 case "$OS_ID" in fedora) return 0 ;; rhel) dnf install -y -q "https://dl.fedoraproject.org/pub/epel/epel-release-latest-${OS_VERSION%%.*}.noarch.rpm" >/dev/null ;; *) dnf install -y -q epel-release >/dev/null ;; esac } cpu_arch() { case "$(uname -m)" in x86_64|amd64) echo amd64 ;; aarch64|arm64) echo arm64 ;; *) fail "Unsupported CPU architecture: $(uname -m)" ;; esac } has_systemd() { [ -d /run/systemd/system ]; } # The user who ran sudo (empty when logged in directly as root) TARGET_USER="${SUDO_USER:-}" [ "$TARGET_USER" = root ] && TARGET_USER="" # For scripts that configure a user's home: sets USER_NAME, USER_GROUP and HOME_DIR setup_user() { USER_NAME="${TARGET_USER:-root}" HOME_DIR="$(getent passwd "$USER_NAME" | cut -d: -f6)" [ -d "$HOME_DIR" ] || fail "Home folder for $USER_NAME not found" USER_GROUP="$(id -gn "$USER_NAME")" } # Runs a command as that user, so files in their home stay theirs run_as_user() { if [ "$USER_NAME" = root ]; then bash -c "$1" else sudo -u "$USER_NAME" -H bash -c "$1" fi } TMP_DIR="$(mktemp -d)" trap 'rm -rf "$TMP_DIR"' EXIT # ---- end of common helpers ---- require_root ARCH="$(cpu_arch)" FILE="k9s_Linux_${ARCH}.tar.gz" BASE="https://github.com/derailed/k9s/releases/latest/download" info "Downloading the latest k9s (${ARCH})" curl -fsSL "${BASE}/${FILE}" -o "$TMP_DIR/${FILE}" curl -fsSL "${BASE}/checksums.sha256" | grep " ${FILE}\$" > "$TMP_DIR/checksum" || fail "Checksum for ${FILE} not found" (cd "$TMP_DIR" && sha256sum -c --quiet checksum) || fail "Checksum verification failed" tar -xzf "$TMP_DIR/${FILE}" -C "$TMP_DIR" k9s install -m 0755 "$TMP_DIR/k9s" /usr/local/bin/k9s ok "k9s installed: $(k9s version --short 2>/dev/null | head -n1)" } main "$@"