feat: support explicit source bundles through the common installer
This commit is contained in:
@@ -29,6 +29,7 @@ type RuntimeCheck struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
type Result struct {
|
||||
Distribution string `json:"distribution"`
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
PlatformReady bool `json:"platform_ready"`
|
||||
WorkspaceReady bool `json:"workspace_ready"`
|
||||
@@ -42,6 +43,7 @@ type Result struct {
|
||||
Journal string `json:"journal,omitempty"`
|
||||
}
|
||||
type journal struct {
|
||||
Distribution string `json:"distribution,omitempty"`
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
Installation string `json:"installation"`
|
||||
ReleaseRevision string `json:"release_revision"`
|
||||
@@ -76,6 +78,7 @@ func Run(ctx context.Context, request Request, runner compose.Runner, check Chec
|
||||
if err != nil || !reflect.DeepEqual(manifest, plan.Release) {
|
||||
return fail("release", "Restore the exact verified release bundle and generate a new plan if its identity changed.")
|
||||
}
|
||||
result.Distribution = manifest.Mode()
|
||||
if err := safeio.EnsurePrivateDirectory(filepath.Join(installation.ProjectDirectory, ".tht")); err != nil {
|
||||
return fail("lock", "The installation control directory must be private and canonical.")
|
||||
}
|
||||
@@ -106,13 +109,20 @@ func Run(ctx context.Context, request Request, runner compose.Runner, check Chec
|
||||
}
|
||||
defer clear(payload)
|
||||
result.Journal = filepath.Join(installation.ControlDirectory(), "installation-apply.json")
|
||||
state := journal{SchemaVersion: 1, Installation: installation.Path, ReleaseRevision: plan.Release.Revision, PlanSeal: plan.InputSeal}
|
||||
state := journal{SchemaVersion: 1, Installation: installation.Path, ReleaseRevision: plan.Release.Revision, PlanSeal: plan.InputSeal, Distribution: manifest.Mode()}
|
||||
if _, err := os.Lstat(result.Journal); err == nil {
|
||||
data, readErr := safeio.ReadCanonicalPrivateRegular(result.Journal, 16<<10)
|
||||
var previous journal
|
||||
if readErr != nil || json.Unmarshal(data, &previous) != nil || previous.SchemaVersion != 1 || previous.Installation != installation.Path || previous.ReleaseRevision != plan.Release.Revision {
|
||||
return fail("journal", "The existing installation journal is invalid or belongs to another release; this command is not a product upgrade.")
|
||||
}
|
||||
previousMode := previous.Distribution
|
||||
if previousMode == "" {
|
||||
previousMode = "prebuilt"
|
||||
}
|
||||
if previousMode != manifest.Mode() {
|
||||
return fail("journal", "Changing between source and prebuilt requires an explicit upgrade or a separate installation.")
|
||||
}
|
||||
} else if !os.IsNotExist(err) {
|
||||
return fail("journal", "Cannot safely read the installation journal.")
|
||||
}
|
||||
@@ -181,6 +191,9 @@ func Run(ctx context.Context, request Request, runner compose.Runner, check Chec
|
||||
return nil
|
||||
}},
|
||||
{"pull", func() error {
|
||||
if manifest.Mode() == "source" {
|
||||
return docker(30*time.Minute, "pull", "--policy", "always", "catalog-db", "qdrant", "embedding", "embedding-model-init")
|
||||
}
|
||||
return docker(30*time.Minute, "--profile", "catalog-maintenance", "--profile", "workspace-maintenance", "pull", "--policy", "always")
|
||||
}},
|
||||
{"runtime-identity", func() error {
|
||||
|
||||
@@ -82,6 +82,7 @@ type dockerFixture struct {
|
||||
calls [][]string
|
||||
fail string
|
||||
wrongImage bool
|
||||
source bool
|
||||
}
|
||||
|
||||
func (r *dockerFixture) Run(_ context.Context, args []string, input io.Reader) (compose.Result, error) {
|
||||
@@ -133,6 +134,9 @@ func (r *dockerFixture) Run(_ context.Context, args []string, input io.Reader) (
|
||||
continue
|
||||
}
|
||||
services[service] = map[string]string{"image": "docker.io/example/" + role + "@sha256:" + strings.Repeat("a", 64)}
|
||||
if r.source && (role == "core" || role == "frontend") {
|
||||
services[service] = map[string]string{"image": "sha256:" + strings.Repeat("a", 64)}
|
||||
}
|
||||
}
|
||||
if r.wrongImage {
|
||||
services["core"] = map[string]string{"image": "unverified:latest"}
|
||||
@@ -149,6 +153,36 @@ func (r *dockerFixture) Run(_ context.Context, args []string, input io.Reader) (
|
||||
return compose.Result{Stdout: `{"ok":true}`}, nil
|
||||
}
|
||||
|
||||
func TestExplicitSourceApplySharesReadinessAndPullsOnlyUpstream(t *testing.T) {
|
||||
path, p := fixture(t)
|
||||
p.Release.Distribution = "source"
|
||||
for _, role := range []string{"core", "frontend"} {
|
||||
p.Release.Images[role][p.Platform] = "sha256:" + strings.Repeat("a", 64)
|
||||
}
|
||||
data, _ := json.Marshal(p.Release)
|
||||
if err := os.WriteFile(p.ManifestPath, data, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = os.Remove(path)
|
||||
_ = os.Remove(path + ".key")
|
||||
if err := preflight.WritePlan(path, &p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := &dockerFixture{source: true}
|
||||
result, err := Run(context.Background(), Request{PlanPath: path, InstallationPath: p.Installation.Path}, r, func(context.Context, preflight.Plan) error { return nil })
|
||||
if err != nil || !result.WorkspaceReady || result.Distribution != "source" {
|
||||
t.Fatalf("source apply: %+v %v", result, err)
|
||||
}
|
||||
for _, call := range flatten(r.calls) {
|
||||
if strings.Contains(call, " pull ") && !strings.Contains(call, "pull --policy always catalog-db qdrant embedding embedding-model-init") {
|
||||
t.Fatal("source application images pulled")
|
||||
}
|
||||
if strings.Contains(call, " build ") {
|
||||
t.Fatal("apply implicitly built source")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyRefusesUnreleasedEffectiveImageBeforePull(t *testing.T) {
|
||||
path, p := fixture(t)
|
||||
r := &dockerFixture{wrongImage: true}
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
// Manifest is the publication/consumer contract. Each platform maps to a single-image digest,
|
||||
// not a mutable tag or multi-platform index. Maintenance roles use Images["core"].
|
||||
type Manifest struct {
|
||||
Distribution string `json:"distribution,omitempty"`
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
Version string `json:"version"`
|
||||
Revision string `json:"revision"`
|
||||
@@ -30,6 +31,13 @@ type Manifest struct {
|
||||
Compose []string `json:"compose"`
|
||||
}
|
||||
|
||||
func (m Manifest) Mode() string {
|
||||
if m.Distribution == "source" {
|
||||
return "source"
|
||||
}
|
||||
return "prebuilt"
|
||||
}
|
||||
|
||||
var hex256 = regexp.MustCompile(`^[a-f0-9]{64}$`)
|
||||
var imageReference = regexp.MustCompile(`^docker\.io/[a-z0-9][a-z0-9._/-]*@sha256:[a-f0-9]{64}$`)
|
||||
|
||||
@@ -37,6 +45,9 @@ func invalidRelease() error {
|
||||
return errors.New("release manifest or packaged files are incomplete, incompatible or invalid")
|
||||
}
|
||||
func (m Manifest) Validate() error {
|
||||
if m.Distribution != "" && m.Distribution != "prebuilt" && m.Distribution != "source" {
|
||||
return invalidRelease()
|
||||
}
|
||||
if m.SchemaVersion != 1 || m.ValidatorProtocol != Protocol || !regexp.MustCompile(`^[0-9]+\.[0-9]+\.[0-9]+(?:-[A-Za-z0-9.-]+)?$`).MatchString(m.Version) || !regexp.MustCompile(`^[a-f0-9]{40}$`).MatchString(m.Revision) {
|
||||
return invalidRelease()
|
||||
}
|
||||
@@ -56,7 +67,11 @@ func (m Manifest) Validate() error {
|
||||
return invalidRelease()
|
||||
}
|
||||
for platform, ref := range m.Images[service] {
|
||||
if (platform != "linux/amd64" && platform != "linux/arm64") || !imageReference.MatchString(ref) {
|
||||
valid := imageReference.MatchString(ref)
|
||||
if m.Mode() == "source" && (service == "core" || service == "frontend") {
|
||||
valid = strings.HasPrefix(ref, "sha256:") && hex256.MatchString(strings.TrimPrefix(ref, "sha256:"))
|
||||
}
|
||||
if (platform != "linux/amd64" && platform != "linux/arm64") || !valid {
|
||||
return invalidRelease()
|
||||
}
|
||||
}
|
||||
@@ -110,6 +125,25 @@ func CheckImages(ctx context.Context, runner Runner, m Manifest, platform string
|
||||
r.Add("image-"+service, "error", "release.images."+service, "Publish the selected Linux architecture before producing an executable plan.")
|
||||
continue
|
||||
}
|
||||
if m.Mode() == "source" && (service == "core" || service == "frontend") {
|
||||
result, err := docker(ctx, runner, "image", "inspect", "--format", "{{json .}}", ref)
|
||||
var image struct {
|
||||
ID string `json:"Id"`
|
||||
OS string `json:"Os"`
|
||||
Architecture string `json:"Architecture"`
|
||||
Config struct {
|
||||
Labels map[string]string `json:"Labels"`
|
||||
} `json:"Config"`
|
||||
}
|
||||
good := err == nil && result.ExitCode == 0 && json.Unmarshal([]byte(result.Stdout), &image) == nil && image.ID == ref &&
|
||||
image.OS+"/"+image.Architecture == platform && image.Config.Labels["org.opencontainers.image.revision"] == m.Revision && image.Config.Labels["org.opencontainers.image.version"] == m.Version
|
||||
outcome := "passed"
|
||||
if !good {
|
||||
outcome = "error"
|
||||
}
|
||||
r.Add("image-"+service, outcome, "release.images."+service, "Require the explicitly built local image, matching source revision, version and architecture; rebuild the selected source bundle if it is missing.")
|
||||
continue
|
||||
}
|
||||
result, err := docker(ctx, runner, "manifest", "inspect", "--verbose", ref)
|
||||
var manifest struct {
|
||||
Descriptor struct {
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/aritmolab/thothii/tools/tht/internal/compose"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type localDocker struct {
|
||||
fakeDocker
|
||||
wrong bool
|
||||
}
|
||||
|
||||
func (d *localDocker) Run(ctx context.Context, args []string, input io.Reader) (compose.Result, error) {
|
||||
if args[0] != "image" {
|
||||
return d.fakeDocker.Run(ctx, args, input)
|
||||
}
|
||||
revision := strings.Repeat("b", 40)
|
||||
if d.wrong {
|
||||
revision = strings.Repeat("c", 40)
|
||||
}
|
||||
value, _ := json.Marshal(map[string]any{"Id": args[len(args)-1], "Os": "linux", "Architecture": "amd64", "Config": map[string]any{"Labels": map[string]string{"org.opencontainers.image.revision": revision, "org.opencontainers.image.version": "1.0.0"}}})
|
||||
return compose.Result{Stdout: string(value)}, nil
|
||||
}
|
||||
func TestSourceImagesNeedExplicitDistributionAndMatchingLocalIdentity(t *testing.T) {
|
||||
m := Manifest{Distribution: "source", Revision: strings.Repeat("b", 40), Version: "1.0.0", Images: map[string]map[string]string{}}
|
||||
for _, role := range []string{"core", "frontend", "catalog", "qdrant", "embedding"} {
|
||||
ref := "docker.io/example/" + role + "@sha256:" + strings.Repeat("a", 64)
|
||||
if role == "core" || role == "frontend" {
|
||||
ref = "sha256:" + strings.Repeat("a", 64)
|
||||
}
|
||||
m.Images[role] = map[string]string{"linux/amd64": ref}
|
||||
}
|
||||
if !CheckImages(context.Background(), &localDocker{}, m, "linux/amd64").OK {
|
||||
t.Fatal("explicit local images rejected")
|
||||
}
|
||||
if CheckImages(context.Background(), &localDocker{wrong: true}, m, "linux/amd64").OK {
|
||||
t.Fatal("different source accepted")
|
||||
}
|
||||
m.Distribution = ""
|
||||
if CheckImages(context.Background(), &localDocker{}, m, "linux/amd64").OK {
|
||||
t.Fatal("implicit source fallback accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user