Last active
March 26, 2026 22:02
-
-
Save sony-level/5a3d8dee19c0936ddd33c67111df0b2b to your computer and use it in GitHub Desktop.
install-terraform-docker.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -euo pipefail | |
| # ============================================================================= | |
| # Install Terraform in a Docker container (no sudo required) | |
| # | |
| # Usage: | |
| # curl -fsSL "<gist_raw_url>" | bash | |
| # # or with a specific version: | |
| # curl -fsSL "<gist_raw_url>" | TF_VERSION=1.12.0 bash | |
| # | |
| # Maintainer: level-sony.fr | |
| # Support: https://level-sony.fr/en/contact/ | |
| # ============================================================================= | |
| TF_VERSION="${TF_VERSION:-1.12.0}" | |
| INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}" | |
| log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] $*"; } | |
| err() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] [ERROR] $*" >&2; } | |
| log "Starting Terraform installation (v${TF_VERSION})..." | |
| log "Maintainer: level-sony.fr" | |
| log "Support: https://level-sony.fr/en/contact/" | |
| # --- Check if already installed ------------------------------------------- | |
| if command -v terraform >/dev/null 2>&1; then | |
| CURRENT=$(terraform --version -json 2>/dev/null | grep -o '"terraform_version":"[^"]*"' | cut -d'"' -f4 || terraform --version | head -1 | awk '{print $2}' | tr -d 'v') | |
| if [ "$CURRENT" = "$TF_VERSION" ]; then | |
| log "Terraform v${TF_VERSION} is already installed. Nothing to do." | |
| exit 0 | |
| else | |
| log "Terraform v${CURRENT} found. Upgrading to v${TF_VERSION}..." | |
| fi | |
| fi | |
| # --- Detect architecture --------------------------------------------------- | |
| ARCH=$(uname -m) | |
| case "$ARCH" in | |
| x86_64) ARCH="amd64" ;; | |
| aarch64) ARCH="arm64" ;; | |
| armv7l) ARCH="arm" ;; | |
| *) err "Unsupported architecture: $ARCH"; exit 1 ;; | |
| esac | |
| OS=$(uname -s | tr '[:upper:]' '[:lower:]') | |
| log "Platform: ${OS}/${ARCH}" | |
| # --- Install unzip if missing ---------------------------------------------- | |
| if ! command -v unzip >/dev/null 2>&1; then | |
| log "unzip not found. Installing..." | |
| if command -v apt-get >/dev/null 2>&1; then | |
| apt-get update -qq && apt-get install -y -qq unzip >/dev/null 2>&1 | |
| elif command -v apk >/dev/null 2>&1; then | |
| apk add --no-cache unzip >/dev/null 2>&1 | |
| elif command -v yum >/dev/null 2>&1; then | |
| yum install -y unzip >/dev/null 2>&1 | |
| else | |
| err "Cannot install unzip. Please install it manually." | |
| exit 1 | |
| fi | |
| log "unzip installed." | |
| fi | |
| # --- Download and install --------------------------------------------------- | |
| DOWNLOAD_URL="https://releases.hashicorp.com/terraform/${TF_VERSION}/terraform_${TF_VERSION}_${OS}_${ARCH}.zip" | |
| TMP_FILE="/tmp/terraform_${TF_VERSION}.zip" | |
| log "Downloading: ${DOWNLOAD_URL}" | |
| curl -fsSL "$DOWNLOAD_URL" -o "$TMP_FILE" | |
| log "Extracting to ${INSTALL_DIR}..." | |
| unzip -o "$TMP_FILE" -d "$INSTALL_DIR" >/dev/null | |
| rm -f "$TMP_FILE" | |
| # --- Verify ----------------------------------------------------------------- | |
| if [ -x "${INSTALL_DIR}/terraform" ]; then | |
| log "Terraform $("${INSTALL_DIR}/terraform" --version | head -1) installed successfully." | |
| else | |
| err "Installation failed. terraform not found in PATH." | |
| exit 1 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment