cli: carlos instances create --restore (#658)
Adds --restore to instances create. The request body carries "restore": true only when asked; ordinary creates send the same bytes as before. When the console's answer says restore: true the command prints "restore accepted" and a line saying the instance wakes with the data it kept; an older console that ignores the field gets the ordinary "created instance" line. --restore with --from, --kind, --addr or --role is refused before any request. Approved copy is committed alongside.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+180
−7
apimode.go+10 −4canary.go+1 −1docs/superpowers/notes/instance-restore-658-cli-copy.json+23 −0instances_cli.go+15 −2instances_restore_test.go+131 −0
diff --git a/apimode.go b/apimode.go| index fd8d23e88..15b8e0e09 100644 |
| --- a/apimode.go |
| +++ b/apimode.go |
| @@ -3363,6 +3363,9 @@ func (s *apiSession) instancesEnable(ctx context.Context, a apiAcct, app string, |
| // rather than imported because this client renders records, never writes |
| // them, and the fields it renders are the contract worth pinning. |
| type apiInstanceRecord struct { |
| + // Restore is true when the create door accepted the request as a restore |
| + // (#658); false from an older console that ignored the field. |
| + Restore bool `json:"restore"` |
| Host string `json:"host"` |
| Slug string `json:"slug"` |
| Pool string `json:"pool"` |
| @@ -3603,13 +3606,15 @@ func (rq *instanceReplicaReq) set() []string { |
| // probe nil means unprobed — the same "absent, not zero" contract the wire |
| // carries for every optional field on this door. |
| func (s *apiSession) instanceCreate(ctx context.Context, a apiAcct, app, host, region, channel, kind, environment string, addrs []string, probe *instanceProbeReq, replicas *instanceReplicaReq, messages ...string) (apiInstanceRecord, error) { |
| - return s.instanceCreateScoped(ctx, a, app, host, region, channel, kind, environment, "", addrs, probe, replicas, messages...) |
| + return s.instanceCreateScoped(ctx, a, app, host, region, channel, kind, environment, "", addrs, probe, replicas, false, messages...) |
| } |
| // instanceCreateScoped is instanceCreate with the person's explicit scope |
| // for binding into an environment a team scope owns (`--scope`, |
| -// team-scoped credentials §3); "" for every other create. |
| -func (s *apiSession) instanceCreateScoped(ctx context.Context, a apiAcct, app, host, region, channel, kind, environment, scope string, addrs []string, probe *instanceProbeReq, replicas *instanceReplicaReq, messages ...string) (apiInstanceRecord, error) { |
| +// team-scoped credentials §3); "" for every other create. restore asks the |
| +// console to bring back a deleted instance on host (#658) and is omitted from |
| +// the body when false. |
| +func (s *apiSession) instanceCreateScoped(ctx context.Context, a apiAcct, app, host, region, channel, kind, environment, scope string, addrs []string, probe *instanceProbeReq, replicas *instanceReplicaReq, restore bool, messages ...string) (apiInstanceRecord, error) { |
| message := firstMutationMessage(messages) |
| in := struct { |
| Host string `json:"host"` |
| @@ -3623,7 +3628,8 @@ func (s *apiSession) instanceCreateScoped(ctx context.Context, a apiAcct, app, h |
| Role string `json:"role,omitempty"` |
| Replicas []string `json:"replicas,omitempty"` |
| Message string `json:"message,omitempty"` |
| - }{host, region, channel, kind, environment, scope, addrs, probe, replicas.role(), replicas.set(), message} |
| + Restore bool `json:"restore,omitempty"` |
| + }{host, region, channel, kind, environment, scope, addrs, probe, replicas.role(), replicas.set(), message, restore} |
| status, body, err := s.postJSON(ctx, apiPath(a, app, "/instances"), in) |
| if err != nil { |
| return apiInstanceRecord{}, err |
diff --git a/canary.go b/canary.go| index 1c260628c..d9eb21290 100644 |
| --- a/canary.go |
| +++ b/canary.go |
| @@ -589,7 +589,7 @@ func runCanaryDeploy(args []string) { |
| case canaryPlanTaken: |
| log.Fatalf("canary: %s", canaryHostTaken(host, canaryHostChannel(pre, recs, host))) |
| case canaryPlanCreate: |
| - rec, cerr := sess.instanceCreateScoped(ctx, acct, app2, host, strings.TrimSpace(*regionFlag), channel, recKind, env, strings.TrimSpace(*scopeFlag), nil, nil, nil) |
| + rec, cerr := sess.instanceCreateScoped(ctx, acct, app2, host, strings.TrimSpace(*regionFlag), channel, recKind, env, strings.TrimSpace(*scopeFlag), nil, nil, nil, false) |
| if cerr != nil { |
| log.Fatalf("canary: create %s: %v", host, cerr) |
| } |
diff --git a/docs/superpowers/notes/instance-restore-658-cli-copy.json b/docs/superpowers/notes/instance-restore-658-cli-copy.json| new file mode 100644 |
| index 000000000..565b39daa |
| --- /dev/null |
| +++ b/docs/superpowers/notes/instance-restore-658-cli-copy.json |
| @@ -0,0 +1,23 @@ |
| +{ |
| + "approved_by": "Paul", |
| + "approved_on": "2026-11-08", |
| + "review": "quvrxpwioc7x3stkg5zt3xyxre", |
| + "strings": [ |
| + { |
| + "id": "cli.restore.flag", |
| + "text": "bring back a deleted instance on this host with the data it kept: the same app's plain instance, created and deleted after restore was available" |
| + }, |
| + { |
| + "id": "cli.restore.done", |
| + "text": "restore accepted" |
| + }, |
| + { |
| + "id": "cli.restore.wakes", |
| + "text": "it wakes with whatever data it kept when it was deleted" |
| + }, |
| + { |
| + "id": "cli.restore.plainonly", |
| + "text": "a restore is a plain instance. Drop --from, --kind, --addr and --role." |
| + } |
| + ] |
| +} |
| \ No newline at end of file |
diff --git a/instances_cli.go b/instances_cli.go| index 730ab193b..fcf48a568 100644 |
| --- a/instances_cli.go |
| +++ b/instances_cli.go |
| @@ -327,6 +327,7 @@ func runInstancesCreate(args []string) { |
| from := fs.String("from", "", "copy this instance's database into the new one. The source must be an instance of the same app, in this console's region") |
| at := fs.Int64("at", 0, "with --from, the transaction id to copy at (default: the latest)") |
| replicas := fs.String("replicas", "", "comma-separated placement codes the account's replicas live in (the HOME record's opt-in; e.g. ie,uk) — this instance's own replica must be --role replica with --region") |
| + restore := fs.Bool("restore", false, "bring back a deleted instance on this host with the data it kept: the same app's plain instance, created and deleted after restore was available") |
| fs.Usage = flagUsage(fs, instancesRecordsUsage) |
| fs.Parse(args) |
| if *host == "" { |
| @@ -342,6 +343,9 @@ func runInstancesCreate(args []string) { |
| if *from != "" && (*kind != "" || set["addr"] || *role != "" || *replicas != "") { |
| log.Fatalf("instances create: %s", "a seeded instance is an ordinary instance. Drop --kind, --addr and --role.") |
| } |
| + if *restore && (*from != "" || *kind != "" || set["addr"] || *role != "") { |
| + log.Fatalf("instances create: %s", "a restore is a plain instance. Drop --from, --kind, --addr and --role.") |
| + } |
| sess := requireAPISession("create", *consoleFlag) |
| printUsing(stdout, sess) |
| acct := resolvedAccount(sess, acctRaw, acctIDRaw) |
| @@ -360,11 +364,17 @@ func runInstancesCreate(args []string) { |
| rec, err := sess.instanceCreateScoped(context.Background(), acct, app2, |
| strings.TrimSpace(*host), strings.TrimSpace(*region), strings.TrimSpace(*channel), |
| strings.TrimSpace(*kind), strings.TrimSpace(*environment), strings.TrimSpace(*scope), addrs, probe, |
| - replicaReqFromFlags(*role, *replicas, set["region"]), message) |
| + replicaReqFromFlags(*role, *replicas, set["region"]), *restore, message) |
| if err != nil { |
| log.Fatalf("instances create: %v", err) |
| } |
| - fmt.Fprintf(stdout, "%s %s\n", colorFor(stdout).ok("created instance"), |
| + // Only the answer's own word says a restore was accepted: an older |
| + // console ignores the field and made an ordinary instance. |
| + accepted := "created instance" |
| + if *restore && rec.Restore { |
| + accepted = "restore accepted" |
| + } |
| + fmt.Fprintf(stdout, "%s %s\n", colorFor(stdout).ok(accepted), |
| instanceCreatedLine(rec, environmentSuffix(rec.Environment))) |
| if rec.PrivateStoreBucket != "" { |
| mutedf(stdout, " private storage: bucket %s\n", rec.PrivateStoreBucket) |
| @@ -372,6 +382,9 @@ func runInstancesCreate(args []string) { |
| if line := instanceRuntimeLine(rec); line != "" { |
| mutedf(stdout, " %s", line) |
| } |
| + if *restore && rec.Restore { |
| + mutedf(stdout, " it wakes with whatever data it kept when it was deleted\n") |
| + } |
| mutedf(stdout, " the host comes live on the next reconcile pass on its box") |
| } |
diff --git a/instances_restore_test.go b/instances_restore_test.go| new file mode 100644 |
| index 000000000..b812573e7 |
| --- /dev/null |
| +++ b/instances_restore_test.go |
| @@ -0,0 +1,131 @@ |
| +package main |
| + |
| +import ( |
| + "io" |
| + "net/http" |
| + "net/http/httptest" |
| + "strings" |
| + "sync" |
| + "testing" |
| +) |
| + |
| +// restoreConsole is a console whose create door records every body it is |
| +// sent and answers 201 with the given JSON. |
| +type restoreConsole struct { |
| + mu sync.Mutex |
| + bodies []string |
| + answer string |
| +} |
| + |
| +func (c *restoreConsole) serve(t *testing.T) *httptest.Server { |
| + t.Helper() |
| + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + b, _ := io.ReadAll(r.Body) |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + if r.Method == http.MethodPost && r.URL.Path == "/api/cli/apps/bab/jam/instances" { |
| + c.bodies = append(c.bodies, string(b)) |
| + w.Header().Set("Content-Type", "application/json") |
| + w.WriteHeader(http.StatusCreated) |
| + io.WriteString(w, c.answer) |
| + return |
| + } |
| + c.bodies = append(c.bodies, "UNEXPECTED "+r.Method+" "+r.URL.Path) |
| + http.NotFound(w, r) |
| + })) |
| + t.Cleanup(ts.Close) |
| + return ts |
| +} |
| + |
| +func runInstancesCreateBin(t *testing.T, ts *httptest.Server, args ...string) (string, error) { |
| + t.Helper() |
| + bin := buildCarlos(t) |
| + full := append([]string{"instances", "create", "--app", "jam", "--account", "bab", "--host", "acme.jam.bab.oncarlos.com"}, args...) |
| + return runCarlosEnv(t, bin, full, "CARLOS_SERVICE_TOKEN=tok", "CARLOS_CONSOLE="+ts.URL) |
| +} |
| + |
| +const restoreAnswerOn = `{"host":"acme.jam.bab.oncarlos.com","slug":"acme","pool":"p","region":"ie","channel":"stable","restore":true}` |
| +const restoreAnswerOff = `{"host":"acme.jam.bab.oncarlos.com","slug":"acme","pool":"p","region":"ie","channel":"stable"}` |
| + |
| +func TestInstancesCreateRestoreSendsTheFlagOnlyWhenGiven(t *testing.T) { |
| + c := &restoreConsole{answer: restoreAnswerOn} |
| + ts := c.serve(t) |
| + if out, err := runInstancesCreateBin(t, ts, "--restore"); err != nil { |
| + t.Fatalf("create --restore: %v\n%s", err, out) |
| + } |
| + if len(c.bodies) != 1 || !strings.Contains(c.bodies[0], `"restore":true`) { |
| + t.Fatalf("bodies = %q, want one carrying \"restore\":true", c.bodies) |
| + } |
| + c.bodies = nil |
| + c.answer = restoreAnswerOff |
| + if out, err := runInstancesCreateBin(t, ts); err != nil { |
| + t.Fatalf("plain create: %v\n%s", err, out) |
| + } |
| + if len(c.bodies) != 1 || strings.Contains(c.bodies[0], "restore") { |
| + t.Fatalf("bodies = %q, want one with no restore key", c.bodies) |
| + } |
| +} |
| + |
| +func TestInstancesCreateRestorePrintsWhatTheAnswerSays(t *testing.T) { |
| + c := &restoreConsole{answer: restoreAnswerOn} |
| + ts := c.serve(t) |
| + out, err := runInstancesCreateBin(t, ts, "--restore") |
| + if err != nil { |
| + t.Fatalf("create --restore: %v\n%s", err, out) |
| + } |
| + wakes := " it wakes with whatever data it kept when it was deleted" |
| + live := "the host comes live on the next reconcile pass on its box" |
| + if !strings.Contains(out, "restore accepted ") || strings.Contains(out, "created instance") { |
| + t.Errorf("output lacks the restore line or still says created:\n%s", out) |
| + } |
| + i, j := strings.Index(out, wakes), strings.Index(out, live) |
| + if i < 0 || j < 0 || i > j { |
| + t.Errorf("want the wakes line before the reconcile line:\n%s", out) |
| + } |
| + |
| + // An older console ignores the field: the answer has no restore, and |
| + // the CLI must not claim one. |
| + c.answer = restoreAnswerOff |
| + out, err = runInstancesCreateBin(t, ts, "--restore") |
| + if err != nil { |
| + t.Fatalf("create --restore, old console: %v\n%s", err, out) |
| + } |
| + if !strings.Contains(out, "created instance ") || strings.Contains(out, "restore accepted") || strings.Contains(out, "it wakes with") { |
| + t.Errorf("an answer without restore must read as an ordinary create:\n%s", out) |
| + } |
| + |
| + c.answer = restoreAnswerOff |
| + out, err = runInstancesCreateBin(t, ts) |
| + if err != nil || !strings.Contains(out, "created instance ") || strings.Contains(out, "restore accepted") { |
| + t.Errorf("plain create: %v\n%s", err, out) |
| + } |
| +} |
| + |
| +func TestInstancesCreateRestoreRefusesTheOtherShapes(t *testing.T) { |
| + const want = "instances create: a restore is a plain instance. Drop --from, --kind, --addr and --role." |
| + for _, extra := range [][]string{ |
| + {"--from", "other.jam.bab.oncarlos.com"}, |
| + {"--kind", "service"}, |
| + {"--addr", "10.0.0.1:8090"}, |
| + {"--role", "replica"}, |
| + } { |
| + c := &restoreConsole{answer: restoreAnswerOn} |
| + ts := c.serve(t) |
| + out, err := runInstancesCreateBin(t, ts, append([]string{"--restore"}, extra...)...) |
| + if err == nil || !strings.Contains(out, want) { |
| + t.Errorf("--restore %v: err %v, output\n%s\nwant %q", extra, err, out, want) |
| + } |
| + if len(c.bodies) != 0 { |
| + t.Errorf("--restore %v sent requests: %q", extra, c.bodies) |
| + } |
| + } |
| +} |
| + |
| +func TestInstancesCreateHelpListsRestore(t *testing.T) { |
| + bin := buildCarlos(t) |
| + out, _ := runCarlosEnv(t, bin, []string{"instances", "create", "-h"}) |
| + want := "bring back a deleted instance on this host with the data it kept: the same app's plain instance, created and deleted after restore was available" |
| + if !strings.Contains(out, "-restore") || !strings.Contains(out, want) { |
| + t.Errorf("instances create -h lacks the restore flag or its help:\n%s", out) |
| + } |
| +} |