blob: 62471b9a13cdc9d4629eb1189693d982a60c4ce8 (
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
|
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
# Dispatch runs as system service instances keyed on the *invoking* user, so
# `bin/service start` brings the services up running as you (e.g. dispatch-api@tradam).
RUN_USER="$(id -un)"
API_UNIT="dispatch-api@${RUN_USER}"
FRONTEND_UNIT="dispatch-frontend@${RUN_USER}"
cmd="${1:-help}"
shift 2>/dev/null || true
usage() {
cat <<EOF
Usage: bin/service <command>
Commands:
install Install the dispatch packages (systemd system unit templates +
application files in /opt/dispatch).
start Start dispatch-api and dispatch-frontend as ${RUN_USER}.
If already running, restart instead.
Prints the frontend URL when ready.
stop Stop dispatch-api and dispatch-frontend.
status Show status of both services.
logs Tail journal output for both services.
Services run as system instances for the current user:
${API_UNIT}
${FRONTEND_UNIT}
Start/stop use sudo (system manager). After install, configure
/etc/dispatch/dispatch-api.conf before starting.
EOF
}
install() {
echo "==> Building fresh package..."
"$PROJECT_DIR/bin/build-pkg"
echo ""
echo "==> Installing..."
"$PROJECT_DIR/bin/install-pkg"
sudo systemctl daemon-reload
echo ""
echo "==> Installed. Edit /etc/dispatch/dispatch-api.conf, then run:"
echo " bin/service start"
}
have_units() {
systemctl cat [email protected] &>/dev/null
}
start() {
if ! have_units; then
echo "dispatch services not installed. Run: bin/service install"
return 1
fi
local api_active frontend_active
api_active=$(systemctl is-active "$API_UNIT" 2>/dev/null || echo "inactive")
frontend_active=$(systemctl is-active "$FRONTEND_UNIT" 2>/dev/null || echo "inactive")
if [ "$api_active" = "active" ] || [ "$frontend_active" = "active" ]; then
echo "==> Services already running (api=$api_active frontend=$frontend_active) — restarting"
sudo systemctl restart "$API_UNIT" "$FRONTEND_UNIT"
echo "dispatch-api + dispatch-frontend restarted (as ${RUN_USER})"
else
sudo systemctl start "$API_UNIT" "$FRONTEND_UNIT"
echo "dispatch-api + dispatch-frontend started (as ${RUN_USER})"
fi
echo " → http://localhost:18391"
}
stop() {
if ! have_units; then
echo "dispatch services not installed."
return 1
fi
sudo systemctl stop "$API_UNIT" "$FRONTEND_UNIT"
echo "dispatch-api + dispatch-frontend stopped"
}
status() {
if ! have_units; then
echo "dispatch services not installed."
return 1
fi
systemctl status "$API_UNIT" "$FRONTEND_UNIT" --no-pager 2>/dev/null || true
}
logs() {
sudo journalctl -u "$API_UNIT" -u "$FRONTEND_UNIT" -f "$@"
}
case "$cmd" in
install) install ;;
start) start ;;
stop) stop ;;
status) status ;;
logs) logs "$@" ;;
help|--help|-h) usage ;;
*) echo "Unknown command: $cmd"; echo; usage; exit 1 ;;
esac
|