Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Formula/bluefin-kubestellar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def caveats
To start kc-agent with Bluefin Server console defaults:
bluefin-kubestellar start

To bridge VM guest loopback (kiosk-proxy) via SSH:
bluefin-kubestellar tunnel

To check status:
bluefin-kubestellar status
EOS
Expand Down
190 changes: 176 additions & 14 deletions files/bin/bluefin-kubestellar
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,45 @@ set -euo pipefail
STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/bluefin-server"
PID_FILE="${STATE_DIR}/kc-agent.pid"
LOG_FILE="${STATE_DIR}/kc-agent.log"
TUNNEL_PID_FILE="${STATE_DIR}/ssh-tunnel.pid"
TUNNEL_LOG_FILE="${STATE_DIR}/ssh-tunnel.log"

CMD="start"
ORIGIN=""
PORT="8585"
SSH_PORT="2222"
LOCAL_PORT="8080"
REMOTE_PORT="8080"
SSH_KEY=""
FOREGROUND=0
AUTO_INSTALL=1
WITH_TUNNEL=0
TUNNEL_ACTION="start"

usage() {
cat <<EOF
Usage: $(basename "$0") [command] [options]

Commands:
start Install (if needed), configure, and launch kc-agent (default)
stop Stop background kc-agent instance
status Check kc-agent process and health endpoint
stop Stop background kc-agent instance (and SSH tunnel if running)
status Check kc-agent process, health endpoint, and SSH tunnel
logs Display kc-agent log output
tunnel [act] Manage SSH tunnel to guest loopback (actions: start, stop, status; default: start)
help Show this help message

Options:
-o, --origin <urls> Allowed origin URL(s) (comma-separated)
Default: http://localhost:8080,http://127.0.0.1:8080
-p, --port <port> kc-agent local health/listen port (default: 8585)
-f, --foreground Run kc-agent in foreground instead of backgrounding
--no-install Do not attempt Homebrew installation if kc-agent is missing
-h, --help Show this help message
-o, --origin <urls> Allowed origin URL(s) (comma-separated)
Default: http://localhost:8080,http://127.0.0.1:8080
-p, --port <port> kc-agent local health/listen port (default: 8585)
-f, --foreground Run kc-agent in foreground instead of backgrounding
--tunnel Start SSH tunnel to guest loopback when starting agent
--ssh-port <port> Host SSH port forwarded from VM (default: 2222)
--local-port <port> Host local port for tunnel (default: 8080)
--remote-port <port> Guest loopback port for tunnel (default: 8080)
-k, --key <path> SSH private key path
--no-install Do not attempt Homebrew installation if kc-agent is missing
-h, --help Show this help message
EOF
}

Expand All @@ -48,6 +62,14 @@ while [ $# -gt 0 ]; do
CMD="$1"
shift
;;
tunnel)
CMD="tunnel"
shift
if [ $# -gt 0 ] && [[ "$1" =~ ^(start|stop|status)$ ]]; then
TUNNEL_ACTION="$1"
shift
fi
;;
-o|--origin)
[ $# -gt 1 ] || { echo "ERROR: --origin requires an argument" >&2; exit 1; }
ORIGIN="$2"
Expand All @@ -66,6 +88,46 @@ while [ $# -gt 0 ]; do
PORT="${1#*=}"
shift
;;
--tunnel)
WITH_TUNNEL=1
shift
;;
--ssh-port)
[ $# -gt 1 ] || { echo "ERROR: --ssh-port requires an argument" >&2; exit 1; }
SSH_PORT="$2"
shift 2
;;
--ssh-port=*)
SSH_PORT="${1#*=}"
shift
;;
--local-port)
[ $# -gt 1 ] || { echo "ERROR: --local-port requires an argument" >&2; exit 1; }
LOCAL_PORT="$2"
shift 2
;;
--local-port=*)
LOCAL_PORT="${1#*=}"
shift
;;
--remote-port)
[ $# -gt 1 ] || { echo "ERROR: --remote-port requires an argument" >&2; exit 1; }
REMOTE_PORT="$2"
shift 2
;;
--remote-port=*)
REMOTE_PORT="${1#*=}"
shift
;;
-k|--key)
[ $# -gt 1 ] || { echo "ERROR: --key requires an argument" >&2; exit 1; }
SSH_KEY="$2"
shift 2
;;
--key=*)
SSH_KEY="${1#*=}"
shift
;;
-f|--foreground)
FOREGROUND=1
shift
Expand All @@ -91,12 +153,93 @@ while [ $# -gt 0 ]; do
esac
done

start_tunnel() {
mkdir -p "$STATE_DIR"
KEY_ARGS=()
if [ -n "$SSH_KEY" ] && [ -f "$SSH_KEY" ]; then
KEY_ARGS=(-i "$SSH_KEY")
else
for CANDIDATE in "$STATE_DIR"/vm-*/id_ed25519 "$HOME/.ssh/id_ed25519"; do
if [ -f "$CANDIDATE" ]; then
KEY_ARGS=(-i "$CANDIDATE")
break
fi
done
fi

if [ -f "$TUNNEL_PID_FILE" ]; then
TPID=$(<"$TUNNEL_PID_FILE")
if kill -0 "$TPID" 2>/dev/null; then
echo "==> SSH tunnel is already running (PID: $TPID, localhost:${LOCAL_PORT} -> guest 127.0.0.1:${REMOTE_PORT})"
return 0
fi
rm -f "$TUNNEL_PID_FILE"
fi

echo "==> Starting SSH port forward: localhost:${LOCAL_PORT} -> VM 127.0.0.1:${REMOTE_PORT} via port ${SSH_PORT}..."
nohup ssh ${KEY_ARGS[@]+"${KEY_ARGS[@]}"} -p "${SSH_PORT}" -N -L "${LOCAL_PORT}:127.0.0.1:${REMOTE_PORT}" \
-o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ExitOnForwardFailure=yes \
-o ServerAliveInterval=10 root@127.0.0.1 > "$TUNNEL_LOG_FILE" 2>&1 &
TPID=$!
echo "$TPID" > "$TUNNEL_PID_FILE"
sleep 0.5
if ! kill -0 "$TPID" 2>/dev/null; then
echo "ERROR: SSH tunnel process exited unexpectedly!" >&2
[ -f "$TUNNEL_LOG_FILE" ] && tail -n 20 "$TUNNEL_LOG_FILE" >&2
rm -f "$TUNNEL_PID_FILE"
return 1
fi
echo "==> SSH tunnel active (PID: $TPID): localhost:${LOCAL_PORT} -> guest 127.0.0.1:${REMOTE_PORT}"
return 0
}

stop_tunnel() {
if [ -f "$TUNNEL_PID_FILE" ]; then
TPID=$(<"$TUNNEL_PID_FILE")
if kill -0 "$TPID" 2>/dev/null; then
kill "$TPID" 2>/dev/null || true
echo "==> Stopped SSH tunnel (PID: $TPID)"
else
echo "==> SSH tunnel process ($TPID) was not running"
fi
rm -f "$TUNNEL_PID_FILE"
fi
}

status_tunnel() {
if [ -f "$TUNNEL_PID_FILE" ]; then
TPID=$(<"$TUNNEL_PID_FILE")
if kill -0 "$TPID" 2>/dev/null; then
echo "==> SSH tunnel is running (PID: $TPID, localhost:${LOCAL_PORT} -> guest 127.0.0.1:${REMOTE_PORT})"
return 0
fi
fi
echo "==> SSH tunnel is not running"
return 3
}

case "$CMD" in
help)
usage
exit 0
;;
tunnel)
case "$TUNNEL_ACTION" in
stop)
stop_tunnel
exit 0
;;
status)
status_tunnel || exit $?
;;
start|*)
start_tunnel
exit $?
;;
esac
;;
stop)
stop_tunnel
if [ -f "$PID_FILE" ]; then
PID=$(<"$PID_FILE")
if kill -0 "$PID" 2>/dev/null; then
Expand All @@ -112,23 +255,39 @@ case "$CMD" in
exit 0
;;
status)
AGENT_RUNNING=0
HEALTH_URL="http://127.0.0.1:${PORT}/health"
if curl --silent --fail --max-time 2 "$HEALTH_URL" >/dev/null 2>&1; then
echo "==> kc-agent is running and healthy at $HEALTH_URL"
if [ -f "$PID_FILE" ]; then
echo "==> Process PID: $(<"$PID_FILE")"
fi
exit 0
fi
if [ -f "$PID_FILE" ]; then
AGENT_RUNNING=1
elif [ -f "$PID_FILE" ]; then
PID=$(<"$PID_FILE")
if kill -0 "$PID" 2>/dev/null; then
echo "==> kc-agent process $PID is running but $HEALTH_URL is not responding" >&2
exit 1
AGENT_RUNNING=2
fi
fi
echo "==> kc-agent is not running"
exit 3
if [ "$AGENT_RUNNING" -eq 0 ]; then
echo "==> kc-agent is not running"
fi

if [ -f "$TUNNEL_PID_FILE" ]; then
TPID=$(<"$TUNNEL_PID_FILE")
if kill -0 "$TPID" 2>/dev/null; then
echo "==> SSH tunnel is running (PID: $TPID, localhost:${LOCAL_PORT} -> guest 127.0.0.1:${REMOTE_PORT})"
fi
fi

if [ "$AGENT_RUNNING" -eq 1 ]; then
exit 0
elif [ "$AGENT_RUNNING" -eq 2 ]; then
exit 1
else
exit 3
fi
;;
logs)
if [ -f "$LOG_FILE" ]; then
Expand All @@ -139,6 +298,9 @@ case "$CMD" in
exit 0
;;
start)
if [ "$WITH_TUNNEL" -eq 1 ]; then
start_tunnel || exit 1
fi
ORIGIN="${ORIGIN:-http://localhost:8080,http://127.0.0.1:8080}"
mkdir -p "$STATE_DIR"

Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_setup_kubestellar.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ def test_formulas_exist_and_configure_service_or_binary() -> None:
agent_text = FORMULA_AGENT.read_text(encoding="utf-8")
assert "service do" in agent_text
assert "KAGENTI_CONTROLLER_URL" in agent_text


def test_bluefin_kubestellar_supports_ssh_tunnel() -> None:
text = SCRIPT.read_text(encoding="utf-8")
assert "tunnel" in text
assert "--tunnel" in text
assert "--ssh-port" in text
assert "-L" in text
Loading