Skip to content

Commit

Permalink
Upload gcloud-cli feature (with tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuanianji committed Jan 8, 2024
1 parent 34fc94b commit 7dfd7e7
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/gcloud-cli-persistence/NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## OS and Architecture Support

Architectures: `amd` and `arm`.
OS: `ubuntu`, `debian`

## Changelog

| Version | Notes |
| ------- | --------------- |
| 0.0.0 | Initial Version |
18 changes: 18 additions & 0 deletions src/gcloud-cli-persistence/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "Google Cloud CLI Persistence",
"id": "gcloud-cli-persistence",
"version": "0.0.0",
"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": {},
"mounts": [
{
"source": "${devcontainerId}-gcloud-cli",
"target": "/dc/gcloud-cli",
"type": "volume"
}
],
"installsAfter": [
"ghcr.io/dhoeric/features/google-cloud-cli"
]
}
35 changes: 35 additions & 0 deletions src/gcloud-cli-persistence/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh
set -e

echo "Activating feature 'gcloud-cli-persistence'"
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"

# chown mount (only attached on startup)
cat <<EOF >>"$_REMOTE_USER_HOME/.bashrc"
sudo chown -R "${_REMOTE_USER}:${_REMOTE_USER}" /dc/gcloud-cli
EOF
chown -R $_REMOTE_USER $_REMOTE_USER_HOME/.bashrc
9 changes: 9 additions & 0 deletions test/gcloud-cli-persistence/scenarios.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"with_node": {
"image": "mcr.microsoft.com/devcontainers/javascript-node:1-18",
"features": {
"github-cli-persistence": {},
"ghcr.io/devcontainers/features/github-cli": {}
}
}
}
26 changes: 26 additions & 0 deletions test/gcloud-cli-persistence/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# NOTE: this is an "auto-generated" test, which means it will be
# executed against an auto-generated devcontainer.json that
# includes the 'gcloud-cli-persistence' Feature with no options.
#
# https://github.com/devcontainers/cli/blob/main/docs/features/test.md
#
# From my tests, this means the `gcloud` CLI will not be installed:
# Thus, here, I only check basic directory existence

# check that `~/.config/gcloud` and `/dc/gcloud-cli` exist`
check "config" bash -c "ls -la ~/.config | grep 'gcloud'"
check "dc" bash -c "ls -la /dc | grep 'gcloud-cli'"

# check that `~/.config/gcloud` is a symlink
# https://unix.stackexchange.com/a/96910
check "~/.config/gcloud is a symlink" bash -c "test -L ~/.config/gcloud && test -d ~/.config/gcloud"

# Report result
reportResults
22 changes: 22 additions & 0 deletions test/gcloud-cli-persistence/with_node.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# check that `gcloud --help` works
check "help" bash -c "gcloud --help | grep 'NAME'"

# check that `.config/gcloud` and `/dc/gcloud-cli` exist under the user (should be node)
check "config" bash -c "ls -la ~/.config | grep 'gloud'"
check "dc" bash -c "ls -la /dc | grep 'gcloud-cli'"

# check that the folders are owned by the user
# `stat -c "%U %G" ~/.config` returns "$USER $GROUP", in this case "node node"
# https://askubuntu.com/a/175060
check "~/.config/gloud owned by user" bash -c "test \"$(stat -c "%U %G" ~/.config)\" = 'node node'"
check "/dc/gcloud-cli owned by user" bash -c "test \"$(stat -c "%U %G" /dc/gcloud-cli)\" = 'node node'"

# Report result
reportResults

0 comments on commit 7dfd7e7

Please sign in to comment.