|
1 | 1 | #!/bin/sh |
2 | 2 | set -e |
3 | 3 |
|
| 4 | +USERNAME=${USERNAME:-${_REMOTE_USER}} |
4 | 5 | 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" |
48 | 49 | fi |
49 | | -EOF |
50 | 50 |
|
51 | | -chmod 755 "$POST_CREATE_SCRIPT_PATH" |
| 51 | +echo "Finished installing $FEATURE_ID" |
0 commit comments