Skip to content

Commit

Permalink
Merge pull request #56 from joshuanianji/oncreate-scripts
Browse files Browse the repository at this point in the history
Use `oncreate.sh` scripts
  • Loading branch information
joshuanianji authored Mar 13, 2024
2 parents 12e044d + f55826a commit af55bf6
Show file tree
Hide file tree
Showing 20 changed files with 325 additions and 249 deletions.
13 changes: 7 additions & 6 deletions src/aws-cli-persistence/NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions src/aws-cli-persistence/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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": {},
Expand All @@ -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"
}
}
83 changes: 44 additions & 39 deletions src/aws-cli-persistence/install.sh
Original file line number Diff line number Diff line change
@@ -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"
9 changes: 9 additions & 0 deletions src/aws-cli-persistence/oncreate.sh
Original file line number Diff line number Diff line change
@@ -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
13 changes: 7 additions & 6 deletions src/gcloud-cli-persistence/NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
6 changes: 4 additions & 2 deletions src/gcloud-cli-persistence/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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": {},
Expand All @@ -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"
}
}
90 changes: 45 additions & 45 deletions src/gcloud-cli-persistence/install.sh
Original file line number Diff line number Diff line change
@@ -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"
9 changes: 9 additions & 0 deletions src/gcloud-cli-persistence/oncreate.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/github-cli-persistence/NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
6 changes: 4 additions & 2 deletions src/github-cli-persistence/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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": {},
Expand All @@ -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"
}
}
Loading

0 comments on commit af55bf6

Please sign in to comment.