Asking for a cluster and building it were two clicks on two pages, with the record sitting at "requested" in between. Nobody wants the record; they want the cluster. Creating it now starts the build and opens the log. A portal with no provisioner still just records the request - that is all it can do - and lands on the list instead, and a build that fails to start does the same rather than showing an error page about a cluster that is nonetheless there. The log used to be a text endpoint you reloaded, which is the same information by a worse route: the interesting moment is usually the one you reloaded past. /clusters/watch follows it as it is written and stops on its own when the cluster stops moving. It polls rather than streams. A build is one writer appending to one file, and asking for the bytes after the ones you already have is the whole of what server-sent events would give here, without a long-lived connection to keep alive through Caddy and the proxy. ?from=N is what makes that cheap: a Terraform log reaches a few hundred kilobytes, and re-sending all of it every two seconds to append one line is most of a megabyte a minute for nothing. An offset past the end means a rebuild truncated the file, so the reader starts again rather than waiting for bytes that will never come. X-Cluster-State is read from the record, not guessed from the log's last line: a build that was killed writes no last line at all. The script backs off to five seconds while nothing is happening, and keeps the view pinned to the bottom only while the reader is already there - scrolling up to read something is deliberate, and yanking the view back down two seconds later undoes it. The script is a file rather than an inline block because the control plane's Content-Security-Policy is script-src 'self', and loosening that for a log viewer would be the wrong trade. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
159 lines
4.9 KiB
Go
159 lines
4.9 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/ilkermanap/hpcod/internal/store"
|
|
)
|
|
|
|
// Watching a build happen.
|
|
//
|
|
// A build takes minutes and writes a log the whole time. Until now the
|
|
// only way to see it was to open /clusters/log and reload, which is the
|
|
// same information and a worse way to get it: the interesting moment is
|
|
// usually the one you reloaded past.
|
|
//
|
|
// The page polls rather than streaming. A build is one writer appending to
|
|
// one file, and a poll that asks for the bytes after the ones it already
|
|
// has is the whole of what server-sent events would give here, without a
|
|
// long-lived connection through Caddy and the proxy to keep alive.
|
|
|
|
// logView is the watch page's data.
|
|
type logView struct {
|
|
pageData
|
|
Cluster store.Cluster
|
|
// Live is false once the cluster has stopped moving, so the page can
|
|
// say "finished" rather than poll a file nobody is writing.
|
|
Live bool
|
|
}
|
|
|
|
// clusterFromQuery resolves ?cluster=N to one the caller may look at.
|
|
func (s *Server) clusterFromQuery(w http.ResponseWriter, r *http.Request) (*store.Cluster, bool) {
|
|
if s.store == nil || s.builder == nil {
|
|
http.NotFound(w, r)
|
|
return nil, false
|
|
}
|
|
id, err := strconv.ParseInt(r.URL.Query().Get("cluster"), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
http.Error(w, "which cluster?", http.StatusBadRequest)
|
|
return nil, false
|
|
}
|
|
live, err := s.store.Live(r.Context())
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return nil, false
|
|
}
|
|
for i := range live {
|
|
if live[i].SerialID == id {
|
|
// Somebody else's build log names their hypervisors and
|
|
// their addresses. Same answer as a cluster that is not
|
|
// there, so a stranger learns nothing either way.
|
|
if !mayAct(r, live[i]) {
|
|
http.NotFound(w, r)
|
|
return nil, false
|
|
}
|
|
return &live[i], true
|
|
}
|
|
}
|
|
http.NotFound(w, r)
|
|
return nil, false
|
|
}
|
|
|
|
// movingStates are the ones worth watching: something is writing.
|
|
func moving(state string) bool {
|
|
return state == "provisioning" || state == "deleting"
|
|
}
|
|
|
|
// pageClusterLog serves the build log, or the part of it the caller does
|
|
// not have yet.
|
|
//
|
|
// ?from=N returns the bytes after N, which is what makes polling cheap: a
|
|
// Terraform log reaches a few hundred kilobytes and re-sending all of it
|
|
// every two seconds to append one line is most of a megabyte a minute for
|
|
// nothing.
|
|
//
|
|
// It stays plain text and remains useful on its own - curl reads it, and
|
|
// so does anyone who would rather tail than watch a page.
|
|
func (s *Server) pageClusterLog(w http.ResponseWriter, r *http.Request) {
|
|
c, ok := s.clusterFromQuery(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
// It names hypervisors and addresses; no reason for it to be cached by
|
|
// anything between here and the browser.
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
// The state is what tells a watcher when to stop asking. Reading it
|
|
// from the record rather than guessing from the log's last line: a
|
|
// build that was killed writes no last line at all.
|
|
w.Header().Set("X-Cluster-State", c.State)
|
|
|
|
f, err := os.Open(s.builder.LogPath(c.SerialID))
|
|
if err != nil {
|
|
// A build that has not written anything yet is not an error. The
|
|
// page is often opened in the same second the build starts.
|
|
w.Header().Set("X-Log-Size", "0")
|
|
return
|
|
}
|
|
defer f.Close()
|
|
|
|
fi, err := f.Stat()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
size := fi.Size()
|
|
w.Header().Set("X-Log-Size", strconv.FormatInt(size, 10))
|
|
|
|
from, _ := strconv.ParseInt(r.URL.Query().Get("from"), 10, 64)
|
|
switch {
|
|
case from < 0 || from > size:
|
|
// The file was replaced - a rebuild truncates it - so whatever
|
|
// the caller has is from a different run. Start again.
|
|
from = 0
|
|
case from == size:
|
|
return
|
|
}
|
|
if _, err := f.Seek(from, io.SeekStart); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
_, _ = io.Copy(w, f)
|
|
}
|
|
|
|
// pageClusterWatch is the window the log runs in.
|
|
func (s *Server) pageClusterWatch(w http.ResponseWriter, r *http.Request) {
|
|
c, ok := s.clusterFromQuery(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
d := logView{pageData: s.base("clusters", r), Cluster: *c, Live: moving(c.State)}
|
|
if err := s.tmpl.ExecuteTemplate(w, "buildlog.html", d); err != nil {
|
|
// The header is already on its way by now; log rather than try to
|
|
// write a status nobody will read.
|
|
fmt.Fprintf(os.Stderr, "buildlog: %v\n", err)
|
|
}
|
|
}
|
|
|
|
//go:embed static/*
|
|
var staticFS embed.FS
|
|
|
|
// staticHandler serves the page's own script. It is a file rather than an
|
|
// inline block because the control plane's Content-Security-Policy is
|
|
// script-src 'self', and loosening that for a log viewer would be the
|
|
// wrong trade.
|
|
func staticHandler() http.Handler {
|
|
sub, err := fs.Sub(staticFS, "static")
|
|
if err != nil {
|
|
panic(err) // the directory is embedded at build time or not at all
|
|
}
|
|
return http.StripPrefix("/static/", http.FileServer(http.FS(sub)))
|
|
}
|