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