Rollcall: bare-metal commissioning server, agent, image build, lab and CI
Go server (API, embedded web UI, CLI) backed by PostgreSQL with AES-GCM sealed BMC secrets; Debian live commissioning agent and live-build recipe; Proxmox lab with pve-vbmc virtual BMCs; Gitea workflows for CI, releases and deployment; project website. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
commit
d9ae89b295
33 files changed
+3248
No files matched your search
@@ -0,0 +1,203 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
const schema = `
|
||||
CREATE TABLE IF NOT EXISTS machines (
|
||||
id text PRIMARY KEY,
|
||||
uuid text,
|
||||
serial text,
|
||||
hostname text,
|
||||
manufacturer text,
|
||||
product text,
|
||||
state text NOT NULL DEFAULT 'new',
|
||||
first_seen timestamptz NOT NULL DEFAULT now(),
|
||||
last_seen timestamptz,
|
||||
commissioned_at timestamptz,
|
||||
macs jsonb NOT NULL DEFAULT '[]',
|
||||
bmc jsonb,
|
||||
bmc_secret bytea,
|
||||
inventory jsonb
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS machines_uuid ON machines(uuid);
|
||||
CREATE INDEX IF NOT EXISTS machines_macs ON machines USING gin(macs);
|
||||
CREATE TABLE IF NOT EXISTS events (
|
||||
id bigserial PRIMARY KEY,
|
||||
ts timestamptz NOT NULL DEFAULT now(),
|
||||
machine_id text NOT NULL,
|
||||
event text NOT NULL,
|
||||
detail text
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS events_machine ON events(machine_id, ts);
|
||||
`
|
||||
|
||||
var errNotFound = errors.New("not found")
|
||||
|
||||
type Store struct{ pool *pgxpool.Pool }
|
||||
|
||||
type Machine struct {
|
||||
ID string `json:"id"`
|
||||
UUID *string `json:"uuid"`
|
||||
Serial *string `json:"serial"`
|
||||
Hostname *string `json:"hostname"`
|
||||
Manufacturer *string `json:"manufacturer"`
|
||||
Product *string `json:"product"`
|
||||
State string `json:"state"`
|
||||
FirstSeen time.Time `json:"first_seen"`
|
||||
LastSeen *time.Time `json:"last_seen"`
|
||||
CommissionedAt *time.Time `json:"commissioned_at"`
|
||||
MACs []string `json:"macs"`
|
||||
BMC json.RawMessage `json:"bmc,omitempty"`
|
||||
Inventory json.RawMessage `json:"inventory,omitempty"`
|
||||
bmcSecret []byte
|
||||
}
|
||||
|
||||
type Event struct {
|
||||
TS time.Time `json:"ts"`
|
||||
Event string `json:"event"`
|
||||
Detail string `json:"detail"`
|
||||
}
|
||||
|
||||
func openStore(ctx context.Context, url string) (*Store, error) {
|
||||
pool, err := pgxpool.New(ctx, url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := pool.Exec(ctx, schema); err != nil {
|
||||
return nil, fmt.Errorf("schema: %w", err)
|
||||
}
|
||||
return &Store{pool: pool}, nil
|
||||
}
|
||||
|
||||
const machineCols = `id, uuid, serial, hostname, manufacturer, product, state, first_seen, last_seen,
|
||||
commissioned_at, macs, bmc, bmc_secret, inventory`
|
||||
|
||||
func scanMachine(row pgx.Row, full bool) (*Machine, error) {
|
||||
m := &Machine{}
|
||||
var macs []byte
|
||||
err := row.Scan(&m.ID, &m.UUID, &m.Serial, &m.Hostname, &m.Manufacturer, &m.Product, &m.State,
|
||||
&m.FirstSeen, &m.LastSeen, &m.CommissionedAt, &macs, &m.BMC, &m.bmcSecret, &m.Inventory)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, errNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_ = json.Unmarshal(macs, &m.MACs)
|
||||
if !full {
|
||||
m.Inventory = nil
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (s *Store) List(ctx context.Context) ([]*Machine, error) {
|
||||
rows, err := s.pool.Query(ctx, `SELECT `+machineCols+` FROM machines ORDER BY first_seen`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []*Machine
|
||||
for rows.Next() {
|
||||
m, err := scanMachine(rows, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
if out == nil {
|
||||
out = []*Machine{}
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) Get(ctx context.Context, id string) (*Machine, error) {
|
||||
return scanMachine(s.pool.QueryRow(ctx, `SELECT `+machineCols+` FROM machines WHERE id=$1`, id), true)
|
||||
}
|
||||
|
||||
func (s *Store) Events(ctx context.Context, id string) ([]Event, error) {
|
||||
rows, err := s.pool.Query(ctx, `SELECT ts, event, coalesce(detail,'') FROM events WHERE machine_id=$1 ORDER BY ts`, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
evs := []Event{}
|
||||
for rows.Next() {
|
||||
var e Event
|
||||
if err := rows.Scan(&e.TS, &e.Event, &e.Detail); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
evs = append(evs, e)
|
||||
}
|
||||
return evs, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) AddEvent(ctx context.Context, id, event, detail string) error {
|
||||
_, err := s.pool.Exec(ctx, `INSERT INTO events(machine_id, event, detail) VALUES ($1,$2,$3)`, id, event, detail)
|
||||
return err
|
||||
}
|
||||
|
||||
// UpsertCommissioned stores a fresh inventory; returns true when the machine was new.
|
||||
func (s *Store) UpsertCommissioned(ctx context.Context, m *Machine, secret []byte) (bool, error) {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
macs, _ := json.Marshal(m.MACs)
|
||||
tag, err := tx.Exec(ctx, `INSERT INTO machines(id, state) VALUES ($1,'new') ON CONFLICT (id) DO NOTHING`, m.ID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
isNew := tag.RowsAffected() == 1
|
||||
if isNew {
|
||||
if _, err := tx.Exec(ctx, `INSERT INTO events(machine_id, event) VALUES ($1,'enlisted')`, m.ID); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
_, err = tx.Exec(ctx, `UPDATE machines SET uuid=$2, serial=$3, hostname=$4, manufacturer=$5, product=$6,
|
||||
state='ready', last_seen=now(), commissioned_at=now(), macs=$7, bmc=$8, bmc_secret=$9, inventory=$10 WHERE id=$1`,
|
||||
m.ID, m.UUID, m.Serial, m.Hostname, m.Manufacturer, m.Product, macs, m.BMC, secret, m.Inventory)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return isNew, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) SetState(ctx context.Context, id, state string) error {
|
||||
tag, err := s.pool.Exec(ctx, `UPDATE machines SET state=$2 WHERE id=$1`, id, state)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return errNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) Delete(ctx context.Context, id string) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err := tx.Exec(ctx, `DELETE FROM events WHERE machine_id=$1`, id); err != nil {
|
||||
return err
|
||||
}
|
||||
tag, err := tx.Exec(ctx, `DELETE FROM machines WHERE id=$1`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return errNotFound
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
Reference in new issue
Block a user