admin control features and admin dashboard

This commit is contained in:
ayurishchev committed 2026-08-23 20:39:22 +03:00
1 parent c630f13c57
commit 37910e410b
69 files changed
+4959 -400

No files matched your search

+73
View File
@@ -148,6 +148,79 @@ func (d *DB) FreeValidator(ctx context.Context, validatorID string) error {
return err
}
// AdminCreateValidator registers a brand-new validator via the admin API.
// Unlike RegisterValidator (used by the agent's self-registration call),
// this refuses to upsert over an existing row.
func (d *DB) AdminCreateValidator(ctx context.Context, validatorID, osPortID string) error {
var exists int
err := d.QueryRowContext(ctx, `SELECT 1 FROM validators WHERE validator_id=?`, validatorID).Scan(&exists)
if err != nil && err != sql.ErrNoRows {
return err
}
if err == nil {
return fmt.Errorf("validator %q: %w", validatorID, ErrConflict)
}
now := timeToDB(Now())
_, err = d.ExecContext(ctx, `
INSERT INTO validators (validator_id, hostname, os_port_id, agent_version, state, created_at, updated_at)
VALUES (?, '', ?, '', ?, ?, ?)
`, validatorID, osPortID, ValidatorIdle, now, now)
if err != nil {
return fmt.Errorf("create validator: %w", err)
}
return nil
}
// AdminUpdateValidatorPort updates an existing validator's Neutron port ID.
func (d *DB) AdminUpdateValidatorPort(ctx context.Context, validatorID, osPortID string) error {
res, err := d.ExecContext(ctx, `
UPDATE validators SET os_port_id=?, updated_at=? WHERE validator_id=?
`, osPortID, timeToDB(Now()), validatorID)
if err != nil {
return fmt.Errorf("update validator port: %w", err)
}
if n, _ := res.RowsAffected(); n == 0 {
return fmt.Errorf("validator %q: %w", validatorID, ErrNotFound)
}
return nil
}
// DeleteValidator removes a validator, refusing if it currently owns an IP.
// ip_queue.owner_validator_id is a permanent historical record (set once an
// IP is claimed, never cleared on completion — see RequeueOrFail/
// ReleaseFIP), so any validator that has ever processed an IP would
// otherwise violate the FK constraint on delete; those historical
// references are cleared in the same transaction once we've confirmed the
// validator isn't currently busy.
func (d *DB) DeleteValidator(ctx context.Context, validatorID string) error {
tx, err := d.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
var currentIPID sql.NullInt64
err = tx.QueryRowContext(ctx, `SELECT current_ip_id FROM validators WHERE validator_id=?`, validatorID).Scan(&currentIPID)
if err == sql.ErrNoRows {
return fmt.Errorf("validator %q: %w", validatorID, ErrNotFound)
}
if err != nil {
return err
}
if currentIPID.Valid {
return fmt.Errorf("validator %q owns ip_id %d: %w", validatorID, currentIPID.Int64, ErrBusy)
}
if _, err := tx.ExecContext(ctx, `UPDATE ip_queue SET owner_validator_id=NULL WHERE owner_validator_id=?`, validatorID); err != nil {
return fmt.Errorf("clear historical ip_queue references: %w", err)
}
if _, err := tx.ExecContext(ctx, `DELETE FROM validators WHERE validator_id=?`, validatorID); err != nil {
return fmt.Errorf("delete validator: %w", err)
}
return tx.Commit()
}
type rowScanner interface {
Scan(dest ...interface{}) error
}