2025-04-17 06:15:55 +01:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
2025-04-18 04:46:00 +01:00
|
|
|
|
PERSON="$1"
|
2025-04-18 03:55:46 +01:00
|
|
|
|
WORKSPACE="$SSH_ORIGINAL_COMMAND"
|
2025-05-16 23:35:00 +00:00
|
|
|
|
IMAGE="localhost/analytics-backend-workspace:latest"
|
2025-04-17 06:15:55 +01:00
|
|
|
|
DEV_USER="devuser"
|
|
|
|
|
|
2025-04-18 09:22:55 +01:00
|
|
|
|
XDG_RUNTIME_DIR="/run/user/$(id -u)"
|
2025-04-18 09:23:37 +01:00
|
|
|
|
LOG_FILE="/tmp/.ssh-router-${PERSON}.log"
|
2025-04-18 09:22:55 +01:00
|
|
|
|
|
2025-04-17 13:00:48 +01:00
|
|
|
|
log() {
|
2025-04-18 09:23:37 +01:00
|
|
|
|
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >>"$LOG_FILE"
|
2025-04-17 13:00:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-13 16:39:45 +01:00
|
|
|
|
if [[ ! -t 0 ]]; then
|
|
|
|
|
log "❌ No TTY allocated — refusing to run tmux without an interactive terminal"
|
|
|
|
|
echo "Error: No TTY. Use 'ssh -t'" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2025-04-18 09:22:55 +01:00
|
|
|
|
# log "🧩 IMAGE = '$IMAGE'"
|
|
|
|
|
# log "🧩 WORKSPACE = '$WORKSPACE'"
|
|
|
|
|
# log "🧩 PERSON = '$PERSON'"
|
|
|
|
|
|
|
|
|
|
# Fallbacks
|
|
|
|
|
if [[ -z "${WORKSPACE:-}" ]]; then
|
|
|
|
|
WORKSPACE="$PERSON"
|
|
|
|
|
log "ℹ️ Defaulted WORKSPACE to $WORKSPACE"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
TMUX_SESSION="$WORKSPACE|analytics-backend"
|
|
|
|
|
|
|
|
|
|
# Start podman socket service if it's not running
|
|
|
|
|
if [[ ! -S "$XDG_RUNTIME_DIR/podman/podman.sock" ]]; then
|
|
|
|
|
log "🔄 Starting Podman socket service for user $USER"
|
|
|
|
|
systemctl --user start podman.socket || {
|
|
|
|
|
log "❌ Failed to start podman.socket via systemd"
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Wait briefly for socket to appear
|
|
|
|
|
sleep 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ ! -S "$XDG_RUNTIME_DIR/podman/podman.sock" ]]; then
|
|
|
|
|
log "❌ Podman socket still missing after startup attempt"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2025-04-18 08:31:42 +01:00
|
|
|
|
|
|
|
|
|
# Check if image exists locally
|
|
|
|
|
if ! podman image exists "$IMAGE"; then
|
|
|
|
|
log "📦 Image $IMAGE not found locally. Pulling from registry..."
|
|
|
|
|
|
|
|
|
|
# Attempt to pull the image from the local registry (insecure HTTP)
|
|
|
|
|
if ! podman pull --tls-verify=false "$IMAGE"; then
|
|
|
|
|
log "❌ Failed to pull image from $IMAGE"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
log "✅ Successfully pulled $IMAGE"
|
2025-04-17 13:00:48 +01:00
|
|
|
|
fi
|
|
|
|
|
|
2025-04-18 08:31:42 +01:00
|
|
|
|
case "$SSH_ORIGINAL_COMMAND" in
|
|
|
|
|
*scp* | *sftp* | *rsync* | *tar*)
|
|
|
|
|
log "❌ File transfers are disabled"
|
|
|
|
|
exit 1
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
2025-04-17 06:15:55 +01:00
|
|
|
|
# Function to start the container if not running
|
|
|
|
|
start_container_if_needed() {
|
2025-04-18 03:55:46 +01:00
|
|
|
|
if ! podman container exists "$WORKSPACE"; then
|
|
|
|
|
log "🚀 Creating container $WORKSPACE..."
|
2025-04-17 06:15:55 +01:00
|
|
|
|
podman run -dit \
|
2025-04-17 06:41:56 +01:00
|
|
|
|
--userns=keep-id \
|
2025-04-18 03:55:46 +01:00
|
|
|
|
--name "$WORKSPACE" \
|
2025-04-17 06:15:55 +01:00
|
|
|
|
--user "$DEV_USER" \
|
2025-04-18 03:55:46 +01:00
|
|
|
|
--hostname "$WORKSPACE" \
|
2025-04-17 06:15:55 +01:00
|
|
|
|
--label auto-cleanup=true \
|
2025-04-17 06:41:56 +01:00
|
|
|
|
-v "${XDG_RUNTIME_DIR}"/podman/podman.sock:/run/podman/podman.sock \
|
2025-04-18 18:55:06 +01:00
|
|
|
|
-v /home/infilytics/data/"$WORKSPACE":/app \
|
2025-04-25 07:09:53 +01:00
|
|
|
|
-v /home/infilytics/secrets/"$WORKSPACE"/gitconfig:/home/"$DEV_USER"/.gitconfig:ro \
|
2025-05-16 13:12:25 +01:00
|
|
|
|
-v /home/infilytics/secrets/"$WORKSPACE"/id_ed25519:/home/"$DEV_USER"/.ssh/id_ed25519:ro \
|
|
|
|
|
-v /home/infilytics/secrets/"$WORKSPACE"/id_ed25519.pub:/home/"$DEV_USER"/.ssh/id_ed25519.pub:ro \
|
2025-04-18 04:18:57 +01:00
|
|
|
|
--entrypoint "/home/$DEV_USER/start.sh" \
|
|
|
|
|
"$IMAGE" "${TMUX_SESSION}"
|
2025-04-18 03:55:46 +01:00
|
|
|
|
elif ! podman inspect -f '{{.State.Running}}' "$WORKSPACE" | grep -q true; then
|
|
|
|
|
log "⚡ Starting existing container $WORKSPACE..."
|
|
|
|
|
podman start "$WORKSPACE" >/dev/null 2>&1
|
2025-04-17 06:15:55 +01:00
|
|
|
|
fi
|
2025-04-18 01:10:56 +01:00
|
|
|
|
sleep 1
|
2025-04-17 06:15:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# After devuser exits...
|
|
|
|
|
check_devuser_attached() {
|
|
|
|
|
# Get list of clients
|
2025-04-18 03:55:46 +01:00
|
|
|
|
client_users=$(podman exec "$WORKSPACE" tmux list-clients -t "$TMUX_SESSION" -F "#{client_user}" 2>/dev/null)
|
2025-04-17 06:15:55 +01:00
|
|
|
|
|
2025-04-17 13:00:48 +01:00
|
|
|
|
if echo "$client_users" | grep -q "$DEV_USER"; then
|
|
|
|
|
log "💡 devuser still attached — container stays running"
|
2025-04-17 06:15:55 +01:00
|
|
|
|
return 0
|
|
|
|
|
else
|
2025-04-18 02:34:24 +01:00
|
|
|
|
log "🏃 $PERSON has logged out — stopping container"
|
2025-04-18 03:55:46 +01:00
|
|
|
|
podman stop "$WORKSPACE" >/dev/null 2>&1
|
2025-04-17 06:15:55 +01:00
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-25 07:29:21 +01:00
|
|
|
|
get_access_mode() {
|
2025-04-18 04:46:00 +01:00
|
|
|
|
local yaml_file="access.yml"
|
|
|
|
|
local workspace="$1"
|
|
|
|
|
local person="$2"
|
|
|
|
|
|
2025-04-18 08:31:42 +01:00
|
|
|
|
if [[ ! "$workspace" =~ ^[a-zA-Z0-9._-]+$ ]]; then
|
|
|
|
|
log "❌ Invalid container name: $WORKSPACE"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2025-04-18 04:46:00 +01:00
|
|
|
|
# Special case: user accessing their own workspace
|
|
|
|
|
if [[ "$workspace" == "$person" ]]; then
|
|
|
|
|
echo "access=rw"
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Check rw
|
|
|
|
|
if yq '.["'"$person"'"].rw // []' "$yaml_file" | grep -q "\b$workspace\b"; then
|
|
|
|
|
echo "access=rw"
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Check ro
|
|
|
|
|
if yq '.["'"$person"'"].ro // []' "$yaml_file" | grep -q "\b$workspace\b"; then
|
|
|
|
|
echo "access=ro"
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# No access → exit with error
|
|
|
|
|
log "❌ $person has no access to $workspace" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 06:15:55 +01:00
|
|
|
|
# === Main ===
|
|
|
|
|
|
2025-04-25 07:29:21 +01:00
|
|
|
|
read -r access_line < <(get_access_mode "$WORKSPACE" "$PERSON") || exit 1
|
2025-04-18 04:46:00 +01:00
|
|
|
|
MODE="${access_line#access=}"
|
|
|
|
|
|
2025-04-17 06:15:55 +01:00
|
|
|
|
case "$MODE" in
|
|
|
|
|
rw)
|
|
|
|
|
start_container_if_needed
|
|
|
|
|
|
2025-04-18 03:25:46 +01:00
|
|
|
|
# Run tmux session inside the container
|
2025-04-18 03:55:46 +01:00
|
|
|
|
if ! podman exec -it --user "$DEV_USER" "$WORKSPACE" tmux has-session -t "$TMUX_SESSION" >/dev/null 2>&1; then
|
2025-05-14 07:35:20 +01:00
|
|
|
|
if ! podman exec -it -e EDITOR=nvim --user "$DEV_USER" "$WORKSPACE" tmux new-session -d -s "$TMUX_SESSION" >/dev/null 2>&1; then
|
2025-04-18 03:25:46 +01:00
|
|
|
|
log "❌ Could not create new tmux session. Please contact admin or try again later."
|
2025-04-17 06:55:36 +01:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2025-04-17 06:15:55 +01:00
|
|
|
|
fi
|
|
|
|
|
|
2025-04-18 03:55:46 +01:00
|
|
|
|
log "⚡ $PERSON is working on $WORKSPACE's workspace"
|
2025-05-14 07:37:20 +01:00
|
|
|
|
if ! podman exec -it -e TERM="$TERM" --user "$DEV_USER" "$WORKSPACE" tmux attach -t "$TMUX_SESSION"; then
|
2025-04-18 03:25:46 +01:00
|
|
|
|
log "❌ Could not attach to tmux session. Please contact admin or try again later."
|
2025-04-18 02:56:25 +01:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2025-04-18 03:55:46 +01:00
|
|
|
|
log "⚡ $PERSON finished working on $WORKSPACE's worksapce"
|
2025-04-18 02:56:25 +01:00
|
|
|
|
|
2025-04-17 06:15:55 +01:00
|
|
|
|
check_devuser_attached
|
2025-04-17 06:36:42 +01:00
|
|
|
|
exit 0
|
2025-04-17 06:15:55 +01:00
|
|
|
|
;;
|
|
|
|
|
ro)
|
2025-04-18 03:55:46 +01:00
|
|
|
|
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"
|
2025-05-14 07:37:20 +01:00
|
|
|
|
if ! podman exec -it -e TERM="$TERM" --user "$DEV_USER" "$WORKSPACE" tmux attach -r -t "$TMUX_SESSION"; then
|
2025-04-18 03:25:46 +01:00
|
|
|
|
log "❌ Could not attach to tmux session. Please contact admin or try again later."
|
2025-04-17 06:55:36 +01:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2025-04-18 03:55:46 +01:00
|
|
|
|
log "🏃 $PERSON stopped viewing $WORKSPACE's workspace"
|
2025-04-17 06:36:42 +01:00
|
|
|
|
exit 0
|
|
|
|
|
else
|
2025-04-18 03:55:46 +01:00
|
|
|
|
log "❌ Workspace for $WORKSPACE does not exist."
|
2025-04-17 06:15:55 +01:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
;;
|
|
|
|
|
*)
|
2025-04-17 13:00:48 +01:00
|
|
|
|
log "❌ Invalid access mode: $MODE"
|
2025-04-17 06:15:55 +01:00
|
|
|
|
exit 1
|
|
|
|
|
;;
|
|
|
|
|
esac
|