Skip to content

Commit b2d91f5

Browse files
authored
cli: include deployed version in baudbot status (#67)
1 parent 722c95a commit b2d91f5

1 file changed

Lines changed: 55 additions & 2 deletions

File tree

bin/baudbot

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ usage() {
4646
echo " start Start the agent (systemd, or --direct for foreground)"
4747
echo " stop Stop the agent"
4848
echo " restart Restart the agent"
49-
echo " status Show agent status"
49+
echo " status Show agent status + deployed version"
5050
echo " logs Tail agent logs"
5151
echo " attach Attach to a tmux session (default: first available)"
5252
echo " sessions List agent tmux and pi sessions"
@@ -81,6 +81,53 @@ has_systemd() {
8181
command -v systemctl &>/dev/null && [ -d /run/systemd/system ]
8282
}
8383

84+
print_deployed_version() {
85+
local agent_user="${BAUDBOT_AGENT_USER:-baudbot_agent}"
86+
local version_file="/home/$agent_user/.pi/agent/baudbot-version.json"
87+
local version_json=""
88+
local short=""
89+
local sha=""
90+
local branch=""
91+
local deployed_at=""
92+
local line=""
93+
94+
if [ -r "$version_file" ]; then
95+
version_json="$(cat "$version_file" 2>/dev/null || true)"
96+
elif [ "$(id -u)" -eq 0 ] && id "$agent_user" >/dev/null 2>&1; then
97+
version_json="$(sudo -u "$agent_user" cat "$version_file" 2>/dev/null || true)"
98+
fi
99+
100+
if [ -z "$version_json" ]; then
101+
local release_target=""
102+
local release_sha=""
103+
104+
release_target="$(readlink -f /opt/baudbot/current 2>/dev/null || true)"
105+
if printf '%s\n' "$release_target" | grep -Eq '/releases/[0-9a-f]{7,40}$'; then
106+
release_sha="${release_target##*/}"
107+
echo -e "${BOLD}deployed version:${RESET} ${release_sha:0:7} sha: $release_sha (from /opt/baudbot/current)"
108+
else
109+
echo -e "${BOLD}deployed version:${RESET} unavailable"
110+
fi
111+
return 0
112+
fi
113+
114+
short="$(printf '%s\n' "$version_json" | grep -E '"short"[[:space:]]*:' | head -1 | sed 's/.*"short"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/')"
115+
sha="$(printf '%s\n' "$version_json" | grep -E '"sha"[[:space:]]*:' | head -1 | sed 's/.*"sha"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/')"
116+
branch="$(printf '%s\n' "$version_json" | grep -E '"branch"[[:space:]]*:' | head -1 | sed 's/.*"branch"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/')"
117+
deployed_at="$(printf '%s\n' "$version_json" | grep -E '"deployed_at"[[:space:]]*:' | head -1 | sed 's/.*"deployed_at"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/')"
118+
119+
if [ -z "$short" ] && [ -n "$sha" ]; then
120+
short="${sha:0:7}"
121+
fi
122+
123+
line="${short:-unknown}"
124+
[ -n "$branch" ] && line="$line (branch: $branch)"
125+
[ -n "$deployed_at" ] && line="$line deployed: $deployed_at"
126+
[ -n "$sha" ] && line="$line sha: $sha"
127+
128+
echo -e "${BOLD}deployed version:${RESET} $line"
129+
}
130+
84131
case "${1:-}" in
85132
start)
86133
shift
@@ -125,7 +172,11 @@ case "${1:-}" in
125172
status)
126173
shift
127174
if has_systemd && systemctl is-enabled baudbot &>/dev/null; then
128-
exec systemctl status baudbot "$@"
175+
status_rc=0
176+
systemctl status baudbot "$@" || status_rc=$?
177+
echo ""
178+
print_deployed_version
179+
exit "$status_rc"
129180
else
130181
# Fallback: check if baudbot_agent has pi running
131182
if pgrep -u baudbot_agent -f "pi --session-control" &>/dev/null; then
@@ -134,6 +185,8 @@ case "${1:-}" in
134185
else
135186
echo "baudbot is not running"
136187
fi
188+
echo ""
189+
print_deployed_version
137190
fi
138191
;;
139192

0 commit comments

Comments
 (0)