Skip to content

Commit af55bf6

Browse files
authored
Merge pull request #56 from joshuanianji/oncreate-scripts
Use `oncreate.sh` scripts
2 parents 12e044d + f55826a commit af55bf6

File tree

20 files changed

+325
-249
lines changed

20 files changed

+325
-249
lines changed

src/aws-cli-persistence/NOTES.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ Shells: `bash`, `zsh`, `fish`
88

99
## Changelog
1010

11-
| Version | Notes |
12-
| ------- | ------------------------ |
13-
| 1.0.2 | Update soft dependencies |
14-
| 1.0.1 | Fix Docs |
15-
| 1.0.0 | Support zsh + refactor |
16-
| 0.0.0 | Initial Version |
11+
| Version | Notes |
12+
| ------- | ----------------------------------------------- |
13+
| 1.0.3 | Move onCreate lifecycle script to `oncreate.sh` |
14+
| 1.0.2 | Update soft dependencies |
15+
| 1.0.1 | Fix Docs |
16+
| 1.0.0 | Support zsh + refactor |
17+
| 0.0.0 | Initial Version |
1718

1819
## References
1920

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "AWS CLI Persistence",
33
"id": "aws-cli-persistence",
4-
"version": "1.0.2",
4+
"version": "1.0.3",
55
"documentationURL": "https://github.com/joshuanianji/devcontainer-features/tree/main/src/aws-cli-persistence",
66
"description": "Avoid extra logins from the AWS CLI by preserving the `~/.aws` folder across container instances.",
77
"options": {},
@@ -17,5 +17,7 @@
1717
"ghcr.io/devcontainers/features/common-utils",
1818
"ghcr.io/meaningful-ooo/devcontainer-features/fish"
1919
],
20-
"postCreateCommand": "/usr/local/share/aws-cli-persistence-post-create.sh"
20+
"onCreateCommand": {
21+
"aws-cli-persistence-setup": "/usr/local/share/aws-cli-persistence/scripts/oncreate.sh"
22+
}
2123
}

src/aws-cli-persistence/install.sh

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,52 @@
11
#!/bin/sh
2-
set -e
32

3+
USERNAME=${USERNAME:-${_REMOTE_USER}}
44
FEATURE_ID="aws-cli-persistence"
5-
6-
echo "Activating feature '$FEATURE_ID'"
7-
echo "User: ${_REMOTE_USER} User home: ${_REMOTE_USER_HOME}"
8-
9-
if [ -z "$_REMOTE_USER" ] || [ -z "$_REMOTE_USER_HOME" ]; then
10-
echo "***********************************************************************************"
11-
echo "*** Require _REMOTE_USER and _REMOTE_USER_HOME to be set (by dev container CLI) ***"
12-
echo "***********************************************************************************"
13-
exit 1
14-
fi
15-
16-
# make /dc/aws-cli folder if doesn't exist
17-
mkdir -p "/dc/aws-cli"
18-
19-
# as to why we move around the folder, check `github-cli-persistence/install.sh`
20-
if [ -e "$_REMOTE_USER_HOME/.aws" ]; then
21-
echo "Moving existing .aws folder to .aws-old"
22-
mv "$_REMOTE_USER_HOME/.aws" "$_REMOTE_USER_HOME/.aws-old"
23-
fi
24-
25-
ln -s /dc/aws-cli "$_REMOTE_USER_HOME/.aws"
26-
# chown .aws folder
27-
chown -R "${_REMOTE_USER}:${_REMOTE_USER}" "$_REMOTE_USER_HOME/.aws"
28-
29-
# --- Generate a '$FEATURE_ID-post-create.sh' script to be executed by the 'postCreateCommand' lifecycle hook
30-
# Looks like this is the best way to run a script in lifecycle hooks
31-
# Source: https://github.com/devcontainers/features/blob/562305d37b97d47331d96306ffc2a0a3cce55e64/src/git-lfs/install.sh#L190C1-L190C109
32-
POST_CREATE_SCRIPT_PATH="/usr/local/share/$FEATURE_ID-post-create.sh"
33-
34-
tee "$POST_CREATE_SCRIPT_PATH" >/dev/null \
35-
<<'EOF'
36-
#!/bin/sh
5+
LIFECYCLE_SCRIPTS_DIR="/usr/local/share/${FEATURE_ID}/scripts"
376

387
set -e
398

40-
# if the user is not root, chown /dc/aws-cli to the user
41-
if [ "$(id -u)" != "0" ]; then
42-
echo "Running post-start.sh for user $USER"
43-
sudo chown -R "$USER:$USER" /dc/aws-cli
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/aws-cli" "${USERNAME}"
44+
create_symlink_dir "$_REMOTE_USER_HOME/.aws" "/dc/aws-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"
4450
fi
45-
EOF
4651

47-
chmod 755 "$POST_CREATE_SCRIPT_PATH"
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/aws-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/aws-cli
9+
fi

src/gcloud-cli-persistence/NOTES.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ Shells: `bash`, `zsh`, `fish`
88

99
## Changelog
1010

11-
| Version | Notes |
12-
| ------- | ------------------------ |
13-
| 1.0.2 | Update soft dependencies |
14-
| 1.0.1 | Fix Docs |
15-
| 1.0.0 | Support zsh + refactor |
16-
| 0.0.0 | Initial Version |
11+
| Version | Notes |
12+
| ------- | ----------------------------------------------- |
13+
| 1.0.3 | Move onCreate lifecycle script to `oncreate.sh` |
14+
| 1.0.2 | Update soft dependencies |
15+
| 1.0.1 | Fix Docs |
16+
| 1.0.0 | Support zsh + refactor |
17+
| 0.0.0 | Initial Version |

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Google Cloud CLI Persistence",
33
"id": "gcloud-cli-persistence",
4-
"version": "1.0.2",
4+
"version": "1.0.3",
55
"documentationURL": "https://github.com/joshuanianji/devcontainer-features/tree/main/src/gcloud-cli-persistence",
66
"description": "Avoid extra logins from the Google Cloud CLI by preserving the `~/.config/gcloud` folder across container instances.",
77
"options": {},
@@ -17,5 +17,7 @@
1717
"ghcr.io/devcontainers/features/common-utils",
1818
"ghcr.io/meaningful-ooo/devcontainer-features/fish"
1919
],
20-
"postCreateCommand": "/usr/local/share/gcloud-cli-persistence-post-create.sh"
20+
"onCreateCommand": {
21+
"gcloud-cli-persistence-setup": "/usr/local/share/gcloud-cli-persistence/scripts/oncreate.sh"
22+
}
2123
}
Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
#!/bin/sh
22
set -e
33

4+
USERNAME=${USERNAME:-${_REMOTE_USER}}
45
FEATURE_ID="gcloud-cli-persistence"
5-
6-
echo "Activating feature '$FEATURE_ID'"
7-
echo "User: ${_REMOTE_USER} User home: ${_REMOTE_USER_HOME}"
8-
9-
if [ -z "$_REMOTE_USER" ] || [ -z "$_REMOTE_USER_HOME" ]; then
10-
echo "***********************************************************************************"
11-
echo "*** Require _REMOTE_USER and _REMOTE_USER_HOME to be set (by dev container CLI) ***"
12-
echo "***********************************************************************************"
13-
exit 1
14-
fi
15-
16-
# make ~/.config folder if doesn't exist
17-
mkdir -p "$_REMOTE_USER_HOME/.config"
18-
mkdir -p "/dc/gcloud-cli"
19-
20-
# if `.config/gcloud` already exists, the `ln -s` command will create an extra
21-
# folder *inside* `.config/gcloud` that symlinks to `gcloud-cli`
22-
# Thus, we want to make sure the folder does NOT exist so the symlink will actually be to ~/.config/gcloud
23-
if [ -e "$_REMOTE_USER_HOME/.config/gcloud" ]; then
24-
echo "Moving existing gcloud folder to gcloud-old"
25-
mv "$_REMOTE_USER_HOME/.config/gcloud" "$_REMOTE_USER_HOME/.config/gcloud-old"
26-
fi
27-
28-
ln -s /dc/gcloud-cli "$_REMOTE_USER_HOME/.config/gcloud"
29-
# chown the entire `.config` folder because devcontainers creates
30-
# a `~/.config/vscode-dev-containers` folder later on
31-
chown -R "${_REMOTE_USER}:${_REMOTE_USER}" "$_REMOTE_USER_HOME/.config"
32-
33-
# --- Generate a '$FEATURE_ID-post-create.sh' script to be executed by the 'postCreateCommand' lifecycle hook
34-
# Looks like this is the best way to run a script in lifecycle hooks
35-
# Source: https://github.com/devcontainers/features/blob/562305d37b97d47331d96306ffc2a0a3cce55e64/src/git-lfs/install.sh#L190C1-L190C109
36-
POST_CREATE_SCRIPT_PATH="/usr/local/share/$FEATURE_ID-post-create.sh"
37-
38-
tee "$POST_CREATE_SCRIPT_PATH" >/dev/null \
39-
<<'EOF'
40-
#!/bin/sh
41-
42-
set -e
43-
44-
# if the user is not root, chown /dc/aws-cli to the user
45-
if [ "$(id -u)" != "0" ]; then
46-
echo "Running post-start.sh for user $USER"
47-
sudo chown -R "$USER:$USER" /dc/gcloud-cli
6+
LIFECYCLE_SCRIPTS_DIR="/usr/local/share/${FEATURE_ID}/scripts"
7+
8+
create_cache_dir() {
9+
if [ -d "$1" ]; then
10+
echo "Cache directory $1 already exists. Skip creation..."
11+
else
12+
echo "Create cache directory $1..."
13+
mkdir -p "$1"
14+
fi
15+
16+
if [ -z "$2" ]; then
17+
echo "No username provided. Skip chown..."
18+
else
19+
echo "Change owner of $1 to $2..."
20+
chown -R "$2:$2" "$1"
21+
fi
22+
}
23+
24+
create_symlink_dir() {
25+
local local_dir=$1
26+
local cache_dir=$2
27+
local username=$3
28+
29+
runuser -u "$username" -- mkdir -p "$(dirname "$local_dir")"
30+
runuser -u "$username" -- mkdir -p "$cache_dir"
31+
32+
# if the folder we want to symlink already exists, the ln -s command will create a folder inside the existing folder
33+
if [ -e "$local_dir" ]; then
34+
echo "Moving existing $local_dir folder to $local_dir-old"
35+
mv "$local_dir" "$local_dir-old"
36+
fi
37+
38+
echo "Symlink $local_dir to $cache_dir for $username..."
39+
runuser -u "$username" -- ln -s "$cache_dir" "$local_dir"
40+
}
41+
42+
create_cache_dir "/dc/gcloud-cli" "${USERNAME}"
43+
create_symlink_dir "$_REMOTE_USER_HOME/.config/gcloud" "/dc/gcloud-cli" "${USERNAME}"
44+
45+
# Set Lifecycle scripts
46+
if [ -f oncreate.sh ]; then
47+
mkdir -p "${LIFECYCLE_SCRIPTS_DIR}"
48+
cp oncreate.sh "${LIFECYCLE_SCRIPTS_DIR}/oncreate.sh"
4849
fi
49-
EOF
5050

51-
chmod 755 "$POST_CREATE_SCRIPT_PATH"
51+
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/aws-cli to the user
6+
if [ "$(id -u)" != "0" ]; then
7+
echo "Running post-start.sh for user $USER"
8+
sudo chown -R "$USER:$USER" /dc/gcloud-cli
9+
fi

src/github-cli-persistence/NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Shells: `bash`, `zsh`, `fish`
1010

1111
| Version | Notes |
1212
| ------- | ---------------------------------------------------- |
13+
| 1.0.3 | Move onCreate lifecycle script to `oncreate.sh` |
1314
| 1.0.2 | Update soft dependencies |
1415
| 1.0.1 | Fix Docs |
1516
| 1.0.0 | Support zsh + refactor |

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Github CLI Persistence",
33
"id": "github-cli-persistence",
4-
"version": "1.0.2",
4+
"version": "1.0.3",
55
"documentationURL": "https://github.com/joshuanianji/devcontainer-features/tree/main/src/github-cli-persistence",
66
"description": "Avoid extra logins from the Github CLI by preserving the `~/.config/gh` folder across container instances.",
77
"options": {},
@@ -17,5 +17,7 @@
1717
"ghcr.io/devcontainers/features/common-utils",
1818
"ghcr.io/meaningful-ooo/devcontainer-features/fish"
1919
],
20-
"postCreateCommand": "/usr/local/share/github-cli-persistence-post-create.sh"
20+
"onCreateCommand": {
21+
"github-cli-persistence-setup": "/usr/local/share/github-cli-persistence/scripts/oncreate.sh"
22+
}
2123
}

0 commit comments

Comments
 (0)