blob: 2bf6ff3eed6910aa4a95ced80d483165ba59e54d (
plain)
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
146
147
|
#!/usr/bin/env bash
# bin/install — build + install Dispatch as a systemd service on Arch Linux.
#
# Usage:
# sudo bin/install # build + install + enable + start
# sudo bin/install --no-build # install only (skip the build step)
# sudo bin/install --uninstall # stop + disable + remove files
#
# What it installs:
# /usr/bin/dispatch-server — backend binary
# /usr/bin/dispatch — CLI binary
# /usr/share/dispatch/web/ — frontend static files
# /etc/dispatch/env — server config (EnvironmentFile)
# /etc/systemd/system/dispatch.service — systemd unit
# /var/lib/dispatch/ — data directory (SQLite DBs)
# /var/log/dispatch/ — journal + trace logs
#
# After install: systemctl status dispatch
# Logs: journalctl -u dispatch -f
# Config: edit /etc/dispatch/env then `systemctl restart dispatch`
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$HERE/.." && pwd)"
if [[ "$(id -u)" -ne 0 ]]; then
echo "install: must run as root (use sudo)" >&2
exit 1
fi
ACTION="install"
NO_BUILD=0
for arg in "$@"; do
case "$arg" in
--no-build) NO_BUILD=1 ;;
--uninstall) ACTION="uninstall" ;;
*) echo "install: unknown flag: $arg" >&2; exit 1 ;;
esac
done
# ─── Uninstall ───────────────────────────────────────────────────────────────
if [[ "$ACTION" == "uninstall" ]]; then
echo "[uninstall] stopping service…"
systemctl stop dispatch 2>/dev/null || true
systemctl disable dispatch 2>/dev/null || true
rm -f /etc/systemd/system/dispatch.service
systemctl daemon-reload
echo "[uninstall] removing files…"
rm -f /usr/bin/dispatch-server /usr/bin/dispatch
rm -rf /usr/share/dispatch/web
# Keep config + data (user might want them)
echo "[uninstall] done."
echo " Kept: /etc/dispatch/env, /var/lib/dispatch/, /var/log/dispatch/"
echo " Remove manually if desired: rm -rf /etc/dispatch /var/lib/dispatch /var/log/dispatch"
exit 0
fi
# ─── Build ───────────────────────────────────────────────────────────────────
if [[ "$NO_BUILD" -eq 0 ]]; then
echo "[install] building…"
"$ROOT/bin/build"
fi
# Verify outputs exist
if [[ ! -f "$ROOT/dist/dispatch-server" ]]; then
echo "install: dist/dispatch-server not found — run bin/build first" >&2
exit 1
fi
# ─── Install binaries ────────────────────────────────────────────────────────
echo "[install] binaries → /usr/bin/"
install -Dm755 "$ROOT/dist/dispatch-server" /usr/bin/dispatch-server
install -Dm755 "$ROOT/dist/dispatch" /usr/bin/dispatch
# ─── Install frontend ────────────────────────────────────────────────────────
if [[ -d "$ROOT/dist/web" ]]; then
echo "[install] frontend → /usr/share/dispatch/web/"
mkdir -p /usr/share/dispatch/web
cp -r "$ROOT/dist/web/"* /usr/share/dispatch/web/
fi
# ─── Install systemd service ─────────────────────────────────────────────────
echo "[install] systemd service → /etc/systemd/system/"
# Detect the real user to run the service as (not root)
REAL_USER="${SUDO_USER:-$USER}"
REAL_GROUP=$(id -gn "$REAL_USER" 2>/dev/null || echo "$REAL_USER")
# Unmask if previously masked
rm -f /etc/systemd/system/dispatch.service 2>/dev/null || true
# Write the service file directly with the user patched in
cat > /etc/systemd/system/dispatch.service << EOF
[Unit]
Description=Dispatch AI Agent Server
After=network.target
[Service]
Type=simple
User=$REAL_USER
Group=$REAL_GROUP
ExecStart=/usr/bin/dispatch-server
EnvironmentFile=/etc/dispatch/env
WorkingDirectory=/var/lib/dispatch
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
chmod 644 /etc/systemd/system/dispatch.service
# ─── Config (don't overwrite if it exists) ───────────────────────────────────
if [[ ! -f /etc/dispatch/env ]]; then
echo "[install] config → /etc/dispatch/env (run sudo bin/setup-env to populate)"
install -Dm600 "$ROOT/systemd/dispatch.env" /etc/dispatch/env
else
echo "[install] config /etc/dispatch/env already exists — keeping"
fi
# ─── Data + log directories ──────────────────────────────────────────────────
mkdir -p /var/lib/dispatch /var/log/dispatch
chown -R "$REAL_USER:$REAL_GROUP" /var/lib/dispatch /var/log/dispatch
# ─── Enable + start ──────────────────────────────────────────────────────────
systemctl daemon-reload
systemctl enable dispatch
if grep -q 'sk-\.\.\.' /etc/dispatch/env 2>/dev/null; then
echo ""
echo "[install] done — but DISPATCH_API_KEY is still the placeholder."
echo " 1. Edit /etc/dispatch/env and set your API key"
echo " 2. systemctl start dispatch"
echo " 3. journalctl -u dispatch -f"
else
echo "[install] starting service…"
systemctl start dispatch
echo ""
echo "[install] done!"
echo " Status: systemctl status dispatch"
echo " Logs: journalctl -u dispatch -f"
echo " Config: /etc/dispatch/env"
echo " Frontend: http://localhost:24991"
echo " API: http://localhost:24991/chat"
fi
|