102 lines
3.3 KiB
Go
102 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"path/filepath"
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/aritmolab/thothii/tools/tht/internal/compose"
|
|
"github.com/aritmolab/thothii/tools/tht/internal/installapply"
|
|
"github.com/aritmolab/thothii/tools/tht/internal/preflight"
|
|
"github.com/aritmolab/thothii/tools/tht/internal/version"
|
|
)
|
|
|
|
func installationApplyCommand(ctx context.Context, installationPath string, args []string, stdout io.Writer) int {
|
|
structured := slices.Contains(args, "--json")
|
|
write := func(result installapply.Result) {
|
|
if structured {
|
|
_ = json.NewEncoder(stdout).Encode(result)
|
|
} else {
|
|
fmt.Fprintf(stdout, "%s: %s\n", result.Stage, result.Action)
|
|
}
|
|
}
|
|
var planPath string
|
|
valid := len(args) > 0 && args[0] == "apply" && filepath.IsAbs(installationPath)
|
|
seenJSON := false
|
|
for index := 1; index < len(args); index++ {
|
|
switch args[index] {
|
|
case "--json":
|
|
if seenJSON {
|
|
valid = false
|
|
}
|
|
seenJSON = true
|
|
case "--plan":
|
|
if planPath != "" || index+1 >= len(args) {
|
|
valid = false
|
|
} else {
|
|
index++
|
|
planPath = args[index]
|
|
}
|
|
default:
|
|
valid = false
|
|
}
|
|
}
|
|
if !valid || !filepath.IsAbs(planPath) {
|
|
write(installapply.Result{SchemaVersion: 1, Stage: "usage", Action: "Use --installation ABS_PATH installation apply --plan ABS_PLAN [--json]."})
|
|
return 2
|
|
}
|
|
runner := compose.NewRunnerWithEnvironment("", installationDockerEnvironment())
|
|
check := func(ctx context.Context, p preflight.Plan) error {
|
|
if version.Current().Commit != p.Release.Revision || version.Current().Version != p.Release.Version {
|
|
return errors.New("use the operator supplied with this release")
|
|
}
|
|
var documents bytes.Buffer
|
|
if installationDocumentsCommand(ctx, installationPath, []string{"validate", "--workspaces", p.WorkspaceDirectory, "--bootstrap", p.BootstrapPath, "--json"}, &documents) != 0 {
|
|
return errors.New("prepared documents failed validation")
|
|
}
|
|
host, err := preflight.InspectHost(p.Installation.ProjectDirectory)
|
|
if err != nil || "linux/"+host.Arch != p.Platform {
|
|
return errors.New("host platform differs from plan")
|
|
}
|
|
report := preflight.CheckHost(ctx, runner, host, p.Release.Requirements)
|
|
if report.OK {
|
|
report.Merge(preflight.CheckImages(ctx, runner, p.Release, p.Platform))
|
|
}
|
|
if report.OK {
|
|
report.Merge(preflight.CheckCompose(ctx, runner, p.Installation, p.Release, p.ManifestPath, p.Platform))
|
|
}
|
|
if report.OK {
|
|
report.Merge(preflight.CheckExternal(ctx, p.Installation))
|
|
}
|
|
if report.OK {
|
|
var out, discarded bytes.Buffer
|
|
bound, cancel := context.WithTimeout(ctx, 60*time.Second)
|
|
defer cancel()
|
|
code := workspaceDocumentsCommand(bound, []string{"probe", "--directory", p.WorkspaceDirectory, "--bootstrap", p.BootstrapPath, "--json"}, &out, &discarded)
|
|
var probes preflight.Report
|
|
if code != 0 || json.Unmarshal(out.Bytes(), &probes) != nil || !probes.OK || len(probes.Checks) == 0 {
|
|
return errors.New("current database/evidence probes failed")
|
|
}
|
|
}
|
|
if !report.OK {
|
|
return errors.New("current prerequisites failed")
|
|
}
|
|
return nil
|
|
}
|
|
result, err := installapply.Run(ctx, installapply.Request{PlanPath: planPath, InstallationPath: installationPath}, runner, check)
|
|
write(result)
|
|
if err != nil {
|
|
if result.NeedsReview() {
|
|
return 3
|
|
}
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|