-
Notifications
You must be signed in to change notification settings - Fork 5
Add snapshot save command (local) #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
anisaoshafi
merged 26 commits into
main
from
drg-764-migrate-state-commands-from-cli-v1-export
May 13, 2026
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
a8208b5
Add snapshot save command
anisaoshafi cff0533
Absolute path and different error
anisaoshafi 21682bb
snapshot only works for aws
anisaoshafi ea992e4
OS-agnostic destination check
anisaoshafi 208e182
Drop needless interface
anisaoshafi 9d04c57
Handle conflicts
anisaoshafi 3d91a28
Drop 'snapshot' subcommand in favor of simplicity
anisaoshafi 84b52ba
Restore 'snapshot' command in favor of clarity
anisaoshafi 4e05dcf
Fix linting & paralellize tests
anisaoshafi 8171252
Improvements
anisaoshafi 475e44c
Move ExportState into aws.Client
anisaoshafi e915375
change default snapshot name
anisaoshafi 420dfdb
local snapshot save always has .zip extension
anisaoshafi aac8705
Remove partial snapshot file when write fails mid-stream
anisaoshafi 19762fc
Use isolated HOME in TestSnapshotSaveLocalStackNotRunning
anisaoshafi f66ba56
Test NewPlainSinkSplit event routing behavior
anisaoshafi ec75a46
Nits
anisaoshafi 9aba0ba
Improve parsing logic
anisaoshafi 2345902
Clean up snapshot save command and plain sink
anisaoshafi 04e7ec3
Use gomock instead
anisaoshafi 16e82a7
Move StateExporter interface where it's used
anisaoshafi a4f776d
Remove redundant tests; handle as test cases
anisaoshafi 112f736
Change ExportState so HTTP response body is always closed inside the …
anisaoshafi 4015b76
Adjust snapshot name when destination not provided: append 3 char hex
anisaoshafi b89d369
Improve rendering of the path in the success message
anisaoshafi 60503f4
Address suggestions
anisaoshafi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/localstack/lstk/internal/config" | ||
| "github.com/localstack/lstk/internal/emulator/aws" | ||
| "github.com/localstack/lstk/internal/endpoint" | ||
| "github.com/localstack/lstk/internal/env" | ||
| "github.com/localstack/lstk/internal/output" | ||
| "github.com/localstack/lstk/internal/runtime" | ||
| "github.com/localstack/lstk/internal/snapshot" | ||
| "github.com/localstack/lstk/internal/ui" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func newSnapshotCmd(cfg *env.Env) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "snapshot", | ||
| Short: "Manage emulator snapshots", | ||
| } | ||
| cmd.AddCommand(newSnapshotSaveCmd(cfg)) | ||
| return cmd | ||
| } | ||
|
|
||
| func newSnapshotSaveCmd(cfg *env.Env) *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "save [destination]", | ||
| Short: "Save a snapshot of the emulator state", | ||
| Long: `Save a snapshot of the running emulator's state. | ||
|
|
||
| Pass [destination] as an absolute or relative path for the exported file: | ||
|
|
||
| lstk snapshot save # saves to ./snapshot-<YYYY-MM-DDTHH-mm-ss>-<hex>.zip | ||
| lstk snapshot save ./my-snapshot.zip # saves to ./my-snapshot.zip | ||
| lstk snapshot save /tmp/my-state # saves to /tmp/my-state.zip | ||
|
|
||
| Cloud destinations are not yet supported.`, | ||
| Args: cobra.MaximumNArgs(1), | ||
| PreRunE: initConfig(nil), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| var destArg string | ||
| if len(args) > 0 { | ||
| destArg = args[0] | ||
| } | ||
|
|
||
| dest, err := snapshot.ParseDestination(destArg, time.Now()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| appConfig, err := config.Get() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get config: %w", err) | ||
| } | ||
|
|
||
| var awsContainer config.ContainerConfig | ||
| var found bool | ||
| for _, c := range appConfig.Containers { | ||
| if c.Type == config.EmulatorAWS { | ||
| awsContainer = c | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| if !found { | ||
| return fmt.Errorf("snapshot is only supported for the AWS emulator") | ||
| } | ||
|
|
||
| rt, err := runtime.NewDockerRuntime(cfg.DockerHost) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| host, _ := endpoint.ResolveHost(cmd.Context(), awsContainer.Port, cfg.LocalStackHost) | ||
| exporter := aws.NewClient() | ||
|
|
||
| if isInteractiveMode(cfg) { | ||
| return ui.RunSnapshotSave(cmd.Context(), rt, []config.ContainerConfig{awsContainer}, exporter, host, dest) | ||
| } | ||
| return snapshot.Save(cmd.Context(), rt, []config.ContainerConfig{awsContainer}, exporter, host, dest, output.NewPlainSink(os.Stdout)) | ||
| }, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package snapshot | ||
|
|
||
| import ( | ||
| "crypto/rand" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| var ( | ||
| // ErrRemoteNotSupported is returned for known remote schemes (s3://, oras://, cloud:). | ||
| ErrRemoteNotSupported = errors.New("remote destinations are not yet supported — coming soon") | ||
| // ErrUnknownScheme is returned for unrecognized URL schemes. | ||
| ErrUnknownScheme = errors.New("unrecognized destination scheme") | ||
| ) | ||
|
|
||
| // displayPath shortens abs for human-readable output: | ||
| // under cwd → ./rel, under home → ~/rel, otherwise unchanged. | ||
| func displayPath(abs, cwd, home string) string { | ||
| if cwd != "" { | ||
| if rel, err := filepath.Rel(cwd, abs); err == nil && !strings.HasPrefix(rel, "..") { | ||
| return "./" + filepath.ToSlash(rel) | ||
| } | ||
| } | ||
| if home != "" { | ||
| if rel, err := filepath.Rel(home, abs); err == nil && !strings.HasPrefix(rel, "..") { | ||
| return "~/" + filepath.ToSlash(rel) | ||
| } | ||
| } | ||
| return abs | ||
| } | ||
|
|
||
| // ParseDestination resolves the user-supplied path to an absolute local path. | ||
| // When dest is empty, a default name based on now (UTC) is used, e.g. | ||
| // "snapshot-2026-05-11T21-04-32-a3f.zip", saved in the current working directory. | ||
| // The returned path always has a .zip extension. | ||
| func ParseDestination(dest string, now time.Time) (string, error) { | ||
| if dest == "" { | ||
| b := make([]byte, 2) | ||
| _, _ = rand.Read(b) | ||
| dest = "./" + now.UTC().Format("snapshot-2006-01-02T15-04-05") + "-" + fmt.Sprintf("%x", b)[:3] | ||
| } else { | ||
| lower := strings.ToLower(dest) | ||
| switch { | ||
| case strings.HasPrefix(lower, "s3://"), | ||
| strings.HasPrefix(lower, "oras://"), | ||
| strings.HasPrefix(lower, "cloud:"): | ||
| return "", ErrRemoteNotSupported | ||
| case strings.Contains(lower, "://"): | ||
| scheme, _, _ := strings.Cut(dest, "://") | ||
| return "", fmt.Errorf("%w: %q", ErrUnknownScheme, scheme+"://") | ||
| } | ||
| } | ||
|
|
||
| if dest == "~" || strings.HasPrefix(dest, "~/") || strings.HasPrefix(dest, `~\`) { | ||
| home, err := os.UserHomeDir() | ||
| if err != nil { | ||
| return "", fmt.Errorf("resolve home directory: %w", err) | ||
| } | ||
| dest = filepath.Join(home, strings.TrimLeft(dest[1:], `/\`)) | ||
| } | ||
|
|
||
| abs, err := filepath.Abs(dest) | ||
| if err != nil { | ||
| return "", fmt.Errorf("resolve path: %w", err) | ||
| } | ||
|
|
||
| parent := filepath.Dir(abs) | ||
| parentInfo, err := os.Stat(parent) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| return "", fmt.Errorf("parent directory %q does not exist — create it first", parent) | ||
| } | ||
| return "", fmt.Errorf("check parent directory: %w", err) | ||
| } | ||
| if !parentInfo.IsDir() { | ||
| return "", fmt.Errorf("parent path %q is not a directory", parent) | ||
| } | ||
|
|
||
| if info, err := os.Stat(abs); err == nil && info.IsDir() { | ||
| return "", fmt.Errorf("%q is a directory — specify a file path like ./my-snapshot", abs) | ||
| } | ||
|
|
||
| if !strings.EqualFold(filepath.Ext(abs), ".zip") { | ||
| abs += ".zip" | ||
| } | ||
|
|
||
| return abs, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.