Skip to content

Commit 656a33d

Browse files
authored
Merge pull request #2658 from jandubois/limactl-ha
Make it possible to call instance.Start() from another program
2 parents 5ab6cbf + 33ea362 commit 656a33d

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

cmd/limactl/edit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func editAction(cmd *cobra.Command, args []string) error {
154154
if err != nil {
155155
return err
156156
}
157-
return instance.Start(ctx, inst, false)
157+
return instance.Start(ctx, inst, "", false)
158158
}
159159

160160
func askWhetherToStart() (bool, error) {

cmd/limactl/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ func startAction(cmd *cobra.Command, args []string) error {
501501
ctx = instance.WithWatchHostAgentTimeout(ctx, timeout)
502502
}
503503

504-
return instance.Start(ctx, inst, launchHostAgentForeground)
504+
return instance.Start(ctx, inst, "", launchHostAgentForeground)
505505
}
506506

507507
func createBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {

pkg/hostagent/events/watcher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func Watch(ctx context.Context, haStdoutPath, haStderrPath string, begin time.Ti
2222
}
2323
defer func() {
2424
_ = haStdoutTail.Stop()
25-
haStdoutTail.Cleanup()
25+
// Do NOT call haStdoutTail.Cleanup(), it prevents the process from ever tailing the file again
2626
}()
2727

2828
haStderrTail, err := tail.TailFile(haStderrPath,
@@ -35,7 +35,7 @@ func Watch(ctx context.Context, haStdoutPath, haStderrPath string, begin time.Ti
3535
}
3636
defer func() {
3737
_ = haStderrTail.Stop()
38-
haStderrTail.Cleanup()
38+
// Do NOT call haStderrTail.Cleanup(), it prevents the process from ever tailing the file again
3939
}()
4040

4141
loop:

pkg/instance/start.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,20 @@ func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
109109
}, nil
110110
}
111111

112-
// Start starts the instance.
112+
// Start starts the hostagent in the background, which in turn will start the instance.
113+
// Start will listen to hostagent events and log them to STDOUT until either the instance
114+
// is running, or has failed to start.
115+
//
116+
// The `limactl` argument allows the caller to specify the full path of the `limactl` executable.
117+
// When called from inside limactl itself it will always be the empty string which uses the name
118+
// of the current executable instead.
119+
//
120+
// The `launchHostAgentForeground` argument makes the hostagent run in the foreground.
121+
// The function will continue to listen and log hostagent events until the instance is
122+
// shut down again.
123+
//
113124
// Start calls Prepare by itself, so you do not need to call Prepare manually before calling Start.
114-
func Start(ctx context.Context, inst *store.Instance, launchHostAgentForeground bool) error {
125+
func Start(ctx context.Context, inst *store.Instance, limactl string, launchHostAgentForeground bool) error {
115126
haPIDPath := filepath.Join(inst.Dir, filenames.HostAgentPID)
116127
if _, err := os.Stat(haPIDPath); !errors.Is(err, os.ErrNotExist) {
117128
return fmt.Errorf("instance %q seems running (hint: remove %q if the instance is not actually running)", inst.Name, haPIDPath)
@@ -144,9 +155,11 @@ func Start(ctx context.Context, inst *store.Instance, launchHostAgentForeground
144155
return err
145156
}
146157

147-
self, err := os.Executable()
148-
if err != nil {
149-
return err
158+
if limactl == "" {
159+
limactl, err = os.Executable()
160+
if err != nil {
161+
return err
162+
}
150163
}
151164
haStdoutPath := filepath.Join(inst.Dir, filenames.HostAgentStdoutLog)
152165
haStderrPath := filepath.Join(inst.Dir, filenames.HostAgentStderrLog)
@@ -182,7 +195,7 @@ func Start(ctx context.Context, inst *store.Instance, launchHostAgentForeground
182195
args = append(args, "--nerdctl-archive", prepared.NerdctlArchiveCache)
183196
}
184197
args = append(args, inst.Name)
185-
haCmd := exec.CommandContext(ctx, self, args...)
198+
haCmd := exec.CommandContext(ctx, limactl, args...)
186199

187200
if launchHostAgentForeground {
188201
haCmd.SysProcAttr = executil.ForegroundSysProcAttr
@@ -214,7 +227,7 @@ func Start(ctx context.Context, inst *store.Instance, launchHostAgentForeground
214227
return err
215228
}
216229
}
217-
if err := syscall.Exec(self, haCmd.Args, haCmd.Environ()); err != nil {
230+
if err := syscall.Exec(limactl, haCmd.Args, haCmd.Environ()); err != nil {
218231
return err
219232
}
220233
} else if err := haCmd.Start(); err != nil {

0 commit comments

Comments
 (0)