Skip to content

Commit b6fdde0

Browse files
committed
Add optional message to be shown to template
Show example with current values already in place. Currently just shown as regular INFO log messages. Signed-off-by: Anders F Björklund <[email protected]>
1 parent c420190 commit b6fdde0

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

examples/docker.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,7 @@ probes:
6666
portForwards:
6767
- guestSocket: "/run/user/{{.UID}}/docker.sock"
6868
hostSocket: "{{.Dir}}/sock/docker.sock"
69+
message: |
70+
To run `docker` on the host (assumes docker-cli is installed):
71+
$ export DOCKER_HOST=unix://{{.Dir}}/sock/docker.sock
72+
$ docker ...

examples/podman.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ probes:
5353
portForwards:
5454
- guestSocket: "/run/user/{{.UID}}/podman/podman.sock"
5555
hostSocket: "{{.Dir}}/sock/podman.sock"
56+
message: |
57+
To run `podman` on the host (assumes podman-remote is installed):
58+
$ export CONTAINER_HOST=unix://{{.Dir}}/sock/podman.sock
59+
$ podman --remote ...

pkg/limayaml/default.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ networks:
196196
# hostPortRange: [1, 65535]
197197
# # Any port still not matched by a rule will not be forwarded (ignored)
198198

199+
# Message. Information to be shown to the user, given as a Go template for the instance.
200+
# The same template variables as for listing instances can be used, for example {{.Dir}}.
201+
# You can view the complete list of variables using `limactl list --list-fields` command.
202+
# message: |
203+
# This will be shown to the user.
204+
199205
# Extra environment variables that will be loaded into the VM at start up.
200206
# These variables are consumed by internal init scripts, and also added
201207
# to /etc/environment.

pkg/limayaml/limayaml.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type LimaYAML struct {
2020
Containerd Containerd `yaml:"containerd,omitempty" json:"containerd,omitempty"`
2121
Probes []Probe `yaml:"probes,omitempty" json:"probes,omitempty"`
2222
PortForwards []PortForward `yaml:"portForwards,omitempty" json:"portForwards,omitempty"`
23+
Message string `yaml:"message,omitempty" json:"message,omitempty"`
2324
Networks []Network `yaml:"networks,omitempty" json:"networks,omitempty"`
2425
Network NetworkDeprecated `yaml:"network,omitempty" json:"network,omitempty"` // DEPRECATED, use `networks` instead
2526
Env map[string]string `yaml:"env,omitempty" json:"env,omitempty"`

pkg/start/start.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package start
22

33
import (
4+
"bufio"
5+
"bytes"
46
"context"
57
"errors"
68
"fmt"
79
"os"
810
"os/exec"
911
"path/filepath"
12+
"text/template"
1013
"time"
1114

1215
"github.com/lima-vm/lima/pkg/downloader"
@@ -147,7 +150,7 @@ func Start(ctx context.Context, inst *store.Instance) error {
147150

148151
watchErrCh := make(chan error)
149152
go func() {
150-
watchErrCh <- watchHostAgentEvents(ctx, inst.Name, haStdoutPath, haStderrPath, begin)
153+
watchErrCh <- watchHostAgentEvents(ctx, inst, haStdoutPath, haStderrPath, begin)
151154
close(watchErrCh)
152155
}()
153156
waitErrCh := make(chan error)
@@ -181,7 +184,7 @@ func waitHostAgentStart(ctx context.Context, haPIDPath, haStderrPath string) err
181184
}
182185
}
183186

184-
func watchHostAgentEvents(ctx context.Context, instName, haStdoutPath, haStderrPath string, begin time.Time) error {
187+
func watchHostAgentEvents(ctx context.Context, inst *store.Instance, haStdoutPath, haStderrPath string, begin time.Time) error {
185188
ctx2, cancel := context.WithTimeout(ctx, 10*time.Minute)
186189
defer cancel()
187190

@@ -210,7 +213,8 @@ func watchHostAgentEvents(ctx context.Context, instName, haStdoutPath, haStderrP
210213
return true
211214
}
212215

213-
logrus.Infof("READY. Run `%s` to open the shell.", LimactlShellCmd(instName))
216+
logrus.Infof("READY. Run `%s` to open the shell.", LimactlShellCmd(inst.Name))
217+
ShowMessage(inst)
214218
err = nil
215219
return true
216220
}
@@ -239,3 +243,29 @@ func LimactlShellCmd(instName string) string {
239243
}
240244
return shellCmd
241245
}
246+
247+
func ShowMessage(inst *store.Instance) error {
248+
if inst.Message == "" {
249+
return nil
250+
}
251+
t, err := template.New("message").Parse(inst.Message)
252+
if err != nil {
253+
return err
254+
}
255+
data, err := store.AddGlobalFields(inst)
256+
if err != nil {
257+
return err
258+
}
259+
var b bytes.Buffer
260+
if err := t.Execute(&b, data); err != nil {
261+
return err
262+
}
263+
scanner := bufio.NewScanner(&b)
264+
for scanner.Scan() {
265+
logrus.Info(scanner.Text())
266+
}
267+
if err := scanner.Err(); err != nil {
268+
return err
269+
}
270+
return nil
271+
}

pkg/store/instance.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Instance struct {
3535
CPUs int `json:"cpus,omitempty"`
3636
Memory int64 `json:"memory,omitempty"` // bytes
3737
Disk int64 `json:"disk,omitempty"` // bytes
38+
Message string `json:"message,omitempty"`
3839
Networks []limayaml.Network `json:"network,omitempty"`
3940
SSHLocalPort int `json:"sshLocalPort,omitempty"`
4041
HostAgentPID int `json:"hostAgentPID,omitempty"`
@@ -82,6 +83,7 @@ func Inspect(instName string) (*Instance, error) {
8283
if err == nil {
8384
inst.Disk = disk
8485
}
86+
inst.Message = y.Message
8587
inst.Networks = y.Networks
8688
inst.SSHLocalPort = *y.SSH.LocalPort // maybe 0
8789

0 commit comments

Comments
 (0)