From c3bdb805279a6042820240866a7437fa0505e800 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Tue, 25 Mar 2025 06:33:21 +0000 Subject: [PATCH] Create symlink to original .azure folder to preserve cliextensions --- src/azure-cli-persistence/install.sh | 21 ++++-- src/azure-cli-persistence/oncreate.sh | 74 ++++++++++++++++++++ test-project/.devcontainer/devcontainer.json | 5 +- 3 files changed, 94 insertions(+), 6 deletions(-) diff --git a/src/azure-cli-persistence/install.sh b/src/azure-cli-persistence/install.sh index 0380ad9..9203672 100644 --- a/src/azure-cli-persistence/install.sh +++ b/src/azure-cli-persistence/install.sh @@ -1,21 +1,32 @@ #!/bin/sh -LIFECYCLE_SCRIPTS_DIR="/usr/local/share/stuartleeks-devcontainer-features/azure-cli-persistence/scripts" +FEATURE_DIR="/usr/local/share/stuartleeks-devcontainer-features/azure-cli-persistence" +LIFECYCLE_SCRIPTS_DIR="$FEATURE_DIR/scripts" +LOG_FILE="$FEATURE_DIR/log.txt" set -e -echo "Activating feature 'azure-cli-persistence'" -echo "User: ${_REMOTE_USER} User home: ${_REMOTE_USER_HOME}" +mkdir -p "${FEATURE_DIR}" +echo "" > "$LOG_FILE" +log() { + echo "$1" + echo "$1" >> "$LOG_FILE" +} + +log "Activating feature 'azure-cli-persistence'" +log "User: ${_REMOTE_USER} User home: ${_REMOTE_USER_HOME}" + +got_old_azure_folder=false if [ -e "$_REMOTE_USER_HOME/.azure" ]; then - echo "Moving existing .azure folder to .azure-old" + log "Moving existing .azure folder to .azure-old" mv "$_REMOTE_USER_HOME/.azure" "$_REMOTE_USER_HOME/.azure-old" + got_old_azure_folder=true fi ln -s /dc/azure/ "$_REMOTE_USER_HOME/.azure" chown -R "${_REMOTE_USER}:${_REMOTE_USER}" "$_REMOTE_USER_HOME/.azure" -# Set Lifecycle scripts if [ -f oncreate.sh ]; then mkdir -p "${LIFECYCLE_SCRIPTS_DIR}" cp oncreate.sh "${LIFECYCLE_SCRIPTS_DIR}/oncreate.sh" diff --git a/src/azure-cli-persistence/oncreate.sh b/src/azure-cli-persistence/oncreate.sh index f4d6f6a..197f6cd 100644 --- a/src/azure-cli-persistence/oncreate.sh +++ b/src/azure-cli-persistence/oncreate.sh @@ -2,6 +2,22 @@ set -e +FEATURE_DIR="/usr/local/share/stuartleeks-devcontainer-features/azure-cli-persistence" +LOG_FILE="$FEATURE_DIR/log.txt" + +log() { + echo "$1" + echo "$1" >> "$LOG_FILE" +} + +if command -v sudo > /dev/null; then + sudo chown -R "$(id -u):$(id -g)" "$LOG_FILE" +else + chown -R "$(id -u):$(id -g)" "$LOG_FILE" +fi + +log "In OnCreate script" + fix_permissions() { local dir dir="${1}" @@ -16,3 +32,61 @@ fix_permissions() { } fix_permissions "/dc/azure" + +# Fix up the cliextensions folder in case the user had an old .azure folder +# that had extensions installed in it (e.g. using the azure-cli feature and specifying extensions to install) + +if [ -d "$HOME/.azure-old" ]; then + got_old_azure_folder=true +else + got_old_azure_folder=false +fi + + +old_cliextensions_folder="$HOME/.azure-old/cliextensions" +new_cliextensions_folder="$HOME/.azure/cliextensions" +new_cliextensions_folder_parent="$HOME/.azure" + +got_old_extensions_folder=false + +if [ "$got_old_azure_folder" = true ]; then + if [ -d "$old_cliextensions_folder" ]; then + got_old_extensions_folder=true + log "cliextensions folder found in old .azure folder" + if [ -d "$new_cliextensions_folder" ]; then + if [ -L "$new_cliextensions_folder" ]; then + symlink_target=$(readlink "$new_cliextensions_folder") + if [ "$symlink_target" = "$old_cliextensions_folder" ]; then + log "cliextensions folder ('$new_cliextensions_folder') already symlinked to '$old_cliextensions_folder' - no action needed" + else + log "cliextensions folder ('$new_cliextensions_folder') is a symlink, but points to a different location ('$symlink_target')" + exit 1 + fi + else + log "cliextensions folder ('$new_cliextensions_folder') already exists in but is not a symlink" + exit 1 + fi + else + log "cliextensions folder ('$new_cliextensions_folder') does not exist - creating symlink to '$old_cliextensions_folder'" + if command -v sudo > /dev/null; then + sudo ln -s "$old_cliextensions_folder" "$new_cliextensions_folder_parent" + else + ln -s "$old_cliextensions_folder" "$new_cliextensions_folder_parent" + fi + fi + fi +fi + +# If we haven't got an old .azure folder with a cliextensions folder in it, check if the new cliextensions folder is a symlink to the old one +# And if so, remove the symlink +# This can happen if the user has installed the azure-cli feature and specified extensions to install +# and then later removed the extensions. +if [ "$got_old_extensions_folder" = false ]; then + if [ -L "$new_cliextensions_folder" ]; then + symlink_target=$(readlink "$new_cliextensions_folder") + if [ "$symlink_target" = "$old_cliextensions_folder" ]; then + log "cliextensions folder ('$new_cliextensions_folder') is a symlink to '$old_cliextensions_folder' - removing symlink" + rm "$new_cliextensions_folder" + fi + fi +fi diff --git a/test-project/.devcontainer/devcontainer.json b/test-project/.devcontainer/devcontainer.json index 4df7b77..ba3fdc6 100644 --- a/test-project/.devcontainer/devcontainer.json +++ b/test-project/.devcontainer/devcontainer.json @@ -10,8 +10,11 @@ // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. "remoteUser": "vscode", "features": { + "ghcr.io/devcontainers/features/azure-cli": { + // "extensions": "containerapp,ssh" + }, "ghcr.io/devcontainers-extra/features/fish-apt-get": {}, "./src/shell-history": {}, "./src/azure-cli-persistence": {} } -} \ No newline at end of file +}