|
1 | 1 | #!/usr/bin/env bash |
| 2 | +# This is part of devcontainers-extra script library |
| 3 | +# source: https://github.com/devcontainers-extra/features |
2 | 4 |
|
3 | | -set -e |
| 5 | +set -ex |
4 | 6 |
|
5 | | -source ./library_scripts.sh |
| 7 | +VERSION=${VERSION:-"latest"} |
6 | 8 |
|
7 | | -# nanolayer is a cli utility which keeps container layers as small as possible |
8 | | -# source code: https://github.com/devcontainers-extra/nanolayer |
9 | | -# `ensure_nanolayer` is a bash function that will find any existing nanolayer installations, |
10 | | -# and if missing - will download a temporary copy that automatically get deleted at the end |
11 | | -# of the script |
12 | | -ensure_nanolayer nanolayer_location "v0.4.45" |
| 9 | +# Clean up |
| 10 | +rm -rf /var/lib/apt/lists/* |
13 | 11 |
|
| 12 | +if [ "$(id -u)" -ne 0 ]; then |
| 13 | + echo -e 'Script must be run as |
| 14 | + root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' |
| 15 | + exit 1 |
| 16 | +fi |
14 | 17 |
|
15 | | -$nanolayer_location \ |
16 | | - install \ |
17 | | - devcontainer-feature \ |
18 | | - "ghcr.io/devcontainers-extra/features/asdf-package:1.0.8" \ |
19 | | - --option plugin='elixir' --option version="$VERSION" |
| 18 | +check_alpine_packages() { |
| 19 | + apk add -v --no-cache "$@" |
| 20 | +} |
20 | 21 |
|
| 22 | +check_packages() { |
| 23 | + if ! dpkg -s "$@" >/dev/null 2>&1; then |
| 24 | + if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then |
| 25 | + echo "Running apt-get update..." |
| 26 | + apt-get update -y |
| 27 | + fi |
| 28 | + apt-get -y install --no-install-recommends "$@" |
| 29 | + fi |
| 30 | +} |
21 | 31 |
|
| 32 | +updaterc() { |
| 33 | + if cat /etc/os-release | grep "ID_LIKE=.*alpine.*\|ID=.*alpine.*" ; then |
| 34 | + echo "Updating /etc/profile" |
| 35 | + echo -e "$1" >>/etc/profile |
| 36 | + fi |
| 37 | + if [[ "$(cat /etc/bash.bashrc)" != *"$1"* ]]; then |
| 38 | + echo "Updating /etc/bash.bashrc" |
| 39 | + echo -e "$1" >>/etc/bash.bashrc |
| 40 | + fi |
| 41 | + if [ -f "/etc/zsh/zshrc" ] && [[ "$(cat /etc/zsh/zshrc)" != *"$1"* ]]; then |
| 42 | + echo "Updating /etc/zsh/zshrc" |
| 43 | + echo -e "$1" >>/etc/zsh/zshrc |
| 44 | + fi |
| 45 | +} |
22 | 46 |
|
23 | | -echo 'Done!' |
| 47 | +install_via_asdf() { |
| 48 | + PLUGIN=$1 |
| 49 | + VERSION=$2 |
| 50 | + REPO=$3 |
24 | 51 |
|
| 52 | + # install git and curl if does not exists |
| 53 | + if cat /etc/os-release | grep "ID_LIKE=.*alpine.*\|ID=.*alpine.*" ; then |
| 54 | + check_alpine_packages curl git ca-certificates |
| 55 | + elif cat /etc/os-release | grep "ID_LIKE=.*debian.*\|ID=.*debian.*"; then |
| 56 | + check_packages curl git ca-certificates |
| 57 | + fi |
| 58 | + |
| 59 | + |
| 60 | + # asdf may be installed somewhere on the machine, but we need it to be accessible to the remote user |
| 61 | + # the code bellow will return 2 only when asdf is available, and 1 otherwise |
| 62 | + set +e |
| 63 | + su "$_REMOTE_USER" <<EOF |
| 64 | + export HOME="$_REMOTE_USER_HOME" |
| 65 | + export PATH="\$HOME/.asdf/bin:\$HOME/.asdf/shims:\$PATH" |
| 66 | +
|
| 67 | + if type asdf >/dev/null 2>&1; then |
| 68 | + exit 2 |
| 69 | + fi |
| 70 | + exit 1 |
| 71 | +EOF |
| 72 | + exit_code=$? |
| 73 | + set -e |
| 74 | + |
| 75 | + if [ "${exit_code}" -eq 2 ]; then |
| 76 | + # asdf already available to remote user, use it |
| 77 | + su "$_REMOTE_USER" <<EOF |
| 78 | + export HOME="$_REMOTE_USER_HOME" |
| 79 | + . "\$HOME/.asdf/asdf.sh" |
| 80 | +
|
| 81 | + if asdf list "$PLUGIN" >/dev/null 2>&1; then |
| 82 | + echo "$PLUGIN already exists - skipping adding it" |
| 83 | + else |
| 84 | + asdf plugin add "$PLUGIN" "$REPO" |
| 85 | + fi |
| 86 | +
|
| 87 | + if [ "${VERSION}" = "latest" ] ; then |
| 88 | + resolved_version=\$(asdf latest "$PLUGIN" "$LATESTVERSIONPATTERN") |
| 89 | + else |
| 90 | + resolved_version=$VERSION |
| 91 | + fi |
| 92 | +
|
| 93 | + asdf install "$PLUGIN" "\$resolved_version" |
| 94 | + asdf global "$PLUGIN" "\$resolved_version" |
| 95 | +EOF |
| 96 | + else |
| 97 | + # asdf is not available to remote user, install it, then update rc files |
| 98 | + |
| 99 | + su "$_REMOTE_USER" <<EOF |
| 100 | + export HOME="$_REMOTE_USER_HOME" |
| 101 | +
|
| 102 | + git clone --depth=1 \ |
| 103 | + -c core.eol=lf \ |
| 104 | + -c core.autocrlf=false \ |
| 105 | + -c fsck.zeroPaddedFilemode=ignore \ |
| 106 | + -c fetch.fsck.zeroPaddedFilemode=ignore \ |
| 107 | + -c receive.fsck.zeroPaddedFilemode=ignore \ |
| 108 | + "https://github.com/asdf-vm/asdf.git" --branch v0.12.0 "\$HOME/.asdf" 2>&1 |
| 109 | +
|
| 110 | + . "\$HOME/.asdf/asdf.sh" |
| 111 | + if asdf list "$PLUGIN" >/dev/null 2>&1; then |
| 112 | + echo "$PLUGIN already exists - skipping adding it" |
| 113 | + else |
| 114 | + asdf plugin add "$PLUGIN" "$REPO" |
| 115 | + fi |
| 116 | +EOF |
| 117 | + |
| 118 | + |
| 119 | + # I resolve the version like this because in bash resolving |
| 120 | + # a subshell take prevedent to su, so we must resolve variables |
| 121 | + # pre using them in final su clause. |
| 122 | + # I hate bash. |
| 123 | + resolved_version=$(su "$_REMOTE_USER" <<EOF |
| 124 | + export HOME="$_REMOTE_USER_HOME" |
| 125 | + . "\$HOME/.asdf/asdf.sh" > /dev/null 2>&1 |
| 126 | +
|
| 127 | + if [ "${VERSION}" = "latest" ] ; then |
| 128 | + asdf latest "$PLUGIN" "$LATESTVERSIONPATTERN" |
| 129 | + else |
| 130 | + echo $VERSION |
| 131 | + fi |
| 132 | +EOF |
| 133 | +) |
| 134 | + su "$_REMOTE_USER" <<EOF |
| 135 | + export HOME="$_REMOTE_USER_HOME" |
| 136 | + . "\$HOME/.asdf/asdf.sh" |
| 137 | + asdf install "$PLUGIN" "$resolved_version" |
| 138 | + asdf global "$PLUGIN" "$resolved_version" |
| 139 | +EOF |
| 140 | + updaterc ". $_REMOTE_USER_HOME/.asdf/asdf.sh" |
| 141 | + fi |
| 142 | +} |
| 143 | + |
| 144 | +install_via_asdf elixir "$VERSION" "https://github.com/asdf-vm/asdf-elixir.git" |
0 commit comments