package provision import ( "context" "errors" "fmt" "log/slog" "net/http" "net/mail" "slices" "strings" "time" "amadan.net/oficina/alta/console" "amadan.net/oficina/alta/homeop" ) // Config is everything the engine reaches. type Config struct { Store *Store Home *homeop.Client Console *console.Client // Public is the client for the team instances' own origins // (/api/version, /api/team). Public *http.Client HomeCfg HomeConfig Account string // CallsZone is CALLS_ZONE for a team's Calls. CallsZone string Now func() time.Time Logger *slog.Logger // AfterAct, when set, runs after every act has taken effect remotely // and before Alta records it. A test returns an error from it to // simulate a crash at exactly that point (§12). AfterAct func(key string) error // Debug, when set, sees errors that are neither a park nor a fail // (tests only: they may carry text the logs must not). Debug func(error) // Observer re-observes an adopted app before its activation // (existing-team-adoption.md). The served alta has none, so it never // activates an adopted app. Observer Observer } // Engine runs §6's state machine. Every step is probe → act → verify → // record; a crash resumes by probing; nothing is inferred from a timeout; // nothing is deleted to "clean up". type Engine struct{ cfg Config } func New(cfg Config) *Engine { if cfg.Now == nil { cfg.Now = time.Now } if cfg.Logger == nil { cfg.Logger = slog.Default() } if cfg.Public == nil { cfg.Public = &http.Client{Timeout: 15 * time.Second} } if cfg.CallsZone == "" { cfg.CallsZone = "UTC" } return &Engine{cfg} } func (e *Engine) Store() *Store { return e.cfg.Store } func (e *Engine) now() time.Time { return e.cfg.Now() } // Park is a step that cannot finish yet: it will be retried. Code is a // fixed word, safe for logs and pages. type Park struct{ Code string } func (p *Park) Error() string { return "parked: " + p.Code } // Fail is a step that needs Paul: Alta will not retry it on its own. type Fail struct{ Code string } func (f *Fail) Error() string { return "failed: " + f.Code } func park(code string) error { return &Park{code} } func fail(code string) error { return &Fail{code} } // errCrash marks an AfterAct hook's simulated crash. var errCrash = errors.New("provision: simulated crash") func (e *Engine) acted(key string) error { if e.cfg.AfterAct != nil { if err := e.cfg.AfterAct(key); err != nil { return fmt.Errorf("%w at %s: %v", errCrash, key, err) } } return nil } // log carries step keys and fixed codes only (§8): never a label, an // address, a key or a platform value. App slugs are compiled constants. func (e *Engine) log(run int64, app, step, outcome string) { e.cfg.Logger.Info("alta step", "run", run, "app", app, "step", step, "outcome", outcome) } // ---- starting runs (§6 steps 0 and 2) ---- // InitializeAttestation is the sentence Paul types to give amendment // §12's baseline confirmation. Alta then signs Home's fixed // baseline_attestation value in directory.initialize. const InitializeAttestation = "This directory has never issued a roster generation or consumer record" // StartInitialize opens Alta's first production run: a directory-only // initialize plan (§6 step 1). func (e *Engine) StartInitialize(ctx context.Context, typed string) (int64, error) { if strings.TrimSpace(typed) != InitializeAttestation { return 0, fail("attestation-mismatch") } ins, o, _ := e.cfg.Home.Inspect(ctx, homeop.Directory) if o != homeop.OK { return 0, fail("home-" + o.String()) } if ins.Generation != nil { return 0, fail("already-initialized") } id, err := e.cfg.Store.NewRun(ctx, homeop.KindInitialize, "", nil, RunOptions{}) if err != nil { return 0, err } e.cfg.Store.Audit(ctx, id, "", "", "run-start", homeop.KindInitialize) return id, e.submitPlans(ctx, id, "", "", "", homeop.KindInitialize, nil, "") } // NewTeam is the New team form. type NewTeam struct { Label, Name, Owner string Apps []string // Forked: the team moves an existing stopgap whose environments Paul // forked to