An installation used to be this one: the deploy scripts and the Ansible defaults carried one site's domain, hypervisor and login, the Proxmox side and the database were set up by hand, and deploying meant copying scripts and a tree of roles beside the binary and keeping them in step with it. The binary now carries the roles, the Terraform module, the schema and the catalogue (embed.go), and installs itself: - hpcod setup: a full-screen installer for administrators. It asks where clusters are built, signs in to Proxmox before asking anything else (root's password once, or tokens), offers nodes and datastores from what Proxmox has, checks every field as it is typed, runs every step with its output live, checks the result and offers a test cluster. -yes runs the same steps from a site file; make deploy uses that. - hpcod verify: each fact checked by using it. - hpcod selftest: the smallest real cluster, by the portal's own path, a job run on it, then taken down. - Closed networks: hpcod serves HTTPS itself from a CA name-constrained to the site's domains, issuing and renewing its own certificate, and answers DNS for the portal and every cluster name. - A simulated Proxmox: Proxmox VE in QEMU on the control plane host, on a private bridge with NAT, for trying it without one. Every step reads back what it changed and is safe to run again. The site's values live in one file; group_vars/all/site.yml is generated from it, and the playbooks refuse to run without it. ci_user and -ssh-user no longer default to a personal account. scripts/B10, B20 and C20 are gone: their work is the setup steps. Proven end to end on a simulated Proxmox (Proxmox VE 9 in a VM on a real host): the install, the Proxmox step by the password path, the head template, verify READY, and a test cluster that ran srun on its compute node and was destroyed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
788 lines
25 KiB
Go
788 lines
25 KiB
Go
// Package httpapi serves the HPC-on-Demand portal.
|
||
//
|
||
// It exposes phases 1 and 2 of the design: what the estate looks like, and
|
||
// where a request would be placed. Nothing here provisions anything - every
|
||
// route is read-only, and /v1/dry-run is a placement decision with no side
|
||
// effects at all.
|
||
//
|
||
// Discovery runs on a timer in the background rather than per request. Polling
|
||
// every backing cloud on each page load would hammer their APIs and make the
|
||
// portal as slow as the slowest site; a cache with an explicit age is both
|
||
// faster and more honest, since the page can say how stale it is.
|
||
package httpapi
|
||
|
||
import (
|
||
"context"
|
||
"crypto/tls"
|
||
"embed"
|
||
"encoding/json"
|
||
"fmt"
|
||
"html/template"
|
||
"io"
|
||
"log"
|
||
"net"
|
||
"net/http"
|
||
"os"
|
||
"sort"
|
||
"strconv"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
|
||
"github.com/ilkermanap/hpcod/internal/capability"
|
||
"github.com/ilkermanap/hpcod/internal/catalog"
|
||
"github.com/ilkermanap/hpcod/internal/inventory"
|
||
"github.com/ilkermanap/hpcod/internal/model"
|
||
"github.com/ilkermanap/hpcod/internal/placement"
|
||
"github.com/ilkermanap/hpcod/internal/provider"
|
||
"github.com/ilkermanap/hpcod/internal/provision"
|
||
"github.com/ilkermanap/hpcod/internal/spec"
|
||
"github.com/ilkermanap/hpcod/internal/store"
|
||
)
|
||
|
||
//go:embed templates/*.html
|
||
var tmplFS embed.FS
|
||
|
||
// Options configures the server.
|
||
type Options struct {
|
||
Listen string
|
||
CatalogPath string
|
||
UserFile string // empty disables auth; only safe on loopback
|
||
TrustedProxy string
|
||
// Tenant portals are served by subdomain off ClusterDomain, resolved
|
||
// through the registry file the provisioner writes. Empty disables
|
||
// tenant routing entirely, which is the state before any tenant is
|
||
// published.
|
||
ClusterRegistry string
|
||
ClusterDomain string
|
||
// ClientCertFile and ClientKeyFile are presented to cluster portals.
|
||
// A portal that requires one accepts the proxy without a password.
|
||
ClientCertFile string
|
||
ClientKeyFile string
|
||
// CatalogPath names the application catalogue. Empty or missing means
|
||
// no applications are published, which is a valid configuration.
|
||
AppCatalogPath string
|
||
// DatabaseDSN is the control plane's own store. Empty leaves the
|
||
// cluster routes answering 503: the portal can still show the estate
|
||
// and plan against it, but nothing can be allocated, because an
|
||
// allocation nobody records is one nothing will ever release.
|
||
DatabaseDSN string
|
||
|
||
// Revision names the build, for /healthz and the startup log. Empty
|
||
// means the caller did not say, which is itself worth seeing.
|
||
Revision string
|
||
|
||
// Provision configures building clusters. With no Template the portal
|
||
// still allocates, and says plainly that it cannot build.
|
||
Provision provision.Options
|
||
|
||
Refresh time.Duration
|
||
SiteTimeout time.Duration
|
||
IdleTimeout time.Duration
|
||
MaxSession time.Duration
|
||
|
||
// TLS, when set, serves the portal itself on TLSListen, with no
|
||
// reverse proxy in front: an installation on a closed network, under
|
||
// a domain no public CA will sign for. HTTPListen then redirects to
|
||
// it, except for /ca.crt - the certificate a browser has to trust
|
||
// first, which cannot be fetched over a connection it does not trust
|
||
// yet. Listen stays as it is, on loopback, for the health checks.
|
||
TLS *tls.Config
|
||
TLSListen string
|
||
HTTPListen string
|
||
CACertFile string
|
||
}
|
||
|
||
type cache struct {
|
||
mu sync.RWMutex
|
||
sites []model.Site
|
||
fetched time.Time
|
||
refreshN int
|
||
}
|
||
|
||
func (c *cache) get() ([]model.Site, time.Time) {
|
||
c.mu.RLock()
|
||
defer c.mu.RUnlock()
|
||
return c.sites, c.fetched
|
||
}
|
||
|
||
func (c *cache) set(s []model.Site) {
|
||
c.mu.Lock()
|
||
defer c.mu.Unlock()
|
||
c.sites, c.fetched = s, time.Now().UTC()
|
||
c.refreshN++
|
||
}
|
||
|
||
// setAt replaces the inventory without claiming it is newer than it is.
|
||
// Applying a policy recomputes what the estate offers; it does not go and
|
||
// look at the estate, and the page says how stale the reading is.
|
||
func (c *cache) setAt(s []model.Site, fetched time.Time) {
|
||
c.mu.Lock()
|
||
defer c.mu.Unlock()
|
||
c.sites, c.fetched = s, fetched
|
||
}
|
||
|
||
// Server is the portal.
|
||
type Server struct {
|
||
opt Options
|
||
provs []provider.Provider
|
||
cache *cache
|
||
tmpl *template.Template
|
||
auth *authStore
|
||
// A watcher rather than the parsed catalogue: it re-reads the file when
|
||
// it changes, so an application published while the portal is running
|
||
// appears without a restart.
|
||
catalog *catalog.Watcher
|
||
store clusterStore
|
||
signups signupLimiter
|
||
builder *provision.Runner
|
||
// clientCert is presented to cluster portals, which accept it in
|
||
// place of asking the person for a password.
|
||
clientCert *tls.Certificate
|
||
// registry maps a cluster to its portal. Shared with the proxy so the
|
||
// page links to exactly what the proxy would route.
|
||
registry *ClusterRegistry
|
||
}
|
||
|
||
func New(opt Options) (*Server, error) {
|
||
// Loaded once here so a catalogue that does not parse stops the portal
|
||
// starting, rather than being discovered on the first plan page. After
|
||
// that the watcher owns it and re-reads it when the file changes, so
|
||
// publishing an application does not need a restart.
|
||
if _, err := catalog.Load(opt.AppCatalogPath); err != nil {
|
||
return nil, err
|
||
}
|
||
apps := catalog.FromFile(opt.AppCatalogPath)
|
||
|
||
provs, err := inventory.Load(opt.CatalogPath)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
t, err := parseTemplates()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
s := &Server{opt: opt, provs: provs, cache: &cache{}, tmpl: t, catalog: apps}
|
||
// The watcher says how many entries it loaded, so saying it again here
|
||
// printed the line twice at every start - which reads as the catalogue
|
||
// having been loaded twice, and is exactly the kind of noise that makes
|
||
// a startup log stop being read.
|
||
apps.Current()
|
||
|
||
if opt.DatabaseDSN != "" {
|
||
db, err := store.Open(context.Background(), opt.DatabaseDSN)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("database: %w", err)
|
||
}
|
||
s.store = db
|
||
log.Printf("control plane database connected")
|
||
if opt.Provision.Template != "" {
|
||
s.builder = provision.NewRunner(db, opt.Provision)
|
||
log.Printf("provisioning enabled: %s from %s",
|
||
opt.Provision.EnvRoot, opt.Provision.Template)
|
||
} else {
|
||
log.Printf("provisioning disabled: no environment template configured")
|
||
}
|
||
} else {
|
||
log.Printf("no database configured - /v1/clusters will refuse to allocate")
|
||
}
|
||
|
||
if opt.ClientCertFile != "" {
|
||
cert, err := tls.LoadX509KeyPair(opt.ClientCertFile, opt.ClientKeyFile)
|
||
if err != nil {
|
||
// Fatal on purpose. Starting without it would leave every
|
||
// cluster portal unreachable and the reason buried in a
|
||
// handshake error per request.
|
||
return nil, fmt.Errorf("proxy client certificate: %w", err)
|
||
}
|
||
s.clientCert = &cert
|
||
log.Printf("cluster portals: signing in with %s", opt.ClientCertFile)
|
||
}
|
||
|
||
if opt.UserFile != "" {
|
||
a, err := newAuthStore(opt.UserFile, opt.IdleTimeout, opt.MaxSession)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("user file %s: %w", opt.UserFile, err)
|
||
}
|
||
if db, ok := s.store.(accountSource); ok && s.store != nil {
|
||
a.accounts = db
|
||
}
|
||
// The session has to reach the cluster subdomains, which are
|
||
// served by this same proxy and now rely on it for their own
|
||
// authentication.
|
||
a.cookieDomain = opt.ClusterDomain
|
||
s.auth = a
|
||
log.Printf("authentication enabled from %s", opt.UserFile)
|
||
} else {
|
||
log.Printf("authentication DISABLED - only safe on loopback")
|
||
}
|
||
return s, nil
|
||
}
|
||
|
||
// parseTemplates is separate so a test can build a Server that renders,
|
||
// without a site catalogue or a database behind it.
|
||
func parseTemplates() (*template.Template, error) {
|
||
return template.New("").Funcs(funcs()).ParseFS(tmplFS, "templates/*.html")
|
||
}
|
||
|
||
// Run refreshes inventory in the background and serves until ctx is done.
|
||
func (s *Server) Run(ctx context.Context) error {
|
||
// Anything that was building when the process before this one stopped
|
||
// is not building now. A deploy restarts this service and systemd
|
||
// kills the whole control group with it, so the goroutine goes and
|
||
// the record stays at "provisioning" - and nothing ever moves it
|
||
// again. Saying so at startup is what puts a Build button back on the
|
||
// page instead of a spinner for a build that is not happening.
|
||
if s.builder != nil {
|
||
n, err := s.builder.Recover(ctx)
|
||
if err != nil {
|
||
// Not fatal: the portal is still worth running, and the
|
||
// records it could not correct are visible on the page.
|
||
log.Printf("recovering interrupted work: %v", err)
|
||
}
|
||
if n > 0 {
|
||
log.Printf("marked %d cluster(s) failed: they were in flight when this process started", n)
|
||
}
|
||
}
|
||
|
||
s.refresh(ctx)
|
||
go func() {
|
||
t := time.NewTicker(s.opt.Refresh)
|
||
defer t.Stop()
|
||
for {
|
||
select {
|
||
case <-ctx.Done():
|
||
return
|
||
case <-t.C:
|
||
s.refresh(ctx)
|
||
}
|
||
}
|
||
}()
|
||
|
||
mux := http.NewServeMux()
|
||
mux.HandleFunc("/healthz", s.healthz)
|
||
mux.HandleFunc("/v1/sites", s.jsonSites)
|
||
mux.HandleFunc("/v1/capacity", s.jsonCapacity)
|
||
mux.HandleFunc("/v1/dry-run", s.jsonDryRun)
|
||
mux.HandleFunc("/v1/clusters", s.jsonClusters)
|
||
mux.HandleFunc("/v1/clusters/", s.jsonCluster)
|
||
mux.HandleFunc("/clusters/new", s.pagePlan)
|
||
mux.HandleFunc("/projects", s.pageProjects)
|
||
mux.HandleFunc("/usage", s.pageUsage)
|
||
// The page was called "Dry run" and lived at /plan, from when it
|
||
// only answered where a cluster would go. It asks for one now, so
|
||
// the old address redirects rather than breaking a bookmark.
|
||
mux.HandleFunc("/plan", func(w http.ResponseWriter, r *http.Request) {
|
||
http.Redirect(w, r, "/clusters/new", http.StatusMovedPermanently)
|
||
})
|
||
mux.HandleFunc("/capmap", s.pageCapMap)
|
||
mux.HandleFunc("/clusters", s.pageClusters)
|
||
mux.HandleFunc("/clusters/log", s.pageClusterLog)
|
||
mux.HandleFunc("/clusters/watch", s.pageClusterWatch)
|
||
mux.Handle("/static/", staticHandler())
|
||
mux.HandleFunc("/sites", s.pageSites)
|
||
mux.HandleFunc("/admin/site-policy", s.setSitePolicy)
|
||
mux.HandleFunc("/signup", s.pageSignup)
|
||
mux.HandleFunc("/admin/accounts", s.pageAccounts)
|
||
// Last, and only for the exact path: ServeMux treats "/" as a catch-all,
|
||
// so without the check every unknown URL would render the landing page
|
||
// and report 200 for something that does not exist.
|
||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||
if r.URL.Path != "/" {
|
||
http.NotFound(w, r)
|
||
return
|
||
}
|
||
s.pageFront(w, r)
|
||
})
|
||
|
||
// The control plane's own responses, and only those. A cluster portal
|
||
// is a different application and sets its own headers; adding these
|
||
// on the way past broke it three ways at once - two
|
||
// Content-Security-Policy headers and two X-Frame-Options, and
|
||
// Referrer-Policy: no-referrer, which makes a browser send
|
||
// "Origin: null" and Rails reject every form with 422.
|
||
var h http.Handler = mux
|
||
if s.auth != nil {
|
||
login, err := tmplFS.ReadFile("templates/login.html")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
h = s.auth.middleware(mux, login, s.opt.TrustedProxy)
|
||
}
|
||
// Wrapped here: everything the control plane serves itself, the login
|
||
// page included, and nothing that is proxied.
|
||
h = securityHeaders(h)
|
||
|
||
// The proxy sits outside those headers. A cluster portal is a
|
||
// different application that sets its own, and adding these on the
|
||
// way past broke it three ways at once: two Content-Security-Policy
|
||
// headers, two X-Frame-Options, and Referrer-Policy: no-referrer -
|
||
// which makes a browser send "Origin: null", so Rails rejected every
|
||
// form with 422.
|
||
if s.opt.ClusterRegistry != "" && s.opt.ClusterDomain != "" {
|
||
reg := NewClusterRegistry(s.opt.ClusterRegistry, s.opt.ClusterDomain)
|
||
s.registry = reg
|
||
h = clusterProxy(reg, s.clientCert, s.mayUseCluster, h)
|
||
log.Printf("cluster portals: *.%s via %s", s.opt.ClusterDomain, s.opt.ClusterRegistry)
|
||
}
|
||
|
||
srv := &http.Server{
|
||
Addr: s.opt.Listen,
|
||
Handler: h,
|
||
ReadHeaderTimeout: 10 * time.Second,
|
||
IdleTimeout: 120 * time.Second,
|
||
}
|
||
go func() {
|
||
<-ctx.Done()
|
||
_ = srv.Close()
|
||
if s.store != nil {
|
||
s.store.Close()
|
||
}
|
||
}()
|
||
|
||
h = s.withCA(h)
|
||
srv.Handler = h
|
||
errs := make(chan error, 3)
|
||
if s.opt.TLS != nil {
|
||
tsrv := &http.Server{
|
||
Addr: s.opt.TLSListen, Handler: h, TLSConfig: s.opt.TLS,
|
||
ReadHeaderTimeout: 10 * time.Second, IdleTimeout: 120 * time.Second,
|
||
}
|
||
go func() {
|
||
log.Printf("hpcod portal listening on https://%s", s.opt.TLSListen)
|
||
errs <- tsrv.ListenAndServeTLS("", "")
|
||
}()
|
||
go func() { <-ctx.Done(); _ = tsrv.Close() }()
|
||
if s.opt.HTTPListen != "" {
|
||
rsrv := &http.Server{
|
||
Addr: s.opt.HTTPListen, Handler: s.withCA(http.HandlerFunc(toHTTPS)),
|
||
ReadHeaderTimeout: 10 * time.Second,
|
||
}
|
||
go func() { errs <- rsrv.ListenAndServe() }()
|
||
go func() { <-ctx.Done(); _ = rsrv.Close() }()
|
||
}
|
||
}
|
||
|
||
log.Printf("hpcod portal listening on http://%s (build %s)", s.opt.Listen, orUnknown(s.opt.Revision))
|
||
go func() { errs <- srv.ListenAndServe() }()
|
||
// The first listener to stop takes the portal with it: a portal that
|
||
// lost its HTTPS listener and kept its loopback one would pass every
|
||
// health check and serve nobody.
|
||
if err := <-errs; err != nil && err != http.ErrServerClosed {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// withCA serves the installation's CA certificate at /ca.crt, to anyone:
|
||
// it is public by nature, and it is what a user needs before they can
|
||
// trust anything else here.
|
||
func (s *Server) withCA(next http.Handler) http.Handler {
|
||
if s.opt.CACertFile == "" {
|
||
return next
|
||
}
|
||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
if r.URL.Path != "/ca.crt" {
|
||
next.ServeHTTP(w, r)
|
||
return
|
||
}
|
||
raw, err := os.ReadFile(s.opt.CACertFile)
|
||
if err != nil {
|
||
http.Error(w, "no CA certificate here", http.StatusNotFound)
|
||
return
|
||
}
|
||
w.Header().Set("Content-Type", "application/x-x509-ca-cert")
|
||
w.Header().Set("Content-Disposition", `attachment; filename="hpcod-ca.crt"`)
|
||
_, _ = w.Write(raw)
|
||
})
|
||
}
|
||
|
||
// toHTTPS sends plain HTTP to the same place over HTTPS.
|
||
func toHTTPS(w http.ResponseWriter, r *http.Request) {
|
||
host := r.Host
|
||
if h, _, err := net.SplitHostPort(host); err == nil {
|
||
host = h
|
||
}
|
||
http.Redirect(w, r, "https://"+host+r.URL.RequestURI(), http.StatusMovedPermanently)
|
||
}
|
||
|
||
func orUnknown(s string) string {
|
||
if s == "" {
|
||
return "unknown"
|
||
}
|
||
return s
|
||
}
|
||
|
||
func (s *Server) refresh(ctx context.Context) {
|
||
start := time.Now()
|
||
sites := inventory.DiscoverAll(ctx, s.provs, s.opt.SiteTimeout)
|
||
|
||
// Policy is applied here rather than in the provider, so changing it
|
||
// takes effect on the next sweep instead of at the next restart. It
|
||
// recomputes from the physical figures, so it does not compound with
|
||
// the default the provider has already applied.
|
||
if s.store != nil {
|
||
if policies, err := s.store.Policies(ctx); err != nil {
|
||
log.Printf("discovery: reading site policy: %v", err)
|
||
} else {
|
||
model.ApplyOvercommit(sites, policies)
|
||
}
|
||
}
|
||
s.cache.set(sites)
|
||
|
||
up := 0
|
||
for _, x := range sites {
|
||
if x.Reachable {
|
||
up++
|
||
}
|
||
}
|
||
log.Printf("discovery: %d/%d sites reachable in %s", up, len(sites), time.Since(start).Round(time.Millisecond))
|
||
}
|
||
|
||
// ---------- JSON ----------
|
||
|
||
func (s *Server) healthz(w http.ResponseWriter, _ *http.Request) {
|
||
sites, at := s.cache.get()
|
||
writeJSON(w, http.StatusOK, map[string]any{
|
||
"status": "ok",
|
||
// Which build is answering. Without it, "did that deploy land?" can
|
||
// only be answered by comparing checksums, and two builds of the
|
||
// same commit differ because Go embeds the source path.
|
||
"revision": s.opt.Revision,
|
||
"sites": len(sites),
|
||
"inventory_age_s": int(time.Since(at).Seconds()),
|
||
})
|
||
}
|
||
|
||
func (s *Server) jsonSites(w http.ResponseWriter, _ *http.Request) {
|
||
sites, at := s.cache.get()
|
||
writeJSON(w, http.StatusOK, map[string]any{"fetched_at": at, "sites": sites})
|
||
}
|
||
|
||
func (s *Server) jsonCapacity(w http.ResponseWriter, _ *http.Request) {
|
||
sites, at := s.cache.get()
|
||
out := make([]map[string]any, 0, len(sites))
|
||
for _, x := range sites {
|
||
out = append(out, map[string]any{
|
||
"site": x.ID,
|
||
"reachable": x.Reachable,
|
||
"free_cores": x.TotalFreeCores(),
|
||
"free_mem_gib": x.TotalFreeMemBytes() / (1 << 30),
|
||
"schedulable": x.Capabilities(),
|
||
"claimed": x.Claimed(),
|
||
})
|
||
}
|
||
writeJSON(w, http.StatusOK, map[string]any{"fetched_at": at, "capacity": out})
|
||
}
|
||
|
||
// jsonDryRun answers "where would this go, and why not elsewhere" without
|
||
// touching a single cloud.
|
||
func (s *Server) jsonDryRun(w http.ResponseWriter, r *http.Request) {
|
||
if r.Method != http.MethodPost {
|
||
w.Header().Set("Allow", "POST")
|
||
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "POST a YAML request body"})
|
||
return
|
||
}
|
||
body, err := io.ReadAll(io.LimitReader(r.Body, 256<<10))
|
||
if err != nil {
|
||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||
return
|
||
}
|
||
req, err := spec.Parse(body)
|
||
if err != nil {
|
||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||
return
|
||
}
|
||
|
||
// Exactly the path a real creation takes - applications folded in,
|
||
// reservations subtracted, then planned. A dry run that took a shorter
|
||
// route would answer a different question from the one it is asked.
|
||
_, at := s.cache.get()
|
||
_, apps, d, err := s.decide(r.Context(), req)
|
||
if err != nil {
|
||
writeJSON(w, statusFor(err), map[string]string{"error": err.Error()})
|
||
return
|
||
}
|
||
|
||
resolved := make([]map[string]any, 0, len(apps))
|
||
for _, a := range apps {
|
||
resolved = append(resolved, map[string]any{
|
||
"ref": a.Ref(), "image": a.Image,
|
||
"requires": a.Requires, "size_gb": a.SizeGB, "signed": a.Signed,
|
||
})
|
||
}
|
||
writeJSON(w, http.StatusOK, map[string]any{
|
||
"inventory_fetched_at": at,
|
||
"decision": d,
|
||
"apps": resolved,
|
||
})
|
||
}
|
||
|
||
// ---------- HTML ----------
|
||
|
||
type pageData struct {
|
||
Page string
|
||
Sites []model.Site
|
||
FetchedAt time.Time
|
||
Age string
|
||
CapMap []capability.Entry
|
||
Request string
|
||
Decision *placement.Decision
|
||
Clusters []clusterView
|
||
Overview *overview
|
||
Accounts []store.Account
|
||
Policies []sitePolicyView
|
||
CanBuild bool
|
||
// What the New cluster form offers and what is currently in it.
|
||
Caps []capabilityRow
|
||
AppList []appChoice
|
||
Projects []store.Project
|
||
Usage []store.Usage
|
||
MemNote string
|
||
CanPickTenant bool
|
||
// Form carries what someone typed back to a page that is redisplaying
|
||
// itself with an error, so a rejected form does not make them start over.
|
||
Form map[string]string
|
||
Done bool
|
||
CanCreate bool
|
||
IsAdmin bool
|
||
Err string
|
||
}
|
||
|
||
// base takes the request so the navigation can be built from the session
|
||
// rather than from whatever the page remembered to pass. An Accounts link
|
||
// shown to someone who cannot use it is a small thing; a page that forgets
|
||
// to hide it is how an admin-only route gets advertised to everyone.
|
||
func (s *Server) base(page string, r *http.Request) pageData {
|
||
sites, at := s.cache.get()
|
||
return pageData{
|
||
Page: page, Sites: sites, FetchedAt: at,
|
||
Age: humanAge(time.Since(at)),
|
||
IsAdmin: isAdmin(r.Context()),
|
||
}
|
||
}
|
||
|
||
func (s *Server) render(w http.ResponseWriter, name string, d pageData) {
|
||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||
if err := s.tmpl.ExecuteTemplate(w, name, d); err != nil {
|
||
log.Printf("template %s: %v", name, err)
|
||
}
|
||
}
|
||
|
||
func (s *Server) pageSites(w http.ResponseWriter, r *http.Request) {
|
||
// No path check here any more. This used to be the catch-all at "/" and
|
||
// carried its own guard; now it is registered at an exact path and the
|
||
// guard rejected every request to it.
|
||
d := s.base("sites", r)
|
||
d.Policies = s.sitePolicies(r)
|
||
s.render(w, "sites.html", d)
|
||
}
|
||
|
||
func (s *Server) pageCapMap(w http.ResponseWriter, r *http.Request) {
|
||
d := s.base("capmap", r)
|
||
d.CapMap = capability.Table()
|
||
s.render(w, "capmap.html", d)
|
||
}
|
||
|
||
// pagePlan answers "where would this go" and, when the answer is
|
||
// somewhere, lets the person say yes.
|
||
//
|
||
// Until now it planned and threw the decision away, which is the same gap
|
||
// the API had: the portal could work out that a cluster was possible and
|
||
// offered no way to have one. Asking and agreeing belong on one page,
|
||
// because the thing you are agreeing to is the answer you were just shown.
|
||
func (s *Server) pagePlan(w http.ResponseWriter, r *http.Request) {
|
||
d := s.base("new", r)
|
||
d.MemNote = memoryNote()
|
||
d.CanPickTenant = isAdmin(r.Context())
|
||
|
||
form := defaultPlanForm()
|
||
show := func() {
|
||
d.Form = form.values
|
||
d.Caps = capabilityRows(form)
|
||
d.AppList = appChoices(s.catalog.Current(), form.values["apps"])
|
||
d.Projects = s.openProjects(r)
|
||
s.render(w, "plan.html", d)
|
||
}
|
||
|
||
if r.Method != http.MethodPost {
|
||
show()
|
||
return
|
||
}
|
||
if err := r.ParseForm(); err != nil {
|
||
d.Err = err.Error()
|
||
show()
|
||
return
|
||
}
|
||
|
||
// The second POST - "yes, build it" - carries the planned request
|
||
// itself rather than the fields it was built from, so what is created
|
||
// is by construction what was shown.
|
||
var req spec.Request
|
||
var err error
|
||
if planned := r.PostFormValue("request"); planned != "" {
|
||
req, err = spec.Parse([]byte(planned))
|
||
form = formFromRequest(r)
|
||
} else {
|
||
form = formFromRequest(r)
|
||
req, err = form.request()
|
||
}
|
||
if err != nil {
|
||
d.Err = err.Error()
|
||
show()
|
||
return
|
||
}
|
||
|
||
// The same path the API takes - applications folded in, reservations
|
||
// subtracted. It used to call placement directly and so planned against
|
||
// hardware that was already promised to somebody.
|
||
req, apps, dec, err := s.decide(r.Context(), req)
|
||
if err != nil {
|
||
d.Err = err.Error()
|
||
show()
|
||
return
|
||
}
|
||
d.Decision = &dec
|
||
d.CanCreate = s.store != nil && userFromContext(r.Context()) != ""
|
||
if d.Request, err = form.asYAML(req); err != nil {
|
||
d.Err = err.Error()
|
||
show()
|
||
return
|
||
}
|
||
|
||
if r.PostFormValue("do") != "create" {
|
||
show()
|
||
return
|
||
}
|
||
|
||
// Optional, and from the form rather than the request document: a
|
||
// project is a fact about who is accountable, not about what hardware
|
||
// is wanted. Empty means none. The store checks it is this tenant's
|
||
// and still open, inside the allocating transaction.
|
||
var projectID *int64
|
||
if v := strings.TrimSpace(r.PostFormValue("project")); v != "" && v != "0" {
|
||
n, perr := strconv.ParseInt(v, 10, 64)
|
||
if perr != nil {
|
||
d.Err = "which project?"
|
||
show()
|
||
return
|
||
}
|
||
projectID = &n
|
||
}
|
||
|
||
c, err := s.createFromPlan(r, req, apps, dec, projectID)
|
||
if err != nil {
|
||
d.Err = err.Error()
|
||
show()
|
||
return
|
||
}
|
||
log.Printf("cluster %s created from the plan page by %s", c.AccountingName, c.TenantName)
|
||
|
||
// 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 starts the build and
|
||
// opens the log, which is the only thing worth looking at for the
|
||
// next few minutes.
|
||
//
|
||
// A portal with no provisioner still records the request - that is
|
||
// all it can do - and says so by landing on the list instead.
|
||
if s.builder != nil {
|
||
if err := s.builder.Start(*c); err != nil {
|
||
// The record exists and the build did not start. Sending
|
||
// them to the list, where the cluster is "requested" with a
|
||
// Build button, is more use than an error page about a
|
||
// cluster that is nonetheless there.
|
||
log.Printf("cluster %s: build did not start: %v", c.AccountingName, err)
|
||
http.Redirect(w, r, "/clusters", http.StatusSeeOther)
|
||
return
|
||
}
|
||
log.Printf("cluster %s: build started from the plan page by %s", c.AccountingName, c.TenantName)
|
||
http.Redirect(w, r, fmt.Sprintf("/clusters/watch?cluster=%d", c.SerialID), http.StatusSeeOther)
|
||
return
|
||
}
|
||
http.Redirect(w, r, "/clusters", http.StatusSeeOther)
|
||
}
|
||
|
||
// ---------- helpers ----------
|
||
|
||
func writeJSON(w http.ResponseWriter, code int, v any) {
|
||
w.Header().Set("Content-Type", "application/json")
|
||
w.WriteHeader(code)
|
||
enc := json.NewEncoder(w)
|
||
enc.SetIndent("", " ")
|
||
_ = enc.Encode(v)
|
||
}
|
||
|
||
func securityHeaders(next http.Handler) http.Handler {
|
||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
h := w.Header()
|
||
h.Set("X-Content-Type-Options", "nosniff")
|
||
h.Set("X-Frame-Options", "DENY")
|
||
h.Set("Referrer-Policy", "no-referrer")
|
||
h.Set("Content-Security-Policy",
|
||
"default-src 'self'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; "+
|
||
"font-src https://fonts.gstatic.com; script-src 'self'; img-src 'self' data:; "+
|
||
"form-action 'self'; frame-ancestors 'none'; base-uri 'none'")
|
||
next.ServeHTTP(w, r)
|
||
})
|
||
}
|
||
|
||
func humanAge(d time.Duration) string {
|
||
switch {
|
||
case d < time.Minute:
|
||
return fmt.Sprintf("%ds ago", int(d.Seconds()))
|
||
case d < time.Hour:
|
||
return fmt.Sprintf("%dm ago", int(d.Minutes()))
|
||
default:
|
||
return fmt.Sprintf("%.1fh ago", d.Hours())
|
||
}
|
||
}
|
||
|
||
func funcs() template.FuncMap {
|
||
return template.FuncMap{
|
||
// Wall time as somebody reads it off a page: "2h 09m", not
|
||
// "2h9m40.123456789s". The seconds matter to nobody looking at a
|
||
// cluster that ran for two hours.
|
||
"shortdur": func(d time.Duration) string {
|
||
switch {
|
||
case d >= 24*time.Hour:
|
||
return fmt.Sprintf("%dd %02dh", int(d.Hours())/24, int(d.Hours())%24)
|
||
case d >= time.Hour:
|
||
return fmt.Sprintf("%dh %02dm", int(d.Hours()), int(d.Minutes())%60)
|
||
case d >= time.Minute:
|
||
return fmt.Sprintf("%dm", int(d.Minutes()))
|
||
default:
|
||
return fmt.Sprintf("%ds", int(d.Seconds()))
|
||
}
|
||
},
|
||
"gib": func(b int64) string {
|
||
if b <= 0 {
|
||
return "0"
|
||
}
|
||
return fmt.Sprintf("%.0f GiB", float64(b)/(1<<30))
|
||
},
|
||
"caps": func(m map[model.Capability]int) []string {
|
||
out := make([]string, 0, len(m))
|
||
for k, v := range m {
|
||
out = append(out, fmt.Sprintf("%s ×%d", k, v))
|
||
}
|
||
sort.Strings(out)
|
||
return out
|
||
},
|
||
"unconfirmed": func(s model.Site) []string {
|
||
adv, claimed := s.Capabilities(), s.Claimed()
|
||
var out []string
|
||
for c, n := range claimed {
|
||
if d := n - adv[c]; d > 0 {
|
||
out = append(out, fmt.Sprintf("%s ×%d", c, d))
|
||
}
|
||
}
|
||
sort.Strings(out)
|
||
return out
|
||
},
|
||
"trusted": func(d model.Device) bool { return d.Capability.Trusted(d.Confidence) },
|
||
"freecores": func(n model.Node) int { return n.FreeCores() },
|
||
"freemem": func(n model.Node) int64 { return n.FreeMemBytes() },
|
||
}
|
||
}
|