GoogleCloudPlatform / GoogleCloudPlatform/cloud-sql-proxy

Linux installation script

Abierto
#2,529 0 comentarios 0 reacciones 1 asignado Reclamado por @hessjcg Ver en GitHub
type: feature request
Lenguaje dominante
Go
Estrellas
1.4k
Forks
352
Merge medio
14 h 54 min
PR fusionados (30 d)
5

Descripción

### Feature Description

Bash script for automatic `cloud-sql-proxy` installation/update with latest version auto pulling from github repo.

### Sample code

```bash
#!/usr/bin/env bash
set -euo pipefail

# Cloud SQL Auth Proxy installer/updater
# - Installs to /usr/local/bin/cloud-sql-proxy
# - By default installs the latest GitHub release
#
# Usage:
# sudo ./install-cloud-sql-proxy.sh
#
# Optional env overrides:
# VERSION=v2.20.0 sudo ./install-cloud-sql-proxy.sh
# INSTALL_DIR=/usr/local/bin sudo ./install-cloud-sql-proxy.sh

REPO="GoogleCloudPlatform/cloud-sql-proxy"
INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
BIN_NAME="cloud-sql-proxy"

need_cmd() {
command -v "$1" >/dev/null 2>&1 || {
echo "Error: missing required command: $1" >&2
exit 1
}
}

need_cmd curl
need_cmd uname
need_cmd mktemp
need_cmd install

if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
echo "Error: please run as root (use sudo)." >&2
exit 1
fi

ARCH="$(uname -m)"
case "$ARCH" in
x86_64|amd64) PLATFORM="linux.amd64" ;;
aarch64|arm64) PLATFORM="linux.arm64" ;;
i386|i686) PLATFORM="linux.386" ;;
*)
echo "Error: unsupported architecture: $ARCH" >&2
exit 1
;;
esac

# Get latest version tag from GitHub unless VERSION is provided.
if [[ -n "${VERSION:-}" ]]; then
TAG="$VERSION"
else
# Using GitHub Releases "latest" endpoint; parse tag_name without jq.
# Example response includes: "tag_name": "v2.20.0"
TAG="$(
curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" \
| sed -nE 's/.*"tag_name"[[:space:]]*:[[:space:]]*"([^"]+)".*/\1/p' \
| head -n1
)"
if [[ -z "$TAG" ]]; then
echo "Error: could not determine latest release tag from GitHub." >&2
exit 1
fi
fi

BASE_URL="https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/${TAG}"
FILE_NAME="${BIN_NAME}.${PLATFORM}"
DOWNLOAD_URL="${BASE_URL}/${FILE_NAME}"

TMPDIR="$(mktemp -d)"
cleanup() { rm -rf "$TMPDIR"; }
trap cleanup EXIT

echo "Installing ${BIN_NAME} ${TAG} (${PLATFORM})"
echo "Downloading: ${DOWNLOAD_URL}"

curl -fsSL "$DOWNLOAD_URL" -o "${TMPDIR}/${BIN_NAME}"
chmod +x "${TMPDIR}/${BIN_NAME}"

# Install atomically with correct permissions
install -m 0755 "${TMPDIR}/${BIN_NAME}" "${INSTALL_DIR}/${BIN_NAME}"

echo "Installed to: ${INSTALL_DIR}/${BIN_NAME}"
echo -n "Installed binary reports: "
if "${INSTALL_DIR}/${BIN_NAME}" --version >/dev/null 2>&1; then
"${INSTALL_DIR}/${BIN_NAME}" --version
else
echo "(version flag not available; run '${BIN_NAME} --help' to verify)"
fi
```

### Alternatives Considered

For now you need to do it manually, of course it's easy, but I guess this script can save a little bit of time for somebody.

### Additional Details

Tested on `Ubuntu 24.04.3 LTS`.
I didn't create a PR because this may be just a script available to download/use only from documentation site.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.