package provision import ( "fmt" "time" "github.com/carlosframework/platform/internal/release" ) // InstallRequest is one "Update now" (spec §4.2). type InstallRequest struct { ID uint64 `json:"id"` FastTrack bool `json:"fast_track,omitempty"` At time.Time `json:"at"` By string `json:"by,omitempty"` } // UpdatePolicy is the app's instance update policy (spec §3.2). The default // channel is the manifest's own Channel (index Q4). type UpdatePolicy struct { InstanceControl []string `json:"instance_control,omitempty"` DefaultUpdates string `json:"default_updates,omitempty"` DefaultMajor *int `json:"default_major,omitempty"` } // EffectiveUpdates resolves one instance's settings: its own explicit values, // else the app's defaults, else today's. A major hold off stable is dropped: // only stable promotions cut tags (spec §3.1), so it could never hold. func EffectiveUpdates(rec Record, p *UpdatePolicy) release.UpdateSettings { u := release.UpdateSettings{Updates: release.UpdatesAuto, Channel: rec.Channel, Pin: rec.Pin} if p != nil { if p.DefaultUpdates != "" { u.Updates = p.DefaultUpdates } u.Major = p.DefaultMajor u.Control = append([]string(nil), p.InstanceControl...) } if rec.Updates != "" { u.Updates = rec.Updates } if rec.Major != nil { u.Major = rec.Major } if u.Channel != "stable" { u.Major = nil } if rec.InstallRequest != nil { u.InstallRequest, u.InstallFastTrack = rec.InstallRequest.ID, rec.InstallRequest.FastTrack } return u } // ValidateUpdates is the record's door check (the console's PUT). func ValidateUpdates(rec Record) error { if rec.Updates != "" && rec.Updates != release.UpdatesAuto && rec.Updates != release.UpdatesManual { return fmt.Errorf("updates must be auto or manual") } if rec.Major != nil && *rec.Major < 0 { return fmt.Errorf("major must be a whole number, 0 or more") } if rec.Major != nil && rec.Channel != "stable" { return fmt.Errorf("major applies only to the stable channel") } if rec.Pin != "" && (!release.ValidVersion(rec.Pin) || rec.Pin == "dev") { return fmt.Errorf("invalid pin %q", rec.Pin) } return nil } // ValidatePolicy is the app policy's door check. func ValidatePolicy(p *UpdatePolicy) error { if p == nil { return nil } if !release.ValidControl(p.InstanceControl) { return fmt.Errorf("instance_control must name updates, channel, major or pin, each once") } if p.DefaultUpdates != "" && p.DefaultUpdates != release.UpdatesAuto && p.DefaultUpdates != release.UpdatesManual { return fmt.Errorf("default_updates must be auto or manual") } if p.DefaultMajor != nil && *p.DefaultMajor < 0 { return fmt.Errorf("default_major must be a whole number, 0 or more") } return nil }