carlos / platform Public

region labels: REGION in status --instances and --layout; --live reads places from the deployment store (plan 2 task 5)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 1d152596081a1f56cb546cd3ffdddd54ee40446c parent c62799f
5 files changed, +268 −15
  • cli.go +91 −6
  • region_labels_e2e_test.go +37 −0
  • status_instances.go +13 −7
  • status_instances_test.go +5 −2
  • statuslive_regions_test.go +122 −0
diff --git a/cli.go b/cli.go
index 087bae4d5..c4108fb1e 100644
--- a/cli.go
+++ b/cli.go
@@ -27,9 +27,11 @@ import (
"github.com/carlosframework/platform/internal/appconfig"
"github.com/carlosframework/platform/internal/caps"
"github.com/carlosframework/platform/internal/edge"
+ "github.com/carlosframework/platform/internal/edgesfile"
"github.com/carlosframework/platform/internal/host"
"github.com/carlosframework/platform/internal/ledger"
"github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/regionlabel"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/status"
@@ -3714,12 +3716,41 @@ func RunStatus(args []string) {
// CARLOS_STATUS_BUCKET-then-CARLOS_DEPLOYMENT_BUCKET rule the agent's status
// loop uses, so this reads exactly the bucket the agents write.
func statusLive(w io.Writer) error {
+ return statusLiveWith(w, os.Getenv, openS3Bucket)
+}
+
+// livePlaces is what statusLive could learn about where its boxes are
+// (region labels §5 row 24): the edges files of the deployment store,
+// which is not the status store when CARLOS_STATUS_BUCKET is set.
+type livePlaces struct {
+ names regionlabel.Names
+ tried bool // statusLive asked for places
+ read bool // and read them
+}
+
+// bucketOpener opens a named bucket; statusLive's is openS3Bucket, a test's
+// stands a directory in for each bucket.
+type bucketOpener func(ctx context.Context, bucket string) (release.Store, error)
+
+func openS3Bucket(ctx context.Context, bucket string) (release.Store, error) {
+ s, err := release.NewDeploymentS3Store(ctx, bucket)
+ if err != nil {
+ return nil, err
+ }
+ return s, nil
+}
+
+// statusLiveWith is statusLive with its environment and its bucket opener
+// passed in. Box reports come from the status store (host.ResolveStatusBucket,
+// unchanged); places come from the deployment store, resolved as direct mode
+// resolves it, read once.
+func statusLiveWith(w io.Writer, getenv func(string) string, open bucketOpener) error {
ctx := context.Background()
- bucket := host.ResolveStatusBucket(os.Getenv)
+ bucket := host.ResolveStatusBucket(getenv)
if bucket == "" {
return fmt.Errorf("set CARLOS_STATUS_BUCKET or CARLOS_DEPLOYMENT_BUCKET")
}
- s, err := release.NewDeploymentS3Store(ctx, bucket)
+ s, err := open(ctx, bucket)
if err != nil {
return err
}
@@ -3727,10 +3758,55 @@ func statusLive(w io.Writer) error {
if err != nil {
return err
}
- printLiveBoxes(w, boxes, time.Now())
+ printLiveBoxesWith(w, boxes, time.Now(), readLivePlaces(ctx, getenv, open))
return nil
}
+// deploymentStoreFromEnv resolves the deployment store as direct mode does
+// (CARLOS_DEPLOYMENT_BUCKET, else CARLOS_DEPLOYMENT_DIR), without exiting
+// when neither is set: nil, nil.
+func deploymentStoreFromEnv(ctx context.Context, getenv func(string) string, open bucketOpener) (release.Store, error) {
+ if b := getenv("CARLOS_DEPLOYMENT_BUCKET"); b != "" {
+ return open(ctx, b)
+ }
+ if d := getenv("CARLOS_DEPLOYMENT_DIR"); d != "" {
+ return release.NewDirStore(d), nil
+ }
+ return nil, nil
+}
+
+// readLivePlaces reads control/edges/ once from the deployment store and
+// folds it as the console does. A failed listing and a read that is not
+// complete (a file that will not read or parse) are both "not read", as
+// they are for the console's map (spec rev 5 §3.2): the boxes keep their
+// own words and one line says so.
+func readLivePlaces(ctx context.Context, getenv func(string) string, open bucketOpener) livePlaces {
+ p := livePlaces{tried: true}
+ s, err := deploymentStoreFromEnv(ctx, getenv, open)
+ if err != nil || s == nil {
+ return p
+ }
+ snap, err := edgesfile.ReadSnapshot(ctx, s)
+ if err != nil || !snap.Complete {
+ return p
+ }
+ // The CLI does not know the console's home name, so a box publishing
+ // the home's "" reads "the home region" (RC-2).
+ p.names, p.read = regionlabel.NamesFrom(snap, ""), true
+ return p
+}
+
+// liveRegion is a box's region on the --live line: its label when the
+// edges files name it, else the word it reports (STATUS_BOX_REGION).
+func liveRegion(b status.Box, p livePlaces) string {
+ if p.read {
+ if code, known := p.names.BoxPlacement(b.Label); known {
+ return regionlabel.ForCode(code, "", p.names).Text
+ }
+ }
+ return b.Region
+}
+
// printLiveBoxes is statusLive's rendering half, split out so it can be
// asserted on without a bucket. One line per box, label-sorted so two runs
// diff on the fleet rather than on list order.
@@ -3743,21 +3819,27 @@ func statusLive(w io.Writer) error {
// metrics block at all is a host running code from before the block existed —
// which is a fact worth stating, not a row of zeros.
func printLiveBoxes(w io.Writer, boxes []status.Box, now time.Time) {
+ printLiveBoxesWith(w, boxes, now, livePlaces{})
+}
+
+// printLiveBoxesWith is printLiveBoxes naming each box's region from places
+// when it could read them, and saying once when it tried and could not.
+func printLiveBoxesWith(w io.Writer, boxes []status.Box, now time.Time, places livePlaces) {
sorted := append([]status.Box(nil), boxes...)
sort.Slice(sorted, func(i, j int) bool { return sorted[i].Label < sorted[j].Label })
for _, b := range sorted {
if status.BoxStale(b, now, status.SnapshotPeriod) {
fmt.Fprintf(w, "%-24s %-12s UNREACHABLE (last heartbeat %s ago)\n",
- b.Label, orDash(b.Region), now.Sub(b.GeneratedAt).Round(time.Minute))
+ b.Label, orDash(liveRegion(b, places)), now.Sub(b.GeneratedAt).Round(time.Minute))
continue
}
m := b.Metrics
if m == nil {
- fmt.Fprintf(w, "%-24s %-12s ok (no metrics block — pre-upgrade host)\n", b.Label, orDash(b.Region))
+ fmt.Fprintf(w, "%-24s %-12s ok (no metrics block — pre-upgrade host)\n", b.Label, orDash(liveRegion(b, places)))
continue
}
fmt.Fprintf(w, "%-24s %-12s release=%s tunnel=%s mem=%s/%s load=%s disk-free=%s alerts=%s\n",
- b.Label, orDash(b.Region), orDash(m.Release), orDash(m.Tunnel),
+ b.Label, orDash(liveRegion(b, places)), orDash(m.Release), orDash(m.Tunnel),
humanBytes(m.MemAvailBytes), humanBytes(m.MemTotalBytes),
humanFloat(m.Load1), humanBytes(m.DiskFreeBytes), alertsWord(m.Alerts))
for _, r := range m.ReleaseRefusals {
@@ -3793,6 +3875,9 @@ func printLiveBoxes(w io.Writer, boxes []status.Box, now time.Time) {
fmt.Fprintln(w, line)
}
}
+ if places.tried && !places.read {
+ mutedf(w, "region names could not be read, so each box shows the region it reports.") // RL-C2
+ }
}
// alertsWord renders a box's alert-sink answer for the live listing. "not
diff --git a/region_labels_e2e_test.go b/region_labels_e2e_test.go
index bb2c09e96..152647a1f 100644
--- a/region_labels_e2e_test.go
+++ b/region_labels_e2e_test.go
@@ -13,6 +13,7 @@ import (
"github.com/carlosframework/platform/internal/console"
"github.com/carlosframework/platform/internal/edgesfile"
+ "github.com/carlosframework/platform/internal/metrics"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/status"
@@ -163,3 +164,39 @@ func TestRegionLabelsListingsThroughTheConsole(t *testing.T) {
t.Fatalf("routes:\n%s", routes)
}
}
+
+func TestRegionLabelsStatusThroughTheConsole(t *testing.T) {
+ e := newLabelsE2E(t)
+ e.out(t, runInstancesEnable, "-app", "shop", "-account", "ops", "-channel", "beta")
+ host := "uk1." + labelsDomain
+ e.out(t, runInstancesCreate, "-app", "shop", "-account", "ops", "-host", host, "-region", "uk")
+ ctx := context.Background()
+ // The uk box's metrics chunk and its status snapshot with a layout
+ // report, as its agent writes them.
+ at := time.Now().UTC()
+ if err := metrics.WriteRaw(ctx, e.rel, metrics.RawChunk{V: 1, Box: labelsUKBox, Hour: metrics.HourOf(at),
+ Samples: []metrics.Sample{{At: at, Units: map[string]metrics.UnitSample{"carlos-exec@" + host: {}}}}}); err != nil {
+ t.Fatal(err)
+ }
+ b := status.CollectBox(nil, status.Probes{MachineID: "uk-machine", Region: "uk", Layout: &status.LayoutReport{
+ Mode: "flat", Rows: map[string]status.RowLayout{host: {Layout: "flat"}}}}, time.Now())
+ if err := status.WriteBox(ctx, e.rel, b); err != nil {
+ t.Fatal(err)
+ }
+ var out bytes.Buffer
+ if err := statusInstances(&out, "", ""); err != nil {
+ t.Fatal(err)
+ }
+ // No reconcile ran, so the unit matches no route and reads as the
+ // box's own: its REGION cell is the box's all the same.
+ if !strings.Contains(out.String(), "REGION") || !strings.Contains(out.String(), "London (uk)") {
+ t.Fatalf("status --instances:\n%s", out.String())
+ }
+ out.Reset()
+ if err := statusLayout(&out, ""); err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(out.String(), "BOX REGION HOST LAYOUT REASON") || !strings.Contains(out.String(), labelsUKBox+" London (uk) "+host) {
+ t.Fatalf("status --layout:\n%s", out.String())
+ }
+}
diff --git a/status_instances.go b/status_instances.go
index aebd33df6..2bff41686 100644
--- a/status_instances.go
+++ b/status_instances.go
@@ -106,7 +106,7 @@ func printStatusInstances(w io.Writer, rows []statusInstanceRow, account string)
return
}
t := newTable(w)
- fmt.Fprintln(t, "BOX\tHOST\tACCOUNT\tAPP\tCPU\tMEM")
+ fmt.Fprintln(t, "BOX\tREGION\tHOST\tACCOUNT\tAPP\tCPU\tMEM")
for _, r := range rows {
host, app := r.Host, r.App
if r.Platform {
@@ -117,8 +117,8 @@ func printStatusInstances(w io.Writer, rows []statusInstanceRow, account string)
// overhead in the picture.
host, app = r.Unit, "(platform)"
}
- fmt.Fprintf(t, "%s\t%s\t%s\t%s\t%s\t%s\n",
- r.Box, dashIfEmpty(host), dashIfEmpty(r.Account), dashIfEmpty(app),
+ fmt.Fprintf(t, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
+ r.Box, statusRegionCell(r.Region, r.RegionName), dashIfEmpty(host), dashIfEmpty(r.Account), dashIfEmpty(app),
cpuCell(r.CPU), memCell(r.Mem))
}
t.Flush()
@@ -218,7 +218,7 @@ func statusLayout(w io.Writer, consoleFlag string) error {
// column widths would drift the heading away from what was reviewed the
// moment any cell is wider than its header word.
func printStatusLayout(w io.Writer, boxes []statusLayoutBox) {
- type line struct{ box, host, layout, reason string }
+ type line struct{ box, region, host, layout, reason string }
var lines []line
for _, b := range boxes {
name := b.Box
@@ -241,7 +241,7 @@ func printStatusLayout(w io.Writer, boxes []statusLayoutBox) {
}
reason += r.Note
}
- lines = append(lines, line{name, r.Host, r.Layout, reason})
+ lines = append(lines, line{box: name, region: statusRegionCell(b.Region, b.RegionName), host: r.Host, layout: r.Layout, reason: reason})
}
}
if len(lines) == 0 {
@@ -254,9 +254,9 @@ func printStatusLayout(w io.Writer, boxes []statusLayoutBox) {
}
return lines[i].host < lines[j].host
})
- fmt.Fprintln(w, "BOX HOST LAYOUT REASON")
+ fmt.Fprintln(w, "BOX REGION HOST LAYOUT REASON")
for _, l := range lines {
- fmt.Fprintf(w, "%s %s %s %s\n", l.box, l.host, l.layout, dashIfEmpty(l.reason))
+ fmt.Fprintf(w, "%s %s %s %s %s\n", l.box, l.region, l.host, l.layout, dashIfEmpty(l.reason))
}
// A box with flat, foreign or external rows beside nested ones, or
// under a nested intent, is the loud case: each of those still shares
@@ -338,3 +338,9 @@ func memCell(v *int64) string {
}
return fmt.Sprintf("%.0fB", b)
}
+
+// statusRegionCell is the REGION column (region labels §5 rows 22 and 23):
+// the console's label, else its code, else a dash (an older console).
+func statusRegionCell(code, name string) string {
+ return dashIfEmpty(placeOr(name, code))
+}
diff --git a/status_instances_test.go b/status_instances_test.go
index b8d9d09ad..e08019046 100644
--- a/status_instances_test.go
+++ b/status_instances_test.go
@@ -64,7 +64,8 @@ func TestPrintStatusLayoutRendersSortedAndMarksStale(t *testing.T) {
printStatusLayout(&buf, boxes)
out := buf.String()
- if !strings.Contains(out, "BOX HOST LAYOUT REASON") {
+ // REGION joined the heading with the region labels (RL-C1).
+ if !strings.Contains(out, "BOX REGION HOST LAYOUT REASON") {
t.Fatalf("missing the approved heading:\n%s", out)
}
if !strings.Contains(out, "alpha (stale)") {
@@ -191,7 +192,9 @@ func TestPrintStatusLayoutSaysNestedByDefaultAndWhyNot(t *testing.T) {
var buf bytes.Buffer
printStatusLayout(&buf, boxes)
out := buf.String()
- if !strings.Contains(out, "kass-1 (nested by default) a.example.com nested") {
+ // These boxes carry no region (an older console): the REGION cell is
+ // the table's dash.
+ if !strings.Contains(out, "kass-1 (nested by default) "+dashIfEmpty("")+" a.example.com nested") {
t.Errorf("nested by default not marked on the box:\n%s", out)
}
if !strings.Contains(out, "kass-2 is not nested by default: isolation not ready: probe: pid1 fail") {
diff --git a/statuslive_regions_test.go b/statuslive_regions_test.go
new file mode 100644
index 000000000..abed7fa47
--- /dev/null
+++ b/statuslive_regions_test.go
@@ -0,0 +1,122 @@
+package main
+
+import (
+ "bytes"
+ "context"
+ "fmt"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/edgesfile"
+ "github.com/carlosframework/platform/internal/release"
+ "github.com/carlosframework/platform/internal/status"
+)
+
+func putLiveEdges(t *testing.T, s release.Store, box, placement, aws string) {
+ t.Helper()
+ body := fmt.Sprintf(`{"v":1,"box":%q,"eip":"192.0.2.10","region":%q,"placement":%q,"hosts":{}}`, box, aws, placement)
+ if _, err := s.Put(context.Background(), edgesfile.Key(box), []byte(body), ""); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// liveBucket is one named bucket for statusLiveWith: a directory store that
+// records every prefix it is asked to list.
+type liveBucket struct {
+ release.Store
+ mu sync.Mutex
+ lists []string
+}
+
+func (b *liveBucket) List(ctx context.Context, prefix string) ([]string, error) {
+ b.mu.Lock()
+ b.lists = append(b.lists, prefix)
+ b.mu.Unlock()
+ return b.Store.List(ctx, prefix)
+}
+
+func (b *liveBucket) listedEdges() bool {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ for _, p := range b.lists {
+ if strings.HasPrefix(p, edgesfile.Prefix) {
+ return true
+ }
+ }
+ return false
+}
+
+// TestStatusLiveWiring: Review Focus 4, labels R2-2 and Astra plan findings
+// 5 and 14, through statusLiveWith, where the stores are chosen: one bucket
+// for both, the status and deployment stores apart, no deployment store, and
+// a deployment store holding an edges file that will not parse.
+func TestStatusLiveWiring(t *testing.T) {
+ ctx, now := context.Background(), time.Now()
+ bucket := func(withBox bool, edges ...string) *liveBucket {
+ b := &liveBucket{Store: release.NewDirStore(t.TempDir())}
+ if withBox {
+ if err := status.WriteBox(ctx, b, status.Box{Label: "uk-box", Region: "London-ish", GeneratedAt: now, Metrics: &status.BoxMetrics{}}); err != nil {
+ t.Fatal(err)
+ }
+ }
+ for _, e := range edges {
+ switch e {
+ case "uk":
+ putLiveEdges(t, b, "uk-box", "uk", "eu-west-2")
+ case "bad":
+ if _, err := b.Put(ctx, edgesfile.Key("bad-box"), []byte("{not json"), ""); err != nil {
+ t.Fatal(err)
+ }
+ }
+ }
+ return b
+ }
+ run := func(env map[string]string, buckets map[string]*liveBucket) string {
+ t.Helper()
+ var out bytes.Buffer
+ open := func(_ context.Context, name string) (release.Store, error) {
+ if b, ok := buckets[name]; ok {
+ return b, nil
+ }
+ return nil, fmt.Errorf("no bucket %q in this test", name)
+ }
+ if err := statusLiveWith(&out, func(k string) string { return env[k] }, open); err != nil {
+ t.Fatal(err)
+ }
+ return out.String()
+ }
+ const warn = "region names could not be read"
+
+ one := bucket(true, "uk")
+ if out := run(map[string]string{"CARLOS_DEPLOYMENT_BUCKET": "one"}, map[string]*liveBucket{"one": one}); !strings.Contains(out, "London (uk)") ||
+ strings.Contains(out, warn) || !one.listedEdges() {
+ t.Fatalf("one bucket for both:\n%s", out)
+ }
+
+ st, dep := bucket(true), bucket(false, "uk")
+ if out := run(map[string]string{"CARLOS_STATUS_BUCKET": "st", "CARLOS_DEPLOYMENT_BUCKET": "dep"},
+ map[string]*liveBucket{"st": st, "dep": dep}); !strings.Contains(out, "London (uk)") || st.listedEdges() || !dep.listedEdges() {
+ t.Fatalf("stores apart: edges must come from the deployment store alone:\n%s", out)
+ }
+
+ only := bucket(true, "uk") // edges here are the status store's, not a deployment store's
+ if out := run(map[string]string{"CARLOS_STATUS_BUCKET": "only"}, map[string]*liveBucket{"only": only}); !strings.Contains(out, "London-ish") ||
+ strings.Count(out, warn) != 1 || only.listedEdges() {
+ t.Fatalf("no deployment store:\n%s", out)
+ }
+
+ bad := bucket(true, "uk", "bad")
+ if out := run(map[string]string{"CARLOS_DEPLOYMENT_BUCKET": "bad"}, map[string]*liveBucket{"bad": bad}); !strings.Contains(out, "London-ish") ||
+ strings.Contains(out, "London (uk)") || strings.Count(out, warn) != 1 {
+ t.Fatalf("an incomplete edges read is not read:\n%s", out)
+ }
+
+ // printLiveBoxes, as every existing test calls it: no places, no line.
+ var b bytes.Buffer
+ printLiveBoxes(&b, []status.Box{{GeneratedAt: now, Label: "uk-box", Region: "London-ish", Metrics: &status.BoxMetrics{}}}, now)
+ if strings.Contains(b.String(), warn) || !strings.Contains(b.String(), "London-ish") {
+ t.Fatalf("printLiveBoxes changed:\n%s", b.String())
+ }
+}