diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 9ba1e62..57572c7 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -11,7 +11,7 @@ jobs: pull-requests: write packages: write steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: "Publish Features" uses: devcontainers/action@v1 diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 037d72b..1551e50 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -19,8 +19,8 @@ on: jobs: test-autogenerated: runs-on: ubuntu-latest - continue-on-error: true strategy: + fail-fast: false matrix: features: - localstack-cli @@ -28,8 +28,9 @@ jobs: - debian:latest - ubuntu:latest - mcr.microsoft.com/devcontainers/base:ubuntu + - mcr.microsoft.com/devcontainers/base:alpine steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: "Install latest devcontainer CLI" run: npm install -g @devcontainers/cli @@ -39,28 +40,15 @@ jobs: test-scenarios: runs-on: ubuntu-latest - continue-on-error: true strategy: matrix: features: - localstack-cli steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: "Install latest devcontainer CLI" run: npm install -g @devcontainers/cli - name: "Generating tests for '${{ matrix.features }}' scenarios" run: devcontainer features test -f ${{ matrix.features }} --skip-autogenerated --skip-duplicated . - - test-global: - runs-on: ubuntu-latest - continue-on-error: true - steps: - - uses: actions/checkout@v3 - - - name: "Install latest devcontainer CLI" - run: npm install -g @devcontainers/cli - - - name: "Testing global scenarios" - run: devcontainer features test --global-scenarios-only . diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 1f8ae55..65b51aa 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -12,7 +12,7 @@ jobs: validate: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: "Validate devcontainer-feature.json files" uses: devcontainers/action@v1 diff --git a/README.md b/README.md index fe91245..a51999e 100644 --- a/README.md +++ b/README.md @@ -11,29 +11,44 @@ Features are designed to install atop a wide-range of base container images (thi > This repo currently a work in progress which might introduce breaking changes without notification. ## List of features -- [LocalStack](https://docs.localstack.cloud/getting-started/): install the LocalStack CLI tool and any of the related _Local™_ tools for: - - AWS CLI - - AWS CDK - - AWS SAM CLI - - Terraform - - Pulumi +- [LocalStack CLI](src/localstack-cli): installs [`lstk`](https://docs.localstack.cloud/aws/developer-tools/running-localstack/lstk/), the LocalStack CLI. + `lstk` starts and manages LocalStack and runs the AWS CLI, Terraform, AWS CDK and AWS SAM against it, replacing `awslocal`, `tflocal`, `cdklocal` and `samlocal`. + It can also install `pulumilocal` for Pulumi. + +Version 1.x of the Feature installs `lstk`. Version 0.x installed the legacy Python `localstack` CLI and is still available as `localstack-cli:0`. ## Usage To reference a feature from this repository, add the desired features to a `devcontainer.json`. Each feature has a `README.md` that shows how to reference the feature and which options are available for that feature. -The example below installs the LocalStack CLI declared in the `./src` directory of this repository. - -See the relevant feature's README for supported options. +The example below installs `lstk` together with Docker-in-Docker and the AWS CLI, which `lstk` needs to start LocalStack and run `lstk aws`. ```json { - "image": "mcr.microsoft.com/devcontainers/base:ubuntu", + "image": "mcr.microsoft.com/devcontainers/base:noble", "features": { - "ghcr.io/localstack/devcontainer-feature/localstack-cli": {} + "ghcr.io/devcontainers/features/docker-in-docker:4": {}, + "ghcr.io/devcontainers/features/aws-cli:1": {}, + "ghcr.io/localstack/devcontainer-feature/localstack-cli:1": {} + }, + "remoteEnv": { + "LOCALSTACK_AUTH_TOKEN": "${localEnv:LOCALSTACK_AUTH_TOKEN}" } } ``` + +For a ready-made setup that also starts LocalStack, use the [LocalStack DevContainer Templates](https://github.com/localstack/devcontainer-template). + +## Testing + +The tests use the [Dev Container CLI](https://github.com/devcontainers/cli): + +```bash +npm install -g @devcontainers/cli +devcontainer features test -f localstack-cli --skip-autogenerated . # scenarios in test/localstack-cli/scenarios.json +devcontainer features test -f localstack-cli --skip-scenarios -i debian:latest . +``` + ## Repo and Feature Structure Similar to the [`devcontainers/features`](https://github.com/devcontainers/features) repo, this repository has a `src` folder. Each feature has its own sub-folder, containing at least a `devcontainer-feature.json` and an entrypoint script `install.sh`. diff --git a/src/localstack-cli/NOTES.md b/src/localstack-cli/NOTES.md index dae03e9..acc1354 100644 --- a/src/localstack-cli/NOTES.md +++ b/src/localstack-cli/NOTES.md @@ -1,13 +1,51 @@ -Available versions of the Localstack CLI can be found here: https://pypi.org/project/localstack/. +This Feature installs [`lstk`](https://docs.localstack.cloud/aws/developer-tools/running-localstack/lstk/), the LocalStack CLI, from its [GitHub releases](https://github.com/localstack/lstk/releases). +The download is checked against the release checksums. +Bash, zsh and fish completions and man pages are installed too. -## OS Support +## Using lstk in the container -This Feature should work on recent versions of Debian/Ubuntu-based distributions with the `apt` package manager installed. +`lstk` needs a Docker daemon to start LocalStack. +Add the `docker-in-docker` or `docker-outside-of-docker` Feature, or use one of the [LocalStack DevContainer Templates](https://github.com/localstack/devcontainer-template). -`bash` is required to execute the `install.sh` script. +Pass your [auth token](https://docs.localstack.cloud/aws/getting-started/auth-token/) from the host: -`cdklocal` option requires `npm` to be installed which is not handled by this feature. -All other tools are installed by `pipx` which is installed via package manager or the Python installation. The latter can be combined with the Python feature. +```json +"remoteEnv": { + "LOCALSTACK_AUTH_TOKEN": "${localEnv:LOCALSTACK_AUTH_TOKEN}" +} +``` -> [!NOTE] -> None of the wrapped tool(s) (ie Terraform, Pulumi or AWS CLI...etc), are handled by this feature, to install the underlying tool(s) please use the respective feature from the [DevContainer Feature Community Index](https://containers.dev/features). \ No newline at end of file +You can also run `lstk login` in a container terminal. + +`lstk start` refuses to run when `AWS_ENDPOINT_URL` or `LSTK_ENDPOINT_URL` is set, because those variables tell `lstk` to target an emulator it does not manage. +Set them only when LocalStack runs outside the container, for example as a Docker Compose service. + +## Tool integrations + +`lstk` replaces the `awslocal`, `tflocal`, `cdklocal` and `samlocal` wrapper scripts. +It does not install the underlying tools. Add the matching Feature for each one you use: + +| Command | Replaces | Feature for the underlying tool | +| --- | --- | --- | +| `lstk aws` | `awslocal` | `ghcr.io/devcontainers/features/aws-cli:1` | +| `lstk terraform`, `lstk tf` | `tflocal` | `ghcr.io/devcontainers/features/terraform:1` | +| `lstk cdk` | `cdklocal` | `ghcr.io/devcontainers-extra/features/aws-cdk:2` (CDK 2.177.0 or newer) | +| `lstk sam` | `samlocal` | `ghcr.io/customink/codespaces-features/sam-cli:1` (SAM CLI 1.95.0 or newer) | + +There is no `lstk` command for Pulumi. +Set `pulumilocal: true` to install the [`pulumi-local`](https://github.com/localstack/pulumi-local) wrapper with `pipx`, and add `ghcr.io/devcontainers-extra/features/pulumi:1` for Pulumi itself. +This is the only option that pulls in Python. + +## Migrating from 0.x + +Version 1.0.0 of this Feature drops the legacy Python `localstack` CLI and the `awslocal`, `cdklocal`, `samlocal`, `tflocal` and `installUsingPython` options. +The `version` option now selects an `lstk` release. +See the [lstk migration guide](https://docs.localstack.cloud/aws/developer-tools/running-localstack/lstk/migration/) for the command mapping. + +If you still need the legacy CLI, pin the old major version: `ghcr.io/localstack/devcontainer-feature/localstack-cli:0`. + +## OS support + +The Feature works on Debian, Ubuntu and Alpine based images on `amd64` and `arm64`. +It installs `curl`, `tar`, `gzip` and `ca-certificates` when they are missing, and `pipx` when `pulumilocal` is enabled. +`bash` is required to run `install.sh`. diff --git a/src/localstack-cli/README.md b/src/localstack-cli/README.md index af6980f..aef22a1 100644 --- a/src/localstack-cli/README.md +++ b/src/localstack-cli/README.md @@ -1,13 +1,13 @@ -# LocalStack CLI (localstack-cli) +# LocalStack CLI (lstk) (localstack-cli) -Installs the Localstack CLI along with needed dependencies and popular "local" tools. +Installs lstk, the LocalStack CLI. lstk starts and manages LocalStack and runs the AWS CLI, Terraform, AWS CDK and AWS SAM against it. ## Example Usage ```json "features": { - "ghcr.io/localstack/devcontainer-feature/localstack-cli:0": {} + "ghcr.io/localstack/devcontainer-feature/localstack-cli:1": {} } ``` @@ -15,27 +15,61 @@ Installs the Localstack CLI along with needed dependencies and popular "local" t | Options Id | Description | Type | Default Value | |-----|-----|-----|-----| -| version | Select or enter a Localstack CLI version. | string | latest | -| awslocal | Install awscli-local, a thin wrapper of AWS CLI for LocalStack. | boolean | false | -| cdklocal | Install aws-cdk-local, a thin wrapper of AWS CDK for LocalStack. | boolean | false | -| pulumilocal | Install pulumi-local, a thin wrapper of Pulumi for LocalStack. | boolean | false | -| samlocal | Install aws-sam-cli-local, a thin wrapper of AWS SAM CLI for LocalStack. | boolean | false | -| tflocal | Install terraform-local, a thin wrapper of Terraform for LocalStack. | boolean | false | -| installUsingPython | Install LocalStack CLI using Python instead of pipx | boolean | false | +| version | Select or enter an lstk version (see https://github.com/localstack/lstk/releases). | string | latest | +| pulumilocal | Install pulumi-local (pulumilocal), the LocalStack wrapper for Pulumi. lstk has no Pulumi command. Installs pipx and Python. | boolean | false | -Available versions of the Localstack CLI can be found here: https://pypi.org/project/localstack/. +This Feature installs [`lstk`](https://docs.localstack.cloud/aws/developer-tools/running-localstack/lstk/), the LocalStack CLI, from its [GitHub releases](https://github.com/localstack/lstk/releases). +The download is checked against the release checksums. +Bash, zsh and fish completions and man pages are installed too. -## OS Support +## Using lstk in the container -This Feature should work on recent versions of Debian/Ubuntu-based distributions with the `apt` package manager installed. +`lstk` needs a Docker daemon to start LocalStack. +Add the `docker-in-docker` or `docker-outside-of-docker` Feature, or use one of the [LocalStack DevContainer Templates](https://github.com/localstack/devcontainer-template). -`bash` is required to execute the `install.sh` script. +Pass your [auth token](https://docs.localstack.cloud/aws/getting-started/auth-token/) from the host: -`cdklocal` option requires `npm` to be installed which is not handled by this feature. -All other tools are installed by `pipx` which is installed via package manager or the Python installation. The latter can be combined with the Python feature. +```json +"remoteEnv": { + "LOCALSTACK_AUTH_TOKEN": "${localEnv:LOCALSTACK_AUTH_TOKEN}" +} +``` + +You can also run `lstk login` in a container terminal. + +`lstk start` refuses to run when `AWS_ENDPOINT_URL` or `LSTK_ENDPOINT_URL` is set, because those variables tell `lstk` to target an emulator it does not manage. +Set them only when LocalStack runs outside the container, for example as a Docker Compose service. + +## Tool integrations + +`lstk` replaces the `awslocal`, `tflocal`, `cdklocal` and `samlocal` wrapper scripts. +It does not install the underlying tools. Add the matching Feature for each one you use: + +| Command | Replaces | Feature for the underlying tool | +| --- | --- | --- | +| `lstk aws` | `awslocal` | `ghcr.io/devcontainers/features/aws-cli:1` | +| `lstk terraform`, `lstk tf` | `tflocal` | `ghcr.io/devcontainers/features/terraform:1` | +| `lstk cdk` | `cdklocal` | `ghcr.io/devcontainers-extra/features/aws-cdk:2` (CDK 2.177.0 or newer) | +| `lstk sam` | `samlocal` | `ghcr.io/customink/codespaces-features/sam-cli:1` (SAM CLI 1.95.0 or newer) | + +There is no `lstk` command for Pulumi. +Set `pulumilocal: true` to install the [`pulumi-local`](https://github.com/localstack/pulumi-local) wrapper with `pipx`, and add `ghcr.io/devcontainers-extra/features/pulumi:1` for Pulumi itself. +This is the only option that pulls in Python. + +## Migrating from 0.x + +Version 1.0.0 of this Feature drops the legacy Python `localstack` CLI and the `awslocal`, `cdklocal`, `samlocal`, `tflocal` and `installUsingPython` options. +The `version` option now selects an `lstk` release. +See the [lstk migration guide](https://docs.localstack.cloud/aws/developer-tools/running-localstack/lstk/migration/) for the command mapping. + +If you still need the legacy CLI, pin the old major version: `ghcr.io/localstack/devcontainer-feature/localstack-cli:0`. + +## OS support + +The Feature works on Debian, Ubuntu and Alpine based images on `amd64` and `arm64`. +It installs `curl`, `tar`, `gzip` and `ca-certificates` when they are missing, and `pipx` when `pulumilocal` is enabled. +`bash` is required to run `install.sh`. -> [!NOTE] -> None of the wrapped tool(s) (ie Terraform, Pulumi or AWS CLI...etc), are handled by this feature, to install the underlying tool(s) please use the respective feature from the [DevContainer Feature Community Index](https://containers.dev/features). --- diff --git a/src/localstack-cli/devcontainer-feature.json b/src/localstack-cli/devcontainer-feature.json index 5f3d4a5..5ce074e 100644 --- a/src/localstack-cli/devcontainer-feature.json +++ b/src/localstack-cli/devcontainer-feature.json @@ -1,68 +1,33 @@ { "id": "localstack-cli", - "version": "0.1.3", - "name": "LocalStack CLI", + "version": "1.0.0", + "name": "LocalStack CLI (lstk)", "documentationURL": "https://github.com/localstack/devcontainer-feature/tree/main/src/localstack-cli", - "description": "Installs the Localstack CLI along with needed dependencies and popular \"local\" tools.", + "description": "Installs lstk, the LocalStack CLI. lstk starts and manages LocalStack and runs the AWS CLI, Terraform, AWS CDK and AWS SAM against it.", "options": { "version": { "type": "string", "proposals": [ - "", - "none", - "lts", "latest", - "stable", - "3.6.0", - "3.5.0", - "3.4.0", - "3.3.0", - "3.2.0", - "3.1.0", - "3.0.2", - "3.0.1" + "1.5.0", + "1.4.5" ], "default": "latest", - "description": "Select or enter a Localstack CLI version." - }, - "awslocal": { - "type": "boolean", - "default": false, - "description": "Install awscli-local, a thin wrapper of AWS CLI for LocalStack." - }, - "cdklocal": { - "type": "boolean", - "default": false, - "description": "Install aws-cdk-local, a thin wrapper of AWS CDK for LocalStack." + "description": "Select or enter an lstk version (see https://github.com/localstack/lstk/releases)." }, "pulumilocal": { "type": "boolean", "default": false, - "description": "Install pulumi-local, a thin wrapper of Pulumi for LocalStack." - }, - "samlocal": { - "type": "boolean", - "default": false, - "description": "Install aws-sam-cli-local, a thin wrapper of AWS SAM CLI for LocalStack." - }, - "tflocal": { - "type": "boolean", - "default": false, - "description": "Install terraform-local, a thin wrapper of Terraform for LocalStack." - }, - "installUsingPython": { - "type": "boolean", - "description": "Install LocalStack CLI using Python instead of pipx", - "default": false + "description": "Install pulumi-local (pulumilocal), the LocalStack wrapper for Pulumi. lstk has no Pulumi command. Installs pipx and Python." } }, + "keywords": [ + "localstack", + "lstk", + "aws", + "emulator" + ], "installsAfter": [ - "ghcr.io/devcontainers/features/python", - "ghcr.io/devcontainers/features/node", - "ghcr.io/devcontainers/features/aws-cli", - "ghcr.io/devcontainers-extra/features/aws-cdk", - "ghcr.io/customink/codespaces-features/sam-cli", - "ghcr.io/devcontainers-extra/features/pulumi", - "ghcr.io/devcontainers/features/terraform" + "ghcr.io/devcontainers/features/python" ] } diff --git a/src/localstack-cli/install.sh b/src/localstack-cli/install.sh index c2c0305..ce39ade 100755 --- a/src/localstack-cli/install.sh +++ b/src/localstack-cli/install.sh @@ -1,236 +1,177 @@ -#!/bin/bash +#!/usr/bin/env bash +# Installs lstk, the LocalStack CLI, from its GitHub release archive. +set -euo pipefail -set -e +LSTK_VERSION="${VERSION:-latest}" +INSTALL_PULUMILOCAL="${PULUMILOCAL:-false}" +LSTK_REPO="https://github.com/localstack/lstk" +INSTALL_DIR="/usr/local/lib/lstk" +BIN_LINK="/usr/local/bin/lstk" -LS_VERSION=${VERSION:-"latest"} -INSTALL_USING_PYTHON=${INSTALLUSINGPYTHON:-false} -test "${AWSLOCAL:-false}" = true && LOCAL_AWS=awscli-local -test "${CDKLOCAL:-false}" = true && LOCAL_CDK=aws-cdk-local -test "${PULUMILOCAL:-false}" = true && LOCAL_PULUMI=pulumi-local -test "${TFLOCAL:-false}" = true && LOCAL_TF="terraform-local" -test "${SAMLOCAL:-false}" = true && LOCAL_SAM="aws-sam-cli-local" -PREFIX=LOCAL_ - -eval 'TOOLS=(${!'"$PREFIX"'@})' - -# Clean up -rm -rf /var/lib/apt/lists/* +log() { echo "(*) $*"; } +fail() { echo "(!) $*" >&2; exit 1; } if [ "$(id -u)" -ne 0 ]; then - echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' - exit 1 -fi - -# Bring in ID, ID_LIKE, VERSION_ID, VERSION_CODENAME -. /etc/os-release -# Get an adjusted ID independent of distro variants -MAJOR_VERSION_ID=$(echo ${VERSION_ID} | cut -d . -f 1) -if [ "${ID}" = "debian" ] || [ "${ID_LIKE}" = "debian" ]; then - ADJUSTED_ID="debian" -else - echo "Linux distro ${ID} not supported." - exit 1 -fi - -# Setup INSTALL_CMD & PKG_MGR_CMD -if type apt-get > /dev/null 2>&1; then - PKG_MGR_CMD=apt-get - INSTALL_CMD="${PKG_MGR_CMD} -y install --no-install-recommends" -elif type microdnf > /dev/null 2>&1; then - PKG_MGR_CMD=microdnf - INSTALL_CMD="${PKG_MGR_CMD} ${INSTALL_CMD_ADDL_REPOS} -y install --refresh --best --nodocs --noplugins --setopt=install_weak_deps=0" -elif type dnf > /dev/null 2>&1; then - PKG_MGR_CMD=dnf - INSTALL_CMD="${PKG_MGR_CMD} ${INSTALL_CMD_ADDL_REPOS} -y install --refresh --best --nodocs --noplugins --setopt=install_weak_deps=0" -else - PKG_MGR_CMD=yum - INSTALL_CMD="${PKG_MGR_CMD} ${INSTALL_CMD_ADDL_REPOS} -y install --noplugins --setopt=install_weak_deps=0" + fail 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' fi -# Ensure apt is in non-interactive to avoid prompts -export DEBIAN_FRONTEND=noninteractive - -# General requirements -REQUIRED_PKGS="" -case ${ADJUSTED_ID} in - debian) - REQUIRED_PKGS="${REQUIRED_PKGS} \ - gcc \ - python3-dev \ - python3-venv" - ;; -esac - -pkg_mgr_update() { - case ${ADJUSTED_ID} in - debian) - if [ "$(find /var/lib/apt/lists -iname '*' | wc -l)" = "1" ]; then - echo "Running apt-get update..." - ${PKG_MGR_CMD} update -y - fi - ;; - esac +# curl, tar, gzip and CA certificates are the only runtime needs. +# lstk itself is a static binary, so no Python or Node.js is required. +install_prerequisites() { + local missing=() + command -v curl > /dev/null 2>&1 || missing+=(curl) + command -v tar > /dev/null 2>&1 || missing+=(tar) + command -v gzip > /dev/null 2>&1 || missing+=(gzip) + [ -f /etc/ssl/certs/ca-certificates.crt ] || [ -f /etc/pki/tls/certs/ca-bundle.crt ] || missing+=(ca-certificates) + [ "${#missing[@]}" -eq 0 ] && return 0 + + log "Installing prerequisites: ${missing[*]}" + if command -v apt-get > /dev/null 2>&1; then + export DEBIAN_FRONTEND=noninteractive + apt-get update -y + apt-get install -y --no-install-recommends "${missing[@]}" + rm -rf /var/lib/apt/lists/* + elif command -v apk > /dev/null 2>&1; then + apk add --no-cache "${missing[@]}" + elif command -v microdnf > /dev/null 2>&1; then + microdnf install -y "${missing[@]}" + microdnf clean all + elif command -v dnf > /dev/null 2>&1; then + dnf install -y "${missing[@]}" + dnf clean all + elif command -v yum > /dev/null 2>&1; then + yum install -y "${missing[@]}" + yum clean all + else + fail "Missing ${missing[*]} and no supported package manager (apt-get, apk, dnf, microdnf, yum) found." + fi } -add_backports() { - case ${ID} in - debian) - echo "deb http://ftp.debian.org/debian ${VERSION_CODENAME}-backports main" | - tee /etc/apt/sources.list.d/backports.list - ;; - ubuntu) - echo "deb http://archive.ubuntu.com/ubuntu ${VERSION_CODENAME}-backports main" | - tee /etc/apt/sources.list.d/backports.list - ;; +detect_arch() { + case "$(uname -m)" in + x86_64 | amd64) echo "amd64" ;; + aarch64 | arm64) echo "arm64" ;; + *) fail "Unsupported architecture $(uname -m). lstk ships linux/amd64 and linux/arm64 builds." ;; esac } -# Checks if packages are installed and installs them if not -check_packages() { - case ${ADJUSTED_ID} in - debian) - if ! dpkg -s "$@" > /dev/null 2>&1; then - add_backports - pkg_mgr_update - ${INSTALL_CMD} "$@" - fi +# Resolves "latest" through the release redirect, which avoids the +# GitHub API and its rate limit for anonymous builds. +resolve_version() { + local requested="$1" + case "${requested}" in + "" | latest) + local url + url=$(curl -fsSLI -o /dev/null -w '%{url_effective}' "${LSTK_REPO}/releases/latest") \ + || fail "Could not resolve the latest lstk release from ${LSTK_REPO}/releases/latest" + requested="${url##*/}" ;; esac + requested="${requested#v}" + [[ "${requested}" =~ ^[0-9]+\.[0-9]+\.[0-9]+([-.][0-9A-Za-z.-]+)?$ ]] \ + || fail "Invalid lstk version '${requested}'. Use 'latest' or a release such as '1.5.0'." + echo "${requested}" } -install_using_pip_strategy() { - local ver="" - case ${LS_VERSION} in - "") - ;& - none) - ;& - lts) - ;& - latest) - ;& - stable) - ver="" - ;; - *) - ver="==${LS_VERSION}" - ;; - esac - - if [ "${INSTALL_USING_PYTHON}" = "true" ]; then - install_with_complete_python_installation "${ver}" || install_with_pipx "${ver}" || return 1 +sha256_check() { + if command -v sha256sum > /dev/null 2>&1; then + sha256sum -c - else - install_with_pipx "${ver}" || install_with_complete_python_installation "${ver}" || return 1 - fi -} - -install_with_pipx() { - echo "(*) Attempting to install globally with pipx..." - local ver="$1" - export - local - - if ! type pipx > /dev/null 2>&1; then - echo "(*) Installing pipx..." - check_packages pipx ${REQUIRED_PKGS} - pipx ensurepath # Ensures PIPX_BIN_DIR is on the PATH - fi - - PIPX_HOME="/usr/local/pipx" \ - PIPX_BIN_DIR=/usr/local/bin \ - pipx install localstack${ver} --include-deps - - echo "(*) Finished installing globally with pipx." -} - -install_with_complete_python_installation() { - local ver="$1" - if ! dpkg -s ${REQUIRED_PKGS} > /dev/null 2>&1; then - check_packages ${REQUIRED_PKGS} + shasum -a 256 -c - fi - export PIPX_HOME=/usr/local/pipx - mkdir -p ${PIPX_HOME} - export PIPX_BIN_DIR=/usr/local/bin - export PYTHONUSERBASE=/tmp/pip-tmp - export PIP_CACHE_DIR=/tmp/pip-tmp/cache - pipx_bin=pipx - if ! type pipx > /dev/null 2>&1; then - pip3 install --disable-pip-version-check --no-cache-dir --user pipx - pipx_bin=/tmp/pip-tmp/bin/pipx - fi - - set +e - ${pipx_bin} install --pip-args '--no-cache-dir --force-reinstall --include-deps' -f localstack${ver} - - # Fail gracefully - if [ "$?" != 0 ]; then - echo "Could not install localstack${ver} via pip3" - rm -rf /tmp/pip-tmp - return 1 - fi - set -e -} - -warn() { - echo "WARNING: $1 not found on PATH, though local wrapper is being installed" } -is_tool() { - ! type $1 &> /dev/null - return $? +install_completions() { + local src="$1/completions" + [ -d "${src}" ] || return 0 + install -D -m 0644 "${src}/lstk.bash" /usr/share/bash-completion/completions/lstk + install -D -m 0644 "${src}/lstk.zsh" /usr/local/share/zsh/site-functions/_lstk + install -D -m 0644 "${src}/lstk.fish" /usr/share/fish/vendor_completions.d/lstk.fish } -check_tool() { - local tool=$(eval echo "\$$1") - set +e - case $tool in - awscli-local) - is_tool aws && warn awscli - ;; - aws-cdk-local) - is_tool cdk && warn aws-cdk - ;; - pulumi-local) - is_tool pulumi && warn pulumi - ;; - terraform-local) - (is_tool terraform || is_tool tofu) && warn terraform/tofu - ;; - aws-sam-cli-local) - is_tool sam && warn aws-sam-cli - ;; - esac - set -e +install_manpages() { + local src="$1/manpages" + [ -d "${src}" ] || return 0 + mkdir -p /usr/local/share/man/man1 + cp "${src}"/*.1 /usr/local/share/man/man1/ } -install_tool() { - local tool=$(eval echo "\$$1") - case $tool in - aws-cdk-local) - npm install -g $tool - ;; - *) - PIPX_HOME="/usr/local/pipx" \ - PIPX_BIN_DIR=/usr/local/bin \ - pipx install $tool - ;; - esac -} - -install_tools() { - for tool in ${TOOLS[@]}; do - check_tool $tool - install_tool $tool - done +# pulumi-local is the one LocalStack wrapper without an lstk equivalent. +# It is a Python package, so pipx is only pulled in when it is requested. +install_pulumilocal() { + if ! command -v pipx > /dev/null 2>&1; then + log "Installing pipx" + if command -v apt-get > /dev/null 2>&1; then + export DEBIAN_FRONTEND=noninteractive + apt-get update -y + apt-get install -y --no-install-recommends pipx + rm -rf /var/lib/apt/lists/* + elif command -v apk > /dev/null 2>&1; then + apk add --no-cache pipx + elif command -v pip3 > /dev/null 2>&1; then + pip3 install --no-cache-dir pipx + else + fail "pulumilocal needs pipx. Add the Python Feature (ghcr.io/devcontainers/features/python) or install pipx in the base image." + fi + fi + log "Installing pulumi-local" + PIPX_HOME=/usr/local/pipx PIPX_BIN_DIR=/usr/local/bin pipx install --force pulumi-local + command -v pulumi > /dev/null 2>&1 \ + || echo "(!) pulumi is not on PATH. Add ghcr.io/devcontainers-extra/features/pulumi:1 to use pulumilocal." } -add_backports - -install_using_pip_strategy - -install_tools +install_prerequisites + +ARCH="$(detect_arch)" +VERSION_NUMBER="$(resolve_version "${LSTK_VERSION}")" +ARCHIVE="lstk_${VERSION_NUMBER}_linux_${ARCH}.tar.gz" +BASE_URL="${LSTK_REPO}/releases/download/v${VERSION_NUMBER}" + +TMP_DIR="$(mktemp -d)" +trap 'rm -rf "${TMP_DIR}"' EXIT + +log "Downloading lstk ${VERSION_NUMBER} (linux/${ARCH})" +curl -fsSL --retry 3 -o "${TMP_DIR}/${ARCHIVE}" "${BASE_URL}/${ARCHIVE}" \ + || fail "Could not download ${BASE_URL}/${ARCHIVE}. Check that lstk ${VERSION_NUMBER} exists." +curl -fsSL --retry 3 -o "${TMP_DIR}/checksums.txt" "${BASE_URL}/checksums.txt" \ + || fail "Could not download ${BASE_URL}/checksums.txt" + +log "Verifying checksum" +(cd "${TMP_DIR}" && grep " ${ARCHIVE}\$" checksums.txt | sha256_check) \ + || fail "Checksum verification failed for ${ARCHIVE}" + +# The archive carries the lstk binary plus the bundled extensions +# (e.g. `lstk doctor`). lstk finds them next to its resolved executable, +# so the archive is unpacked as a whole and only the binary is linked. +rm -rf "${INSTALL_DIR}" +mkdir -p "${INSTALL_DIR}" +tar -xzf "${TMP_DIR}/${ARCHIVE}" -C "${INSTALL_DIR}" +ln -sf "${INSTALL_DIR}/lstk" "${BIN_LINK}" + +install_completions "${INSTALL_DIR}" +install_manpages "${INSTALL_DIR}" + +# Let the container user run `lstk update` without sudo. Access goes through +# an `lstk` group rather than user ownership: on Linux hosts the devcontainer +# CLI remaps the remote user's UID after Features install, which would leave +# a user-owned directory pointing at a stale UID. Group membership survives it. +chown -R root:root "${INSTALL_DIR}" +if [ -n "${_REMOTE_USER:-}" ] && [ "${_REMOTE_USER}" != "root" ] && id -u "${_REMOTE_USER}" > /dev/null 2>&1; then + if command -v groupadd > /dev/null 2>&1; then + getent group lstk > /dev/null 2>&1 || groupadd -r lstk + usermod -aG lstk "${_REMOTE_USER}" + else + getent group lstk > /dev/null 2>&1 || addgroup -S lstk + addgroup "${_REMOTE_USER}" lstk + fi + chgrp -R lstk "${INSTALL_DIR}" + chmod -R g+rwX "${INSTALL_DIR}" + find "${INSTALL_DIR}" -type d -exec chmod g+s {} + +fi -# Clean up -rm -rf /var/lib/apt/lists/* +if [ "${INSTALL_PULUMILOCAL}" = "true" ]; then + install_pulumilocal +fi -echo "Done!" \ No newline at end of file +"${BIN_LINK}" --version +log "Done." diff --git a/test/localstack-cli/alpine.sh b/test/localstack-cli/alpine.sh new file mode 100755 index 0000000..f818ba1 --- /dev/null +++ b/test/localstack-cli/alpine.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -e + +source dev-container-features-test-lib + +check "distro is alpine" bash -c "grep ID=alpine /etc/os-release" +check "lstk version" lstk --version +check "bundled doctor extension resolves" bash -c "lstk doctor --help" +check "remote user is in the lstk group" bash -c "id -nG | grep -qw lstk" +check "install dir writable by the lstk group" bash -c "test -w /usr/local/lib/lstk && test -w /usr/local/lib/lstk/lstk" + +reportResults diff --git a/test/localstack-cli/install_using_python_3_12_bookworm.sh b/test/localstack-cli/install_using_python_3_12_bookworm.sh deleted file mode 100644 index 9e6e109..0000000 --- a/test/localstack-cli/install_using_python_3_12_bookworm.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -set -e - -# Import test library for `check` command -source dev-container-features-test-lib - - -echo -e "\n🔄 Testing 'O.S'" -if cat /etc/os-release | grep -q 'PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"'; then - echo -e "\n✅ Passed 'O.S is Linux 12 (bookworm)'!\n" -else - echo -e "\n❌ Failed 'O.S is other than Linux 12 (bookworm)'!\n" -fi - -# Check to make sure the user is vscode -check "user is vscode" whoami | grep vscode -check "version" localstack --version - -# Report result -reportResults \ No newline at end of file diff --git a/test/localstack-cli/install_with_awslocal_tool.sh b/test/localstack-cli/install_with_awslocal_tool.sh deleted file mode 100644 index 3db45ee..0000000 --- a/test/localstack-cli/install_with_awslocal_tool.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -set -e - -# Import test library for `check` command -source dev-container-features-test-lib - -# Check to make sure the user is vscode -check "user is vscode" whoami | grep vscode -check "version" localstack --version -check "awslocal" type awslocal - -export PIPX_HOME="/usr/local/pipx" -export PIPX_BIN_DIR=/usr/local/bin -check "awslocal via pipx" sudo -E pipx list > tmp_file && cat tmp_file | grep "package awscli-local" - -# Report result -reportResults \ No newline at end of file diff --git a/test/localstack-cli/install_with_cdklocal_tool.sh b/test/localstack-cli/install_with_cdklocal_tool.sh deleted file mode 100644 index ba3945e..0000000 --- a/test/localstack-cli/install_with_cdklocal_tool.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -set -e - -# Import test library for `check` command -source dev-container-features-test-lib - -# Check to make sure the user is vscode -check "user is vscode" whoami | grep vscode -check "version" localstack --version -check "cdklocal" type cdklocal -check "cdklocal via npm" npm list -g --depth=0 | grep aws-cdk-local - -# Report result -reportResults \ No newline at end of file diff --git a/test/localstack-cli/install_with_multiple_tools.sh b/test/localstack-cli/install_with_multiple_tools.sh deleted file mode 100644 index 49d7df6..0000000 --- a/test/localstack-cli/install_with_multiple_tools.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -set -e - -# Import test library for `check` command -source dev-container-features-test-lib - -# Check to make sure the user is vscode -check "user is vscode" whoami | grep vscode -check "version" localstack --version -check "awslocal" type awslocal -check "cdklocal" type cdklocal -check "tflocal" type tflocal - -# Report result -reportResults \ No newline at end of file diff --git a/test/localstack-cli/install_with_python.sh b/test/localstack-cli/install_with_python.sh deleted file mode 100644 index fa23c48..0000000 --- a/test/localstack-cli/install_with_python.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -set -e - -# Import test library for `check` command -source dev-container-features-test-lib - -# Check to make sure the user is vscode -check "user is vscode" whoami | grep vscode -check "version" localstack --version - - -# Report result -reportResults \ No newline at end of file diff --git a/test/localstack-cli/install_with_python_using_python_3_11_bullseye.sh b/test/localstack-cli/install_with_python_using_python_3_11_bullseye.sh deleted file mode 100644 index 7ffed0a..0000000 --- a/test/localstack-cli/install_with_python_using_python_3_11_bullseye.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -set -e - -# Import test library for `check` command -source dev-container-features-test-lib - -# Check to make sure the user is vscode -check "user is vscode" whoami | grep vscode -check "version" localstack --version - -echo -e "\n\n🔄 Testing 'O.S'" -if cat /etc/os-release | grep -q 'PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"'; then - echo -e "\n\n✅ Passed 'O.S is Linux 11 (bullseye)'!" -else - echo -e "\n\n❌ Failed 'O.S is other than Linux 11 (bullseye)'!" -fi - - -# Report result -reportResults \ No newline at end of file diff --git a/test/localstack-cli/latest_on_bookworm.sh b/test/localstack-cli/latest_on_bookworm.sh new file mode 100755 index 0000000..aa649a9 --- /dev/null +++ b/test/localstack-cli/latest_on_bookworm.sh @@ -0,0 +1,20 @@ +#!/bin/bash +set -e + +source dev-container-features-test-lib + +check "user is vscode" bash -c "whoami | grep vscode" +check "lstk version" lstk --version +check "lstk links into the install dir" bash -c "readlink -f \$(command -v lstk) | grep '^/usr/local/lib/lstk/lstk\$'" +check "bundled extensions shipped next to lstk" test -x /usr/local/lib/lstk/bundled-extensions +check "bundled doctor extension resolves" bash -c "lstk doctor --help" +check "bash completion installed" test -f /usr/share/bash-completion/completions/lstk +check "zsh completion installed" test -f /usr/local/share/zsh/site-functions/_lstk +check "man page installed" test -f /usr/local/share/man/man1/lstk.1 +check "remote user is in the lstk group" bash -c "id -nG | grep -qw lstk" +check "install dir writable by the lstk group" bash -c "test -w /usr/local/lib/lstk && test -w /usr/local/lib/lstk/lstk" +check "config lives in the user's home" bash -c "lstk config path | grep '^/home/vscode/'" +check "no deprecated wrappers" bash -c "! command -v localstack && ! command -v awslocal && ! command -v tflocal && ! command -v cdklocal && ! command -v samlocal" +check "pulumilocal not installed by default" bash -c "! command -v pulumilocal" + +reportResults diff --git a/test/localstack-cli/pinned_version_on_noble.sh b/test/localstack-cli/pinned_version_on_noble.sh new file mode 100755 index 0000000..16cc675 --- /dev/null +++ b/test/localstack-cli/pinned_version_on_noble.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -e + +source dev-container-features-test-lib + +check "distro is noble" bash -c "grep VERSION_CODENAME=noble /etc/os-release" +check "lstk is 1.4.5" bash -c "lstk --version | grep -w 1.4.5" + +reportResults diff --git a/test/localstack-cli/pinned_version_with_v_prefix.sh b/test/localstack-cli/pinned_version_with_v_prefix.sh new file mode 100755 index 0000000..7b2637e --- /dev/null +++ b/test/localstack-cli/pinned_version_with_v_prefix.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -e + +source dev-container-features-test-lib + +check "distro is jammy" bash -c "grep VERSION_CODENAME=jammy /etc/os-release" +check "lstk is 1.4.4" bash -c "lstk --version | grep -w 1.4.4" + +reportResults diff --git a/test/localstack-cli/pulumilocal.sh b/test/localstack-cli/pulumilocal.sh new file mode 100755 index 0000000..55e7095 --- /dev/null +++ b/test/localstack-cli/pulumilocal.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +source dev-container-features-test-lib + +check "lstk version" lstk --version +check "pulumi available" pulumi version +check "pulumilocal installed" bash -c "command -v pulumilocal" +check "pulumilocal wraps pulumi" pulumilocal version + +reportResults diff --git a/test/localstack-cli/scenarios.json b/test/localstack-cli/scenarios.json index 621f07d..fa8dfa8 100644 --- a/test/localstack-cli/scenarios.json +++ b/test/localstack-cli/scenarios.json @@ -1,104 +1,59 @@ { - "version_lts": { - "image": "mcr.microsoft.com/devcontainers/base:bullseye", - "user": "vscode", - "features": { - "localstack-cli": { - "version": "lts" - } - } - }, - "version_none": { - "image": "mcr.microsoft.com/devcontainers/base:focal", - "user": "vscode", - "features": { - "localstack-cli": { - "version": "none" - } - } - }, - "version_empty": { - "image": "mcr.microsoft.com/devcontainers/base:bookworm", - "user": "vscode", - "features": { - "localstack-cli": { - "version": "" - } - } - }, - "version_3_2_0": { - "image": "mcr.microsoft.com/devcontainers/base:jammy", - "user": "vscode", - "features": { - "localstack-cli": { - "version": "3.2.0" - } - } - }, - "install_with_python": { - "image": "mcr.microsoft.com/devcontainers/base:jammy", - "user": "vscode", - "features": { - "localstack-cli": { - "version": "latest", - "installUsingPython": true - } - } - }, - "install_using_python_3_12_bookworm": { - "image": "mcr.microsoft.com/devcontainers/python:1-3.12-bookworm", - "user": "vscode", - "features": { - "localstack-cli": { - "version": "latest" - } - } - }, - "install_with_python_using_python_3_11_bullseye": { - "image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye", - "user": "vscode", - "features": { - "localstack-cli": { - "version": "latest", - "installUsingPython": "true" - } - } - }, - "install_with_awslocal_tool": { - "image": "mcr.microsoft.com/devcontainers/base:jammy", - "user": "vscode", - "features": { - "localstack-cli": { - "version": "latest", - "awslocal": true - } - } - }, - "install_with_cdklocal_tool": { - "image": "mcr.microsoft.com/devcontainers/base:jammy", - "user": "vscode", - "features": { - "ghcr.io/devcontainers/features/node:1": {}, - "localstack-cli": { - "version": "latest", - "cdklocal": true - } - } - }, - "install_with_multiple_tools": { - "image": "mcr.microsoft.com/devcontainers/base:jammy", - "user": "vscode", - "features": { - "ghcr.io/devcontainers/features/python:1": { - "installTools": false - }, - "ghcr.io/devcontainers/features/node:1": {}, - "localstack-cli": { - "version": "latest", - "awslocal": true, - "tflocal": true, - "cdklocal": true - } - } - } -} \ No newline at end of file + "latest_on_bookworm": { + "image": "mcr.microsoft.com/devcontainers/base:bookworm", + "user": "vscode", + "features": { + "localstack-cli": {} + } + }, + "pinned_version_on_noble": { + "image": "mcr.microsoft.com/devcontainers/base:noble", + "user": "vscode", + "features": { + "localstack-cli": { + "version": "1.4.5" + } + } + }, + "pinned_version_with_v_prefix": { + "image": "mcr.microsoft.com/devcontainers/base:jammy", + "user": "vscode", + "features": { + "localstack-cli": { + "version": "v1.4.4" + } + } + }, + "slim_image_without_curl": { + "image": "debian:trixie-slim", + "features": { + "localstack-cli": {} + } + }, + "alpine": { + "image": "mcr.microsoft.com/devcontainers/base:alpine", + "user": "vscode", + "features": { + "localstack-cli": {} + } + }, + "with_docker_and_aws_cli": { + "image": "mcr.microsoft.com/devcontainers/base:bookworm", + "user": "vscode", + "features": { + "ghcr.io/devcontainers/features/docker-outside-of-docker:1": {}, + "ghcr.io/devcontainers/features/aws-cli:1": {}, + "localstack-cli": {} + } + }, + "pulumilocal": { + "image": "mcr.microsoft.com/devcontainers/base:bookworm", + "user": "vscode", + "features": { + "ghcr.io/devcontainers-extra/features/pulumi:1": {}, + "localstack-cli": { + "pulumilocal": true + } + } + } +} diff --git a/test/localstack-cli/slim_image_without_curl.sh b/test/localstack-cli/slim_image_without_curl.sh new file mode 100755 index 0000000..2fccc90 --- /dev/null +++ b/test/localstack-cli/slim_image_without_curl.sh @@ -0,0 +1,10 @@ +#!/bin/bash +set -e + +source dev-container-features-test-lib + +check "distro is trixie" bash -c "grep VERSION_CODENAME=trixie /etc/os-release" +check "lstk version" lstk --version +check "curl was installed as a prerequisite" bash -c "command -v curl" + +reportResults diff --git a/test/localstack-cli/test.sh b/test/localstack-cli/test.sh old mode 100644 new mode 100755 index 270e5fe..4b56a33 --- a/test/localstack-cli/test.sh +++ b/test/localstack-cli/test.sh @@ -1,12 +1,11 @@ #!/bin/bash - set -e -# Optional: Import test library source dev-container-features-test-lib -# Definition specific tests -check "version" localstack --version +check "lstk is on PATH" bash -c "command -v lstk" +check "lstk version" lstk --version +check "lstk help lists tool proxies" bash -c "lstk --help | grep -E '^ +terraform '" +check "legacy localstack CLI is not installed" bash -c "! command -v localstack" -# Report result -reportResults \ No newline at end of file +reportResults diff --git a/test/localstack-cli/version_3_2_0.sh b/test/localstack-cli/version_3_2_0.sh deleted file mode 100644 index 9b273bb..0000000 --- a/test/localstack-cli/version_3_2_0.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -set -e - -# Import test library for `check` command -source dev-container-features-test-lib - -# Check to make sure the user is vscode -check "user is vscode" whoami | grep vscode - -check "version" localstack --version - -check "version is 3.2.0" localstack --version | grep "3.2.0" - -# Report result -reportResults \ No newline at end of file diff --git a/test/localstack-cli/version_empty.sh b/test/localstack-cli/version_empty.sh deleted file mode 100644 index e183ce1..0000000 --- a/test/localstack-cli/version_empty.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -set -e - -# Import test library for `check` command -source dev-container-features-test-lib - -# Check to make sure the user is vscode -check "user is vscode" whoami | grep vscode - -check "version" localstack --version - -check "version is empty (3.5.0)" localstack --version | grep "3.5.0" - -# Report result -reportResults \ No newline at end of file diff --git a/test/localstack-cli/version_lts.sh b/test/localstack-cli/version_lts.sh deleted file mode 100644 index 72cad02..0000000 --- a/test/localstack-cli/version_lts.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -set -e - -# Import test library for `check` command -source dev-container-features-test-lib - -# Check to make sure the user is vscode -check "user is vscode" whoami | grep vscode - -check "version" localstack --version - -check "version is lts (3.5.0)" localstack --version | grep "3.5.0" - -# Report result -reportResults \ No newline at end of file diff --git a/test/localstack-cli/version_none.sh b/test/localstack-cli/version_none.sh deleted file mode 100644 index 24341d7..0000000 --- a/test/localstack-cli/version_none.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -set -e - -# Import test library for `check` command -source dev-container-features-test-lib - -# Check to make sure the user is vscode -check "user is vscode" whoami | grep vscode - -check "version" localstack --version - -check "version is none (3.5.0)" localstack --version | grep "3.5.0" - -# Report result -reportResults \ No newline at end of file diff --git a/test/localstack-cli/with_docker_and_aws_cli.sh b/test/localstack-cli/with_docker_and_aws_cli.sh new file mode 100755 index 0000000..e810d39 --- /dev/null +++ b/test/localstack-cli/with_docker_and_aws_cli.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +source dev-container-features-test-lib + +check "docker is reachable" bash -c "timeout 60 bash -c 'until docker info > /dev/null 2>&1; do sleep 1; done'" +check "aws cli available" aws --version +check "lstk reports a stopped emulator" bash -c "lstk --non-interactive status 2>&1 | grep -i 'not running'" +check "lstk setup aws writes a localstack profile" bash -c "lstk --non-interactive setup aws && aws configure get endpoint_url --profile localstack | grep localhost.localstack.cloud:4566" + +reportResults