#!/bin/bash # ============================================================================== # helm-install.sh # # Installs or updates Helm v3 and v4 to the latest stable releases. # # Usage: # curl -fsSL https://curl-pipe-bash.watzinger.biz/helm-install.sh | sudo bash # # What it does: # - Resolves the latest stable release for Helm v3 and v4 via GitHub API # - Skips download if the version is already installed (idempotent) # - Verifies the SHA-256 checksum of each downloaded tarball # - Installs to /opt/helm/// with /opt/helm//latest symlinks # - Removes the previous versioned install directory after an update # - Adds both major versions to PATH via /etc/profile.d/helm.sh (system-wide) # - Appends a source line to ~/.bashrc (non-login interactive shells); when # run via `sudo bash`, the invoking user's ~/.bashrc, not root's # - Cleans up temp files on exit, even on failure # # Requirements: curl, tar, sha256sum, sudo # ============================================================================== set -euo pipefail # ── Configuration ────────────────────────────────────────────────────────────── HELM_BASE="/opt/helm" PROFILE_SCRIPT="/etc/profile.d/helm.sh" OS="$(uname -s | tr '[:upper:]' '[:lower:]')" ARCH="$(uname -m)" TMP_DIR="$(mktemp -d)" case "${ARCH}" in x86_64) ARCH="amd64" ;; aarch64|arm64) ARCH="arm64" ;; armv7l) ARCH="arm" ;; i686) ARCH="386" ;; *) echo "[ERROR] Unsupported architecture: ${ARCH}"; exit 1 ;; esac # ── Cleanup on exit ──────────────────────────────────────────────────────────── cleanup() { rm -rf "$TMP_DIR" } trap cleanup EXIT # ── Helpers ──────────────────────────────────────────────────────────────────── err() { echo "[ERROR] $*" >&2; } info() { echo "[INFO] $*"; } # ── Dependency check ─────────────────────────────────────────────────────────── check_dependencies() { local missing=() for cmd in curl tar sha256sum; do command -v "$cmd" &>/dev/null || missing+=("$cmd") done if [ ${#missing[@]} -gt 0 ]; then err "Missing required tools: ${missing[*]}" err "Install them and re-run this script." exit 1 fi } # ── Resolve latest stable version for a given major ─────────────────────────── get_latest_version() { local major="$1" curl -fsSL "https://api.github.com/repos/helm/helm/releases" \ | grep '"tag_name"' \ | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' \ | grep -v -E '(alpha|beta|rc)' \ | grep "^v${major}\." \ | sort -V \ | tail -n1 } # ── Download, verify, and install a single Helm version ─────────────────────── install_helm_version() { local major="$1" local version="$2" # e.g. v3.17.3 local ver_no_v="${version#v}" # e.g. 3.17.3 local install_dir="${HELM_BASE}/${major}/${ver_no_v}" local latest_link="${HELM_BASE}/${major}/latest" local tarball="helm-${version}-${OS}-${ARCH}.tar.gz" local base_url="https://get.helm.sh" if [[ -x "${install_dir}/helm" ]]; then info "[helm${major}] ${version} already installed at ${install_dir} - skipping download." else info "[helm${major}] Downloading ${version}..." curl -fsSL --progress-bar "${base_url}/${tarball}" -o "${TMP_DIR}/${tarball}" info "[helm${major}] Verifying checksum..." local expected_hash expected_hash="$(curl -fsSL "${base_url}/${tarball}.sha256")" echo "${expected_hash} ${TMP_DIR}/${tarball}" | sha256sum --check --status info "[helm${major}] Checksum verified OK." info "[helm${major}] Installing ${version}..." sudo mkdir -p "${install_dir}" sudo tar -xzf "${TMP_DIR}/${tarball}" -C "${TMP_DIR}" sudo mv "${TMP_DIR}/${OS}-${ARCH}/helm" "${install_dir}/helm" sudo chmod +x "${install_dir}/helm" info "[helm${major}] Installed ${version} → ${install_dir}/helm" fi # Capture old install dir before updating the symlink local old_install_dir="" if [[ -L "${latest_link}" ]]; then old_install_dir="$(readlink -f "${latest_link}")" fi local current_ver="" [[ -n "${old_install_dir}" ]] && current_ver="$(basename "${old_install_dir}")" if [[ -z "${current_ver}" ]]; then sudo ln -sfn "${install_dir}" "${latest_link}" info "[helm${major}] Symlink created: ${latest_link} → ${install_dir}" else local higher higher="$(printf '%s\n%s\n' "${current_ver}" "${ver_no_v}" | sort -V | tail -n1)" if [[ "${higher}" == "${ver_no_v}" && "${ver_no_v}" != "${current_ver}" ]]; then sudo ln -sfn "${install_dir}" "${latest_link}" info "[helm${major}] Symlink updated: ${latest_link} → ${install_dir} (was ${current_ver})" if [[ -n "${old_install_dir}" && -d "${old_install_dir}" \ && "${old_install_dir}" != "${install_dir}" ]]; then info "[helm${major}] Removing old install: ${old_install_dir}" sudo rm -rf "${old_install_dir}" fi else info "[helm${major}] Symlink unchanged at ${current_ver} (${ver_no_v} is not newer)." fi fi } # ── Configure system-wide environment ───────────────────────────────────────── configure_environment() { if [ -f "$PROFILE_SCRIPT" ] && grep -q "helm" "$PROFILE_SCRIPT"; then info "System profile already configured in $PROFILE_SCRIPT." else info "Writing environment config to $PROFILE_SCRIPT..." sudo tee "$PROFILE_SCRIPT" > /dev/null <<'EOF' # Helm environment - managed by helm-install.sh export PATH="${PATH}:/opt/helm/3/latest:/opt/helm/4/latest" EOF sudo chmod 644 "$PROFILE_SCRIPT" info "System profile written: $PROFILE_SCRIPT" fi # Under `sudo bash`, $HOME is /root, so register in the invoking user's # ~/.bashrc instead, so the PATH entries land where they'll actually be used. local target_user target_home if [ "$(id -u)" -eq 0 ] && [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then target_user="$SUDO_USER" target_home=$(getent passwd "$target_user" | cut -d: -f6) else target_user="$(id -un)" target_home="$HOME" fi if [ -z "$target_home" ] || [ ! -d "$target_home" ]; then err "Could not determine home directory for user $target_user." exit 1 fi local bashrc="$target_home/.bashrc" local bashrc_marker="# helm-install.sh" if grep -q "$bashrc_marker" "$bashrc" 2>/dev/null; then info "$bashrc already sources $PROFILE_SCRIPT." else info "Adding source line to $bashrc for non-login shells..." local created=false [ -e "$bashrc" ] || created=true cat >> "$bashrc" <