-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathstate.go
More file actions
515 lines (478 loc) · 14.6 KB
/
state.go
File metadata and controls
515 lines (478 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/sst/sst/v3/cmd/sst/cli"
"github.com/sst/sst/v3/cmd/sst/mosaic/ui"
"github.com/sst/sst/v3/internal/util"
"github.com/sst/sst/v3/pkg/id"
"github.com/sst/sst/v3/pkg/process"
"github.com/sst/sst/v3/pkg/project/provider"
"github.com/sst/sst/v3/pkg/state"
)
var CmdState = &cli.Command{
Name: "state",
Description: cli.Description{
Short: "Manage state of your app",
},
Children: []*cli.Command{
{
Name: "edit",
Description: cli.Description{
Short: "Edit the state of your app",
Long: strings.Join([]string{
"Edit the raw state of your app directly.",
"",
"This opens your state file in your local editor (`$EDITOR`, or `vim` by default).",
"When you save and exit, SST pushes those changes back to your backend.",
"",
":::danger",
"This command is dangerous. If you make an invalid change, you can corrupt your state and break deploys.",
"Only use this if you understand the state format and know exactly what you are changing.",
"Consider using safer commands like `sst state remove` or `sst state repair` first.",
":::",
}, "\n"),
},
Run: func(c *cli.Cli) error {
p, err := c.InitProject()
if err != nil {
return err
}
defer p.Cleanup()
update, err := p.Lock("edit")
if err != nil {
return util.NewReadableError(err, "Could not lock state")
}
defer p.Unlock()
defer func() {
update.TimeCompleted = time.Now().UTC().Format(time.RFC3339)
provider.PutUpdate(p.Backend(), p.App().Name, p.App().Stage, update)
}()
workdir, err := p.NewWorkdir(update.ID)
if err != nil {
return err
}
defer workdir.Cleanup()
path, err := workdir.Pull()
if err != nil {
return util.NewReadableError(err, "Could not pull state")
}
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vim"
}
editorArgs := append(strings.Fields(editor), path)
fmt.Println(editorArgs)
cmd := process.Command(editorArgs[0], editorArgs[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return util.NewReadableError(err, "Could not start editor")
}
if err := cmd.Wait(); err != nil {
return util.NewReadableError(err, "Editor exited with error")
}
return workdir.Push(update.ID)
},
},
{
Name: "export",
Flags: []cli.Flag{
{
Name: "decrypt",
Type: "bool",
Description: cli.Description{
Short: "Decrypt the state",
Long: "Decrypt the state before printing it out.",
},
},
},
Description: cli.Description{
Short: "Prints the state of your app",
Long: strings.Join([]string{
"Prints the state of your app.",
"",
"This pull the state of your app from the cloud provider and then prints it out.",
"You can write this to a file or view it directly in your terminal.",
"",
"This can be run for specific stages as well.",
"",
"```bash frame=\"none\"",
"sst state export --stage production",
"```",
"",
"By default, it runs on your personal stage.",
}, "\n"),
},
Run: func(c *cli.Cli) error {
p, err := c.InitProject()
if err != nil {
return err
}
defer p.Cleanup()
workdir, err := p.NewWorkdir(id.Descending())
if err != nil {
return err
}
defer workdir.Cleanup()
_, err = workdir.Pull()
if err != nil {
return util.NewReadableError(err, "Could not pull state")
}
exported, err := workdir.Export()
if err != nil {
return err
}
if c.Bool("decrypt") {
passphrase, err := provider.Passphrase(p.Backend(), p.App().Name, p.App().Stage)
if err != nil {
return err
}
exported, err = state.Decrypt(c.Context, passphrase, exported)
if err != nil {
return err
}
}
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
return encoder.Encode(exported)
},
},
{
Name: "list",
Description: cli.Description{
Short: "List all deployed stages",
Long: strings.Join([]string{
"Lists all the stages of your app for the current set of credentials.",
"",
":::note",
"This does not list the stages that are deployed in other accounts.",
":::",
"",
"This pulls the state of your app from the cloud provider and then prints out all the stages that are listed in the state.",
}, "\n"),
},
Run: func(c *cli.Cli) error {
p, err := c.InitProject()
if err != nil {
return err
}
defer p.Cleanup()
backend := p.Backend()
currentStage := p.App().Stage
stages, err := provider.ListStages(backend, p.App().Name)
if err != nil {
return err
}
lines, err := provider.Info(backend)
if err != nil {
ui.Error("Failed to load provider information")
return err
}
renderKeyValue("App", p.App().Name)
for _, line := range lines {
renderKeyValue(line.Key, line.Value)
}
if len(stages) == 0 {
fmt.Println(
ui.TEXT_NORMAL_BOLD.Render(indent("Stages:")) +
ui.TEXT_NORMAL.Render(currentStage) + " " + ui.TEXT_WARNING_DIM.Render("(not deployed)"),
)
return nil
}
currentDeployed := false
for i, stage := range stages {
rendered := ui.TEXT_GRAY.Render(stage)
if stage == currentStage {
rendered = ui.TEXT_NORMAL.Render(stage)
currentDeployed = true
}
if i == 0 {
fmt.Println(ui.TEXT_NORMAL_BOLD.Render(indent("Stages:")) + rendered)
continue
}
fmt.Println(indent("") + rendered)
}
if !currentDeployed {
fmt.Println(indent("") + ui.TEXT_NORMAL.Render(currentStage) + " " + ui.TEXT_WARNING_DIM.Render("(not deployed)"))
}
return nil
},
},
{
Name: "remove",
Args: []cli.Argument{
{
Name: "target",
Required: true,
Description: cli.Description{
Short: "The name of the resource to remove",
Long: "The name of the resource to remove.",
},
},
},
Description: cli.Description{
Short: "Remove a resource from only the state",
Long: strings.Join([]string{
"Removes the reference for the given resource from the state.",
"",
":::note",
"This does not remove the resource itself.",
":::",
"",
"This does not remove the resource itself, it only edits the state of your app.",
"",
"```bash frame=\"none\"",
"sst state remove MyBucket",
"```",
"",
"Here, `MyBucket` is the name of the resource as defined in your `sst.config.ts`.",
"",
"```ts title=\"sst.config.ts\"",
"new sst.aws.Bucket(\"MyBucket\");",
"```",
"",
"This command will:",
"",
"1. Find the resource with the given name in the state.",
"2. Remove that from the state. It does not remove the children of this resource.",
"3. Runs a `repair` to remove any dependencies to this resource.",
"",
"You can run this for specific stages as well.",
"",
"```bash frame=\"none\"",
"sst state remove MyBucket --stage production",
"```",
"",
"By default, it runs on your personal stage.",
}, "\n"),
},
Run: func(c *cli.Cli) error {
p, err := c.InitProject()
if err != nil {
return err
}
defer p.Cleanup()
update, err := p.Lock("edit")
if err != nil {
return util.NewReadableError(err, "Could not lock state")
}
defer p.Unlock()
defer func() {
update.TimeCompleted = time.Now().UTC().Format(time.RFC3339)
provider.PutUpdate(p.Backend(), p.App().Name, p.App().Stage, update)
}()
workdir, err := p.NewWorkdir(update.ID)
if err != nil {
return err
}
defer workdir.Cleanup()
_, err = workdir.Pull()
if err != nil {
return util.NewReadableError(err, "Could not pull state")
}
checkpoint, err := workdir.Export()
if err != nil {
return util.NewReadableError(err, "Could not export state")
}
target := c.Positional(0)
muts := state.Remove(target, checkpoint)
err = confirmMutations(muts)
if err != nil {
return err
}
err = workdir.Import(checkpoint)
if err != nil {
return util.NewReadableError(err, "Could not import state")
}
err = workdir.Push(update.ID)
if err != nil {
return err
}
ui.Success("Resource removed")
return nil
},
},
{
Name: "repair",
Description: cli.Description{
Short: "Repair the state of your app",
Long: strings.Join([]string{
"Repairs the state of your app if it's corrupted.",
"",
"Sometimes, if something goes wrong with your app, or if the state was directly",
"edited, the state can become corrupted. This will cause your `sst deploy` command",
"to fail.",
"",
"This command looks for the following issues and fixes them.",
"",
"1. Since the state is a list of resources, if one resource depends on another,",
" it needs to be listed after the one it depends on. This command finds resources",
" that depend on each other but are not ordered correctly and **reorders them**.",
"",
"2. If resource B depends on resource A, but resource A is not listed in the state,",
" it'll **remove the dependency**.",
"",
"This command does this by going through all the resources in the state, fixing the",
"issues and updating the state.",
"",
"You can run this for specific stages as well.",
"",
"```bash frame=\"none\"",
"sst state repair --stage production",
"```",
"",
"If the current state cannot be read, you can opt into restoring the latest valid",
"snapshot with `--dangerously-revert`. This is unsafe and can orphan or recreate",
"resources on future deploys.",
"",
"By default, it runs on your personal stage.",
}, "\n"),
},
Flags: []cli.Flag{
{
Name: "dangerously-revert",
Type: "bool",
Description: cli.Description{
Short: "Dangerously restore latest valid snapshot if current state cannot be read",
},
},
},
Run: func(c *cli.Cli) error {
p, err := c.InitProject()
if err != nil {
return err
}
defer p.Cleanup()
update, err := p.Lock("repair")
if err != nil {
return util.NewReadableError(err, "Could not lock state")
}
defer p.Unlock()
defer func() {
update.TimeCompleted = time.Now().UTC().Format(time.RFC3339)
provider.PutUpdate(p.Backend(), p.App().Name, p.App().Stage, update)
}()
workdir, err := p.NewWorkdir(update.ID)
if err != nil {
return err
}
defer workdir.Cleanup()
_, pullErr := workdir.Pull()
if pullErr != nil && !errors.Is(pullErr, provider.ErrStateNotFound) {
return util.NewReadableError(pullErr, "Could not pull state")
}
recoveredSnapshotID := ""
var checkpoint *apitype.CheckpointV3
if pullErr == nil {
checkpoint, err = workdir.Export()
}
if pullErr != nil || err != nil {
if !c.Bool("dangerously-revert") {
return repairSnapshotFlagRequired(pullErr, err)
}
snapshot, snapshotID, recoverErr := provider.LatestValidSnapshot(p.Backend(), p.App().Name, p.App().Stage)
if recoverErr != nil {
return util.NewReadableError(recoverErr, "Could not recover state")
}
err = workdir.ImportRaw(snapshot)
if err != nil {
return util.NewReadableError(err, "Could not restore snapshot")
}
recoveredSnapshotID = snapshotID
checkpoint, err = workdir.Export()
if err != nil {
return util.NewReadableError(err, "Could not export state")
}
}
if checkpoint == nil {
return util.NewReadableError(nil, "Could not export state")
}
muts := state.Repair(checkpoint)
err = confirmRepairMutations(recoveredSnapshotID, muts)
if err != nil {
return err
}
// prompt for confirmation to continue
fmt.Print("Do you want to commit these changes? (y/n): ")
var response string
_, err = fmt.Scanln(&response)
if err != nil {
return fmt.Errorf("failed to read user input: %w", err)
}
if strings.ToLower(response) != "y" {
return util.NewReadableError(nil, "Cancelled repair")
}
err = workdir.Import(checkpoint)
if err != nil {
return util.NewReadableError(err, "Could not import state")
}
err = workdir.Push(update.ID)
if err != nil {
return err
}
ui.Success("State repaired")
return nil
},
},
},
}
func confirmMutations(muts []state.Mutation) error {
if len(muts) == 0 {
return util.NewReadableError(nil, "No changes made")
}
fmt.Println("Removing:")
for _, item := range muts {
if item.Remove != nil {
fmt.Printf("- %s → %s\n", item.Remove.Resource.Type().DisplayName(), item.Remove.Resource.Name())
}
if item.RemoveDependency != nil {
fmt.Printf("- dependency from %s → %s on %s → %s\n", item.RemoveDependency.Resource.Type().DisplayName(), item.RemoveDependency.Resource.Name(), item.RemoveDependency.Dependency.Type().DisplayName(), item.RemoveDependency.Dependency.Name())
}
if item.RemoveProperty != nil {
fmt.Printf("- property dependency from %s → %s → %s on %s → %s\n", item.RemoveProperty.Resource.URNName(), item.RemoveProperty.Resource.Name(), item.RemoveProperty.Property, item.RemoveProperty.Dependency.Type().DisplayName(), item.RemoveProperty.Dependency.Name())
}
}
// prompt for confirmation to continue
fmt.Print("Do you want to commit these changes? (y/n): ")
var response string
_, err := fmt.Scanln(&response)
if err != nil {
return util.NewReadableError(err, "failed to read user input")
}
if strings.ToLower(response) != "y" {
return util.NewReadableError(nil, "Abandoning changes")
}
return nil
}
func confirmRepairMutations(snapshotID string, muts []state.Mutation) error {
if snapshotID != "" {
fmt.Printf("Recovering state from snapshot: %s\n", snapshotID)
}
if len(muts) == 0 {
if snapshotID == "" {
return util.NewReadableError(nil, "No changes made")
}
return nil
}
return confirmMutations(muts)
}
func repairSnapshotFlagRequired(pullErr error, exportErr error) error {
cause := exportErr
if cause == nil {
cause = pullErr
}
return util.NewReadableError(cause, "State is missing or corrupted. Re-run `sst state repair --dangerously-revert` to restore the latest valid snapshot. This can orphan or recreate resources.")
}
func indent(key string) string {
return fmt.Sprintf("%-12s", key)
}
func renderKeyValue(key string, value string) {
fmt.Println(ui.TEXT_NORMAL_BOLD.Render(indent(key+":")) + ui.TEXT_GRAY.Render(value))
}