Files
ilkerandClaude Opus 5.5 772579648c hpcod: one binary installs, checks and tests the control plane
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>
2026-11-16 18:20:04 +01:00

220 lines
6.6 KiB
Go

package setup
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
)
// fakePVE answers the handful of API calls setup makes, the way Proxmox
// does: a ticket for the right password, CSRF required on writes, data
// wrapped in {"data": ...}, refusals in the status line.
type fakePVE struct {
mu sync.Mutex
content map[string]string // storage -> content list
tokens map[string]bool // "user!id"
deleted []string
zones map[string]bool
volumes []string
csrfMiss bool
polls int
}
func newFakePVE() *fakePVE {
return &fakePVE{
content: map[string]string{"local": "iso,vztmpl,backup", "local-zfs": "images,rootdir"},
tokens: map[string]bool{"root@pam!hpcod-tf": true},
zones: map[string]bool{},
}
}
func (f *fakePVE) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.mu.Lock()
defer f.mu.Unlock()
data := func(v any) { _ = json.NewEncoder(w).Encode(map[string]any{"data": v}) }
path := strings.TrimPrefix(r.URL.Path, "/api2/json")
_ = r.ParseForm()
if path == "/access/ticket" {
if r.PostFormValue("username") != "root@pam" || r.PostFormValue("password") != "right" {
http.Error(w, "authentication failure", http.StatusUnauthorized)
return
}
data(map[string]string{"ticket": "T", "CSRFPreventionToken": "C"})
return
}
authed := false
if c, err := r.Cookie("PVEAuthCookie"); err == nil && c.Value == "T" {
authed = true
if r.Method != http.MethodGet && r.Header.Get("CSRFPreventionToken") != "C" {
f.csrfMiss = true
http.Error(w, "Permission check failed", http.StatusUnauthorized)
return
}
}
if strings.HasPrefix(r.Header.Get("Authorization"), "PVEAPIToken=root@pam!hpcod-tf=") {
authed = true
}
if !authed {
http.Error(w, "no ticket", http.StatusUnauthorized)
return
}
switch {
case path == "/version":
data(map[string]string{"version": "8.4.1"})
case path == "/nodes":
data([]map[string]string{{"node": "hv01", "status": "online"}})
case path == "/storage" && r.Method == http.MethodGet:
data([]map[string]string{
{"storage": "local", "type": "dir", "content": f.content["local"]},
{"storage": "local-zfs", "type": "zfspool", "content": f.content["local-zfs"]}})
case strings.HasPrefix(path, "/storage/"):
id := strings.TrimPrefix(path, "/storage/")
if r.Method == http.MethodPut {
f.content[id] = r.PostFormValue("content")
}
data(map[string]string{"content": f.content[id]})
case path == "/cluster/sdn/zones" && r.Method == http.MethodGet:
data([]any{})
case strings.Contains(path, "/token/"):
id := strings.Replace(strings.TrimPrefix(path, "/access/users/"), "/token/", "!", 1)
switch r.Method {
case http.MethodDelete:
f.deleted = append(f.deleted, id)
delete(f.tokens, id)
data(nil)
case http.MethodPost:
if f.tokens[id] {
http.Error(w, "Token already exists", http.StatusBadRequest)
return
}
f.tokens[id] = true
data(map[string]string{"full-tokenid": id, "value": "SECRET"})
}
case strings.HasSuffix(path, "/download-url"):
data("UPID:hv01:dl")
case strings.Contains(path, "/tasks/"):
f.polls++
if f.polls < 2 {
data(map[string]string{"status": "running"})
return
}
f.volumes = append(f.volumes, "local:import/x.qcow2")
data(map[string]string{"status": "stopped", "exitstatus": "OK"})
default:
http.NotFound(w, r)
}
}
func fakeSite(url string) Site {
s := goodSite()
s["PVE_ENDPOINT"] = url
s["PVE_INSECURE_TLS"] = "true"
return s
}
func TestProbeWithPassword(t *testing.T) {
f := newFakePVE()
srv := httptest.NewTLSServer(f)
defer srv.Close()
p, err := ProbeProxmox(context.Background(), fakeSite(srv.URL), Credentials{Password: "right"})
if err != nil {
t.Fatal(err)
}
if p.Major != 8 || len(p.Nodes) != 1 || len(p.Storages) != 2 {
t.Errorf("probe: %+v", p)
}
if !p.Storages[1].Holds("images") {
t.Errorf("local-zfs should hold images: %+v", p.Storages[1])
}
}
// The first thing an operator sees when they mistype the password.
func TestProbeExplainsAWrongPassword(t *testing.T) {
srv := httptest.NewTLSServer(newFakePVE())
defer srv.Close()
_, err := ProbeProxmox(context.Background(), fakeSite(srv.URL), Credentials{Password: "wrong"})
if err == nil || !strings.Contains(err.Error(), "refused the credentials") {
t.Fatalf("want a plain explanation, got %v", err)
}
}
func TestProbeExplainsAnUntrustedCertificate(t *testing.T) {
srv := httptest.NewTLSServer(newFakePVE())
defer srv.Close()
s := fakeSite(srv.URL)
s["PVE_INSECURE_TLS"] = "false"
_, err := ProbeProxmox(context.Background(), s, Credentials{Password: "right"})
if err == nil || !strings.Contains(err.Error(), "self-signed") {
t.Fatalf("want advice about the certificate, got %v", err)
}
}
// A token's secret is shown once. One that exists is replaced, not
// reported as a conflict, and writes carry the CSRF token.
func TestCreateTokenReplacesAnExistingOne(t *testing.T) {
f := newFakePVE()
srv := httptest.NewTLSServer(f)
defer srv.Close()
p, err := PVEWithPassword(context.Background(), srv.URL, true, "root@pam", "right")
if err != nil {
t.Fatal(err)
}
tok, err := p.CreateToken(context.Background(), "root@pam", "hpcod-tf", "x")
if err != nil {
t.Fatal(err)
}
if tok != "root@pam!hpcod-tf=SECRET" {
t.Errorf("token %q", tok)
}
if len(f.deleted) != 1 || f.csrfMiss {
t.Errorf("deleted %v, csrf missing %v", f.deleted, f.csrfMiss)
}
}
// Proxmox replaces the content list it is given. Adding snippets must keep
// what the datastore already held.
func TestAddContentKeepsWhatWasThere(t *testing.T) {
f := newFakePVE()
srv := httptest.NewTLSServer(f)
defer srv.Close()
p := PVEWithToken(srv.URL, true, "root@pam!hpcod-tf=SECRET")
if err := addContent(context.Background(), p, "local", "snippets"); err != nil {
t.Fatal(err)
}
if got := f.content["local"]; got != "iso,vztmpl,backup,snippets" {
t.Errorf("content is now %q", got)
}
// Already there: nothing written.
if err := addContent(context.Background(), p, "local", "snippets"); err != nil {
t.Fatal(err)
}
if got := f.content["local"]; got != "iso,vztmpl,backup,snippets" {
t.Errorf("a second add changed it to %q", got)
}
}
func TestWaitTaskFollowsToTheEnd(t *testing.T) {
f := newFakePVE()
srv := httptest.NewTLSServer(f)
defer srv.Close()
p := PVEWithToken(srv.URL, true, "root@pam!hpcod-tf=SECRET")
var upid string
if err := p.Post(context.Background(), "/nodes/hv01/storage/local/download-url", nil, &upid); err != nil {
t.Fatal(err)
}
if err := p.WaitTask(context.Background(), "hv01", upid, time.Minute); err != nil {
t.Fatal(err)
}
if f.polls < 2 {
t.Errorf("returned after %d poll(s), before the task stopped", f.polls)
}
}