Skip to content

Commit a6482d0

Browse files
committed
feat: configurable start timeout
Allows configuring the duration to wait for the "running" event from the host agent before timing out. Signed-off-by: Antoine Cotten <[email protected]>
1 parent 6b6073f commit a6482d0

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

cmd/limactl/start.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ $ limactl start --name=default https://raw.githubusercontent.com/lima-vm/lima/ma
5353
startCommand.Flags().Bool("tty", isatty.IsTerminal(os.Stdout.Fd()), "enable TUI interactions such as opening an editor, defaults to true when stdout is a terminal")
5454
startCommand.Flags().String("name", "", "override the instance name")
5555
startCommand.Flags().Bool("list-templates", false, "list available templates and exit")
56+
startCommand.Flags().Duration("timeout", start.DefaultWatchHostAgentEventsTimeout, "duration to wait for the instance to be running before timing out")
5657
return startCommand
5758
}
5859

@@ -354,6 +355,15 @@ func startAction(cmd *cobra.Command, args []string) error {
354355
if err != nil {
355356
return err
356357
}
358+
359+
timeout, err := cmd.Flags().GetDuration("timeout")
360+
if err != nil {
361+
return err
362+
}
363+
if timeout > 0 {
364+
ctx = start.WithWatchHostAgentTimeout(ctx, timeout)
365+
}
366+
357367
return start.Start(ctx, inst)
358368
}
359369

pkg/start/start.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import (
2121
"github.com/sirupsen/logrus"
2222
)
2323

24-
func ensureDisk(ctx context.Context, instName, instDir string, y *limayaml.LimaYAML) error {
24+
// DefaultWatchHostAgentEventsTimeout is the duration to wait for the instance
25+
// to be running before timing out.
26+
const DefaultWatchHostAgentEventsTimeout = 10 * time.Minute
27+
28+
func ensureDisk(instName, instDir string, y *limayaml.LimaYAML) error {
2529
qCfg := qemu.Config{
2630
Name: instName,
2731
InstanceDir: instDir,
@@ -90,7 +94,7 @@ func Start(ctx context.Context, inst *store.Instance) error {
9094
return err
9195
}
9296

93-
if err := ensureDisk(ctx, inst.Name, inst.Dir, y); err != nil {
97+
if err := ensureDisk(inst.Name, inst.Dir, y); err != nil {
9498
return err
9599
}
96100
nerdctlArchiveCache, err := ensureNerdctlArchiveCache(y)
@@ -185,7 +189,7 @@ func waitHostAgentStart(ctx context.Context, haPIDPath, haStderrPath string) err
185189
}
186190

187191
func watchHostAgentEvents(ctx context.Context, inst *store.Instance, haStdoutPath, haStderrPath string, begin time.Time) error {
188-
ctx2, cancel := context.WithTimeout(ctx, 10*time.Minute)
192+
ctx, cancel := context.WithTimeout(ctx, watchHostAgentTimeout(ctx))
189193
defer cancel()
190194

191195
var (
@@ -221,7 +225,7 @@ func watchHostAgentEvents(ctx context.Context, inst *store.Instance, haStdoutPat
221225
return false
222226
}
223227

224-
if xerr := hostagentevents.Watch(ctx2, haStdoutPath, haStderrPath, begin, onEvent); xerr != nil {
228+
if xerr := hostagentevents.Watch(ctx, haStdoutPath, haStderrPath, begin, onEvent); xerr != nil {
225229
return xerr
226230
}
227231

@@ -236,6 +240,23 @@ func watchHostAgentEvents(ctx context.Context, inst *store.Instance, haStdoutPat
236240
return nil
237241
}
238242

243+
type watchHostAgentEventsTimeoutKey = struct{}
244+
245+
// WithWatchHostAgentEventsTimeout sets the value of the timeout to use for
246+
// watchHostAgentEvents in the given Context.
247+
func WithWatchHostAgentTimeout(ctx context.Context, timeout time.Duration) context.Context {
248+
return context.WithValue(ctx, watchHostAgentEventsTimeoutKey{}, timeout)
249+
}
250+
251+
// watchHostAgentEventsTimeout returns the value of the timeout to use for
252+
// watchHostAgentEvents contained in the given Context, or its default value.
253+
func watchHostAgentTimeout(ctx context.Context) time.Duration {
254+
if timeout, ok := ctx.Value(watchHostAgentEventsTimeoutKey{}).(time.Duration); ok {
255+
return timeout
256+
}
257+
return DefaultWatchHostAgentEventsTimeout
258+
}
259+
239260
func LimactlShellCmd(instName string) string {
240261
shellCmd := fmt.Sprintf("limactl shell %s", instName)
241262
if instName == "default" {

0 commit comments

Comments
 (0)