Files
ilkerandClaude Opus 5 8ddbeac006 hpcod: the commands find the database the same way the service does
"hpcod account -list" answered "no database: pass -db or set
$HPCOD_DB_DSN" when run as root on the machine where the database is,
because the DSN lives in /etc/hpcod/db.env and only the systemd unit was
reading it. These commands are the recovery path - the one you reach for
when the portal will not let you in - and a recovery tool that needs an
incantation remembered correctly is not much of one.

They now look at the flag, then the environment, then that file. The error,
when there is genuinely nowhere to look, names all three instead of naming
one and leaving the operator to guess the rest.

An unreadable file is reported separately from an absent one, because it
usually means the command is not running as root and that is worth saying
rather than hiding behind "no database".

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 08:28:18 +02:00

83 lines
2.2 KiB
Go

package main
import (
"context"
"flag"
"fmt"
"strings"
"github.com/ilkermanap/hpcod/internal/store"
)
// state moves a cluster through its lifecycle.
//
// Allocation records a cluster as "requested" and provisioning is a
// separate step, so something has to say when the cluster actually exists.
// Without it the record stays "requested" forever - and anything reading
// the lifecycle, the reaper most of all, would be working from a state that
// was true for about a minute.
func runState(args []string) error {
fs := flag.NewFlagSet("state", flag.ExitOnError)
db := fs.String("db", "", "control plane database DSN; defaults to $HPCOD_DB_DSN")
serial := fs.Int64("cluster", 0, "serial_id of the cluster")
set := fs.String("set", "", "new state: "+strings.Join(clusterStates, ", "))
if err := fs.Parse(args); err != nil {
return err
}
dsn, err := resolveDSN(*db)
if err != nil {
return err
}
switch {
case *serial <= 0:
return fmt.Errorf("-cluster is required")
case *set == "":
return fmt.Errorf("-set is required, one of: %s", strings.Join(clusterStates, ", "))
}
// Checked here as well as by the database, so the error names the
// alternatives instead of quoting a constraint.
if !validState(*set) {
return fmt.Errorf("unknown state %q; expected one of: %s", *set, strings.Join(clusterStates, ", "))
}
ctx := context.Background()
st, err := store.Open(ctx, dsn)
if err != nil {
return err
}
defer st.Close()
if err := st.SetState(ctx, *serial, *set); err != nil {
return err
}
// Read it back: SetState reports rows affected, which is not the same
// as the row now saying what was asked for.
live, err := st.Live(ctx)
if err != nil {
return err
}
for _, c := range live {
if c.SerialID == *serial {
fmt.Printf("%s (cluster %d) is now %s\n", c.AccountingName, c.ClusterID, c.State)
return nil
}
}
return fmt.Errorf("cluster %d is no longer live", *serial)
}
// clusterStates mirrors the CHECK constraint in db/001-schema.sql.
var clusterStates = []string{
"requested", "placed", "provisioning", "ready",
"expiring", "deleting", "deleted", "failed",
}
func validState(s string) bool {
for _, v := range clusterStates {
if v == s {
return true
}
}
return false
}