Skip to content

Commit d3e2612

Browse files
committed
Add a feature to install asdf
This installs the asdf version manager, via a given release version (either a version tag or latest). It's then tested using a combination of the existing shell-based test framework, and the features test helper. Typically, the data directory (used by asdf for plugin data) would be relative to the user's home directory. We can't do this in a Dev Container feature because it's not possible to set the path with dynamic/run time elements in it. The Dev Container CLI tool does two things with values set in `containerEnv`: 1. Add as a Docker layer, 2. Add to `/etc/environment` The former is the problem because whilst we _could_ use a script to set the latter, we can't do this correctly as a layer and it wouldn't be available for plugins (which as mentioned later, need to be additional features because of other limitations). This is because the CLI doesn't support any environment variables (because it's called too early by default) and the user we need to know about depends on values we can only know at runtime. In addition to this `/etc/environment` will not be sourced anywhere when we need it (e.g.: during the plugin installation stage) so this is no good. There are open issues tracking these (including questioning why `/etc/environment` is used which is bad or in practice unused for non-bash shells which makes it generally unhelpful), and for handling environment variables in `containerEnv` but these don't help the current problem (and it would seem, are unlikely to be fixed any time soon). Instead, we install into `/opt/asdf` and override the default asdf data directory. This is set to be owned by the "remote user", which will default to the "container user" if not set. The devcontainer spec doesn't support any more complex data structures aside from booleans and strings, so the best way to handle plugins is through additional features, which come next. We also need to override the default base image when testing because the scripts depend on `curl` and there's no sensible way to depend on just `curl` (and the default `ubuntu:focal` image doesn't include it). This doesn't yet handle testing versions, which is something that in the future should be covered by "scenarios". https://github.com/asdf-vm/asdf https://asdf-vm.com/guide/getting-started.html devcontainers/spec#44 devcontainers/spec#57 devcontainers/spec#766 https://containers.dev/implementors/spec/#users https://containers.dev/implementors/features/#user-env-var https://github.com/devcontainers/spec/blob/main/docs/specs/features-user-env-variables.md devcontainers/spec#255 devcontainers/spec#251 devcontainers/spec#19 devcontainers/spec#220 devcontainers/cli#525 devcontainers/spec#91 devcontainers/cli#1231 Handle specific asdf versions (and latest)
1 parent 848a4d0 commit d3e2612

4 files changed

Lines changed: 155 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"id": "asdf",
3+
"version": "1.0.0",
4+
"name": "asdf",
5+
"documentationURL": "https://github.com/thoughtbot/devcontainers/tree/main/features/src/asdf",
6+
"description": "Installs the asdf version manager",
7+
"options": {
8+
"version": {
9+
"type": "string",
10+
"proposals": [
11+
"latest",
12+
"v0.20.0"
13+
],
14+
"default": "latest",
15+
"description": "asdf version to install"
16+
}
17+
},
18+
"containerEnv": {
19+
"ASDF_DATA_DIR": "/opt/asdf",
20+
"PATH": "/opt/asdf/shims:$PATH"
21+
}
22+
}

‎features/src/asdf/install.sh‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
gh_api() {
6+
path=$1
7+
authentication=
8+
9+
if [ -n "${GH_TOKEN+set}" ]; then
10+
authentication="-H \"Authorization: Bearer $GH_TOKEN\""
11+
fi
12+
13+
curl --silent "$authentication -H Accept: application/vnd.github+json" \
14+
-H "X-GitHub-Api-Version: 2026-03-10" \
15+
"https://api.github.com/$path"
16+
}
17+
18+
USERNAME="${USERNAME:-"${_REMOTE_USER:-"automatic"}"}"
19+
20+
case "$USERNAME" in
21+
auto*)
22+
USERNAME=
23+
uid_1000="$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)"
24+
possible_users="vscode node codespace $uid_1000"
25+
26+
while read -r user; do
27+
if id -u "$user" > /dev/null 2>&1; then
28+
USERNAME="$user"
29+
break
30+
fi
31+
done <<-EOT
32+
$(echo "$possible_users" | tr ' ' '\n')
33+
EOT
34+
35+
if [ "$USERNAME" = "" ]; then
36+
USERNAME=root
37+
fi
38+
;;
39+
"none")
40+
USERNAME="root"
41+
;;
42+
esac
43+
44+
if [ "$VERSION" = "latest" ]; then
45+
VERSION=$(gh_api repos/asdf-vm/asdf/releases/latest | jq -r ".name")
46+
fi
47+
48+
kernel_name=$(uname -s | tr '[:upper:]' '[:lower:]')
49+
architecture=$(dpkg --print-architecture)
50+
51+
if ! command -v asdf > /dev/null; then
52+
filename="asdf-$VERSION-$kernel_name-$architecture.tar.gz"
53+
54+
curl -Lo "$filename" "https://github.com/asdf-vm/asdf/releases/download/$VERSION/$filename"
55+
tar -xzvf "$filename" asdf
56+
sudo mv asdf /usr/local/bin/asdf
57+
58+
rm "$filename"
59+
fi
60+
61+
mkdir -p /opt/asdf
62+
chown "$USERNAME" /opt/asdf

‎features/test/asdf/test-utils.sh‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
3+
VERBOSE="${VERBOSE:-0}"
4+
LAST_COMMAND_OUTPUT=
5+
6+
# assert_command command [expected status code]
7+
#
8+
# Run a command and assert it's exit code is 0 or one given
9+
assert_command() {
10+
cmd="$1"
11+
expected_return_status="${2:-0}"
12+
13+
LAST_COMMAND_OUTPUT=
14+
15+
# Always capture stdout and stderr, and don't let a failing command
16+
# cause this function to fail
17+
if LAST_COMMAND_OUTPUT="$(printf '%s' "$cmd" | sh -a 2>&1)"; then
18+
status="$?" # ok
19+
else
20+
status="$?" # also ok
21+
fi
22+
23+
if [ ! "$VERBOSE" -eq 0 ]; then
24+
echo "Command: $cmd"
25+
echo "Command output: $LAST_COMMAND_OUTPUT"
26+
echo "Status code: $status"
27+
fi
28+
29+
if [ "$status" -ne "$expected_return_status" ]; then
30+
echo "$cmd exited with: $status"
31+
exit 1
32+
fi
33+
}
34+
35+
# assert_directory path
36+
#
37+
# Assert that a path exists and is a directory
38+
assert_directory() {
39+
directory=$1
40+
41+
assert_command "[ \$(stat -c '%F' $directory) = directory ]"
42+
}
43+
44+
# assert_owner path user
45+
#
46+
# Assert a path is owned by a given user
47+
assert_owner() {
48+
path=$1
49+
owner=$2
50+
51+
assert_command "[ \$(stat -c '%U' $path) = $owner ]"
52+
}

‎features/test/asdf/test.sh‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
cwd=$(readlink -f "${0%/*}")
6+
7+
. "$cwd/test-utils.sh"
8+
9+
echo "Checking asdf runs..."
10+
assert_command "asdf --version"
11+
12+
echo "Checking asdf data directory exists..."
13+
assert_directory /opt/asdf
14+
15+
echo "Checking asdf data directory is owned by the container user..."
16+
assert_owner /opt/asdf "$USER"
17+
18+
echo "Checking the current user can write to the asdf data directory..."
19+
touch /opt/asdf/tmp-file

0 commit comments

Comments
 (0)