-
Notifications
You must be signed in to change notification settings - Fork 0
Handle container startup failures #5
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ import ( | |
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/localstack/lstk/internal/container" | ||
| "github.com/localstack/lstk/internal/runtime" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
|
|
@@ -13,7 +15,17 @@ var rootCmd = &cobra.Command{ | |
| Short: "LocalStack CLI", | ||
| Long: "lstk is the command-line interface for LocalStack.", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| if err := runStart(cmd.Context()); err != nil { | ||
| rt, err := runtime.NewDockerRuntime() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| onProgress := func(msg string) { | ||
| fmt.Println(msg) | ||
| } | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The onProgress callback decouples the business logic from how output is displayed. Without it, the |
||
|
|
||
| if err := container.Start(cmd.Context(), rt, onProgress); err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package container | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/localstack/lstk/internal/runtime" | ||
| ) | ||
|
|
||
| func Start(ctx context.Context, rt runtime.Runtime, onProgress func(string)) error { | ||
| // TODO: hardcoded for now, later should be configurable | ||
| containers := []runtime.ContainerConfig{ | ||
| { | ||
| Image: "localstack/localstack-pro:latest", | ||
| Name: "localstack-aws", | ||
| Port: "4566", | ||
| HealthPath: "/_localstack/health", | ||
| }, | ||
| } | ||
|
|
||
| for _, config := range containers { | ||
| onProgress(fmt.Sprintf("Pulling %s...", config.Image)) | ||
| progress := make(chan runtime.PullProgress) | ||
| go func() { | ||
| for p := range progress { | ||
| if p.Total > 0 { | ||
| onProgress(fmt.Sprintf(" %s: %s %.1f%%", p.LayerID, p.Status, float64(p.Current)/float64(p.Total)*100)) | ||
| } else if p.Status != "" { | ||
| onProgress(fmt.Sprintf(" %s: %s", p.LayerID, p.Status)) | ||
| } | ||
| } | ||
| }() | ||
| if err := rt.PullImage(ctx, config.Image, progress); err != nil { | ||
| return fmt.Errorf("failed to pull image %s: %w", config.Image, err) | ||
| } | ||
|
|
||
| onProgress(fmt.Sprintf("Starting %s...", config.Name)) | ||
| containerID, err := rt.Start(ctx, config) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to start %s: %w", config.Name, err) | ||
| } | ||
|
|
||
| onProgress(fmt.Sprintf("Waiting for %s to be ready...", config.Name)) | ||
| healthURL := fmt.Sprintf("http://localhost:%s%s", config.Port, config.HealthPath) | ||
| if err := awaitStartup(ctx, rt, containerID, config.Name, healthURL); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| onProgress(fmt.Sprintf("%s ready (container: %s)", config.Name, containerID[:12])) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // awaitStartup polls until one of two outcomes: | ||
| // - Success: health endpoint returns 200 (license is valid, LocalStack is ready) | ||
| // - Failure: container stops running (e.g., license activation failed), returns error with container logs | ||
| // | ||
| // TODO: move to Runtime interface if other runtimes (k8s?) need native readiness probes | ||
| func awaitStartup(ctx context.Context, rt runtime.Runtime, containerID, name, healthURL string) error { | ||
| client := &http.Client{Timeout: 2 * time.Second} | ||
|
|
||
| for { | ||
| running, err := rt.IsRunning(ctx, containerID) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to check container status: %w", err) | ||
| } | ||
| if !running { | ||
| logs, logsErr := rt.Logs(ctx, containerID, 20) | ||
| if logsErr != nil || logs == "" { | ||
| return fmt.Errorf("%s exited unexpectedly", name) | ||
| } | ||
| return fmt.Errorf("%s exited unexpectedly:\n%s", name, logs) | ||
| } | ||
|
|
||
| resp, err := client.Get(healthURL) | ||
| if err == nil && resp.StatusCode == http.StatusOK { | ||
| resp.Body.Close() | ||
| return nil | ||
| } | ||
| if resp != nil { | ||
| resp.Body.Close() | ||
| } | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case <-time.After(1 * time.Second): | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,10 @@ package runtime | |
| import "context" | ||
|
|
||
| type ContainerConfig struct { | ||
| Image string | ||
| Name string | ||
| Ports map[string]string | ||
| Image string | ||
| Name string | ||
| Port string | ||
| HealthPath string | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to configure multiple ports yet, so simplified and made |
||
| } | ||
|
|
||
| type PullProgress struct { | ||
|
|
@@ -20,4 +21,5 @@ type Runtime interface { | |
| PullImage(ctx context.Context, image string, progress chan<- PullProgress) error | ||
| Start(ctx context.Context, config ContainerConfig) (string, error) | ||
| IsRunning(ctx context.Context, containerID string) (bool, error) | ||
| Logs(ctx context.Context, containerID string, tail int) (string, error) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This disables test caching.