fix(ssh): fix issues with tmux_cmd, improve logs, suppress outputs

This commit is contained in:
yeti 2025-04-18 01:10:56 +01:00
parent fcf765da20
commit 0142d20648

View File

@ -7,7 +7,7 @@ TMUX_SESSION="analytics-backend"
DEV_USER="devuser" DEV_USER="devuser"
VIEW_USER="viewer" VIEW_USER="viewer"
TMUX_CMD="tmux -S /tmp/tmux-shared/dev-socket" TMUX_CMD=(tmux -S /tmp/tmux-shared/dev-socket)
log() { log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
@ -32,21 +32,22 @@ start_container_if_needed() {
"$IMAGE" "$IMAGE"
elif ! podman inspect -f '{{.State.Running}}' "$CONTAINER" | grep -q true; then elif ! podman inspect -f '{{.State.Running}}' "$CONTAINER" | grep -q true; then
log "⚡ Starting existing container $CONTAINER..." log "⚡ Starting existing container $CONTAINER..."
podman start "$CONTAINER" podman start "$CONTAINER" >/dev/null 2>&1
fi fi
sleep 1
} }
# After devuser exits... # After devuser exits...
check_devuser_attached() { check_devuser_attached() {
# Get list of clients # Get list of clients
client_users=$(podman exec "$CONTAINER" "$TMUX_CMD" list-clients -t "$TMUX_SESSION" -F "#{client_user}" 2>/dev/null) client_users=$(podman exec "$CONTAINER" "${TMUX_CMD[@]}" list-clients -t "$TMUX_SESSION" -F "#{client_user}" 2>/dev/null)
if echo "$client_users" | grep -q "$DEV_USER"; then if echo "$client_users" | grep -q "$DEV_USER"; then
log "💡 devuser still attached — container stays running" log "💡 devuser still attached — container stays running"
return 0 return 0
else else
log "⚠️ devuser has exited — stopping container" log "⚠️ $CONTAINER has exited — stopping container"
podman stop "$CONTAINER" podman stop "$CONTAINER" >/dev/null 2>&1
return 1 return 1
fi fi
} }
@ -58,16 +59,15 @@ rw)
start_container_if_needed start_container_if_needed
# Run $TMUX_CMD session inside the container # Run $TMUX_CMD session inside the container
if ! podman exec -it --user "$DEV_USER" "$CONTAINER" "$TMUX_CMD" has-session -t "$TMUX_SESSION" 2>/dev/null; then if ! podman exec -it --user "$DEV_USER" "$CONTAINER" "${TMUX_CMD[@]}" has-session -t "$TMUX_SESSION" >/dev/null 2>&1; then
if ! podman exec -it --user "$DEV_USER" "$CONTAINER" "$TMUX_CMD" new-session -s "$TMUX_SESSION"; then if ! podman exec -it --user "$DEV_USER" "$CONTAINER" "${TMUX_CMD[@]}" new-session -s "$TMUX_SESSION" >/dev/null 2>&1; then
log "❌ Could not attach to $TMUX_CMD session. Please contact admin or try again later." log "❌ Could not create new ${TMUX_CMD[*]} session. Please contact admin or try again later."
podman logs "$CONTAINER"
exit 1 exit 1
fi fi
else else
if ! podman exec -it --user "$DEV_USER" "$CONTAINER" "$TMUX_CMD" attach -t "$TMUX_SESSION"; then log "$CONTAINER reattaching..."
log "❌ Could not attach to $TMUX_CMD session. Please contact admin or try again later." if ! podman exec -it --user "$DEV_USER" "$CONTAINER" "${TMUX_CMD[@]}" attach -t "$TMUX_SESSION" 2>/dev/null; then
podman logs "$CONTAINER" log "❌ Could not attach to ${TMUX_CMD[*]} session. Please contact admin or try again later."
exit 1 exit 1
fi fi
fi fi
@ -76,10 +76,9 @@ rw)
exit 0 exit 0
;; ;;
ro) ro)
if podman container exists "$CONTAINER" && podman inspect -f '{{.State.Running}}' "$CONTAINER" | grep -q true; then if (podman container exists "$CONTAINER" && podman inspect -f '{{.State.Running}}' "$CONTAINER" | grep -q true) >/dev/null 2>&1; then
if ! podman exec -it --user "$VIEW_USER" "$CONTAINER" "$TMUX_CMD" attach -r -t "$TMUX_SESSION"; then if podman exec -it --user "$VIEW_USER" "$CONTAINER" "${TMUX_CMD[@]}" attach -r -t "$TMUX_SESSION"; then
log "❌ Could not attach to $TMUX_CMD session. Please contact admin or try again later." log "❌ Could not attach to ${TMUX_CMD[*]} session. Please contact admin or try again later."
podman logs "$CONTAINER"
exit 1 exit 1
fi fi
exit 0 exit 0