feat: publish Tend v0.2 Preview 2 source
Export the reviewed allowlisted snapshot from private source commit 8aab3db43f35e6a49aa497f45d73701b13fc9f32 and tree 992132ea4703437dc13ffdbb04a077816c02caf9. This includes routed singleton continuity, deployment evidence, strict schema-2 configuration, restricted transport, and the independently compilable public-tree guard. AI-Assisted: OpenAI Codex Signed-off-by: Cole Speelman <crspeelman@gmail.com>
This commit is contained in:
+292
-76
@@ -14,6 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gamertan.com/tend/internal/config"
|
||||
"gamertan.com/tend/internal/eventlog"
|
||||
"gamertan.com/tend/internal/state"
|
||||
)
|
||||
|
||||
@@ -29,6 +30,7 @@ type Report struct {
|
||||
Release string `json:"release,omitempty"`
|
||||
ActiveRelease string `json:"active_release,omitempty"`
|
||||
PreviousRelease string `json:"previous_release,omitempty"`
|
||||
EventWarnings int `json:"event_warnings,omitempty"`
|
||||
}
|
||||
type Status struct {
|
||||
State *state.Record `json:"state,omitempty"`
|
||||
@@ -37,14 +39,18 @@ type Status struct {
|
||||
}
|
||||
|
||||
type Manager struct {
|
||||
Operator Operator
|
||||
Now func() time.Time
|
||||
Prepare func(config.Config, string, string, string) (string, error)
|
||||
Inspect func(config.Config, string, string, string) error
|
||||
Operator Operator
|
||||
Now func() time.Time
|
||||
Prepare func(config.Config, string, string, string) (string, error)
|
||||
Inspect func(config.Config, string, string, string) error
|
||||
ReadIdentity func(string) (releaseIdentity, error)
|
||||
OperationID func() (string, error)
|
||||
AppendEvent func(string, eventlog.Event) error
|
||||
Sleep func(context.Context, time.Duration) error
|
||||
}
|
||||
|
||||
func NewManager(operator Operator) Manager {
|
||||
return Manager{Operator: operator, Now: time.Now, Prepare: prepareRelease, Inspect: inspectArtifact}
|
||||
return Manager{Operator: operator, Now: time.Now, Prepare: prepareRelease, Inspect: inspectArtifact, ReadIdentity: readReleaseIdentity, OperationID: eventlog.OperationID, AppendEvent: eventlog.Append, Sleep: sleepContext}
|
||||
}
|
||||
|
||||
func (m Manager) Deploy(ctx context.Context, cfg config.Config, request Request) (Report, error) {
|
||||
@@ -66,10 +72,44 @@ func (m Manager) Deploy(ctx context.Context, cfg config.Config, request Request)
|
||||
if err != nil {
|
||||
return Report{}, err
|
||||
}
|
||||
started := m.Now()
|
||||
eventWarnings := 0
|
||||
identity, identityErr := m.ReadIdentity(release)
|
||||
if identityErr != nil {
|
||||
eventWarnings++
|
||||
}
|
||||
operationID := ""
|
||||
if m.OperationID != nil {
|
||||
operationID, err = m.OperationID()
|
||||
if err != nil {
|
||||
eventWarnings++
|
||||
operationID = ""
|
||||
}
|
||||
}
|
||||
emit := func(phase, slot, outcome string) {
|
||||
if m.AppendEvent == nil || identityErr != nil || operationID == "" {
|
||||
return
|
||||
}
|
||||
event := eventlog.Event{Version: eventlog.Version, OperationID: operationID, Service: cfg.Service.Name, ArtifactDigest: request.ApprovedSHA256, Commit: identity.Commit, ReleaseVersion: identity.Version, Phase: phase, Slot: slot, DurationMillis: max(0, m.Now().Sub(started).Milliseconds()), Outcome: outcome, ObservedAt: m.Now().UTC().Format(time.RFC3339Nano)}
|
||||
if eventErr := m.AppendEvent(cfg.Deployment.EventLog, event); eventErr != nil {
|
||||
eventWarnings++
|
||||
}
|
||||
}
|
||||
record, err := loadOrBootstrap(cfg, m.Now())
|
||||
if err != nil {
|
||||
return Report{}, err
|
||||
}
|
||||
attemptAt := m.Now().UTC().Format(time.RFC3339)
|
||||
record.DesiredRelease = release
|
||||
record.CandidateRelease = release
|
||||
record.LastAttemptRelease = release
|
||||
record.LastAttemptOutcome = "running"
|
||||
record.LastAttemptAt = attemptAt
|
||||
record.UpdatedAt = attemptAt
|
||||
if err := state.Store(cfg.Deployment.StateFile, cfg.Deployment.Root, record); err != nil {
|
||||
return Report{}, err
|
||||
}
|
||||
emit("candidate", inactiveSlot(cfg, record), "running")
|
||||
switch cfg.Deployment.Strategy {
|
||||
case "blue_green":
|
||||
err = m.deployBlueGreen(ctx, cfg, record, release)
|
||||
@@ -79,13 +119,64 @@ func (m Manager) Deploy(ctx context.Context, cfg config.Config, request Request)
|
||||
err = errors.New("unsupported strategy")
|
||||
}
|
||||
if err != nil {
|
||||
return Report{}, err
|
||||
failed := record
|
||||
failed.CandidateRelease = ""
|
||||
failed.LastAttemptOutcome = "failed"
|
||||
failed.UpdatedAt = m.Now().UTC().Format(time.RFC3339)
|
||||
_ = state.Store(cfg.Deployment.StateFile, cfg.Deployment.Root, failed)
|
||||
emit("activation", inactiveSlot(cfg, record), "failed")
|
||||
return Report{EventWarnings: eventWarnings}, err
|
||||
}
|
||||
emit("activation", inactiveSlot(cfg, record), "succeeded")
|
||||
updated, err := state.Load(cfg.Deployment.StateFile, cfg.Deployment.Root, cfg.Deployment.Strategy)
|
||||
if err != nil {
|
||||
return Report{}, err
|
||||
}
|
||||
return Report{Validated: true, Mutation: "activated", Release: release, ActiveRelease: updated.ActiveRelease, PreviousRelease: updated.PreviousRelease}, nil
|
||||
return Report{Validated: true, Mutation: "activated", Release: release, ActiveRelease: updated.ActiveRelease, PreviousRelease: updated.PreviousRelease, EventWarnings: eventWarnings}, nil
|
||||
}
|
||||
|
||||
type releaseIdentity struct {
|
||||
Version string `json:"version"`
|
||||
Commit string `json:"commit"`
|
||||
}
|
||||
|
||||
func readReleaseIdentity(release string) (releaseIdentity, error) {
|
||||
b, err := os.ReadFile(filepath.Join(release, "RELEASE.json"))
|
||||
if err != nil {
|
||||
return releaseIdentity{}, fmt.Errorf("read installed release identity: %w", err)
|
||||
}
|
||||
if len(b) > 1<<20 {
|
||||
return releaseIdentity{}, errors.New("installed release identity is too large")
|
||||
}
|
||||
var identity releaseIdentity
|
||||
if err := json.Unmarshal(b, &identity); err != nil {
|
||||
return releaseIdentity{}, errors.New("decode installed release identity")
|
||||
}
|
||||
if identity.Version == "" || identity.Commit == "" {
|
||||
return releaseIdentity{}, errors.New("installed release identity is incomplete")
|
||||
}
|
||||
return identity, nil
|
||||
}
|
||||
|
||||
func inactiveSlot(cfg config.Config, record state.Record) string {
|
||||
if cfg.Deployment.Strategy == "singleton_candidate" {
|
||||
return "singleton"
|
||||
}
|
||||
if record.ActiveSlot == "blue" {
|
||||
return "green"
|
||||
}
|
||||
return "blue"
|
||||
}
|
||||
|
||||
func sleepContext(ctx context.Context, duration time.Duration) error {
|
||||
timer := time.NewTimer(duration)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func loadOrBootstrap(cfg config.Config, now time.Time) (state.Record, error) {
|
||||
@@ -103,13 +194,13 @@ func loadOrBootstrap(cfg config.Config, now time.Time) (state.Record, error) {
|
||||
if err != nil {
|
||||
return state.Record{}, fmt.Errorf("bootstrap active slot: %w", err)
|
||||
}
|
||||
return state.Record{SchemaVersion: 1, Strategy: cfg.Deployment.Strategy, ActiveSlot: slot, ActiveRelease: release, UpdatedAt: now.UTC().Format(time.RFC3339)}, nil
|
||||
return state.Record{SchemaVersion: 1, Strategy: cfg.Deployment.Strategy, DesiredRelease: release, ActiveSlot: slot, ActiveRelease: release, UpdatedAt: now.UTC().Format(time.RFC3339)}, nil
|
||||
case "singleton_candidate":
|
||||
release, err := resolveReleaseLink(cfg.Deployment.Root, cfg.Deployment.Singleton.CurrentLink)
|
||||
if err != nil {
|
||||
return state.Record{}, fmt.Errorf("bootstrap singleton: %w", err)
|
||||
}
|
||||
return state.Record{SchemaVersion: 1, Strategy: cfg.Deployment.Strategy, ActiveSlot: "singleton", ActiveRelease: release, UpdatedAt: now.UTC().Format(time.RFC3339)}, nil
|
||||
return state.Record{SchemaVersion: 1, Strategy: cfg.Deployment.Strategy, DesiredRelease: release, ActiveSlot: "singleton", ActiveRelease: release, UpdatedAt: now.UTC().Format(time.RFC3339)}, nil
|
||||
}
|
||||
return state.Record{}, errors.New("unsupported strategy")
|
||||
}
|
||||
@@ -180,7 +271,11 @@ func (m Manager) deployBlueGreen(ctx context.Context, cfg config.Config, record
|
||||
if err = m.probePublic(ctx, cfg, true); err != nil {
|
||||
return fmt.Errorf("public-origin smoke failed: %w", err)
|
||||
}
|
||||
next := state.Record{SchemaVersion: 1, Strategy: cfg.Deployment.Strategy, ActiveSlot: inactive, ActiveRelease: release, PreviousSlot: record.ActiveSlot, PreviousRelease: record.ActiveRelease, UpdatedAt: m.Now().UTC().Format(time.RFC3339)}
|
||||
previous := slotConfig(bg, record.ActiveSlot)
|
||||
if err = m.continuityWindow(ctx, cfg, previous.Address, true); err != nil {
|
||||
return fmt.Errorf("activation continuity failed: %w", err)
|
||||
}
|
||||
next := state.Record{SchemaVersion: 1, Strategy: cfg.Deployment.Strategy, DesiredRelease: release, ActiveSlot: inactive, ActiveRelease: release, PreviousSlot: record.ActiveSlot, PreviousRelease: record.ActiveRelease, LastAttemptRelease: release, LastAttemptOutcome: "succeeded", LastAttemptAt: record.LastAttemptAt, UpdatedAt: m.Now().UTC().Format(time.RFC3339)}
|
||||
if err = state.Store(cfg.Deployment.StateFile, cfg.Deployment.Root, next); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -188,62 +283,10 @@ func (m Manager) deployBlueGreen(ctx context.Context, cfg config.Config, record
|
||||
}
|
||||
|
||||
func (m Manager) deploySingleton(ctx context.Context, cfg config.Config, record state.Record, release string) (err error) {
|
||||
single := *cfg.Deployment.Singleton
|
||||
candidateUnit := cfg.Service.Name + "-tend-candidate.service"
|
||||
env := map[string]string{single.ListenEnv: single.CandidateAddress}
|
||||
binary := filepath.Join(release, cfg.Build.Binary)
|
||||
if err = m.Operator.StartCandidate(ctx, candidateUnit, binary, cfg.Service.EnvironmentFile, env); err != nil {
|
||||
if err = m.activateSingletonRelease(ctx, cfg, release, true); err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
stopCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
_ = m.Operator.Stop(stopCtx, candidateUnit)
|
||||
}()
|
||||
if err = m.probeAll(ctx, cfg, single.CandidateAddress); err != nil {
|
||||
return fmt.Errorf("candidate failed: %w", err)
|
||||
}
|
||||
oldCurrent, err := resolveReleaseLink(cfg.Deployment.Root, single.CurrentLink)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
oldPrevious, previousErr := resolveReleaseLink(cfg.Deployment.Root, single.PreviousLink)
|
||||
currentChanged := false
|
||||
previousChanged := false
|
||||
defer func() {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if currentChanged {
|
||||
_ = replaceSymlink(single.CurrentLink, oldCurrent)
|
||||
_ = m.Operator.Restart(ctx, single.Unit)
|
||||
}
|
||||
if previousChanged {
|
||||
if previousErr == nil {
|
||||
_ = replaceSymlink(single.PreviousLink, oldPrevious)
|
||||
} else {
|
||||
_ = removeSymlink(single.PreviousLink)
|
||||
}
|
||||
}
|
||||
}()
|
||||
if err = replaceSymlink(single.PreviousLink, oldCurrent); err != nil {
|
||||
return err
|
||||
}
|
||||
previousChanged = true
|
||||
if err = replaceSymlink(single.CurrentLink, release); err != nil {
|
||||
return err
|
||||
}
|
||||
currentChanged = true
|
||||
if err = m.Operator.Restart(ctx, single.Unit); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = m.probeAll(ctx, cfg, single.Address); err != nil {
|
||||
return fmt.Errorf("post-activation smoke failed: %w", err)
|
||||
}
|
||||
if err = m.probePublic(ctx, cfg, true); err != nil {
|
||||
return fmt.Errorf("public-origin smoke failed: %w", err)
|
||||
}
|
||||
next := state.Record{SchemaVersion: 1, Strategy: cfg.Deployment.Strategy, ActiveSlot: "singleton", ActiveRelease: release, PreviousSlot: "singleton", PreviousRelease: record.ActiveRelease, UpdatedAt: m.Now().UTC().Format(time.RFC3339)}
|
||||
next := state.Record{SchemaVersion: 1, Strategy: cfg.Deployment.Strategy, DesiredRelease: release, ActiveSlot: "singleton", ActiveRelease: release, PreviousSlot: "singleton", PreviousRelease: record.ActiveRelease, LastAttemptRelease: release, LastAttemptOutcome: "succeeded", LastAttemptAt: record.LastAttemptAt, UpdatedAt: m.Now().UTC().Format(time.RFC3339)}
|
||||
if err = state.Store(cfg.Deployment.StateFile, cfg.Deployment.Root, next); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -263,6 +306,21 @@ func (m Manager) Rollback(ctx context.Context, cfg config.Config) (state.Record,
|
||||
if record.PreviousRelease == "" {
|
||||
return state.Record{}, errors.New("no previous release is recorded")
|
||||
}
|
||||
started := m.Now()
|
||||
identity, identityErr := m.ReadIdentity(record.PreviousRelease)
|
||||
digest, digestErr := releaseDigest(record.PreviousRelease)
|
||||
operationID := ""
|
||||
if m.OperationID != nil {
|
||||
operationID, _ = m.OperationID()
|
||||
}
|
||||
emit := func(outcome string) {
|
||||
if m.AppendEvent == nil || identityErr != nil || digestErr != nil || operationID == "" {
|
||||
return
|
||||
}
|
||||
event := eventlog.Event{Version: eventlog.Version, OperationID: operationID, Service: cfg.Service.Name, ArtifactDigest: digest, Commit: identity.Commit, ReleaseVersion: identity.Version, Phase: "rollback", Slot: record.PreviousSlot, DurationMillis: max(0, m.Now().Sub(started).Milliseconds()), Outcome: outcome, ObservedAt: m.Now().UTC().Format(time.RFC3339Nano)}
|
||||
_ = m.AppendEvent(cfg.Deployment.EventLog, event)
|
||||
}
|
||||
emit("running")
|
||||
switch cfg.Deployment.Strategy {
|
||||
case "blue_green":
|
||||
err = m.rollbackBlueGreen(ctx, cfg, record)
|
||||
@@ -272,10 +330,29 @@ func (m Manager) Rollback(ctx context.Context, cfg config.Config) (state.Record,
|
||||
err = errors.New("unsupported strategy")
|
||||
}
|
||||
if err != nil {
|
||||
emit("failed")
|
||||
return state.Record{}, err
|
||||
}
|
||||
emit("succeeded")
|
||||
return state.Load(cfg.Deployment.StateFile, cfg.Deployment.Root, cfg.Deployment.Strategy)
|
||||
}
|
||||
|
||||
func releaseDigest(release string) (string, error) {
|
||||
name := filepath.Base(release)
|
||||
if !strings.HasPrefix(name, "sha256-") {
|
||||
return "", errors.New("release is not content addressed")
|
||||
}
|
||||
digest := strings.TrimPrefix(name, "sha256-")
|
||||
if len(digest) != 64 {
|
||||
return "", errors.New("release digest is invalid")
|
||||
}
|
||||
for _, character := range digest {
|
||||
if !strings.ContainsRune("0123456789abcdef", character) {
|
||||
return "", errors.New("release digest is invalid")
|
||||
}
|
||||
}
|
||||
return digest, nil
|
||||
}
|
||||
func (m Manager) rollbackBlueGreen(ctx context.Context, cfg config.Config, record state.Record) (err error) {
|
||||
bg := *cfg.Deployment.BlueGreen
|
||||
slot := slotConfig(bg, record.PreviousSlot)
|
||||
@@ -321,36 +398,148 @@ func (m Manager) rollbackBlueGreen(ctx context.Context, cfg config.Config, recor
|
||||
if err = m.probePublic(ctx, cfg, false); err != nil {
|
||||
return err
|
||||
}
|
||||
next := state.Record{SchemaVersion: 1, Strategy: record.Strategy, ActiveSlot: record.PreviousSlot, ActiveRelease: record.PreviousRelease, PreviousSlot: record.ActiveSlot, PreviousRelease: record.ActiveRelease, UpdatedAt: m.Now().UTC().Format(time.RFC3339)}
|
||||
next := state.Record{SchemaVersion: 1, Strategy: record.Strategy, DesiredRelease: record.PreviousRelease, ActiveSlot: record.PreviousSlot, ActiveRelease: record.PreviousRelease, PreviousSlot: record.ActiveSlot, PreviousRelease: record.ActiveRelease, LastAttemptRelease: record.PreviousRelease, LastAttemptOutcome: "rolled_back", LastAttemptAt: m.Now().UTC().Format(time.RFC3339), UpdatedAt: m.Now().UTC().Format(time.RFC3339)}
|
||||
return state.Store(cfg.Deployment.StateFile, cfg.Deployment.Root, next)
|
||||
}
|
||||
func (m Manager) rollbackSingleton(ctx context.Context, cfg config.Config, record state.Record) (err error) {
|
||||
if err = m.activateSingletonRelease(ctx, cfg, record.PreviousRelease, false); err != nil {
|
||||
return err
|
||||
}
|
||||
next := state.Record{SchemaVersion: 1, Strategy: record.Strategy, DesiredRelease: record.PreviousRelease, ActiveSlot: "singleton", ActiveRelease: record.PreviousRelease, PreviousSlot: "singleton", PreviousRelease: record.ActiveRelease, LastAttemptRelease: record.PreviousRelease, LastAttemptOutcome: "rolled_back", LastAttemptAt: m.Now().UTC().Format(time.RFC3339), UpdatedAt: m.Now().UTC().Format(time.RFC3339)}
|
||||
return state.Store(cfg.Deployment.StateFile, cfg.Deployment.Root, next)
|
||||
}
|
||||
|
||||
// activateSingletonRelease keeps public traffic on a proven process while the
|
||||
// installed fixed-address unit changes release. The transient candidate first
|
||||
// receives traffic, remains healthy through the handoff, and is stopped only
|
||||
// after Caddy points back to the verified installed unit.
|
||||
func (m Manager) activateSingletonRelease(ctx context.Context, cfg config.Config, release string, checkMarkers bool) (err error) {
|
||||
single := *cfg.Deployment.Singleton
|
||||
candidateUnit := cfg.Service.Name + "-tend-candidate.service"
|
||||
env := map[string]string{single.ListenEnv: single.CandidateAddress}
|
||||
binary := filepath.Join(release, cfg.Build.Binary)
|
||||
if err = m.Operator.StartCandidate(ctx, candidateUnit, binary, cfg.Service.EnvironmentFile, env); err != nil {
|
||||
return err
|
||||
}
|
||||
stopCandidate := true
|
||||
defer func() {
|
||||
if !stopCandidate {
|
||||
return
|
||||
}
|
||||
stopCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
_ = m.Operator.Stop(stopCtx, candidateUnit)
|
||||
}()
|
||||
probeLocal := m.probeHealthReadiness
|
||||
if checkMarkers {
|
||||
probeLocal = m.probeAll
|
||||
}
|
||||
if err = probeLocal(ctx, cfg, single.CandidateAddress); err != nil {
|
||||
return fmt.Errorf("candidate failed: %w", err)
|
||||
}
|
||||
|
||||
oldHandler, err := os.ReadFile(single.CaddyHandler)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read current Caddy handler: %w", err)
|
||||
}
|
||||
oldCurrent, err := resolveReleaseLink(cfg.Deployment.Root, single.CurrentLink)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = replaceSymlink(single.CurrentLink, record.PreviousRelease); err != nil {
|
||||
return err
|
||||
}
|
||||
oldPrevious, previousErr := resolveReleaseLink(cfg.Deployment.Root, single.PreviousLink)
|
||||
handlerChanged := false
|
||||
currentChanged := false
|
||||
previousChanged := false
|
||||
defer func() {
|
||||
if err != nil {
|
||||
_ = replaceSymlink(single.CurrentLink, oldCurrent)
|
||||
_ = m.Operator.Restart(ctx, single.Unit)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
recoveryErr := error(nil)
|
||||
if currentChanged {
|
||||
if restoreErr := replaceSymlink(single.CurrentLink, oldCurrent); restoreErr != nil {
|
||||
recoveryErr = errors.Join(recoveryErr, restoreErr)
|
||||
} else if restoreErr = m.Operator.Restart(ctx, single.Unit); restoreErr != nil {
|
||||
recoveryErr = errors.Join(recoveryErr, restoreErr)
|
||||
} else if restoreErr = m.probeHealthReadiness(ctx, cfg, single.Address); restoreErr != nil {
|
||||
recoveryErr = errors.Join(recoveryErr, restoreErr)
|
||||
}
|
||||
}
|
||||
if previousChanged {
|
||||
var restoreErr error
|
||||
if previousErr == nil {
|
||||
restoreErr = replaceSymlink(single.PreviousLink, oldPrevious)
|
||||
} else {
|
||||
restoreErr = removeSymlink(single.PreviousLink)
|
||||
}
|
||||
recoveryErr = errors.Join(recoveryErr, restoreErr)
|
||||
}
|
||||
if handlerChanged && recoveryErr == nil {
|
||||
if restoreErr := atomicWrite(single.CaddyHandler, oldHandler, 0o644); restoreErr != nil {
|
||||
recoveryErr = errors.Join(recoveryErr, restoreErr)
|
||||
} else if restoreErr = m.Operator.ValidateCaddy(ctx, single.CaddyConfig); restoreErr != nil {
|
||||
recoveryErr = errors.Join(recoveryErr, restoreErr)
|
||||
} else if restoreErr = m.Operator.ReloadCaddy(ctx); restoreErr != nil {
|
||||
recoveryErr = errors.Join(recoveryErr, restoreErr)
|
||||
}
|
||||
}
|
||||
if recoveryErr != nil && handlerChanged {
|
||||
stopCandidate = false
|
||||
err = errors.Join(err, fmt.Errorf("singleton recovery incomplete; candidate remains routed for operator recovery: %w", recoveryErr))
|
||||
}
|
||||
}()
|
||||
|
||||
candidateHandler, err := renderHandler(single.CaddyHandlerTemplate, single.CandidateAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = atomicWrite(single.CaddyHandler, candidateHandler, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
handlerChanged = true
|
||||
if err = m.Operator.ValidateCaddy(ctx, single.CaddyConfig); err != nil {
|
||||
return fmt.Errorf("candidate Caddy validation failed: %w", err)
|
||||
}
|
||||
if err = m.Operator.ReloadCaddy(ctx); err != nil {
|
||||
return fmt.Errorf("candidate Caddy reload failed: %w", err)
|
||||
}
|
||||
if err = m.probePublic(ctx, cfg, checkMarkers); err != nil {
|
||||
return fmt.Errorf("candidate public-origin smoke failed: %w", err)
|
||||
}
|
||||
|
||||
if err = replaceSymlink(single.PreviousLink, oldCurrent); err != nil {
|
||||
return err
|
||||
}
|
||||
previousChanged = true
|
||||
if err = replaceSymlink(single.CurrentLink, release); err != nil {
|
||||
return err
|
||||
}
|
||||
currentChanged = true
|
||||
if err = m.Operator.Restart(ctx, single.Unit); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = m.probeHealthReadiness(ctx, cfg, single.Address); err != nil {
|
||||
if err = probeLocal(ctx, cfg, single.Address); err != nil {
|
||||
return fmt.Errorf("post-activation smoke failed: %w", err)
|
||||
}
|
||||
installedHandler, err := renderHandler(single.CaddyHandlerTemplate, single.Address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = m.probePublic(ctx, cfg, false); err != nil {
|
||||
if err = atomicWrite(single.CaddyHandler, installedHandler, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = replaceSymlink(single.PreviousLink, record.ActiveRelease)
|
||||
next := state.Record{SchemaVersion: 1, Strategy: record.Strategy, ActiveSlot: "singleton", ActiveRelease: record.PreviousRelease, PreviousSlot: "singleton", PreviousRelease: record.ActiveRelease, UpdatedAt: m.Now().UTC().Format(time.RFC3339)}
|
||||
return state.Store(cfg.Deployment.StateFile, cfg.Deployment.Root, next)
|
||||
if err = m.Operator.ValidateCaddy(ctx, single.CaddyConfig); err != nil {
|
||||
return fmt.Errorf("installed Caddy validation failed: %w", err)
|
||||
}
|
||||
if err = m.Operator.ReloadCaddy(ctx); err != nil {
|
||||
return fmt.Errorf("installed Caddy reload failed: %w", err)
|
||||
}
|
||||
if err = m.probePublic(ctx, cfg, checkMarkers); err != nil {
|
||||
return fmt.Errorf("public-origin smoke failed: %w", err)
|
||||
}
|
||||
if err = m.continuityWindow(ctx, cfg, single.CandidateAddress, checkMarkers); err != nil {
|
||||
return fmt.Errorf("activation continuity failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m Manager) Status(ctx context.Context, cfg config.Config) (Status, error) {
|
||||
@@ -463,6 +652,33 @@ func (m Manager) probePublic(ctx context.Context, cfg config.Config, checkMarker
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m Manager) continuityWindow(ctx context.Context, cfg config.Config, previousAddress string, checkMarkers bool) error {
|
||||
steps := cfg.Deployment.ActivationWindowSecs * 4
|
||||
if steps < 1 {
|
||||
steps = 1
|
||||
}
|
||||
for step := 0; step < steps; step++ {
|
||||
if err := m.probePublic(ctx, cfg, checkMarkers); err != nil {
|
||||
return err
|
||||
}
|
||||
if previousAddress != "" {
|
||||
if err := m.probeHealthReadiness(ctx, cfg, previousAddress); err != nil {
|
||||
return fmt.Errorf("previous slot lost continuity: %w", err)
|
||||
}
|
||||
}
|
||||
if step+1 < steps {
|
||||
sleep := m.Sleep
|
||||
if sleep == nil {
|
||||
sleep = sleepContext
|
||||
}
|
||||
if err := sleep(ctx, 250*time.Millisecond); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m Manager) probe(ctx context.Context, cfg config.Config, address string, checks []config.Smoke) error {
|
||||
timeout := time.Duration(cfg.Deployment.CandidateTimeoutSecs) * time.Second
|
||||
for _, check := range checks {
|
||||
|
||||
Reference in New Issue
Block a user