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
4 changes: 4 additions & 0 deletions .github/release-lines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ pinned:
# filters name v5 only: the forge.Merger it exercises lives on v5, and v4 is
# feature-frozen.
forgejo-smoke.yml: [-v4]
# Publishes the headless Twitch streamer image (#15594) from v5 only: the
# dashboard's `?kiosk=1` mode it depends on lives on v5 and v4 is
# feature-frozen. The image is operator tooling, not a hive release channel.
stream-image.yml: [-v4]

# Workflows that deliberately do NOT pin to the release lines. Each must keep a
# wildcard branch filter; if one is ever narrowed to a fixed list, the guard
Expand Down
146 changes: 146 additions & 0 deletions .github/workflows/stream-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# stream-image (#15594): build and publish the headless Twitch streamer image
# (src/deploy/stream/Dockerfile) that puts the hive Activity Monitor on a 24/7
# stream.
#
# ── Why a sibling of docker.yml, not a row in it ────────────────────────────
#
# docker.yml builds the hive binary image: multi-arch, cosign-signed, release
# channels, a test-image lane. The streamer is Xvfb + Chromium + ffmpeg plus a
# 100-line Go proxy — none of that machinery applies, and bolting a second
# Dockerfile onto docker.yml's matrix would make the hive image's publish gate
# depend on apt mirrors for chromium. ci-runner-image.yml is the precedent for
# a small, separately-published operator image; this follows it.
#
# ── Triggers ────────────────────────────────────────────────────────────────
#
# push (v5) path-filtered to src/deploy/stream/** and this file. Pushes
# ghcr.io/<owner>/hive-stream:<sha> (immutable) and
# ghcr.io/<owner>/hive-stream:v5-latest (moving — what the
# Kustomize overlay references, so a merged fix rolls on the
# next pod restart).
# pull_request builds only (no login, no push), same path filter. The
# Dockerfile compiles the auth proxy and runs its tests, so
# this is the CI gate for src/deploy/stream changes.
# workflow_dispatch builds AND pushes from whatever ref it is run on; the
# moving tag is `<ref>-latest`.
#
# `branches: [v5]` only: the dashboard's `?kiosk=1` mode the streamer depends
# on lives on v5 and v4 is feature-frozen, so it is listed as `[-v4]` under
# `pinned` in .github/release-lines.yml.
#
# ── Where it runs ───────────────────────────────────────────────────────────
#
# `ubuntu-latest`, like ci-runner-image.yml: the build is one apt-get of
# chromium/ffmpeg and a Go compile; GitHub-hosted runners have the egress and
# docker.yml already proves the GITHUB_TOKEN → GHCR push path from them.
#
# ── Cache policy ────────────────────────────────────────────────────────────
#
# No `type=gha` cache — same reasoning as ci-runner-image.yml (the Actions
# cache is the resource #7206 measured as full). This builds rarely.
name: Stream Image

on:
push:
branches: [v5]
paths:
- 'src/deploy/stream/**'
- '.github/workflows/stream-image.yml'
pull_request:
paths:
- 'src/deploy/stream/**'
- '.github/workflows/stream-image.yml'
workflow_dispatch:

concurrency:
group: stream-image-${{ github.ref }}
cancel-in-progress: false

permissions:
contents: read
packages: write

env:
IMAGE: ghcr.io/${{ github.repository_owner }}/hive-stream

jobs:
build:
name: ${{ github.event_name == 'pull_request' && 'Build (PR gate, no push)' || 'Build and push' }}
# GitHub-hosted on purpose — see the header. Do not route this through
# HIVE_RUNNER_LABELS.
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

- name: Resolve tags
id: ref
env:
EVENT: ${{ github.event_name }}
REF_NAME: ${{ github.ref_name }}
SHA: ${{ github.sha }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
set -euo pipefail
if [ "$EVENT" = "pull_request" ]; then
echo "tags=${IMAGE}:pr-${PR_NUMBER}" >> "$GITHUB_OUTPUT"
echo "push=false" >> "$GITHUB_OUTPUT"
exit 0
fi
# Moving tag is "<branch>-latest"; a branch name with "/" is not a
# valid tag component, so flatten it.
line="$(printf '%s' "$REF_NAME" | tr '/' '-')"
{
echo "tags=${IMAGE}:${SHA}"$'\n'"${IMAGE}:${line}-latest"
echo "push=true"
} >> "$GITHUB_OUTPUT"

- name: Log in to GHCR
if: steps.ref.outputs.push == 'true'
uses: docker/login-action@81c6f93d4bc991e510b342a00b419b3bb4efc5c6 # v4.7.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1

- name: Build${{ steps.ref.outputs.push == 'true' && ' and push' || '' }}
id: build
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
# Repo root: the Dockerfile compiles src/deploy/stream/authproxy.
context: .
file: src/deploy/stream/Dockerfile
platforms: linux/amd64
push: ${{ steps.ref.outputs.push == 'true' }}
tags: ${{ steps.ref.outputs.tags }}
provenance: false
sbom: false
labels: |
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.description=hivecommons/hive headless Twitch streamer for the Activity Monitor (#15594)

- name: Summary
env:
TAGS: ${{ steps.ref.outputs.tags }}
DIGEST: ${{ steps.build.outputs.digest }}
PUSHED: ${{ steps.ref.outputs.push }}
run: |
{
echo "## hive-stream image"
echo
if [ "$PUSHED" = "true" ]; then
echo "Pushed:"
printf '%s\n' "$TAGS" | sed 's/^/- `/; s/$/`/'
echo
echo "Digest: \`$DIGEST\`"
echo
echo 'Roll it: `kubectl -n hive-stream rollout restart deploy/hive-stream` (the overlay tracks `v5-latest`).'
else
echo "Built (not pushed): \`$TAGS\`"
fi
} >> "$GITHUB_STEP_SUMMARY"
1 change: 1 addition & 0 deletions changelog.d/added-activity-monitor-twitch-streamer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `src/deploy/stream/`: a headless Twitch streamer image (Xvfb + Chromium + ffmpeg, published as `ghcr.io/hivecommons/hive-stream`) with Kustomize manifests that put the dashboard's Activity Monitor on an unattended 24/7 stream, plus a dashboard `?kiosk=1` display mode that hides the chrome, dialogs and every other section ([#15594](https://github.com/hivecommons/hive/issues/15594)). Guide: `src/docs/streaming-the-activity-monitor.md`.
1 change: 1 addition & 0 deletions src/deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Use it indirectly through the dashboard terminal link. If a terminal pane says i
## Other directories

- `kustomize/overlays/`: the `standalone` (self-hosted, hub-less), `openshift`, `openshift-netadmin` and `backup-exec-restriction` overlays on top of `k8s/` and `inference/`. See the [standalone overlay README](kustomize/overlays/standalone/README.md).
- `stream/`: the headless Twitch streamer that puts the dashboard's Activity Monitor on a 24/7 stream (Xvfb + Chromium + ffmpeg image, Kustomize base and the `hive-hosted` overlay). See [its README](stream/README.md) and [Streaming the Activity Monitor](../docs/streaming-the-activity-monitor.md).
- `ci-runners/`: manifests for the self-hosted GitHub Actions runners that serve this repository. See [its README](ci-runners/README.md).
- `legacy-redirect/`: the hub legacy-host redirect that keeps the request path and query (#6430). See [its README](legacy-redirect/README.md).
- `dibs-domain-cutover/`: staged manifests for moving dibs to `dibs.hivecommons.dev` (#5925). See [its README](dibs-domain-cutover/README.md).
Expand Down
2 changes: 2 additions & 0 deletions src/deploy/stream/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
out/
49 changes: 49 additions & 0 deletions src/deploy/stream/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# hive-stream: headless Twitch streamer for the Hive activity monitor
# (hivecommons/hive#15594). See README.md here and
# src/docs/streaming-the-activity-monitor.md.
#
# Build context is the REPO ROOT (like src/Dockerfile):
# docker build -f src/deploy/stream/Dockerfile -t hive-stream .
#
# Base images are pinned by digest for the same supply-chain reason as
# src/Dockerfile.contributor (#3746); the golang digest is the one
# src/Dockerfile uses, the bookworm-slim digest the one Dockerfile.contributor
# uses, so a refresh of either is a one-line bump in lockstep with them.
ARG BASE_REGISTRY=docker.io/library

FROM --platform=$BUILDPLATFORM ${BASE_REGISTRY}/golang:1.27-alpine@sha256:f2c872bcee949b7be9c490efd02b48a8a2fbf947cdf533f1c58525ec9d8170fd AS authproxy
ARG TARGETOS TARGETARCH
WORKDIR /build
COPY src/deploy/stream/authproxy/*.go ./
# Stdlib-only: a throwaway module keeps this stage independent of the root
# go.mod/go.sum (no download, no replace directives to satisfy).
RUN go mod init hive-stream/authproxy >/dev/null 2>&1 \
&& CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -ldflags='-s -w' -o /out/authproxy .

FROM ${BASE_REGISTRY}/debian:bookworm-slim@sha256:7c7b2c966bc9ee8cedfeef67e0e279108992c77681fa595db4a9d65c06ccc587
ENV DEBIAN_FRONTEND=noninteractive
# No PulseAudio on purpose: Twitch requires an audio track, which ffmpeg
# synthesises from anullsrc; Chromium runs --mute-audio.
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates curl tini procps \
xvfb x11-utils xdotool \
chromium \
ffmpeg \
fonts-dejavu-core fonts-noto-core fonts-noto-color-emoji fonts-liberation \
&& apt-get clean && rm -rf /var/lib/apt/lists/* \
&& useradd --uid 1000 --create-home --shell /usr/sbin/nologin stream \
&& install -d -o stream -g stream /run/stream

COPY --from=authproxy /out/authproxy /usr/local/bin/authproxy
COPY src/deploy/stream/entrypoint.sh src/deploy/stream/healthcheck.sh /usr/local/bin/
RUN chmod 0755 /usr/local/bin/entrypoint.sh /usr/local/bin/healthcheck.sh \
&& chromium --version && ffmpeg -version | head -1 && Xvfb -help >/dev/null 2>&1

USER stream
ENV HOME=/home/stream RUN_DIR=/run/stream \
DASHBOARD_PATH="/?kiosk=1#section-activity-monitor-section" \
TWITCH_INGEST=rtmp://live.twitch.tv/app \
VIDEO_SIZE=1920x1080 FPS=30 BITRATE=4500k RELOAD_EVERY=30m
HEALTHCHECK --interval=30s --timeout=5s --start-period=90s --retries=3 CMD ["/usr/local/bin/healthcheck.sh"]
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/entrypoint.sh"]
41 changes: 41 additions & 0 deletions src/deploy/stream/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# `src/deploy/stream/` — headless Twitch streamer for the Activity Monitor

Puts the dashboard's Activity Monitor (stage tracker) on an unattended 24/7
Twitch stream. Operator guide, Twitch-side setup, and troubleshooting:
[`src/docs/streaming-the-activity-monitor.md`](../../docs/streaming-the-activity-monitor.md).

| File | Purpose |
| --- | --- |
| `Dockerfile` | `debian:bookworm-slim` + Xvfb, Chromium, ffmpeg, xdotool, tini; builds `authproxy` in a `golang` stage. Build context is the **repo root**. Published as `ghcr.io/hivecommons/hive-stream:<sha>` / `:v5-latest` by `.github/workflows/stream-image.yml`. |
| `entrypoint.sh` | Supervisor: Xvfb → authproxy → Chromium `--kiosk` (restart with backoff) → periodic reload (`RELOAD_EVERY`) → ffmpeg `x11grab` + `anullsrc` → libx264 CBR → RTMP. Masks the stream key in ffmpeg output. |
| `authproxy/` | Loopback reverse proxy (stdlib Go) that adds `Authorization: Bearer $DASHBOARD_TOKEN` to every dashboard request so the token is never in a URL, in Chromium's history, or on screen. Logs when the upstream answers with a login redirect. |
| `healthcheck.sh` | Chromium alive + ffmpeg progress file fresher than 20 s with `frame>0`. Used by the image `HEALTHCHECK` and the Deployment's probes. |
| `docker-compose.yaml`, `.env.example` | Run the same image from a laptop. |
| `k8s/` | Kustomize base: Namespace `hive-stream`, ConfigMap, Deployment (1 replica, non-root, 512 Mi `/dev/shm`, 1–2 CPU / 2–3 Gi), NetworkPolicy (DNS, dashboard Service, public RTMP/HTTPS only). `secret.yaml` is a **template** and is not in the kustomization. |
| `overlays/hive-hosted/` | hive's own hosted spoke on `hive-oke`. |

## Environment

| Variable | Default | Meaning |
| --- | --- | --- |
| `DASHBOARD_URL` | required | Dashboard origin (`http://hive.<ns>.svc.cluster.local:3002` in-cluster, or the public route). |
| `DASHBOARD_TOKEN` | — | Spoke `dashboard.auth_token` (in-cluster) or a GitHub token of a hub user with access (public route). |
| `DASHBOARD_PATH` | `/?kiosk=1#section-activity-monitor-section` | Path Chromium opens. `?kiosk=1` is the dashboard's unattended display mode. |
| `TWITCH_STREAM_KEY` | — | Primary Stream key. |
| `TWITCH_INGEST` | `rtmp://live.twitch.tv/app` | Ingest; pick a closer one from Twitch's ingest list if needed. |
| `VIDEO_SIZE` / `FPS` / `BITRATE` / `X264_PRESET` | `1920x1080` / `30` / `4500k` / `veryfast` | Encode settings. Keyframe interval is `2×FPS`. |
| `RELOAD_EVERY` | `30m` | Page reload cadence (picks up dashboard rolls). |
| `STREAM_OUTPUT` | — | Dry run: `null` discards, `*.flv` writes a file, anything else is a raw ffmpeg output. |
| `STREAM_SECONDS` | `0` | `>0` stops ffmpeg after N s and exits 0 (dry run); a non-zero ffmpeg exit fails the run. |

## Dry run (no Twitch, no cluster)

```sh
docker build -t hive-stream:local -f src/deploy/stream/Dockerfile .
docker run --rm --shm-size=512m -v "$PWD/src/deploy/stream/out:/out" \
-e DASHBOARD_URL=https://<hive>.hive.hivecommons.dev -e DASHBOARD_TOKEN=<github token> \
-e STREAM_OUTPUT=/out/test.flv -e STREAM_SECONDS=20 hive-stream:local
ffmpeg -ss 15 -i src/deploy/stream/out/test.flv -frames:v 1 src/deploy/stream/out/frame.png
```

`out/` is git-ignored.
121 changes: 121 additions & 0 deletions src/deploy/stream/authproxy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Command authproxy is the streamer's local credential injector
// (src/deploy/stream, hivecommons/hive#15594).
//
// Chromium cannot attach an Authorization header to a kiosk URL, and putting
// the token in the URL would paint it on the stream. So the browser talks to
// this proxy on loopback and the proxy forwards to the dashboard origin with
// `Authorization: Bearer $DASHBOARD_TOKEN` added. The token never reaches the
// renderer, the URL bar, or a screenshot. Stdlib only; one binary, one flag.
package main

import (
"flag"
"fmt"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"time"
)

const (
defaultListen = "127.0.0.1:8099"
dialTimeout = 10 * time.Second
responseHeaderWindow = 60 * time.Second
idleConnWindow = 90 * time.Second
// Hop-by-hop Upgrade requests (ttyd websockets) are passed straight
// through by httputil; FlushInterval -1 keeps SSE (/api/events) flowing.
sseFlushInterval = -1
)

func main() {
listen := flag.String("listen", envOr("AUTHPROXY_LISTEN", defaultListen), "loopback address to serve the browser on")
target := flag.String("target", os.Getenv("DASHBOARD_URL"), "dashboard origin, e.g. https://hive.example.com (path is ignored)")
flag.Parse()

token := strings.TrimSpace(os.Getenv("DASHBOARD_TOKEN"))
rp, err := newProxy(*target, token)
if err != nil {
log.Fatalf("authproxy: %v", err)
}
mux := http.NewServeMux()
mux.HandleFunc("/-/authproxy/healthz", func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte("ok\n")) })
mux.Handle("/", rp)
srv := &http.Server{Addr: *listen, Handler: mux, ReadHeaderTimeout: dialTimeout}
log.Printf("authproxy: %s -> %s (token %s)", *listen, redactURL(*target), yesNo(token != ""))
log.Fatal(srv.ListenAndServe())
}

// newProxy builds the reverse proxy. It is split from main so a unit test can
// assert the header is injected and the Host rewritten without a listener.
func newProxy(target, token string) (*httputil.ReverseProxy, error) {
if target == "" {
return nil, fmt.Errorf("DASHBOARD_URL (or -target) is required")
}
u, err := url.Parse(target)
if err != nil || u.Scheme == "" || u.Host == "" {
return nil, fmt.Errorf("DASHBOARD_URL %q must be an absolute http(s) origin", redactURL(target))
}
origin := &url.URL{Scheme: u.Scheme, Host: u.Host}
rp := &httputil.ReverseProxy{
Rewrite: func(pr *httputil.ProxyRequest) {
pr.SetURL(origin)
pr.Out.Host = origin.Host
// Never forward the loopback client's own headers as if they
// were the user's — this proxy IS the client.
pr.Out.Header.Del("X-Forwarded-For")
if token != "" {
pr.Out.Header.Set("Authorization", "Bearer "+token)
}
},
FlushInterval: sseFlushInterval,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{Timeout: dialTimeout}).DialContext,
ResponseHeaderTimeout: responseHeaderWindow,
IdleConnTimeout: idleConnWindow,
ForceAttemptHTTP2: true,
},
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
log.Printf("authproxy: upstream error for %s: %v", r.URL.Path, err)
http.Error(w, "dashboard unreachable", http.StatusBadGateway)
},
}
// Cookies the dashboard sets are scoped to the loopback origin by the
// browser, which is what we want; nothing else needs rewriting.
rp.ModifyResponse = func(resp *http.Response) error {
// A hub login redirect means the token was rejected: surface it in the
// logs instead of letting Chromium bounce to github.com forever.
if loc := resp.Header.Get("Location"); resp.StatusCode/100 == 3 && strings.Contains(loc, "/login") {
log.Printf("authproxy: upstream redirected %s to login (%s) — DASHBOARD_TOKEN rejected or missing", resp.Request.URL.Path, redactURL(loc))
}
return nil
}
return rp, nil
}

func envOr(k, def string) string {
if v := os.Getenv(k); v != "" {
return v
}
return def
}

func yesNo(b bool) string {
if b {
return "set"
}
return "UNSET"
}

// redactURL drops the query so a token someone put in DASHBOARD_URL by
// mistake does not end up in the logs.
func redactURL(s string) string {
if i := strings.IndexByte(s, '?'); i >= 0 {
return s[:i] + "?…"
}
return s
}
Loading
Loading