diff --git a/src/edgedb-cli/NOTES.md b/src/edgedb-cli/NOTES.md index a746cd3..d13ec47 100644 --- a/src/edgedb-cli/NOTES.md +++ b/src/edgedb-cli/NOTES.md @@ -8,6 +8,7 @@ Shells: `bash`, `zsh`, `fish` ## Changelog -| Version | Notes | -| ------- | --------------- | -| 1.0.0 | Initial Version | +| Version | Notes | +| ------- | ---------------------- | +| 1.0.1 | Fix for non-root users | +| 1.0.0 | Initial Version | diff --git a/src/edgedb-cli/devcontainer-feature.json b/src/edgedb-cli/devcontainer-feature.json index 2d0b052..b734317 100644 --- a/src/edgedb-cli/devcontainer-feature.json +++ b/src/edgedb-cli/devcontainer-feature.json @@ -1,7 +1,7 @@ { "name": "EdgeDB", "id": "edgedb-cli", - "version": "1.0.0", + "version": "1.0.1", "documentationURL": "https://github.com/joshuanianji/devcontainer-features/tree/main/src/edgedb-cli", "description": "EdgeDB CLI via the official installation script. Includes the VSCode extension and mounts ~/.local/share/edgedb for data persistence.", "options": {}, diff --git a/src/edgedb-cli/install.sh b/src/edgedb-cli/install.sh index 0e0217d..864178d 100644 --- a/src/edgedb-cli/install.sh +++ b/src/edgedb-cli/install.sh @@ -6,6 +6,17 @@ LIFECYCLE_SCRIPTS_DIR="/usr/local/share/edgedb-cli/scripts" set -e +# Checks if packages are installed and installs them if not +check_packages() { + if ! dpkg -s "$@" >/dev/null 2>&1; then + if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then + echo "Running apt-get update..." + apt-get update -y + fi + apt-get -y install --no-install-recommends "$@" + fi +} + create_cache_dir() { if [ -d "$1" ]; then echo "Cache directory $1 already exists. Skip creation..." @@ -23,36 +34,48 @@ create_cache_dir() { } create_symlink_dir() { - # ln -s target_dir source_dir - local source_dir=$1 - local target_dir=$2 + # local dir is the folder edgedb will use + # cache_dir is the /dc/edgedb-cli folder + local local_dir=$1 + local cache_dir=$2 local username=$3 - if [ -d "$source_dir" ]; then - echo "Symlink $source_dir to $target_dir..." - ln -s "$source_dir" "$target_dir" - else - echo "Creating source dir $source_dir..." - mkdir -p "$source_dir" - fi + 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 "$source_dir" ]; then - echo "Moving existing $source_dir folder to $source_dir-old" - mv "$source_dir" "$source_dir-old" + if [ -e "$local_dir" ]; then + echo "Moving existing $local_dir folder to $local_dir-old" + mv "$local_dir" "$local_dir-old" fi - echo "Symlink $source_dir to $target_dir..." - ln -s "$target_dir" "$source_dir" + echo "Symlink $local_dir to $cache_dir for $username..." + runuser -u "$username" -- ln -s "$cache_dir" "$local_dir" +} - echo "Change owner of $source_dir to $username..." - chown -R "$username:$username" "$source_dir" +install_edgedb() { + local username=$1 + + echo "Installing EdgeDB CLI..." + curl https://sh.edgedb.com --proto '=https' -sSf1 -o /tmp/edgedb-cli.sh + chmod +x /tmp/edgedb-cli.sh + + # install edgedb for a specific user if possible + echo "Installing EdgeDB CLI for $username..." + if [ -z "$username" ]; then + /tmp/edgedb-cli.sh -y + else + runuser -u "$username" -- /tmp/edgedb-cli.sh -y + fi } export DEBIAN_FRONTEND=noninteractive +check_packages curl ca-certificates + create_cache_dir "/dc/edgedb-cli" "${USERNAME}" create_symlink_dir "$_REMOTE_USER_HOME/.local/share/edgedb" "/dc/edgedb-cli" "${USERNAME}" +install_edgedb "${USERNAME}" # Set Lifecycle scripts if [ -f oncreate.sh ]; then diff --git a/test/edgedb-cli/_default.sh b/test/edgedb-cli/_default.sh new file mode 100644 index 0000000..9a4af96 --- /dev/null +++ b/test/edgedb-cli/_default.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +set -e + +# This is the default test script that tests everything +# It is not run as a scenario, but is run by other test scripts. + +# Optional: Import test library +source dev-container-features-test-lib + +# check that the command is available +check "help" bash -c "edgedb --help | grep 'Usage'" + +# check that `.config/gh` and `/dc/github-cli` exist under the user (should be node) +check "~/.local/share/edgedb exists" bash -c "ls -la ~/.local/share | grep 'edgedb'" +check "/dc/edgedb-cli exists" bash -c "ls -la /dc | grep 'edgedb-cli'" + +# check that the folders are owned by the user +# https://askubuntu.com/a/175060 +echo "Checking ownership of ~/.local/share/edgedb and /dc/edgedb-cli (ensure it is owned by $USER)" +ls -al ~/.local/share/ + +check "~/.local/share/edgedb owned by user" bash -c "test \"$(stat -c "%U" ~/.local/share/edgedb)\" = $USER" +check "/dc/edgedb-cli owned by user" bash -c "test \"$(stat -c "%U" /dc/edgedb-cli)\" = $USER" + +# Report result +reportResults diff --git a/test/edgedb-cli/scenarios.json b/test/edgedb-cli/scenarios.json index 9e26dfe..76507d4 100644 --- a/test/edgedb-cli/scenarios.json +++ b/test/edgedb-cli/scenarios.json @@ -1 +1,8 @@ -{} \ No newline at end of file +{ + "with_node": { + "image": "mcr.microsoft.com/devcontainers/javascript-node:1-18", + "features": { + "edgedb-cli": {} + } + } +} \ No newline at end of file diff --git a/test/edgedb-cli/with_node.sh b/test/edgedb-cli/with_node.sh new file mode 100644 index 0000000..1fe2fa4 --- /dev/null +++ b/test/edgedb-cli/with_node.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +set -e + +# Run default test script (in same folder) +# See: https://github.com/devcontainers/features/blob/562305d37b97d47331d96306ffc2a0a3cce55e64/test/azure-cli/install_extensions_bookworm.sh +./_default.sh