-
Notifications
You must be signed in to change notification settings - Fork 4
/
entrypoint.sh
executable file
·76 lines (56 loc) · 2.41 KB
/
entrypoint.sh
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
#!/usr/bin/env bash
set -e
[ "${COMMAND_NOT_FOUND_AUTOINSTALL}" = 'n' ] && unset COMMAND_NOT_FOUND_AUTOINSTALL || export COMMAND_NOT_FOUND_AUTOINSTALL=y
umask 0022
DEFAULT_BUILD_USER='builduser'
setToDefaults() {
BUILD_USER="$DEFAULT_BUILD_USER" && BUILD_USER_HOME='/workspace'
}
autoInstallPrerequisites() {
TOOL=tools/prerequisites
[ -x "$TOOL" ] || return
grep -qE 'Usage:.*(check.*install|install.*check)' "$TOOL" || return
"$TOOL" check || "$TOOL" install -y
}
# for backwards compatibility
if [ -z "$BUILD_USER" ] && [ -z "$BUILD_USER_HOME" ] && [ -z "$BUILD_USER_UID" ]; then
setToDefaults
[ -z "$USE_UID_FROM" ] && USE_UID_FROM="$BUILD_USER_HOME"
[ "$PWD" == "/" ] && cd "$BUILD_USER_HOME"
fi
# ignore PARAMS BUILD_USER and BUILD_USER_HOME (use defaults) if not root
[ `id -u` -eq 0 ] || setToDefaults
[ -z "$BUILD_USER" ] && BUILD_USER="$DEFAULT_BUILD_USER"
[ -n "$USE_UID_FROM" ] && BUILD_USER_UID=`stat -c "%u" $USE_UID_FROM`
if [ `id -u` -eq 0 ]; then
# better read HOME/DHOME from /etc/default/useradd /etc/adduser.conf
[ -z "$BUILD_USER_HOME" ] && BUILD_USER_HOME=/home/$BUILD_USER
USERADD="useradd -G sudo -s /bin/bash -d $BUILD_USER_HOME"
[ -d "$BUILD_USER_HOME" ] && USERADD="$USERADD -M" || USERADD="$USERADD -m"
if [ -n "$BUILD_USER_UID" ]; then
USERADD="$USERADD -u $BUILD_USER_UID"
# delete a user if there is already a user with that UID
TMP_DEL_USER=`getent passwd $BUILD_USER_UID | cut -d':' -f1` && [ -n "$TMP_DEL_USER" ] && [ "$DEFAULT_BUILD_USER" != "$TMP_DEL_USER" ] && userdel $TMP_DEL_USER >/dev/null 2>/dev/null
fi
[ -n "$BUILD_USER_GID" ] && USERADD="$USERADD -g $BUILD_USER_GID" && (getent group "$BUILD_USER_GID" || groupadd "$BUILD_USER_GID" "$BUILD_USER")
USERADD="$USERADD $BUILD_USER"
# remove the default builduser created in Dockerfile that exists in image
userdel "$DEFAULT_BUILD_USER"
eval "$USERADD"
fi
# if there are missing prerequisites we try to install them via tools/prerequisites
if [ "${AUTOINSTALL_PREREQUISITES}" != 'n' ]; then
export -f autoInstallPrerequisites
if [ `id -u` -eq 0 ]; then
su "$BUILD_USER" -c autoInstallPrerequisites || true
else
autoInstallPrerequisites || true
fi
unset autoInstallPrerequisites
fi
DEFAULT_SHELL=`getent passwd $BUILD_USER | cut -f 7 -d':'`
if [ `id -u` -eq 0 ]; then
[ "$#" -gt 0 ] && exec gosu "$BUILD_USER" "$@" || exec gosu "$BUILD_USER" "$DEFAULT_SHELL"
else
[ "$#" -gt 0 ] && exec "$@" || exec "$DEFAULT_SHELL"
fi