refactor: container -> workspace

This commit is contained in:
yeti
2025-04-18 03:55:46 +01:00
parent 6cf8dbd73a
commit 54b7c9086b
2 changed files with 25 additions and 25 deletions

View File

@ -2,35 +2,35 @@
MODE="$1" # 'rw' or 'readonly'
PERSON="$2"
CONTAINER="$SSH_ORIGINAL_COMMAND"
WORKSPACE="$SSH_ORIGINAL_COMMAND"
IMAGE="analytics-backend-workspace" # change to match your setup
TMUX_SESSION="analytics-backend"
TMUX_SESSION="$WORKSPACE@analytics-backend"
DEV_USER="devuser"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
if [[ ! "$CONTAINER" =~ ^[a-zA-Z0-9._-]+$ ]]; then
log "❌ Invalid container name: $CONTAINER"
if [[ ! "$WORKSPACE" =~ ^[a-zA-Z0-9._-]+$ ]]; then
log "❌ Invalid container name: $WORKSPACE"
exit 1
fi
# Function to start the container if not running
start_container_if_needed() {
if ! podman container exists "$CONTAINER"; then
log "🚀 Creating container $CONTAINER..."
if ! podman container exists "$WORKSPACE"; then
log "🚀 Creating container $WORKSPACE..."
podman run -dit \
--userns=keep-id \
--name "$CONTAINER" \
--name "$WORKSPACE" \
--user "$DEV_USER" \
--hostname "$CONTAINER" \
--hostname "$WORKSPACE" \
--label auto-cleanup=true \
-v "${XDG_RUNTIME_DIR}"/podman/podman.sock:/run/podman/podman.sock \
"$IMAGE"
elif ! podman inspect -f '{{.State.Running}}' "$CONTAINER" | grep -q true; then
log "⚡ Starting existing container $CONTAINER..."
podman start "$CONTAINER" >/dev/null 2>&1
"$IMAGE" -- "$TMUX_SESSION"
elif ! podman inspect -f '{{.State.Running}}' "$WORKSPACE" | grep -q true; then
log "⚡ Starting existing container $WORKSPACE..."
podman start "$WORKSPACE" >/dev/null 2>&1
fi
sleep 1
}
@ -38,14 +38,14 @@ 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 "$WORKSPACE" tmux list-clients -t "$TMUX_SESSION" -F "#{client_user}" 2>/dev/null)
if echo "$client_users" | grep -q "$DEV_USER"; then
log "💡 devuser still attached — container stays running"
return 0
else
log "🏃 $PERSON has logged out — stopping container"
podman stop "$CONTAINER" >/dev/null 2>&1
podman stop "$WORKSPACE" >/dev/null 2>&1
return 1
fi
}
@ -57,34 +57,34 @@ rw)
start_container_if_needed
# Run tmux session inside the container
if ! podman exec -it --user "$DEV_USER" "$CONTAINER" tmux has-session -t "$TMUX_SESSION" >/dev/null 2>&1; then
if ! podman exec -it --user "$DEV_USER" "$CONTAINER" tmux new-session -d -s "$TMUX_SESSION" >/dev/null 2>&1; then
if ! podman exec -it --user "$DEV_USER" "$WORKSPACE" tmux has-session -t "$TMUX_SESSION" >/dev/null 2>&1; then
if ! podman exec -it --user "$DEV_USER" "$WORKSPACE" tmux new-session -d -s "$TMUX_SESSION" >/dev/null 2>&1; then
log "❌ Could not create new tmux session. Please contact admin or try again later."
exit 1
fi
fi
log "$PERSON is working on $CONTAINER"
if ! podman exec -it --user "$DEV_USER" "$CONTAINER" tmux attach -t "$TMUX_SESSION"; then
log "$PERSON is working on $WORKSPACE's workspace"
if ! podman exec -it --user "$DEV_USER" "$WORKSPACE" tmux attach -t "$TMUX_SESSION"; then
log "❌ Could not attach to tmux session. Please contact admin or try again later."
exit 1
fi
log "$PERSON finished working on $CONTAINER"
log "$PERSON finished working on $WORKSPACE's worksapce"
check_devuser_attached
exit 0
;;
ro)
if (podman container exists "$CONTAINER" && podman inspect -f '{{.State.Running}}' "$CONTAINER" | grep -q true) >/dev/null 2>&1; then
log "📜 $PERSON is viewing $CONTAINER"
if ! podman exec -it --user "$DEV_USER" "$CONTAINER" tmux attach -r -t "$TMUX_SESSION"; then
if (podman container exists "$WORKSPACE" && podman inspect -f '{{.State.Running}}' "$WORKSPACE" | grep -q true) >/dev/null 2>&1; then
log "📜 $PERSON is viewing $WORKSPACE's workspace"
if ! podman exec -it --user "$DEV_USER" "$WORKSPACE" tmux attach -r -t "$TMUX_SESSION"; then
log "❌ Could not attach to tmux session. Please contact admin or try again later."
exit 1
fi
log "🏃 $PERSON stopped viewing $CONTAINER"
log "🏃 $PERSON stopped viewing $WORKSPACE's workspace"
exit 0
else
log "Container $CONTAINER does not exist."
log "Workspace for $WORKSPACE does not exist."
exit 1
fi
;;

View File

@ -1,2 +1,2 @@
#!/bin/bash
tmux new-session -s analytics-backend
tmux new-session -s "$1"