Skip to content

Commit 32a164b

Browse files
install ruby if not available
1 parent b4a5d11 commit 32a164b

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

src/gem/install.sh

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,84 @@
11
#!/usr/bin/env bash
22

3-
set -eu
4-
53
: ${GEM:=}
64
gems=${GEM//,/ }
75

8-
if [ -z "${gems}" ]; then
9-
echo "No RubyGems specified. Skip installation..."
10-
exit 0
11-
fi
6+
USERNAME=${USERNAME:-${_REMOTE_USER:-"automatic"}}
7+
8+
set -e
9+
10+
export TMPDIR=$(mktemp -d /tmp/feature.XXXXXX)
11+
trap "rm -rf $TMPDIR" EXIT
12+
13+
# Clean up
14+
rm -rf /var/lib/apt/lists/*
1215

1316
if [ "$(id -u)" -ne 0 ]; then
1417
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
1518
exit 1
1619
fi
1720

21+
architecture="$(dpkg --print-architecture)"
22+
if [ "${architecture}" != "amd64" ] && [ "${architecture}" != "arm64" ]; then
23+
echo "(!) Architecture $architecture unsupported"
24+
exit 1
25+
fi
26+
27+
# Determine the appropriate non-root user
28+
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
29+
USERNAME=""
30+
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
31+
for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
32+
if id -u "${CURRENT_USER}" >/dev/null 2>&1; then
33+
USERNAME=${CURRENT_USER}
34+
break
35+
fi
36+
done
37+
if [ "${USERNAME}" = "" ]; then
38+
USERNAME=root
39+
fi
40+
elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} >/dev/null 2>&1; then
41+
USERNAME=root
42+
fi
43+
44+
apt_get_update() {
45+
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
46+
echo "Running apt-get update..."
47+
apt-get update -y
48+
fi
49+
}
50+
51+
# Checks if packages are installed and installs them if not
52+
check_packages() {
53+
if ! dpkg -s "$@" >/dev/null 2>&1; then
54+
apt_get_update
55+
apt-get -y install --no-install-recommends "$@"
56+
fi
57+
}
58+
1859
export DEBIAN_FRONTEND=noninteractive
1960

61+
### BEGIN install
62+
63+
if [ -z "${gems}" ]; then
64+
echo "No RubyGems specified. Skip installation..."
65+
exit 0
66+
fi
67+
68+
check_packages ruby-full ca-certificates
69+
echo "Installing gems..."
70+
2071
args=("$gems")
2172
[ -n "${VERSION:-}" ] && args+=(--version "${VERSION}")
2273
[ -n "${PRERELEASE:-}" ] && args+=(--prerelease)
2374

24-
echo "Installing gems..."
2575
gem install "${args[@]}"
2676

2777
echo "Done!"
78+
79+
### END install
80+
81+
# Clean up
82+
rm -rf /var/lib/apt/lists/*
83+
84+
echo "Done!"

test/gem/test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
set -e
44

5+
# This test can be run with the following command (from the root of this repo)
6+
# devcontainer features test --features gem --base-image mcr.microsoft.com/devcontainers/base:ubuntu .
7+
58
# Import 'check' command
69
source dev-container-features-test-lib
710

0 commit comments

Comments
 (0)