-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
57 lines (48 loc) · 1.61 KB
/
entrypoint.sh
File metadata and controls
57 lines (48 loc) · 1.61 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
#!/usr/bin/env bash
set -euo pipefail
# Defaults
DEFAULT_USER="dev"
DEFAULT_UID=1000
DEFAULT_GID=1000
# 1) Decide the target UID/GID
uid="${HOST_UID:-}"
gid="${HOST_GID:-}"
if [[ -z "${uid}" || -z "${gid}" ]]; then
if [[ -d "/work" ]]; then
# Owner of the bind-mounted /work directory (Linux-only stat flags)
uid="$(stat -c '%u' /work || echo ${DEFAULT_UID})"
gid="$(stat -c '%g' /work || echo ${DEFAULT_GID})"
else
uid="${DEFAULT_UID}"
gid="${DEFAULT_GID}"
fi
fi
# 2) Ensure group exists
if ! getent group "${gid}" >/dev/null 2>&1; then
groupadd -g "${gid}" "${DEFAULT_USER}" 2>/dev/null || \
groupadd -g "${gid}" "grp${gid}"
fi
group_name="$(getent group "${gid}" | cut -d: -f1)"
# 3) Ensure user exists
if ! getent passwd "${uid}" >/dev/null 2>&1; then
useradd -m -u "${uid}" -g "${group_name}" -s /bin/bash "${DEFAULT_USER}" 2>/dev/null || \
useradd -m -u "${uid}" -g "${gid}" -s /bin/bash "user${uid}"
fi
user_name="$(getent passwd "${uid}" | cut -d: -f1)"
home_dir="$(getent passwd "${uid}" | cut -d: -f6)"
# 4) Ensure writable HOME and /work
mkdir -p "${home_dir}" /work
chown -R "${uid}:${gid}" "${home_dir}" /work || true
# 5) Helpful env defaults
export HOME="${home_dir}"
export CMAKE_PREFIX_PATH="/opt/llvm-mlir:${CMAKE_PREFIX_PATH:-}"
export LD_LIBRARY_PATH="/opt/llvm-mlir/lib:${LD_LIBRARY_PATH:-}"
export PATH="/opt/llvm-mlir/bin:${PATH:-/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin}"
export CC="${CC:-clang}"
export CXX="${CXX:-clang++}"
# 6) Default command
if [[ $# -eq 0 ]]; then
set -- bash
fi
# 7) Drop privileges to the detected user
exec gosu "${uid}:${gid}" "$@"