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

View File

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