|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -eo pipefail |
| 4 | + |
| 5 | +echo "[Info] AMPStart for Docker" |
| 6 | +ARCH=$(uname -m) |
| 7 | + |
| 8 | +# Context check |
| 9 | +[[ -z "${AMPUSERID}" ]] && { echo "[Error] This docker image cannot be used directly by itself - it must be started by ampinstmgr"; exit 100; } |
| 10 | + |
| 11 | +# Create /etc/machine-id (addresses Proton/dbus issues) |
| 12 | +mkdir -p /var/lib/dbus |
| 13 | +rm -f /etc/machine-id /var/lib/dbus/machine-id |
| 14 | +dbus-uuidgen --ensure=/etc/machine-id |
| 15 | +ln -s /etc/machine-id /var/lib/dbus/machine-id |
| 16 | + |
| 17 | +# Create /tmp/.X11-unix (for Xvfb etc) |
| 18 | +install -d -o root -g root -m 1777 /tmp/.X11-unix |
| 19 | + |
| 20 | +# Set up amp user and group |
| 21 | +: "${AMPUSERID:?AMPUSERID not set}" |
| 22 | +: "${AMPGROUPID:?AMPGROUPID not set}" |
| 23 | + |
| 24 | +echo "[Info] Setting up amp user and group..." |
| 25 | + |
| 26 | +if getent group amp >/dev/null; then |
| 27 | + groupmod -o -g "${AMPGROUPID}" amp |
| 28 | +else |
| 29 | + groupadd -r -o -g "${AMPGROUPID}" amp |
| 30 | +fi |
| 31 | + |
| 32 | +if id amp &>/dev/null; then |
| 33 | + usermod -o -u "${AMPUSERID}" -g amp amp |
| 34 | +else |
| 35 | + useradd -m -d /home/amp -s /bin/bash -c "AMP Process User" \ |
| 36 | + -o -u "${AMPUSERID}" -g amp amp |
| 37 | +fi |
| 38 | + |
| 39 | +getent group tty >/dev/null && usermod -aG tty amp |
| 40 | + |
| 41 | +install -d -m 0755 /home/amp |
| 42 | +touch /home/amp/.gitconfig |
| 43 | +chown -R amp:amp /home/amp |
| 44 | + |
| 45 | +# Make AMP binary executable |
| 46 | +AMP_BIN="/AMP/AMP_Linux_${ARCH}" |
| 47 | +chmod +x "${AMP_BIN}" || { echo "[Error] AMP binary not found or cannot be made executable"; exit 101; } |
| 48 | + |
| 49 | +# Install extra dependencies if needed (non-fatal) |
| 50 | +REQUIRED_DEPS=() |
| 51 | +if [[ -n "${AMP_CONTAINER_DEPS:-}" ]]; then |
| 52 | + # shellcheck disable=SC2207 |
| 53 | + REQUIRED_DEPS=($(jq -r '.[]? | select(type=="string" and length>0)' <<<"${AMP_CONTAINER_DEPS}" 2>/dev/null || echo)) |
| 54 | +fi |
| 55 | + |
| 56 | +if ((${#REQUIRED_DEPS[@]})); then |
| 57 | + echo "[Info] Installing extra dependencies..." |
| 58 | + ( |
| 59 | + set +e |
| 60 | + apt-get update || echo "[Warn] apt-get update failed; continuing" |
| 61 | + apt-get install -y --no-install-recommends --allow-downgrades \ |
| 62 | + -o APT::Keep-Downloaded-Packages="false" "${REQUIRED_DEPS[@]}" \ |
| 63 | + || echo "[Warn] apt-get install failed (bad package name?); continuing" |
| 64 | + apt-get clean >/dev/null 2>&1 || true |
| 65 | + rm -rf /var/lib/apt/lists/* || true |
| 66 | + ) |
| 67 | +fi |
| 68 | + |
| 69 | +# Set custom mountpoint permissions if needed (non-fatal) |
| 70 | +if [[ -n "${AMP_MOUNTPOINTS:-}" ]]; then |
| 71 | + echo "[Info] Updating custom mountpoint permissions..." |
| 72 | + IFS=':' read -r -a dirs <<< "${AMP_MOUNTPOINTS}" |
| 73 | + for dir in "${dirs[@]}"; do |
| 74 | + [[ -n "${dir}" ]] || continue |
| 75 | + if [[ -e "${dir}" ]]; then |
| 76 | + chown -R amp:amp "${dir}" 2>/dev/null || echo "[Warn] chown failed for ${dir}; continuing" |
| 77 | + else |
| 78 | + echo "[Warn] Mountpoint not found: ${dir}; skipping" |
| 79 | + fi |
| 80 | + done |
| 81 | +fi |
| 82 | + |
| 83 | +# Run custom start script if it exists (non-fatal) |
| 84 | +if [[ -f "/AMP/customstart.sh" ]]; then |
| 85 | + echo "[Info] Running customstart.sh..." |
| 86 | + chmod +x /AMP/customstart.sh 2>/dev/null || true |
| 87 | + ( set +e; /AMP/customstart.sh; rc=$?; ((rc==0)) || echo "[Warn] customstart.sh exited with $rc; continuing" ) |
| 88 | +fi |
| 89 | + |
| 90 | +# Set XDG_RUNTIME_DIR (stop Wine/Proton whining) |
| 91 | +XDG_RUNTIME_DIR="/run/user/${AMPUSERID}" |
| 92 | +install -d -m 0700 -o amp -g amp "${XDG_RUNTIME_DIR}" |
| 93 | + |
| 94 | +# Handoff |
| 95 | +echo "[Info] Starting AMP..." |
| 96 | +ARGS=$@ |
| 97 | +# Use specific PATH for Redis to include Rust cargo bin |
| 98 | +keep_env=( |
| 99 | + HOME=/home/amp |
| 100 | + USER=amp LOGNAME=amp SHELL=/bin/bash |
| 101 | + LANG="${LANG:-en_US.UTF-8}" LANGUAGE="${LANGUAGE:-en_US:en}" LC_ALL="${LC_ALL:-en_US.UTF-8}" |
| 102 | + PATH=/usr/local/cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games |
| 103 | + MAIL=/var/mail/amp |
| 104 | + XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR}" |
| 105 | +) |
| 106 | +# Always keep these AMP_ env vars if set |
| 107 | +for v in AMPHOSTPLATFORM AMP_CONTAINER AMP_CONTAINER_HOST_NETWORK AMPMEMORYLIMIT AMPSWAPLIMIT AMPCONTAINERCPUS; do |
| 108 | + if [[ -n "${!v-}" ]]; then keep_env+=("$v=${!v}"); fi |
| 109 | +done |
| 110 | +# Extra passthrough of env vars listed in AMP_ADDITIONAL_ENV_VARS in the Dockerfile |
| 111 | +if [[ -n "${AMP_ADDITIONAL_ENV_VARS-}" ]]; then |
| 112 | + for v in ${AMP_ADDITIONAL_ENV_VARS}; do |
| 113 | + if [[ -n "${!v-}" ]]; then keep_env+=("$v=${!v}"); fi |
| 114 | + done |
| 115 | +fi |
| 116 | + |
| 117 | +exec gosu amp:amp env -i "${keep_env[@]}" \ |
| 118 | + bash -c "cd /AMP && exec ${AMP_BIN} ${ARGS}" |
0 commit comments