Repository navigation
Expand file tree
/
Copy pathdx-orb-init
More file actions
executable file
·106 lines (100 loc) · 3.62 KB
/
Copy pathdx-orb-init
File metadata and controls
executable file
·106 lines (100 loc) · 3.62 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
#!/bin/sh
# dx Orb entrypoint: supervises dxd without systemd.
#
# Cloudflare Containers start it from the image ENTRYPOINT (under tini as
# PID 1); E2B starts it as the template's start command, so it is already
# running in every sandbox created from the template. Core's daemon installer
# (apps/core/src/execution/e2b/daemon-installer.ts) puts the dxd binary and its
# configuration in place and sends SIGUSR1; this loop then starts dxd as
# `user` and restarts it whenever it exits, as the systemd unit did. dxd
# updates itself in place (exec), so a self-update keeps its PID.
set -u
if [ "$(id -u)" = 0 ]; then
exec setpriv --reuid=user --regid=user --init-groups -- "$0" "$@"
fi
# Cloudflare Containers name the host after the 64-hex object ID; the
# provider passes a short, stable name (E2B passes none and keeps its own).
# Map it in /etc/hosts first so sudo still resolves its own host, then set
# it. Where the runtime refuses sethostname, record it for the login hook
# /etc/profile.d/dx-orb-hostname.sh, which shows it in the prompt instead.
if [ -n "${DX_ORB_HOSTNAME:-}" ] && [ "$(hostname)" != "$DX_ORB_HOSTNAME" ]; then
if ! grep -qw -- "$DX_ORB_HOSTNAME" /etc/hosts 2>/dev/null; then
printf '127.0.1.1\t%s\n' "$DX_ORB_HOSTNAME" | sudo -n tee -a /etc/hosts >/dev/null 2>&1 || :
fi
if sudo -n hostname "$DX_ORB_HOSTNAME" 2>/dev/null; then
sudo -n rm -f /etc/dx-orb-hostname 2>/dev/null || :
else
printf '%s\n' "$DX_ORB_HOSTNAME" | sudo -n tee /etc/dx-orb-hostname >/dev/null 2>&1 || :
fi
fi
state=/home/user/.local/state/dxd
binary="$state/bin/dxd"
config="$state/config.json"
log="$state/dxd.log"
umask 077
mkdir -p "$state/bin"
echo $$ >"$state/supervisor.pid"
child=
stop() {
if [ -n "$child" ]; then kill -TERM "$child" 2>/dev/null; fi
exit 0
}
# Cloudflare announces a planned stop with SIGTERM and kills the container
# up to 15 minutes later. The Orb container object intercepts
# DX_ORB_STOP_NOTICE (set only there) and answers once it has snapshotted
# the filesystem, so the workspace survives the stop; only then stop.
announce_stop() {
if [ -n "${DX_ORB_STOP_NOTICE:-}" ]; then
curl -fsS -m 600 -X POST -o /dev/null "$DX_ORB_STOP_NOTICE" 2>/dev/null || :
fi
stop
}
trap announce_stop TERM
trap stop INT
# SIGUSR1 only interrupts the current wait: the installer's "start now".
trap ':' USR1
# Waits up to $1 seconds; SIGUSR1 ends the wait early.
pause() {
sleep "$1" &
wait $! 2>/dev/null
kill $! 2>/dev/null
}
backoff=0
while :; do
# An installer from before this image (a rolled-back Core) runs dxd as a
# systemd unit; stand down while that unit exists so dxd never runs twice.
if [ -e /etc/systemd/system/dxd.service ]; then
pause 5
continue
fi
if [ ! -x "$binary" ] || [ ! -f "$config" ]; then
pause 1
continue
fi
if [ -f "$log" ] && [ "$(stat -c %s "$log")" -gt 1048576 ]; then
mv -f "$log" "$log.1"
fi
started=$(date +%s)
# The environment systemd gave the dxd unit, plus a UTF-8 locale.
env -i HOME=/home/user USER=user LOGNAME=user SHELL=/bin/bash LANG=C.UTF-8 \
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
"$binary" >>"$log" 2>&1 </dev/null &
child=$!
echo "$child" >"$state/dxd.pid"
while kill -0 "$child" 2>/dev/null; do
wait "$child"
done
child=
rm -f "$state/dxd.pid"
# Restart at once after a long run; back off up to 30 s while it keeps
# failing (systemd: RestartSec=500ms).
if [ $(($(date +%s) - started)) -ge 60 ]; then backoff=0; fi
if [ "$backoff" -eq 0 ]; then
backoff=1
pause 0.5
else
backoff=$((backoff * 2))
if [ "$backoff" -gt 30 ]; then backoff=30; fi
pause "$backoff"
fi
done