diff --git a/src/aws-cli-persistence/NOTES.md b/src/aws-cli-persistence/NOTES.md index a8a92cd..42172ee 100644 --- a/src/aws-cli-persistence/NOTES.md +++ b/src/aws-cli-persistence/NOTES.md @@ -8,12 +8,13 @@ Shells: `bash`, `zsh`, `fish` ## Changelog -| Version | Notes | -| ------- | ------------------------ | -| 1.0.2 | Update soft dependencies | -| 1.0.1 | Fix Docs | -| 1.0.0 | Support zsh + refactor | -| 0.0.0 | Initial Version | +| Version | Notes | +| ------- | ----------------------------------------------- | +| 1.0.3 | Move onCreate lifecycle script to `oncreate.sh` | +| 1.0.2 | Update soft dependencies | +| 1.0.1 | Fix Docs | +| 1.0.0 | Support zsh + refactor | +| 0.0.0 | Initial Version | ## References diff --git a/src/aws-cli-persistence/devcontainer-feature.json b/src/aws-cli-persistence/devcontainer-feature.json index ca332ee..c8d4dea 100644 --- a/src/aws-cli-persistence/devcontainer-feature.json +++ b/src/aws-cli-persistence/devcontainer-feature.json @@ -1,7 +1,7 @@ { "name": "AWS CLI Persistence", "id": "aws-cli-persistence", - "version": "1.0.2", + "version": "1.0.3", "documentationURL": "https://github.com/joshuanianji/devcontainer-features/tree/main/src/aws-cli-persistence", "description": "Avoid extra logins from the AWS CLI by preserving the `~/.aws` folder across container instances.", "options": {}, @@ -17,5 +17,7 @@ "ghcr.io/devcontainers/features/common-utils", "ghcr.io/meaningful-ooo/devcontainer-features/fish" ], - "postCreateCommand": "/usr/local/share/aws-cli-persistence-post-create.sh" + "onCreateCommand": { + "aws-cli-persistence-setup": "/usr/local/share/aws-cli-persistence/scripts/oncreate.sh" + } } \ No newline at end of file diff --git a/src/aws-cli-persistence/install.sh b/src/aws-cli-persistence/install.sh index bf51027..7542716 100644 --- a/src/aws-cli-persistence/install.sh +++ b/src/aws-cli-persistence/install.sh @@ -1,47 +1,52 @@ #!/bin/sh -set -e +USERNAME=${USERNAME:-${_REMOTE_USER}} FEATURE_ID="aws-cli-persistence" - -echo "Activating feature '$FEATURE_ID'" -echo "User: ${_REMOTE_USER} User home: ${_REMOTE_USER_HOME}" - -if [ -z "$_REMOTE_USER" ] || [ -z "$_REMOTE_USER_HOME" ]; then - echo "***********************************************************************************" - echo "*** Require _REMOTE_USER and _REMOTE_USER_HOME to be set (by dev container CLI) ***" - echo "***********************************************************************************" - exit 1 -fi - -# make /dc/aws-cli folder if doesn't exist -mkdir -p "/dc/aws-cli" - -# as to why we move around the folder, check `github-cli-persistence/install.sh` -if [ -e "$_REMOTE_USER_HOME/.aws" ]; then - echo "Moving existing .aws folder to .aws-old" - mv "$_REMOTE_USER_HOME/.aws" "$_REMOTE_USER_HOME/.aws-old" -fi - -ln -s /dc/aws-cli "$_REMOTE_USER_HOME/.aws" -# chown .aws folder -chown -R "${_REMOTE_USER}:${_REMOTE_USER}" "$_REMOTE_USER_HOME/.aws" - -# --- Generate a '$FEATURE_ID-post-create.sh' script to be executed by the 'postCreateCommand' lifecycle hook -# Looks like this is the best way to run a script in lifecycle hooks -# Source: https://github.com/devcontainers/features/blob/562305d37b97d47331d96306ffc2a0a3cce55e64/src/git-lfs/install.sh#L190C1-L190C109 -POST_CREATE_SCRIPT_PATH="/usr/local/share/$FEATURE_ID-post-create.sh" - -tee "$POST_CREATE_SCRIPT_PATH" >/dev/null \ - <<'EOF' -#!/bin/sh +LIFECYCLE_SCRIPTS_DIR="/usr/local/share/${FEATURE_ID}/scripts" set -e -# if the user is not root, chown /dc/aws-cli to the user -if [ "$(id -u)" != "0" ]; then - echo "Running post-start.sh for user $USER" - sudo chown -R "$USER:$USER" /dc/aws-cli +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/aws-cli" "${USERNAME}" +create_symlink_dir "$_REMOTE_USER_HOME/.aws" "/dc/aws-cli" "${USERNAME}" + +# Set Lifecycle scripts +if [ -f oncreate.sh ]; then + mkdir -p "${LIFECYCLE_SCRIPTS_DIR}" + cp oncreate.sh "${LIFECYCLE_SCRIPTS_DIR}/oncreate.sh" fi -EOF -chmod 755 "$POST_CREATE_SCRIPT_PATH" +echo "Finished installing $FEATURE_ID" diff --git a/src/aws-cli-persistence/oncreate.sh b/src/aws-cli-persistence/oncreate.sh new file mode 100644 index 0000000..281790b --- /dev/null +++ b/src/aws-cli-persistence/oncreate.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +set -e + +# if the user is not root, chown /dc/aws-cli to the user +if [ "$(id -u)" != "0" ]; then + echo "Running oncreate.sh for user $USER" + sudo chown -R "$USER:$USER" /dc/aws-cli +fi diff --git a/src/gcloud-cli-persistence/NOTES.md b/src/gcloud-cli-persistence/NOTES.md index 97d4a09..42e0ca1 100644 --- a/src/gcloud-cli-persistence/NOTES.md +++ b/src/gcloud-cli-persistence/NOTES.md @@ -8,9 +8,10 @@ Shells: `bash`, `zsh`, `fish` ## Changelog -| Version | Notes | -| ------- | ------------------------ | -| 1.0.2 | Update soft dependencies | -| 1.0.1 | Fix Docs | -| 1.0.0 | Support zsh + refactor | -| 0.0.0 | Initial Version | +| Version | Notes | +| ------- | ----------------------------------------------- | +| 1.0.3 | Move onCreate lifecycle script to `oncreate.sh` | +| 1.0.2 | Update soft dependencies | +| 1.0.1 | Fix Docs | +| 1.0.0 | Support zsh + refactor | +| 0.0.0 | Initial Version | diff --git a/src/gcloud-cli-persistence/devcontainer-feature.json b/src/gcloud-cli-persistence/devcontainer-feature.json index 4647c62..54dda61 100644 --- a/src/gcloud-cli-persistence/devcontainer-feature.json +++ b/src/gcloud-cli-persistence/devcontainer-feature.json @@ -1,7 +1,7 @@ { "name": "Google Cloud CLI Persistence", "id": "gcloud-cli-persistence", - "version": "1.0.2", + "version": "1.0.3", "documentationURL": "https://github.com/joshuanianji/devcontainer-features/tree/main/src/gcloud-cli-persistence", "description": "Avoid extra logins from the Google Cloud CLI by preserving the `~/.config/gcloud` folder across container instances.", "options": {}, @@ -17,5 +17,7 @@ "ghcr.io/devcontainers/features/common-utils", "ghcr.io/meaningful-ooo/devcontainer-features/fish" ], - "postCreateCommand": "/usr/local/share/gcloud-cli-persistence-post-create.sh" + "onCreateCommand": { + "gcloud-cli-persistence-setup": "/usr/local/share/gcloud-cli-persistence/scripts/oncreate.sh" + } } \ No newline at end of file diff --git a/src/gcloud-cli-persistence/install.sh b/src/gcloud-cli-persistence/install.sh index 16cb04f..8b8f3fb 100644 --- a/src/gcloud-cli-persistence/install.sh +++ b/src/gcloud-cli-persistence/install.sh @@ -1,51 +1,51 @@ #!/bin/sh set -e +USERNAME=${USERNAME:-${_REMOTE_USER}} FEATURE_ID="gcloud-cli-persistence" - -echo "Activating feature '$FEATURE_ID'" -echo "User: ${_REMOTE_USER} User home: ${_REMOTE_USER_HOME}" - -if [ -z "$_REMOTE_USER" ] || [ -z "$_REMOTE_USER_HOME" ]; then - echo "***********************************************************************************" - echo "*** Require _REMOTE_USER and _REMOTE_USER_HOME to be set (by dev container CLI) ***" - echo "***********************************************************************************" - exit 1 -fi - -# make ~/.config folder if doesn't exist -mkdir -p "$_REMOTE_USER_HOME/.config" -mkdir -p "/dc/gcloud-cli" - -# if `.config/gcloud` already exists, the `ln -s` command will create an extra -# folder *inside* `.config/gcloud` that symlinks to `gcloud-cli` -# Thus, we want to make sure the folder does NOT exist so the symlink will actually be to ~/.config/gcloud -if [ -e "$_REMOTE_USER_HOME/.config/gcloud" ]; then - echo "Moving existing gcloud folder to gcloud-old" - mv "$_REMOTE_USER_HOME/.config/gcloud" "$_REMOTE_USER_HOME/.config/gcloud-old" -fi - -ln -s /dc/gcloud-cli "$_REMOTE_USER_HOME/.config/gcloud" -# chown the entire `.config` folder because devcontainers creates -# a `~/.config/vscode-dev-containers` folder later on -chown -R "${_REMOTE_USER}:${_REMOTE_USER}" "$_REMOTE_USER_HOME/.config" - -# --- Generate a '$FEATURE_ID-post-create.sh' script to be executed by the 'postCreateCommand' lifecycle hook -# Looks like this is the best way to run a script in lifecycle hooks -# Source: https://github.com/devcontainers/features/blob/562305d37b97d47331d96306ffc2a0a3cce55e64/src/git-lfs/install.sh#L190C1-L190C109 -POST_CREATE_SCRIPT_PATH="/usr/local/share/$FEATURE_ID-post-create.sh" - -tee "$POST_CREATE_SCRIPT_PATH" >/dev/null \ - <<'EOF' -#!/bin/sh - -set -e - -# if the user is not root, chown /dc/aws-cli to the user -if [ "$(id -u)" != "0" ]; then - echo "Running post-start.sh for user $USER" - sudo chown -R "$USER:$USER" /dc/gcloud-cli +LIFECYCLE_SCRIPTS_DIR="/usr/local/share/${FEATURE_ID}/scripts" + +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/gcloud-cli" "${USERNAME}" +create_symlink_dir "$_REMOTE_USER_HOME/.config/gcloud" "/dc/gcloud-cli" "${USERNAME}" + +# Set Lifecycle scripts +if [ -f oncreate.sh ]; then + mkdir -p "${LIFECYCLE_SCRIPTS_DIR}" + cp oncreate.sh "${LIFECYCLE_SCRIPTS_DIR}/oncreate.sh" fi -EOF -chmod 755 "$POST_CREATE_SCRIPT_PATH" +echo "Finished installing $FEATURE_ID" diff --git a/src/gcloud-cli-persistence/oncreate.sh b/src/gcloud-cli-persistence/oncreate.sh new file mode 100644 index 0000000..c505b52 --- /dev/null +++ b/src/gcloud-cli-persistence/oncreate.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +set -e + +# if the user is not root, chown /dc/aws-cli to the user +if [ "$(id -u)" != "0" ]; then + echo "Running post-start.sh for user $USER" + sudo chown -R "$USER:$USER" /dc/gcloud-cli +fi diff --git a/src/github-cli-persistence/NOTES.md b/src/github-cli-persistence/NOTES.md index 7a976bd..5e25f0b 100644 --- a/src/github-cli-persistence/NOTES.md +++ b/src/github-cli-persistence/NOTES.md @@ -10,6 +10,7 @@ Shells: `bash`, `zsh`, `fish` | Version | Notes | | ------- | ---------------------------------------------------- | +| 1.0.3 | Move onCreate lifecycle script to `oncreate.sh` | | 1.0.2 | Update soft dependencies | | 1.0.1 | Fix Docs | | 1.0.0 | Support zsh + refactor | diff --git a/src/github-cli-persistence/devcontainer-feature.json b/src/github-cli-persistence/devcontainer-feature.json index 3af1efb..0498003 100644 --- a/src/github-cli-persistence/devcontainer-feature.json +++ b/src/github-cli-persistence/devcontainer-feature.json @@ -1,7 +1,7 @@ { "name": "Github CLI Persistence", "id": "github-cli-persistence", - "version": "1.0.2", + "version": "1.0.3", "documentationURL": "https://github.com/joshuanianji/devcontainer-features/tree/main/src/github-cli-persistence", "description": "Avoid extra logins from the Github CLI by preserving the `~/.config/gh` folder across container instances.", "options": {}, @@ -17,5 +17,7 @@ "ghcr.io/devcontainers/features/common-utils", "ghcr.io/meaningful-ooo/devcontainer-features/fish" ], - "postCreateCommand": "/usr/local/share/github-cli-persistence-post-create.sh" + "onCreateCommand": { + "github-cli-persistence-setup": "/usr/local/share/github-cli-persistence/scripts/oncreate.sh" + } } \ No newline at end of file diff --git a/src/github-cli-persistence/install.sh b/src/github-cli-persistence/install.sh index 7e2ce95..c4e5d79 100644 --- a/src/github-cli-persistence/install.sh +++ b/src/github-cli-persistence/install.sh @@ -1,50 +1,51 @@ #!/bin/sh set -e +USERNAME=${USERNAME:-${_REMOTE_USER}} FEATURE_ID="github-cli-persistence" - -echo "Activating feature '$FEATURE_ID'" -echo "User: ${_REMOTE_USER} User home: ${_REMOTE_USER_HOME}" - -if [ -z "$_REMOTE_USER" ] || [ -z "$_REMOTE_USER_HOME" ]; then - echo "***********************************************************************************" - echo "*** Require _REMOTE_USER and _REMOTE_USER_HOME to be set (by dev container CLI) ***" - echo "***********************************************************************************" - exit 1 -fi - -# make ~/.config folder if doesn't exist -mkdir -p "$_REMOTE_USER_HOME/.config" -mkdir -p "/dc/github-cli" - -# if `.config/gh` already exists, the `ln -s` command will create an extra -# folder *inside* `.config/gh` that symlinks to `github-cli` -# Thus, we want to make sure the folder does NOT exist so the symlink will actually be to ~/.config/gh -if [ -e "$_REMOTE_USER_HOME/.config/gh" ]; then - echo "Moving existing gh folder to gh-old" - mv "$_REMOTE_USER_HOME/.config/gh" "$_REMOTE_USER_HOME/.config/gh-old" -fi - -ln -s /dc/github-cli "$_REMOTE_USER_HOME/.config/gh" -# chown the entire `.config` folder because devcontainers creates -chown -R "${_REMOTE_USER}:${_REMOTE_USER}" "$_REMOTE_USER_HOME/.config/gh" - -# --- Generate a '$FEATURE_ID-post-create.sh' script to be executed by the 'postCreateCommand' lifecycle hook -# Looks like this is the best way to run a script in lifecycle hooks -# Source: https://github.com/devcontainers/features/blob/562305d37b97d47331d96306ffc2a0a3cce55e64/src/git-lfs/install.sh#L190C1-L190C109 -POST_CREATE_SCRIPT_PATH="/usr/local/share/$FEATURE_ID-post-create.sh" - -tee "$POST_CREATE_SCRIPT_PATH" >/dev/null \ - <<'EOF' -#!/bin/sh - -set -e - -# if the user is not root, chown /dc/aws-cli to the user -if [ "$(id -u)" != "0" ]; then - echo "Running post-start.sh for user $USER" - sudo chown -R "$USER:$USER" /dc/github-cli +LIFECYCLE_SCRIPTS_DIR="/usr/local/share/${FEATURE_ID}/scripts" + +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/github-cli" "${USERNAME}" +create_symlink_dir "$_REMOTE_USER_HOME/.config/gh" "/dc/github-cli" "${USERNAME}" + +# Set Lifecycle scripts +if [ -f oncreate.sh ]; then + mkdir -p "${LIFECYCLE_SCRIPTS_DIR}" + cp oncreate.sh "${LIFECYCLE_SCRIPTS_DIR}/oncreate.sh" fi -EOF -chmod 755 "$POST_CREATE_SCRIPT_PATH" +echo "Finished installing $FEATURE_ID" diff --git a/src/github-cli-persistence/oncreate.sh b/src/github-cli-persistence/oncreate.sh new file mode 100644 index 0000000..7df5153 --- /dev/null +++ b/src/github-cli-persistence/oncreate.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +set -e + +# if the user is not root, chown /dc/aws-cli to the user +if [ "$(id -u)" != "0" ]; then + echo "Running post-start.sh for user $USER" + sudo chown -R "$USER:$USER" /dc/github-cli +fi diff --git a/src/mount-pnpm-store/NOTES.md b/src/mount-pnpm-store/NOTES.md index 89f3798..babfd1b 100644 --- a/src/mount-pnpm-store/NOTES.md +++ b/src/mount-pnpm-store/NOTES.md @@ -38,13 +38,14 @@ The volume mount is called `global-devcontainer-pnpm-store`, so ensure that no o ## Changelog -| Version | Notes | -| ------- | ---------------------- | -| 1.0.1 | Fix Docs | -| 1.0.0 | Support zsh + refactor | -| 0.1.1 | Fix mount name | -| 0.1.0 | Documentation | -| 0.0.0 | Initial Version | +| Version | Notes | +| ------- | ----------------------------------------------- | +| 1.0.2 | Move onCreate lifecycle script to `oncreate.sh` | +| 1.0.1 | Fix Docs | +| 1.0.0 | Support zsh + refactor | +| 0.1.1 | Fix mount name | +| 0.1.0 | Documentation | +| 0.0.0 | Initial Version | ## References diff --git a/src/mount-pnpm-store/devcontainer-feature.json b/src/mount-pnpm-store/devcontainer-feature.json index 3b97c69..c6dd8f0 100644 --- a/src/mount-pnpm-store/devcontainer-feature.json +++ b/src/mount-pnpm-store/devcontainer-feature.json @@ -1,7 +1,7 @@ { "name": "Mount pnpm Store", "id": "mount-pnpm-store", - "version": "1.0.1", + "version": "1.0.2", "documentationURL": "https://github.com/joshuanianji/devcontainer-features/tree/main/src/mount-pnpm-store", "description": "Sets pnpm store to ~/.pnpm-store and mounts it to a volume to share between multiple devcontainers", "options": {}, @@ -17,5 +17,7 @@ "ghcr.io/devcontainers/features/common-utils", "ghcr.io/meaningful-ooo/devcontainer-features/fish" ], - "postCreateCommand": "/usr/local/share/mount-pnpm-store-post-create.sh" + "onCreateCommand": { + "mount-pnpm-store-setup": "/usr/local/share/mount-pnpm-store/scripts/oncreate.sh" + } } \ No newline at end of file diff --git a/src/mount-pnpm-store/install.sh b/src/mount-pnpm-store/install.sh index e7b5693..e6560c7 100644 --- a/src/mount-pnpm-store/install.sh +++ b/src/mount-pnpm-store/install.sh @@ -1,55 +1,52 @@ #!/bin/sh -set -e +USERNAME=${USERNAME:-${_REMOTE_USER}} FEATURE_ID="mount-pnpm-store" - -echo "Activating feature '$FEATURE_ID'" -echo "User: ${_REMOTE_USER} User home: ${_REMOTE_USER_HOME}" - -if [ -z "$_REMOTE_USER" ] || [ -z "$_REMOTE_USER_HOME" ]; then - echo "***********************************************************************************" - echo "*** Require _REMOTE_USER and _REMOTE_USER_HOME to be set (by dev container CLI) ***" - echo "***********************************************************************************" - exit 1 -fi - -# make /dc/mounted-pnpm-store folder if doesn't exist -mkdir -p "/dc/mounted-pnpm-store" - -# as to why we move around the folder, check `github-cli-persistence/install.sh` -if [ -e "$_REMOTE_USER_HOME/.pnpm-store" ]; then - echo "Moving existing .pnpm-store folder to .pnpm-store-old" - mv "$_REMOTE_USER_HOME/.pnpm-store" "$_REMOTE_USER_HOME/.pnpm-store-old" -fi - -ln -s /dc/mounted-pnpm-store "$_REMOTE_USER_HOME/.pnpm-store" -chown -R "$_REMOTE_USER:$_REMOTE_USER" "$_REMOTE_USER_HOME/.pnpm-store" - -# --- Generate a '$FEATURE_ID-post-create.sh' script to be executed by the 'postCreateCommand' lifecycle hook -# Looks like this is the best way to run a script in lifecycle hooks -# Source: https://github.com/devcontainers/features/blob/562305d37b97d47331d96306ffc2a0a3cce55e64/src/git-lfs/install.sh#L190C1-L190C109 -POST_CREATE_SCRIPT_PATH="/usr/local/share/$FEATURE_ID-post-create.sh" - -tee "$POST_CREATE_SCRIPT_PATH" >/dev/null \ - <<'EOF' -#!/bin/sh +LIFECYCLE_SCRIPTS_DIR="/usr/local/share/${FEATURE_ID}/scripts" set -e -# set pnpm config (if it's installed) -if type pnpm >/dev/null 2>&1; then - echo "Setting pnpm store location to $_REMOTE_USER_HOME/.pnpm-store" - pnpm config set store-dir ~/.pnpm-store --global -else - echo "WARN: pnpm is not installed! Please ensure pnpm is installed and in your PATH." - echo "WARN: pnpm store location will not be set." -fi - -# if the user is not root, chown /dc/mounted-pnpm-store to the user -if [ "$(id -u)" != "0" ]; then - echo "Running post-start.sh for user $USER" - sudo chown -R "$USER:$USER" /dc/mounted-pnpm-store +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/mounted-pnpm-store" "${USERNAME}" +create_symlink_dir "$_REMOTE_USER_HOME/.pnpm-store" "/dc/mounted-pnpm-store" "${USERNAME}" + +# Set Lifecycle scripts +if [ -f oncreate.sh ]; then + mkdir -p "${LIFECYCLE_SCRIPTS_DIR}" + cp oncreate.sh "${LIFECYCLE_SCRIPTS_DIR}/oncreate.sh" fi -EOF -chmod 755 "$POST_CREATE_SCRIPT_PATH" +echo "Finished installing $FEATURE_ID" diff --git a/src/mount-pnpm-store/oncreate.sh b/src/mount-pnpm-store/oncreate.sh new file mode 100644 index 0000000..278745d --- /dev/null +++ b/src/mount-pnpm-store/oncreate.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +set -e + +# set pnpm config (if it's installed) +if type pnpm >/dev/null 2>&1; then + echo "Setting pnpm store location to $_REMOTE_USER_HOME/.pnpm-store" + pnpm config set store-dir ~/.pnpm-store --global +else + echo "WARN: pnpm is not installed! Please ensure pnpm is installed and in your PATH." + echo "WARN: pnpm store location will not be set." +fi + +# if the user is not root, chown /dc/mounted-pnpm-store to the user +if [ "$(id -u)" != "0" ]; then + echo "Running post-start.sh for user $USER" + sudo chown -R "$USER:$USER" /dc/mounted-pnpm-store +fi diff --git a/src/terraform-cli-persistence/NOTES.md b/src/terraform-cli-persistence/NOTES.md index 0c8361d..9a95787 100644 --- a/src/terraform-cli-persistence/NOTES.md +++ b/src/terraform-cli-persistence/NOTES.md @@ -8,11 +8,12 @@ Shells: `bash`, `zsh`, `fish` ## Changelog -| Version | Notes | -| ------- | ---------------------- | -| 1.0.1 | Fix Docs | -| 1.0.0 | Support zsh + refactor | -| 0.0.0 | Initial Version | +| Version | Notes | +| ------- | ----------------------------------------------- | +| 1.0.2 | Move onCreate lifecycle script to `oncreate.sh` | +| 1.0.1 | Fix Docs | +| 1.0.0 | Support zsh + refactor | +| 0.0.0 | Initial Version | ## References diff --git a/src/terraform-cli-persistence/devcontainer-feature.json b/src/terraform-cli-persistence/devcontainer-feature.json index 441690a..b6a4288 100644 --- a/src/terraform-cli-persistence/devcontainer-feature.json +++ b/src/terraform-cli-persistence/devcontainer-feature.json @@ -1,7 +1,7 @@ { "name": "Terraform CLI Persistence", "id": "terraform-cli-persistence", - "version": "1.0.1", + "version": "1.0.2", "documentationURL": "https://github.com/joshuanianji/devcontainer-features/tree/main/src/terraform-cli-persistence", "description": "Avoid extra logins from the Terraform CLI by preserving the `~/.terraform.d` folder across container instances.", "options": {}, @@ -17,5 +17,7 @@ "ghcr.io/devcontainers/features/common-utils", "ghcr.io/meaningful-ooo/devcontainer-features/fish" ], - "postCreateCommand": "/usr/local/share/terraform-cli-persistence-post-create.sh" + "onCreateCommand": { + "terraform-cli-persistence-setup": "/usr/local/share/terraform-cli-persistence/scripts/oncreate.sh" + } } \ No newline at end of file diff --git a/src/terraform-cli-persistence/install.sh b/src/terraform-cli-persistence/install.sh index 93cf38a..1612ad1 100644 --- a/src/terraform-cli-persistence/install.sh +++ b/src/terraform-cli-persistence/install.sh @@ -1,48 +1,52 @@ #!/bin/sh -set -e +USERNAME=${USERNAME:-${_REMOTE_USER}} FEATURE_ID="terraform-cli-persistence" - -echo "Activating feature '$FEATURE_ID'" -echo "User: ${_REMOTE_USER} User home: ${_REMOTE_USER_HOME}" - -if [ -z "$_REMOTE_USER" ] || [ -z "$_REMOTE_USER_HOME" ]; then - echo "***********************************************************************************" - echo "*** Require _REMOTE_USER and _REMOTE_USER_HOME to be set (by dev container CLI) ***" - echo "***********************************************************************************" - exit 1 -fi - -# make /dc/terraform-cli folder if doesn't exist -mkdir -p "/dc/terraform-cli" - -# as to why we move around the folder, check `github-cli-persistence/install.sh` -if [ -e "$_REMOTE_USER_HOME/.terraform.d" ]; then - echo "Moving existing .terraform.d folder to .terraform.d-old" - mv "$_REMOTE_USER_HOME/.terraform.d" "$_REMOTE_USER_HOME/.terraform.d-old" -fi - -ln -s /dc/terraform-cli "$_REMOTE_USER_HOME/.terraform.d" -# chown the entire `.config` folder because devcontainers creates -# a `~/.config/vscode-dev-containers` folder later on -chown -R "${_REMOTE_USER}:${_REMOTE_USER}" "$_REMOTE_USER_HOME/.terraform.d" - -# --- Generate a '$FEATURE_ID-post-create.sh' script to be executed by the 'postCreateCommand' lifecycle hook -# Looks like this is the best way to run a script in lifecycle hooks -# Source: https://github.com/devcontainers/features/blob/562305d37b97d47331d96306ffc2a0a3cce55e64/src/git-lfs/install.sh#L190C1-L190C109 -POST_CREATE_SCRIPT_PATH="/usr/local/share/$FEATURE_ID-post-create.sh" - -tee "$POST_CREATE_SCRIPT_PATH" >/dev/null \ - <<'EOF' -#!/bin/sh +LIFECYCLE_SCRIPTS_DIR="/usr/local/share/${FEATURE_ID}/scripts" set -e -# if the user is not root, chown /dc/aws-cli to the user -if [ "$(id -u)" != "0" ]; then - echo "Running post-start.sh for user $USER" - sudo chown -R "$USER:$USER" /dc/terraform-cli +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/terraform-cli" "${USERNAME}" +create_symlink_dir "$_REMOTE_USER_HOME/.terraform.d" "/dc/terraform-cli" "${USERNAME}" + +# Set Lifecycle scripts +if [ -f oncreate.sh ]; then + mkdir -p "${LIFECYCLE_SCRIPTS_DIR}" + cp oncreate.sh "${LIFECYCLE_SCRIPTS_DIR}/oncreate.sh" fi -EOF -chmod 755 "$POST_CREATE_SCRIPT_PATH" +echo "Finished installing $FEATURE_ID" diff --git a/src/terraform-cli-persistence/oncreate.sh b/src/terraform-cli-persistence/oncreate.sh new file mode 100644 index 0000000..9d75024 --- /dev/null +++ b/src/terraform-cli-persistence/oncreate.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +set -e + +# if the user is not root, chown /dc/aws-cli to the user +if [ "$(id -u)" != "0" ]; then + echo "Running post-start.sh for user $USER" + sudo chown -R "$USER:$USER" /dc/terraform-cli +fi