Skip to content

Commit 4c19a42

Browse files
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.
1 parent 4b1f9f8 commit 4c19a42

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

src/azure-cli-persistence/NOTES.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## OS and Architecture Support
2+
3+
Architectures: `amd` and `arm`
4+
5+
OS: `ubuntu`, `debian`
6+
7+
Shells: `bash`, `zsh`, `fish`
8+
9+
## Changelog
10+
11+
| Version | Notes |
12+
| ------- | ----------------------------------------------- |
13+
| 0.0.1 | Initial Version |
14+
15+
## References
16+
17+
- [stuartleeks/azure-cli-persistence](https://github.com/stuartleeks/dev-container-features/tree/main/src/azure-cli-persistence)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
# Azure CLI Persistence (azure-cli-persistence)
3+
4+
Avoid extra logins from the Azure CLI by preserving the `~/.azure` folder across container instances.
5+
6+
## Example Usage
7+
8+
```json
9+
"features": {
10+
"ghcr.io/joshuanianji/devcontainer-features/azure-cli-persistence:1": {}
11+
}
12+
```
13+
14+
## Options
15+
16+
| Options Id | Description | Type | Default Value |
17+
|-----|-----|-----|-----|
18+
19+
20+
## OS and Architecture Support
21+
22+
Architectures: `amd` and `arm`
23+
24+
OS: `ubuntu`, `debian`
25+
26+
Shells: `bash`, `zsh`, `fish`
27+
28+
## Changelog
29+
30+
| Version | Notes |
31+
| ------- | ----------------------------------------------- |
32+
| 0.0.1 | Initial Version |
33+
34+
## References
35+
36+
- [stuartleeks/azure-cli-persistence](https://github.com/stuartleeks/dev-container-features/tree/main/src/azure-cli-persistence)
37+
38+
39+
---
40+
41+
_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`._
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "Azure CLI Persistence",
3+
"id": "azure-cli-persistence",
4+
"version": "0.0.1",
5+
"documentationURL": "https://github.com/joshuanianji/devcontainer-features/tree/main/src/azure-cli-persistence",
6+
"description": "Avoid extra logins from the Azure CLI by preserving the `~/.azure` folder across container instances.",
7+
"options": {},
8+
"mounts": [
9+
{
10+
"source": "${devcontainerId}-azure-cli",
11+
"target": "/dc/azure-cli",
12+
"type": "volume"
13+
}
14+
],
15+
"installsAfter": [
16+
"ghcr.io/devcontainers/features/azure-cli",
17+
"ghcr.io/devcontainers/features/common-utils",
18+
"ghcr.io/meaningful-ooo/devcontainer-features/fish"
19+
],
20+
"onCreateCommand": {
21+
"azure-cli-persistence-setup": "/usr/local/share/azure-cli-persistence/scripts/oncreate.sh"
22+
}
23+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
3+
USERNAME=${USERNAME:-${_REMOTE_USER}}
4+
FEATURE_ID="azure-cli-persistence"
5+
LIFECYCLE_SCRIPTS_DIR="/usr/local/share/${FEATURE_ID}/scripts"
6+
7+
set -e
8+
9+
create_cache_dir() {
10+
if [ -d "$1" ]; then
11+
echo "Cache directory $1 already exists. Skip creation..."
12+
else
13+
echo "Create cache directory $1..."
14+
mkdir -p "$1"
15+
fi
16+
17+
if [ -z "$2" ]; then
18+
echo "No username provided. Skip chown..."
19+
else
20+
echo "Change owner of $1 to $2..."
21+
chown -R "$2:$2" "$1"
22+
fi
23+
}
24+
25+
create_symlink_dir() {
26+
local local_dir=$1
27+
local cache_dir=$2
28+
local username=$3
29+
30+
runuser -u "$username" -- mkdir -p "$(dirname "$local_dir")"
31+
runuser -u "$username" -- mkdir -p "$cache_dir"
32+
33+
# if the folder we want to symlink already exists, the ln -s command will create a folder inside the existing folder
34+
if [ -e "$local_dir" ]; then
35+
echo "Moving existing $local_dir folder to $local_dir-old"
36+
mv "$local_dir" "$local_dir-old"
37+
fi
38+
39+
echo "Symlink $local_dir to $cache_dir for $username..."
40+
runuser -u "$username" -- ln -s "$cache_dir" "$local_dir"
41+
}
42+
43+
create_cache_dir "/dc/azure-cli" "${USERNAME}"
44+
create_symlink_dir "$_REMOTE_USER_HOME/.azure" "/dc/azure-cli" "${USERNAME}"
45+
46+
# Set Lifecycle scripts
47+
if [ -f oncreate.sh ]; then
48+
mkdir -p "${LIFECYCLE_SCRIPTS_DIR}"
49+
cp oncreate.sh "${LIFECYCLE_SCRIPTS_DIR}/oncreate.sh"
50+
fi
51+
52+
echo "Finished installing $FEATURE_ID"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
# if the user is not root, chown /dc/azure-cli to the user
6+
if [ "$(id -u)" != "0" ]; then
7+
echo "Running oncreate.sh for user $USER"
8+
sudo chown -R "$USER:$USER" /dc/azure-cli
9+
fi

0 commit comments

Comments
 (0)