feat(ssh): more improvements to the router script

- add ability to connect to tmux sockets
- create variables for dev user
- fix issue with variable usage USER
This commit is contained in:
phoenix 2025-04-17 13:00:48 +01:00
parent b65b1e9b0b
commit 578e3f4829

View File

@ -7,10 +7,21 @@ TMUX_SESSION="analytics-backend"
DEV_USER="devuser"
VIEW_USER="viewer"
TMUX_CMD="tmux -S /tmp/tmux_shared/dev-socket"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
if [[ ! "$CONTAINER" =~ ^[a-zA-Z0-9._-]+$ ]]; then
log "❌ Invalid container name: $CONTAINER"
exit 1
fi
# Function to start the container if not running
start_container_if_needed() {
if ! podman container exists "$CONTAINER"; then
echo "🚀 Creating container $CONTAINER..."
log "🚀 Creating container $CONTAINER..."
podman run -dit \
--userns=keep-id \
--name "$CONTAINER" \
@ -18,9 +29,9 @@ start_container_if_needed() {
--hostname "$CONTAINER" \
--label auto-cleanup=true \
-v "${XDG_RUNTIME_DIR}"/podman/podman.sock:/run/podman/podman.sock \
"$IMAGE" sh -c "tmux has-session -t $TMUX_SESSION 2>/dev/null || tmux new-session -s $TMUX_SESSION"
"$IMAGE" sh -c "$TMUX_CMD has-session -t $TMUX_SESSION 2>/dev/null || $TMUX_CMD new-session -s $TMUX_SESSION"
elif ! podman inspect -f '{{.State.Running}}' "$CONTAINER" | grep -q true; then
echo "⚡ Starting existing container $CONTAINER..."
log "⚡ Starting existing container $CONTAINER..."
podman start "$CONTAINER"
fi
}
@ -28,13 +39,13 @@ start_container_if_needed() {
# After devuser exits...
check_devuser_attached() {
# Get list of clients
client_users=$(podman exec "$CONTAINER" tmux 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 "$USER"; then
echo "💡 devuser still attached — container stays running"
if echo "$client_users" | grep -q "$DEV_USER"; then
log "💡 devuser still attached — container stays running"
return 0
else
echo "⚠️ devuser has exited — stopping container"
log "⚠️ devuser has exited — stopping container"
podman stop "$CONTAINER"
return 1
fi
@ -46,16 +57,16 @@ case "$MODE" in
rw)
start_container_if_needed
# Run tmux session inside the container
if ! podman exec -it --user "$USER" "$CONTAINER" tmux has-session -t "$TMUX_SESSION" 2>/dev/null; then
if ! podman exec -it --user "$USER" "$CONTAINER" tmux new-session -s "$TMUX_SESSION"; then
echo "❌ Could not attach to tmux session. Please contact admin or try again later."
# 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" new-session -s "$TMUX_SESSION"; then
log "❌ Could not attach to $TMUX_CMD session. Please contact admin or try again later."
podman logs "$CONTAINER"
exit 1
fi
else
if ! podman exec -it --user "$USER" "$CONTAINER" tmux attach -t "$TMUX_SESSION"; then
echo "❌ Could not attach to tmux session. Please contact admin or try again later."
if ! podman exec -it --user "$DEV_USER" "$CONTAINER" "$TMUX_CMD" attach -t "$TMUX_SESSION"; then
log "❌ Could not attach to $TMUX_CMD session. Please contact admin or try again later."
podman logs "$CONTAINER"
exit 1
fi
@ -66,19 +77,19 @@ rw)
;;
ro)
if podman container exists "$CONTAINER" && podman inspect -f '{{.State.Running}}' "$CONTAINER" | grep -q true; then
if ! podman exec -it --user "$VIEW_USER" "$CONTAINER" tmux attach -r -t "$TMUX_SESSION"; then
echo "❌ Could not attach to tmux session. Please contact admin or try again later."
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."
podman logs "$CONTAINER"
exit 1
fi
exit 0
else
echo "❌ Container $CONTAINER does not exist."
log "❌ Container $CONTAINER does not exist."
exit 1
fi
;;
*)
echo "❌ Invalid access mode: $MODE"
log "❌ Invalid access mode: $MODE"
exit 1
;;
esac