|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Installs Kubernetes/Rancher tooling and supporting utilities for DevOps workflows. |
| 4 | +# Intended for use inside the Docker image build where root privileges are available. |
| 5 | + |
| 6 | +set -euo pipefail |
| 7 | +IFS=$'\n\t' |
| 8 | + |
| 9 | +PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH" |
| 10 | + |
| 11 | +SUDO="" |
| 12 | +if [[ $(id -u) -ne 0 ]]; then |
| 13 | + if command -v sudo >/dev/null 2>&1; then |
| 14 | + SUDO="sudo" |
| 15 | + else |
| 16 | + echo "This script requires root privileges or sudo access." >&2 |
| 17 | + exit 1 |
| 18 | + fi |
| 19 | +fi |
| 20 | + |
| 21 | +case "$(uname -m)" in |
| 22 | + x86_64 | amd64) |
| 23 | + LINUX_ARCH="amd64" |
| 24 | + ;; |
| 25 | + aarch64 | arm64) |
| 26 | + LINUX_ARCH="arm64" |
| 27 | + ;; |
| 28 | + *) |
| 29 | + echo "Unsupported architecture: $(uname -m)" >&2 |
| 30 | + exit 1 |
| 31 | + ;; |
| 32 | +esac |
| 33 | + |
| 34 | +BIN_DIR="${BIN_DIR:-/usr/local/bin}" |
| 35 | +TMPDIR="$(mktemp -d)" |
| 36 | +trap 'rm -rf "${TMPDIR}"' EXIT |
| 37 | + |
| 38 | +install_apt_packages() { |
| 39 | + local packages=( |
| 40 | + bash-completion |
| 41 | + dnsutils |
| 42 | + jq |
| 43 | + mariadb-client |
| 44 | + mysql-shell |
| 45 | + netcat-openbsd |
| 46 | + nfs-common |
| 47 | + percona-toolkit |
| 48 | + socat |
| 49 | + yq |
| 50 | + ) |
| 51 | + |
| 52 | + $SUDO apt-get update |
| 53 | + $SUDO apt-get install -y --no-install-recommends "${packages[@]}" |
| 54 | + $SUDO apt-get clean |
| 55 | + $SUDO rm -rf /var/lib/apt/lists/* |
| 56 | +} |
| 57 | + |
| 58 | +fetch_latest_github_tag() { |
| 59 | + local repo="$1" |
| 60 | + curl -fsSL "https://api.github.com/repos/${repo}/releases/latest" | jq -r '.tag_name' |
| 61 | +} |
| 62 | + |
| 63 | +verify_with_checksums() { |
| 64 | + local checksum_file="$1" |
| 65 | + local artifact="$2" |
| 66 | + local pattern="$3" |
| 67 | + |
| 68 | + local sum |
| 69 | + sum="$(awk -v target="${pattern}" '$2 == target {print $1; exit}' "${checksum_file}")" |
| 70 | + if [[ -z "${sum}" ]]; then |
| 71 | + echo "Checksum for ${pattern} not found in ${checksum_file}" >&2 |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | + echo "${sum} ${artifact}" | sha256sum --check --status |
| 75 | +} |
| 76 | + |
| 77 | +install_kubectl() { |
| 78 | + local version="${KUBECTL_VERSION:-$(curl -fsSL https://dl.k8s.io/release/stable.txt)}" |
| 79 | + local artifact="${TMPDIR}/kubectl" |
| 80 | + |
| 81 | + curl -fsSLo "${artifact}" "https://dl.k8s.io/release/${version}/bin/linux/${LINUX_ARCH}/kubectl" |
| 82 | + curl -fsSLo "${artifact}.sha256" "https://dl.k8s.io/release/${version}/bin/linux/${LINUX_ARCH}/kubectl.sha256" |
| 83 | + ( |
| 84 | + cd "${TMPDIR}" |
| 85 | + printf '%s %s\n' "$(cat "$(basename "${artifact}.sha256")")" "$(basename "${artifact}")" | sha256sum --check --status - |
| 86 | + ) |
| 87 | + $SUDO install -m 0755 "${artifact}" "${BIN_DIR}/kubectl" |
| 88 | +} |
| 89 | + |
| 90 | +install_helm() { |
| 91 | + local raw_tag="${HELM_VERSION:-$(fetch_latest_github_tag helm/helm)}" |
| 92 | + local base="helm-${raw_tag}-linux-${LINUX_ARCH}" |
| 93 | + |
| 94 | + curl -fsSLo "${TMPDIR}/${base}.tar.gz" "https://get.helm.sh/${base}.tar.gz" |
| 95 | + curl -fsSLo "${TMPDIR}/${base}.tar.gz.sha256sum" "https://get.helm.sh/${base}.tar.gz.sha256sum" |
| 96 | + (cd "${TMPDIR}" && sha256sum --check "${base}.tar.gz.sha256sum") |
| 97 | + tar -xzf "${TMPDIR}/${base}.tar.gz" -C "${TMPDIR}" |
| 98 | + $SUDO install -m 0755 "${TMPDIR}/linux-${LINUX_ARCH}/helm" "${BIN_DIR}/helm" |
| 99 | +} |
| 100 | + |
| 101 | +install_rancher_cli() { |
| 102 | + local version="${RANCHER_CLI_VERSION:-2.12.3}" |
| 103 | + local tarball="rancher-linux-${LINUX_ARCH}-v${version}.tar.gz" |
| 104 | + local checksum_url="https://releases.rancher.com/cli2/v${version}/${tarball}.sha256sum" |
| 105 | + local checksum_file="${TMPDIR}/${tarball}.sha256sum" |
| 106 | + |
| 107 | + curl -fsSLo "${TMPDIR}/${tarball}" "https://releases.rancher.com/cli2/v${version}/${tarball}" |
| 108 | + if curl -fsSLo "${checksum_file}" "${checksum_url}"; then |
| 109 | + (cd "${TMPDIR}" && sha256sum --check "${tarball}.sha256sum") |
| 110 | + else |
| 111 | + rm -f "${checksum_file}" |
| 112 | + echo "Checksum file not available for Rancher CLI ${version}; skipping verification." >&2 |
| 113 | + fi |
| 114 | + tar -xzf "${TMPDIR}/${tarball}" -C "${TMPDIR}" |
| 115 | + if [[ -f "${TMPDIR}/rancher-v${version}/rancher" ]]; then |
| 116 | + $SUDO install -m 0755 "${TMPDIR}/rancher-v${version}/rancher" "${BIN_DIR}/rancher" |
| 117 | + else |
| 118 | + $SUDO install -m 0755 "${TMPDIR}/rancher-${version}/rancher" "${BIN_DIR}/rancher" |
| 119 | + fi |
| 120 | + if [[ -f "${TMPDIR}/rancher-v${version}/rancher-compose" ]]; then |
| 121 | + $SUDO install -m 0755 "${TMPDIR}/rancher-v${version}/rancher-compose" "${BIN_DIR}/rancher-compose" |
| 122 | + fi |
| 123 | +} |
| 124 | + |
| 125 | +install_kustomize() { |
| 126 | + local raw_tag="${KUSTOMIZE_VERSION:-$(fetch_latest_github_tag kubernetes-sigs/kustomize)}" |
| 127 | + local version="${raw_tag##*/}" |
| 128 | + local encoded_tag="${raw_tag//\//%2F}" |
| 129 | + local artifact="kustomize_${version}_linux_${LINUX_ARCH}.tar.gz" |
| 130 | + local checksum_file="${TMPDIR}/checksums.txt" |
| 131 | + |
| 132 | + curl -fsSLo "${TMPDIR}/${artifact}" "https://github.com/kubernetes-sigs/kustomize/releases/download/${encoded_tag}/${artifact}" |
| 133 | + curl -fsSLo "${checksum_file}" "https://github.com/kubernetes-sigs/kustomize/releases/download/${encoded_tag}/checksums.txt" |
| 134 | + verify_with_checksums "${checksum_file}" "${TMPDIR}/${artifact}" "${artifact}" |
| 135 | + tar -xzf "${TMPDIR}/${artifact}" -C "${TMPDIR}" |
| 136 | + $SUDO install -m 0755 "${TMPDIR}/kustomize" "${BIN_DIR}/kustomize" |
| 137 | +} |
| 138 | + |
| 139 | +install_k9s() { |
| 140 | + local raw_tag="${K9S_VERSION:-$(fetch_latest_github_tag derailed/k9s)}" |
| 141 | + local version="${raw_tag#v}" |
| 142 | + local artifact="k9s_Linux_${LINUX_ARCH}.tar.gz" |
| 143 | + local checksum_file="${TMPDIR}/checksums.txt" |
| 144 | + local checksum_url="https://github.com/derailed/k9s/releases/download/${raw_tag}/checksums.txt" |
| 145 | + |
| 146 | + curl -fsSLo "${TMPDIR}/${artifact}" "https://github.com/derailed/k9s/releases/download/${raw_tag}/${artifact}" |
| 147 | + if ! curl -fsSLo "${checksum_file}" "${checksum_url}"; then |
| 148 | + checksum_file="${TMPDIR}/checksums.sha256" |
| 149 | + checksum_url="https://github.com/derailed/k9s/releases/download/${raw_tag}/checksums.sha256" |
| 150 | + curl -fsSLo "${checksum_file}" "${checksum_url}" |
| 151 | + fi |
| 152 | + verify_with_checksums "${checksum_file}" "${TMPDIR}/${artifact}" "${artifact}" |
| 153 | + tar -xzf "${TMPDIR}/${artifact}" -C "${TMPDIR}" |
| 154 | + $SUDO install -m 0755 "${TMPDIR}/k9s" "${BIN_DIR}/k9s" |
| 155 | +} |
| 156 | + |
| 157 | +install_stern() { |
| 158 | + local raw_tag="${STERN_VERSION:-$(fetch_latest_github_tag stern/stern)}" |
| 159 | + local version="${raw_tag#v}" |
| 160 | + local artifact="stern_${version}_linux_${LINUX_ARCH}.tar.gz" |
| 161 | + local checksum_file="${TMPDIR}/checksums.txt" |
| 162 | + |
| 163 | + curl -fsSLo "${TMPDIR}/${artifact}" "https://github.com/stern/stern/releases/download/${raw_tag}/${artifact}" |
| 164 | + curl -fsSLo "${checksum_file}" "https://github.com/stern/stern/releases/download/${raw_tag}/checksums.txt" |
| 165 | + verify_with_checksums "${checksum_file}" "${TMPDIR}/${artifact}" "${artifact}" |
| 166 | + tar -xzf "${TMPDIR}/${artifact}" -C "${TMPDIR}" |
| 167 | + $SUDO install -m 0755 "${TMPDIR}/stern" "${BIN_DIR}/stern" |
| 168 | +} |
| 169 | + |
| 170 | +install_longhornctl() { |
| 171 | + local raw_tag="${LONGHORNCTL_VERSION:-$(fetch_latest_github_tag longhorn/cli)}" |
| 172 | + local version="${raw_tag#v}" |
| 173 | + local artifact="longhornctl-linux-${LINUX_ARCH}" |
| 174 | + |
| 175 | + curl -fsSLo "${TMPDIR}/${artifact}" "https://github.com/longhorn/cli/releases/download/${raw_tag}/${artifact}" |
| 176 | + curl -fsSLo "${TMPDIR}/${artifact}.sha256" "https://github.com/longhorn/cli/releases/download/${raw_tag}/${artifact}.sha256" |
| 177 | + (cd "${TMPDIR}" && sha256sum --check "$(basename "${artifact}.sha256")") |
| 178 | + $SUDO install -m 0755 "${TMPDIR}/${artifact}" "${BIN_DIR}/longhornctl" |
| 179 | +} |
| 180 | + |
| 181 | +install_kubectl_neat() { |
| 182 | + local raw_tag="${KUBECTL_NEAT_VERSION:-$(fetch_latest_github_tag itaysk/kubectl-neat)}" |
| 183 | + local version="${raw_tag#v}" |
| 184 | + local artifact="kubectl-neat_linux_${LINUX_ARCH}.tar.gz" |
| 185 | + local checksum_file="${TMPDIR}/checksums.txt" |
| 186 | + |
| 187 | + curl -fsSLo "${TMPDIR}/${artifact}" "https://github.com/itaysk/kubectl-neat/releases/download/${raw_tag}/${artifact}" |
| 188 | + curl -fsSLo "${checksum_file}" "https://github.com/itaysk/kubectl-neat/releases/download/${raw_tag}/checksums.txt" |
| 189 | + verify_with_checksums "${checksum_file}" "${TMPDIR}/${artifact}" "${artifact}" |
| 190 | + tar -xzf "${TMPDIR}/${artifact}" -C "${TMPDIR}" |
| 191 | + $SUDO install -m 0755 "${TMPDIR}/kubectl-neat" "${BIN_DIR}/kubectl-neat" |
| 192 | +} |
| 193 | + |
| 194 | +install_proxysql_admin() { |
| 195 | + if command -v pipx >/dev/null 2>&1; then |
| 196 | + if ! pipx list 2>/dev/null | grep -q "proxysql-admin-tool"; then |
| 197 | + if ! pipx install proxysql-admin-tool; then |
| 198 | + echo "pipx install proxysql-admin-tool failed; skipping proxysql-admin-tool installation." >&2 |
| 199 | + fi |
| 200 | + fi |
| 201 | + else |
| 202 | + echo "pipx not available; skipping proxysql-admin-tool installation." >&2 |
| 203 | + fi |
| 204 | +} |
| 205 | + |
| 206 | +install_apt_packages |
| 207 | + |
| 208 | +install_kubectl |
| 209 | +install_helm |
| 210 | +install_rancher_cli |
| 211 | +install_kustomize |
| 212 | +install_k9s |
| 213 | +install_stern |
| 214 | +install_longhornctl |
| 215 | +install_kubectl_neat |
| 216 | +install_proxysql_admin |
| 217 | + |
| 218 | +cat <<'EOF' |
| 219 | +
|
| 220 | +DevOps tooling installation completed: |
| 221 | + - kubectl |
| 222 | + - helm |
| 223 | + - rancher CLI |
| 224 | + - kustomize |
| 225 | + - k9s |
| 226 | + - stern |
| 227 | + - longhornctl |
| 228 | + - kubectl-neat |
| 229 | + - proxysql-admin-tool (via pipx) |
| 230 | + - Supporting utilities installed via apt |
| 231 | +
|
| 232 | +EOF |
0 commit comments