From 4c19a42d137f7c1051458b6e42062e58e99f6e68 Mon Sep 17 00:00:00 2001 From: Robin Mordasiewicz Date: Mon, 25 Mar 2024 12:22:49 -0400 Subject: [PATCH 1/4] This is a new Dev Container feature called `azure-cli-persistence`. It aims to preserve the `~/.azure` folder across container instances of Azure CLI, avoiding extra logins. The feature consists of an extension file (`devcontainer.json`) and two shell scripts (`install.sh` and `oncreate.sh`). The `devcontainer.json` file defines the feature's metadata, such as its name, version, documentation URL, description, options, mounts, installsAfter dependencies, and onCreateCommand. The mounts section is used to mount the source volume `${devcontainerId}-azure-cli` to the target directory `/dc/azure-cli`. The `install.sh` script sets up the cache directories for the Azure CLI and creates a symlink from the user's home folder (`.azure`) to the container's `/dc/azure-cli` directory. It also sets up lifecycle scripts if they exist. The `oncreate.sh` script is empty in this version, but it can be used to run additional setup tasks when a new container instance is created. Overall, this feature helps maintain the Azure CLI configuration across container instances, reducing the need for repeated logins and improving the development experience. --- src/azure-cli-persistence/NOTES.md | 17 ++++++ src/azure-cli-persistence/README.md | 41 +++++++++++++++ .../devcontainer-feature.json | 23 ++++++++ src/azure-cli-persistence/install.sh | 52 +++++++++++++++++++ src/azure-cli-persistence/oncreate.sh | 9 ++++ 5 files changed, 142 insertions(+) create mode 100644 src/azure-cli-persistence/NOTES.md create mode 100644 src/azure-cli-persistence/README.md create mode 100644 src/azure-cli-persistence/devcontainer-feature.json create mode 100644 src/azure-cli-persistence/install.sh create mode 100644 src/azure-cli-persistence/oncreate.sh diff --git a/src/azure-cli-persistence/NOTES.md b/src/azure-cli-persistence/NOTES.md new file mode 100644 index 0000000..c7c4f56 --- /dev/null +++ b/src/azure-cli-persistence/NOTES.md @@ -0,0 +1,17 @@ +## OS and Architecture Support + +Architectures: `amd` and `arm` + +OS: `ubuntu`, `debian` + +Shells: `bash`, `zsh`, `fish` + +## Changelog + +| Version | Notes | +| ------- | ----------------------------------------------- | +| 0.0.1 | Initial Version | + +## References + +- [stuartleeks/azure-cli-persistence](https://github.com/stuartleeks/dev-container-features/tree/main/src/azure-cli-persistence) diff --git a/src/azure-cli-persistence/README.md b/src/azure-cli-persistence/README.md new file mode 100644 index 0000000..ebba9b7 --- /dev/null +++ b/src/azure-cli-persistence/README.md @@ -0,0 +1,41 @@ + +# Azure CLI Persistence (azure-cli-persistence) + +Avoid extra logins from the Azure CLI by preserving the `~/.azure` folder across container instances. + +## Example Usage + +```json +"features": { + "ghcr.io/joshuanianji/devcontainer-features/azure-cli-persistence:1": {} +} +``` + +## Options + +| Options Id | Description | Type | Default Value | +|-----|-----|-----|-----| + + +## OS and Architecture Support + +Architectures: `amd` and `arm` + +OS: `ubuntu`, `debian` + +Shells: `bash`, `zsh`, `fish` + +## Changelog + +| Version | Notes | +| ------- | ----------------------------------------------- | +| 0.0.1 | Initial Version | + +## References + +- [stuartleeks/azure-cli-persistence](https://github.com/stuartleeks/dev-container-features/tree/main/src/azure-cli-persistence) + + +--- + +_Note: This file was auto-generated from the [devcontainer-feature.json](https://github.com/joshuanianji/devcontainer-features/blob/main/src/azure-cli-persistence/devcontainer-feature.json). Add additional notes to a `NOTES.md`._ diff --git a/src/azure-cli-persistence/devcontainer-feature.json b/src/azure-cli-persistence/devcontainer-feature.json new file mode 100644 index 0000000..90ba102 --- /dev/null +++ b/src/azure-cli-persistence/devcontainer-feature.json @@ -0,0 +1,23 @@ +{ + "name": "Azure CLI Persistence", + "id": "azure-cli-persistence", + "version": "0.0.1", + "documentationURL": "https://github.com/joshuanianji/devcontainer-features/tree/main/src/azure-cli-persistence", + "description": "Avoid extra logins from the Azure CLI by preserving the `~/.azure` folder across container instances.", + "options": {}, + "mounts": [ + { + "source": "${devcontainerId}-azure-cli", + "target": "/dc/azure-cli", + "type": "volume" + } + ], + "installsAfter": [ + "ghcr.io/devcontainers/features/azure-cli", + "ghcr.io/devcontainers/features/common-utils", + "ghcr.io/meaningful-ooo/devcontainer-features/fish" + ], + "onCreateCommand": { + "azure-cli-persistence-setup": "/usr/local/share/azure-cli-persistence/scripts/oncreate.sh" + } +} diff --git a/src/azure-cli-persistence/install.sh b/src/azure-cli-persistence/install.sh new file mode 100644 index 0000000..ad8e039 --- /dev/null +++ b/src/azure-cli-persistence/install.sh @@ -0,0 +1,52 @@ +#!/bin/sh + +USERNAME=${USERNAME:-${_REMOTE_USER}} +FEATURE_ID="azure-cli-persistence" +LIFECYCLE_SCRIPTS_DIR="/usr/local/share/${FEATURE_ID}/scripts" + +set -e + +create_cache_dir() { + if [ -d "$1" ]; then + echo "Cache directory $1 already exists. Skip creation..." + else + echo "Create cache directory $1..." + mkdir -p "$1" + fi + + if [ -z "$2" ]; then + echo "No username provided. Skip chown..." + else + echo "Change owner of $1 to $2..." + chown -R "$2:$2" "$1" + fi +} + +create_symlink_dir() { + local local_dir=$1 + local cache_dir=$2 + local username=$3 + + runuser -u "$username" -- mkdir -p "$(dirname "$local_dir")" + runuser -u "$username" -- mkdir -p "$cache_dir" + + # if the folder we want to symlink already exists, the ln -s command will create a folder inside the existing folder + if [ -e "$local_dir" ]; then + echo "Moving existing $local_dir folder to $local_dir-old" + mv "$local_dir" "$local_dir-old" + fi + + echo "Symlink $local_dir to $cache_dir for $username..." + runuser -u "$username" -- ln -s "$cache_dir" "$local_dir" +} + +create_cache_dir "/dc/azure-cli" "${USERNAME}" +create_symlink_dir "$_REMOTE_USER_HOME/.azure" "/dc/azure-cli" "${USERNAME}" + +# Set Lifecycle scripts +if [ -f oncreate.sh ]; then + mkdir -p "${LIFECYCLE_SCRIPTS_DIR}" + cp oncreate.sh "${LIFECYCLE_SCRIPTS_DIR}/oncreate.sh" +fi + +echo "Finished installing $FEATURE_ID" diff --git a/src/azure-cli-persistence/oncreate.sh b/src/azure-cli-persistence/oncreate.sh new file mode 100644 index 0000000..0e354a8 --- /dev/null +++ b/src/azure-cli-persistence/oncreate.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +set -e + +# if the user is not root, chown /dc/azure-cli to the user +if [ "$(id -u)" != "0" ]; then + echo "Running oncreate.sh for user $USER" + sudo chown -R "$USER:$USER" /dc/azure-cli +fi From 653d55dda20b2b24ccc1112f5b20125a1eac73cb Mon Sep 17 00:00:00 2001 From: Robin Mordasiewicz Date: Mon, 25 Mar 2024 12:35:52 -0400 Subject: [PATCH 2/4] These files appear to be new additions for a test suite related to the `azure-cli-persistence` feature, which is designed to persist Azure CLI configuration across container restarts. The tests are written in Bash and use the `dev-container-features-test-lib` library for reporting test results. The `scenarios.json` file defines four different test scenarios: one using Node.js, one using Zsh shell, one using Fish shell, and one using a root user. Each scenario specifies an image to use as the base container and lists the required features (including `azure-cli-persistence`). The `test.sh` file is empty, but it's likely that it would import the test library and then run the tests for each scenario defined in `scenarios.json`. The other files (`with_node.sh`, `zsh_shell.sh`, `fish_shell.sh`, and `root_user.sh`) are specific test scripts for each scenario, which likely contain commands to check that the Azure CLI configuration is persisted across container restarts. Overall, these files suggest that the `azure-cli-persistence` feature is designed to work with different shells (Node.js, Zsh, Fish, and root user) and that it can be tested using a set of automated tests. --- README.md | 1 + test/azure-cli-persistence/_default.sh | 26 +++++++++++++++++ test/azure-cli-persistence/fish_shell.sh | 7 +++++ test/azure-cli-persistence/root_user.sh | 9 ++++++ test/azure-cli-persistence/scenarios.json | 35 +++++++++++++++++++++++ test/azure-cli-persistence/test.sh | 21 ++++++++++++++ test/azure-cli-persistence/with_node.sh | 7 +++++ test/azure-cli-persistence/zsh_shell.sh | 7 +++++ 8 files changed, 113 insertions(+) create mode 100755 test/azure-cli-persistence/_default.sh create mode 100644 test/azure-cli-persistence/fish_shell.sh create mode 100644 test/azure-cli-persistence/root_user.sh create mode 100644 test/azure-cli-persistence/scenarios.json create mode 100644 test/azure-cli-persistence/test.sh create mode 100644 test/azure-cli-persistence/with_node.sh create mode 100644 test/azure-cli-persistence/zsh_shell.sh diff --git a/README.md b/README.md index b256f59..35a241b 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ This repo contains my custom devcontainer features. | [github-cli-persistence](./src/github-cli-persistence) | Avoid extra logins from the Github CLI by preserving the `~/.config/gh` folder across container instances. | | [terraform-cli-persistence](./src/terraform-cli-persistence) | Avoid extra logins from the Terraform CLI by preserving the `~/.terraform.d` folder across container instances. | | [aws-cli-persistence](./src/aws-cli-persistence) | Avoid extra logins from the AWS CLI by preserving the `~/.aws` folder across container instances. | +| [azure-cli-persistence](./src/azure-cli-persistence) | Avoid extra logins from the Azure CLI by preserving the `~/.azure` folder across container instances. | | [gcloud-cli-persistence](./src/gcloud-cli-persistence) | Avoid extra logins from the Google Cloud CLI by preserving the `~/.config/gcloud` folder across container instances. | | [lamdera](./src/lamdera) | Installs [Lamdera](https://dashboard.lamdera.app/), a type-safe full-stack web-app platform for Elm (v1.1.0 and later). | | [mount-pnpm-store](./src/mount-pnpm-store) | Mounts the pnpm store to a volume to share between multiple devcontainers. | diff --git a/test/azure-cli-persistence/_default.sh b/test/azure-cli-persistence/_default.sh new file mode 100755 index 0000000..a8e7700 --- /dev/null +++ b/test/azure-cli-persistence/_default.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +set -e + +# This script is the "default" test script for the azure-cli-persistence feature. +# It is not run as a scenario, but is run by other test scripts. + +# Optional: Import test library +source dev-container-features-test-lib + +# check that `azure --help` works +check "help" bash -c "azure help | grep 'usage'" + +# check that `.azure` and `/dc/azure-cli` exist under the user (should be node) +check "~/.azure existence" bash -c "ls -la ~ | grep '.azure'" +check "/dc/azure-cli existence" bash -c "ls -la /dc | grep 'azure-cli'" + +# check that the folders are owned by the user +# https://askubuntu.com/a/175060 +echo "Checking ownership of ~/.azure and /dc/azure-cli (ensure it is owned by $USER)" + +check "~/.azure owned by user" bash -c "test \"$(stat -c "%U" ~/.azure)\" = $USER" +check "/dc/azure-cli owned by user" bash -c "test \"$(stat -c "%U" /dc/azure-cli)\" = $USER" + +# Report result +reportResults diff --git a/test/azure-cli-persistence/fish_shell.sh b/test/azure-cli-persistence/fish_shell.sh new file mode 100644 index 0000000..1fe2fa4 --- /dev/null +++ b/test/azure-cli-persistence/fish_shell.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +set -e + +# Run default test script (in same folder) +# See: https://github.com/devcontainers/features/blob/562305d37b97d47331d96306ffc2a0a3cce55e64/test/azure-cli/install_extensions_bookworm.sh +./_default.sh diff --git a/test/azure-cli-persistence/root_user.sh b/test/azure-cli-persistence/root_user.sh new file mode 100644 index 0000000..1898f45 --- /dev/null +++ b/test/azure-cli-persistence/root_user.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +set -e + +source dev-container-features-test-lib + +# Run default test script (in same folder) +# See: https://github.com/devcontainers/features/blob/562305d37b97d47331d96306ffc2a0a3cce55e64/test/azure-cli/install_extensions_bookworm.sh +./_default.sh diff --git a/test/azure-cli-persistence/scenarios.json b/test/azure-cli-persistence/scenarios.json new file mode 100644 index 0000000..2deae0d --- /dev/null +++ b/test/azure-cli-persistence/scenarios.json @@ -0,0 +1,35 @@ +{ + "with_node": { + "image": "mcr.microsoft.com/devcontainers/javascript-node:1-18", + "features": { + "ghcr.io/devcontainers/features/azure-cli": {}, + "azure-cli-persistence": {} + } + }, + "zsh_shell": { + "image": "mcr.microsoft.com/devcontainers/base:debian", + "features": { + "ghcr.io/devcontainers/features/common-utils:2": { + "configureZshAsDefaultShell": true + }, + "ghcr.io/devcontainers/features/azure-cli": {}, + "azure-cli-persistence": {} + } + }, + "fish_shell": { + "image": "mcr.microsoft.com/devcontainers/base:debian", + "features": { + "ghcr.io/meaningful-ooo/devcontainer-features/fish:1": {}, + "ghcr.io/devcontainers/features/azure-cli": {}, + "azure-cli-persistence": {} + } + }, + "root_user": { + "image": "mcr.microsoft.com/devcontainers/base:debian", + "features": { + "ghcr.io/devcontainers/features/azure-cli": {}, + "azure-cli-persistence": {} + }, + "remoteUser": "root" + } +} diff --git a/test/azure-cli-persistence/test.sh b/test/azure-cli-persistence/test.sh new file mode 100644 index 0000000..73d1474 --- /dev/null +++ b/test/azure-cli-persistence/test.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +set -e + +# Optional: Import test library +source dev-container-features-test-lib + +# NOTE: azure is not installed inside the `test.sh` run +# see: test/github-cli-persistence/test.sh + + +# check that `~/.azure` and `/dc/azure-cli` exist` +check "~/.azure existence" bash -c "ls -la ~ | grep '.azure'" +check "/dc/azure-cli existence" bash -c "ls -la /dc | grep 'azure-cli'" + +# check that `~/.azure` is a symlink +# https://unix.stackexchange.com/a/96910 +check "~/.azure is a symlink" bash -c "test -L ~/.azure && test -d ~/.azure" + +# Report result +reportResults diff --git a/test/azure-cli-persistence/with_node.sh b/test/azure-cli-persistence/with_node.sh new file mode 100644 index 0000000..1fe2fa4 --- /dev/null +++ b/test/azure-cli-persistence/with_node.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +set -e + +# Run default test script (in same folder) +# See: https://github.com/devcontainers/features/blob/562305d37b97d47331d96306ffc2a0a3cce55e64/test/azure-cli/install_extensions_bookworm.sh +./_default.sh diff --git a/test/azure-cli-persistence/zsh_shell.sh b/test/azure-cli-persistence/zsh_shell.sh new file mode 100644 index 0000000..1fe2fa4 --- /dev/null +++ b/test/azure-cli-persistence/zsh_shell.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +set -e + +# Run default test script (in same folder) +# See: https://github.com/devcontainers/features/blob/562305d37b97d47331d96306ffc2a0a3cce55e64/test/azure-cli/install_extensions_bookworm.sh +./_default.sh From 312186f127f4017da321150fb4d088fd1171a286 Mon Sep 17 00:00:00 2001 From: Joshua Ji Date: Thu, 9 May 2024 22:47:20 +0000 Subject: [PATCH 3/4] Update name and ID --- src/azure-cli-persistence/devcontainer-feature.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/azure-cli-persistence/devcontainer-feature.json b/src/azure-cli-persistence/devcontainer-feature.json index 90ba102..6d6113d 100644 --- a/src/azure-cli-persistence/devcontainer-feature.json +++ b/src/azure-cli-persistence/devcontainer-feature.json @@ -1,9 +1,9 @@ { - "name": "Azure CLI Persistence", - "id": "azure-cli-persistence", + "name": "Azure CLI Persistence (forked)", + "id": "azure-cli-persistence-forked", "version": "0.0.1", "documentationURL": "https://github.com/joshuanianji/devcontainer-features/tree/main/src/azure-cli-persistence", - "description": "Avoid extra logins from the Azure CLI by preserving the `~/.azure` folder across container instances.", + "description": "(fork of stuartleeks): Avoid extra logins from the Azure CLI by preserving the `~/.azure` folder across container instances.", "options": {}, "mounts": [ { @@ -20,4 +20,4 @@ "onCreateCommand": { "azure-cli-persistence-setup": "/usr/local/share/azure-cli-persistence/scripts/oncreate.sh" } -} +} \ No newline at end of file From 761ecba36aa04cc405ac182686dc90189822dfb8 Mon Sep 17 00:00:00 2001 From: Joshua Ji Date: Thu, 9 May 2024 22:49:58 +0000 Subject: [PATCH 4/4] fix `azure --help` test --- test/azure-cli-persistence/_default.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/azure-cli-persistence/_default.sh b/test/azure-cli-persistence/_default.sh index a8e7700..37faa77 100755 --- a/test/azure-cli-persistence/_default.sh +++ b/test/azure-cli-persistence/_default.sh @@ -9,7 +9,7 @@ set -e source dev-container-features-test-lib # check that `azure --help` works -check "help" bash -c "azure help | grep 'usage'" +check "help" bash -c "az help | grep 'usage'" # check that `.azure` and `/dc/azure-cli` exist under the user (should be node) check "~/.azure existence" bash -c "ls -la ~ | grep '.azure'"