Skip to content

Commit ce83ac7

Browse files
committed
Add azd-cli-persistence. Update marker file location
1 parent e05fb53 commit ce83ac7

File tree

11 files changed

+281
-14
lines changed

11 files changed

+281
-14
lines changed

src/azd-cli-persistence/NOTES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
## Changelog
3+
4+
| Version | Notes |
5+
| ------- | ------------------------------------------------------------ |
6+
| 0.0.1 | Initial version |

src/azd-cli-persistence/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
# Azure Dev CLI Persistence (azd-cli-persistence)
3+
4+
Configure Azure Dev CLI (azd) to store configuration in a mounted volume to preserve across container builds
5+
6+
## Example Usage
7+
8+
```json
9+
"features": {
10+
"ghcr.io/stuartleeks/dev-container-features/azd-cli-persistence:0": {}
11+
}
12+
```
13+
14+
## Options
15+
16+
| Options Id | Description | Type | Default Value |
17+
|-----|-----|-----|-----|
18+
19+
20+
21+
## Changelog
22+
23+
| Version | Notes |
24+
| ------- | ------------------------------------------------------------ |
25+
| 0.0.1 | Initial version |
26+
27+
---
28+
29+
_Note: This file was auto-generated from the [devcontainer-feature.json](https://github.com/stuartleeks/dev-container-features/blob/main/src/azure-cli-persistence/devcontainer-feature.json). Add additional notes to a `NOTES.md`._
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "Azure Developer CLI Persistence (azd)",
3+
"id": "azd-cli-persistence",
4+
"version": "0.0.1",
5+
"description": "Configure Azure Dev CLI (azd) to store configuration in a mounted volume to preserve across container builds",
6+
"options": {},
7+
"mounts": [
8+
{
9+
"source": "${devcontainerId}-azure-dev",
10+
"target": "/dc/azure-dev",
11+
"type": "volume"
12+
}
13+
],
14+
"installsAfter": [
15+
"ghcr.io/azure/azure-dev/azd"
16+
],
17+
"onCreateCommand": {
18+
"azd-cli-persistence": "/usr/local/share/stuartleeks-devcontainer-features/azd-cli-persistence/scripts/oncreate.sh"
19+
}
20+
}

src/azd-cli-persistence/install.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/sh
2+
3+
FEATURE_DIR="/usr/local/share/stuartleeks-devcontainer-features/azd-cli-persistence"
4+
LIFECYCLE_SCRIPTS_DIR="$FEATURE_DIR/scripts"
5+
LOG_FILE="$FEATURE_DIR/log.txt"
6+
7+
set -e
8+
9+
mkdir -p "${FEATURE_DIR}"
10+
11+
echo "" > "$LOG_FILE"
12+
log() {
13+
echo "$1"
14+
echo "$1" >> "$LOG_FILE"
15+
}
16+
17+
log "Activating feature 'azd-cli-persistence'"
18+
log "User: ${_REMOTE_USER} User home: ${_REMOTE_USER_HOME}"
19+
20+
# check if marker file exists to avoid re-running oncreate script actions
21+
if [ -f "$$FEATURE_DIR/markers/install" ]; then
22+
log "Feature 'azd-cli-persistence' already installed, skipping oncreate actions"
23+
exit 0
24+
fi
25+
26+
got_old_azure_folder=false
27+
if [ -e "$_REMOTE_USER_HOME/.azd" ]; then
28+
log "Moving existing .azd folder to .azd-old"
29+
mv "$_REMOTE_USER_HOME/.azd" "$_REMOTE_USER_HOME/.azd-old"
30+
got_old_azure_folder=true
31+
else
32+
log "No existing .azd folder found at '$_REMOTE_USER_HOME/.azd'"
33+
fi
34+
35+
log "symlinking ~/.azd"
36+
ln -s /dc/azure-dev/ "$_REMOTE_USER_HOME/.azd"
37+
chown -R "${_REMOTE_USER}:${_REMOTE_USER}" "$_REMOTE_USER_HOME/.azd"
38+
39+
if [ -f oncreate.sh ]; then
40+
mkdir -p "${LIFECYCLE_SCRIPTS_DIR}"
41+
cp oncreate.sh "${LIFECYCLE_SCRIPTS_DIR}/oncreate.sh"
42+
fi
43+
44+
log "Adding marker file to indicate persistence is installed"
45+
mkdir -p "$FEATURE_DIR/markers"
46+
touch "$FEATURE_DIR/markers/install"
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
# NOTES
6+
# `install.sh`
7+
# - moves `.azd` to `.~azd-old` if it exists
8+
# - creates symlink from `~/.azd` to the mounted volume
9+
#
10+
# `oncreate.sh`
11+
# - if the old `.azd/extensions` folder exists
12+
# - and is symlinked to the same location then exit (success)
13+
# - and is symlinked to a different location then fail (unexpected/unplanned state)
14+
# - and not a symlink then copy extensions from the old folder to the new location, adding marker files to copied extensions. Additionally clean up any extensions with the marker file that no longer exist in the source folder (i.e. extensions that the user has removed but we previously copied).
15+
# - if the old `.azd/extensions` folder does not exist then check if the new `.azd/extensions` folder is a symlink to the old location and if so, remove the symlink
16+
17+
18+
FEATURE_DIR="/usr/local/share/stuartleeks-devcontainer-features/azd-cli-persistence"
19+
LOG_FILE="$FEATURE_DIR/log.txt"
20+
21+
log() {
22+
echo "$1"
23+
echo "$1" >> "$LOG_FILE"
24+
}
25+
26+
if command -v sudo > /dev/null; then
27+
sudo chown -R "$(id -u):$(id -g)" "$LOG_FILE"
28+
else
29+
chown -R "$(id -u):$(id -g)" "$LOG_FILE"
30+
fi
31+
32+
log "In OnCreate script"
33+
# check if marker file exists to avoid re-running oncreate script actions
34+
if [ -f "$HOME/.stuartleeks/azd-cli-persistence-oncreate" ]; then
35+
log "Feature 'azd-cli-persistence' oncreate actions already run, skipping"
36+
exit 0
37+
fi
38+
39+
40+
fix_permissions() {
41+
local dir
42+
dir="${1}"
43+
44+
if [ ! -w "${dir}" ]; then
45+
echo "Fixing permissions of '${dir}'..."
46+
sudo chown -R "$(id -u):$(id -g)" "${dir}"
47+
echo "Done!"
48+
else
49+
echo "Permissions of '${dir}' are OK!"
50+
fi
51+
}
52+
53+
fix_permissions "/dc/azure-dev"
54+
55+
add_marker() {
56+
log "Adding marker file to indicate oncreate actions have been run"
57+
mkdir -p "$HOME/markers"
58+
if command -v sudo > /dev/null; then
59+
sudo touch "$FEATURE_DIR/markers/oncreate"
60+
else
61+
touch "$FEATURE_DIR/markers/oncreate"
62+
fi
63+
}
64+
65+
merge_extensions() {
66+
local src_dir
67+
local dest_dir
68+
src_dir="${1}"
69+
dest_dir="${2}"
70+
log "Merging extensions from '${src_dir}' to '${dest_dir}'..."
71+
for extension in "${src_dir}"/*; do
72+
extension_name=$(basename "${extension}")
73+
if [ -d "${dest_dir}/${extension_name}" ]; then
74+
log " Extension '${extension_name}' already exists in destination - skipping"
75+
else
76+
log " Copying extension '${extension_name}' to destination"
77+
cp -R "${extension}" "${dest_dir}/"
78+
# Add a marker file to indicate that this extension was copied from the source folder
79+
touch "${dest_dir}/${extension_name}/.azd-cli-persistence-copied"
80+
fi
81+
done
82+
83+
log "Checking for extensions that were copied previously but are no longer in the source folder..."
84+
for extension in "${dest_dir}"/*; do
85+
extension_name=$(basename "${extension}")
86+
if [ -f "${extension}/.azd-cli-persistence-copied" ]; then
87+
if [ ! -d "${src_dir}/${extension_name}" ]; then
88+
log " Extension '${extension_name}' was copied previously but is no longer in source - removing"
89+
rm -rf "${extension}"
90+
else
91+
log " Extension '${extension_name}' was copied but is still present in source - keeping"
92+
fi
93+
else
94+
log " Extension '${extension_name}' was not copied by azure-cli-persistence - keeping"
95+
fi
96+
done
97+
}
98+
99+
# Fix up the extensions folder in case the user had an old .azd folder
100+
# that had extensions installed in it
101+
102+
if [ -d "$HOME/.azd-old" ]; then
103+
got_old_azure_folder=true
104+
else
105+
got_old_azure_folder=false
106+
fi
107+
108+
109+
old_extensions_folder="$HOME/.azc-old/extensions"
110+
new_extensions_folder="$HOME/.azc/extensions"
111+
new_extensions_folder_parent="$HOME/.azc"
112+
113+
if [ -d "$old_extensions_folder" ]; then
114+
log "extensions folder found in old .azd folder"
115+
if [ -d "$new_extensions_folder" ]; then
116+
if [ -L "$new_extensions_folder" ]; then
117+
symlink_target=$(readlink "$new_extensions_folder")
118+
if [ "$symlink_target" = "$old_extensions_folder" ]; then
119+
log "extensions folder ('$new_extensions_folder') already symlinked to '$old_extensions_folder' - no action needed"
120+
add_marker
121+
log "Done"
122+
exit 0
123+
else
124+
log "extensions folder ('$new_extensions_folder') is a symlink, but points to a different location ('$symlink_target')"
125+
exit 1
126+
fi
127+
else
128+
log "extensions folder ('$new_extensions_folder') already exists but is not a symlink"
129+
merge_extensions "$old_extensions_folder" "$new_extensions_folder"
130+
add_marker
131+
log "Done"
132+
exit 0
133+
fi
134+
else
135+
log "extensions folder ('$new_extensions_folder') does not exist - creating symlink to '$old_extensions_folder'"
136+
log "Creating symlink..."
137+
if command -v sudo > /dev/null; then
138+
sudo ln -s "$old_extensions_folder" "$new_extensions_folder_parent"
139+
else
140+
ln -s "$old_extensions_folder" "$new_extensions_folder_parent"
141+
fi
142+
add_marker
143+
log "Done"
144+
exit 0
145+
fi
146+
else
147+
log "No old .azd/extensions folder found"
148+
# If we haven't got an old .azd folder with a extensions folder in it, check if the new extensions folder is a symlink to the old one
149+
# And if so, remove the symlink
150+
# This can happen if the user has installed extensions before this feature runs
151+
if [ -L "$new_extensions_folder" ]; then
152+
symlink_target=$(readlink "$new_extensions_folder")
153+
if [ "$symlink_target" = "$old_extensions_folder" ]; then
154+
log "extensions folder ('$new_extensions_folder') is a symlink to '$old_extensions_folder' which doesn't exist- removing symlink"
155+
rm "$new_extensions_folder"
156+
fi
157+
fi
158+
fi
159+
160+
add_marker
161+
log "Done"
162+

src/azure-cli-persistence/NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
| Version | Notes |
55
| ------- | ------------------------------------------------------------ |
6+
| 0.0.9 | Move marker file location out of user home dir |
67
| 0.0.8 | Add logic to merge extensions when the extensions folder already exists in both old and new .azure folders (see [#36](https://github.com/stuartleeks/dev-container-features/issues/36)) |
78
| 0.0.7 | Add marker files to prevent double installation when a feature is [always installed](https://code.visualstudio.com/docs/devcontainers/containers#_always-installed-features) and in a devcontainer.json file. |
89
| 0.0.6 | Add symlink for cliextensions folder |

src/azure-cli-persistence/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Preserve ~/.azure folder across container instances (avoids needing to re-login
2222

2323
| Version | Notes |
2424
| ------- | ------------------------------------------------------------ |
25-
| 0.0.8 | Add logic to merge extensions when the extensions folder already exists in both old and new .azure folders (see [#36](https://github.com/stuartleeks/dev-container-features/issues/36)) |
25+
| 0.0.9 | Move marker file location out of user home dir |
26+
| 0.0.8 | Add logic to merge extensions when the extensions folder already exists in both old and new .azure folders (see [#36](https://github.com/stuartleeks/dev-container-features/issues/36)) (see [#36](https://github.com/stuartleeks/dev-container-features/issues/36)) |
2627
| 0.0.7 | Add marker files to prevent double installation when a feature is [always installed](https://code.visualstudio.com/docs/devcontainers/containers#_always-installed-features) and in a devcontainer.json file. |
2728
| 0.0.6 | Add symlink for cliextensions folder |
2829
| 0.0.5 | Use lifecycle scripts |

src/azure-cli-persistence/devcontainer-feature.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Azure CLI Persistence",
33
"id": "azure-cli-persistence",
4-
"version": "0.0.8",
4+
"version": "0.0.9",
55
"description": "Preserve ~/.azure folder across container instances (avoids needing to re-login after rebuilding)",
66
"options": {},
77
"mounts": [

src/azure-cli-persistence/install.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ log "Activating feature 'azure-cli-persistence'"
1818
log "User: ${_REMOTE_USER} User home: ${_REMOTE_USER_HOME}"
1919

2020
# check if marker file exists to avoid re-running oncreate script actions
21-
if [ -f "$_REMOTE_USER_HOME/.stuartleeks/azure-cli-persistence-install" ]; then
21+
if [ -f "$FEATURE_DIR/markers/install" ]; then
2222
log "Feature 'azure-cli-persistence' already installed, skipping oncreate actions"
2323
exit 0
2424
fi
@@ -41,5 +41,5 @@ if [ -f oncreate.sh ]; then
4141
fi
4242

4343
log "Adding marker file to indicate persistence is installed"
44-
mkdir -p "$_REMOTE_USER_HOME/.stuartleeks"
45-
touch "$_REMOTE_USER_HOME/.stuartleeks/azure-cli-persistence-install"
44+
mkdir -p "$FEATURE_DIR/markers"
45+
touch "$FEATURE_DIR/markers/install"

src/azure-cli-persistence/oncreate.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fi
3131

3232
log "In OnCreate script"
3333
# check if marker file exists to avoid re-running oncreate script actions
34-
if [ -f "$HOME/.stuartleeks/azure-cli-persistence-oncreate" ]; then
34+
if [ -f "$FEATURE_DIR/markers/oncreate" ]; then
3535
log "Feature 'azure-cli-persistence' oncreate actions already run, skipping"
3636
exit 0
3737
fi
@@ -54,11 +54,11 @@ fix_permissions "/dc/azure"
5454

5555
add_marker() {
5656
log "Adding marker file to indicate oncreate actions have been run"
57-
mkdir -p "$HOME/.stuartleeks"
57+
mkdir -p "$FEATURE_DIR/markers"
5858
if command -v sudo > /dev/null; then
59-
sudo touch "$HOME/.stuartleeks/azure-cli-persistence-oncreate"
59+
sudo touch "$FEATURE_DIR/markers/oncreate"
6060
else
61-
touch "$HOME/.stuartleeks/azure-cli-persistence-oncreate"
61+
touch "$FEATURE_DIR/markers/oncreate"
6262
fi
6363
}
6464

0 commit comments

Comments
 (0)