Skip to content

Commit

Permalink
Merge pull request #472 from afbjorklund/message
Browse files Browse the repository at this point in the history
Add optional message to be shown to template
  • Loading branch information
AkihiroSuda authored Dec 14, 2021
2 parents 9c4cb2d + 2a7b7ed commit ecc4f8b
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 38 deletions.
30 changes: 2 additions & 28 deletions cmd/limactl/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"path/filepath"
"reflect"
"sort"
"strings"
Expand All @@ -13,38 +12,13 @@ import (

"github.com/docker/go-units"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/store/dirnames"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

type formatData struct {
store.Instance
LimaHome string
IdentityFile string
}

func addGlobalFields(inst *store.Instance) (formatData, error) {
var data formatData
data.Instance = *inst
// Add IdentityFile
configDir, err := dirnames.LimaConfigDir()
if err != nil {
return formatData{}, err
}
data.IdentityFile = filepath.Join(configDir, filenames.UserPrivateKey)
// Add LimaHome
data.LimaHome, err = dirnames.LimaDir()
if err != nil {
return formatData{}, err
}
return data, nil
}

func fieldNames() []string {
names := []string{}
var data formatData
var data store.FormatData
t := reflect.TypeOf(data)
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
Expand Down Expand Up @@ -161,7 +135,7 @@ func listAction(cmd *cobra.Command, args []string) error {
logrus.WithError(err).Errorf("instance %q does not exist?", instName)
continue
}
data, err := addGlobalFields(inst)
data, err := store.AddGlobalFields(inst)
if err != nil {
logrus.WithError(err).Error("Cannot add global fields to instance data")
continue
Expand Down
4 changes: 4 additions & 0 deletions examples/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ probes:
portForwards:
- guestSocket: "/run/user/{{.UID}}/docker.sock"
hostSocket: "{{.Dir}}/sock/docker.sock"
message: |
To run `docker` on the host (assumes docker-cli is installed):
$ export DOCKER_HOST=unix://{{.Dir}}/sock/docker.sock
$ docker ...
4 changes: 4 additions & 0 deletions examples/podman.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ probes:
portForwards:
- guestSocket: "/run/user/{{.UID}}/podman/podman.sock"
hostSocket: "{{.Dir}}/sock/podman.sock"
message: |
To run `podman` on the host (assumes podman-remote is installed):
$ export CONTAINER_HOST=unix://{{.Dir}}/sock/podman.sock
$ podman{{if eq .HostOS "linux"}}-remote{{end}} ...
7 changes: 7 additions & 0 deletions pkg/limayaml/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ networks:
# hostPortRange: [1, 65535]
# # Any port still not matched by a rule will not be forwarded (ignored)

# Message. Information to be shown to the user, given as a Go template for the instance.
# The same template variables as for listing instances can be used, for example {{.Dir}}.
# You can view the complete list of variables using `limactl list --list-fields` command.
# It also includes {{.HostOS}} and {{.HostArch}} vars, for the runtime GOOS and GOARCH.
# message: |
# This will be shown to the user.

# Extra environment variables that will be loaded into the VM at start up.
# These variables are consumed by internal init scripts, and also added
# to /etc/environment.
Expand Down
22 changes: 15 additions & 7 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
if o.Arch != nil {
y.Arch = o.Arch
}
y.Arch = pointer.String(resolveArch(y.Arch))
y.Arch = pointer.String(ResolveArch(y.Arch))

y.Images = append(append(o.Images, y.Images...), d.Images...)
for i := range y.Images {
Expand Down Expand Up @@ -400,13 +400,21 @@ func FillPortForwardDefaults(rule *PortForward, instDir string) {
}
}

func resolveArch(s *string) Arch {
func NewArch(arch string) Arch {
switch arch {
case "amd64":
return X8664
case "arm64":
return AARCH64
default:
logrus.Warnf("Unknown arch: %s", arch)
return arch
}
}

func ResolveArch(s *string) Arch {
if s == nil || *s == "" || *s == "default" {
if runtime.GOARCH == "amd64" {
return X8664
} else {
return AARCH64
}
return NewArch(runtime.GOARCH)
}
return *s
}
1 change: 1 addition & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type LimaYAML struct {
Containerd Containerd `yaml:"containerd,omitempty" json:"containerd,omitempty"`
Probes []Probe `yaml:"probes,omitempty" json:"probes,omitempty"`
PortForwards []PortForward `yaml:"portForwards,omitempty" json:"portForwards,omitempty"`
Message string `yaml:"message,omitempty" json:"message,omitempty"`
Networks []Network `yaml:"networks,omitempty" json:"networks,omitempty"`
Network NetworkDeprecated `yaml:"network,omitempty" json:"network,omitempty"` // DEPRECATED, use `networks` instead
Env map[string]string `yaml:"env,omitempty" json:"env,omitempty"`
Expand Down
36 changes: 33 additions & 3 deletions pkg/start/start.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package start

import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"text/template"
"time"

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

watchErrCh := make(chan error)
go func() {
watchErrCh <- watchHostAgentEvents(ctx, inst.Name, haStdoutPath, haStderrPath, begin)
watchErrCh <- watchHostAgentEvents(ctx, inst, haStdoutPath, haStderrPath, begin)
close(watchErrCh)
}()
waitErrCh := make(chan error)
Expand Down Expand Up @@ -181,7 +184,7 @@ func waitHostAgentStart(ctx context.Context, haPIDPath, haStderrPath string) err
}
}

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

Expand Down Expand Up @@ -210,7 +213,8 @@ func watchHostAgentEvents(ctx context.Context, instName, haStdoutPath, haStderrP
return true
}

logrus.Infof("READY. Run `%s` to open the shell.", LimactlShellCmd(instName))
logrus.Infof("READY. Run `%s` to open the shell.", LimactlShellCmd(inst.Name))
ShowMessage(inst)
err = nil
return true
}
Expand Down Expand Up @@ -239,3 +243,29 @@ func LimactlShellCmd(instName string) string {
}
return shellCmd
}

func ShowMessage(inst *store.Instance) error {
if inst.Message == "" {
return nil
}
t, err := template.New("message").Parse(inst.Message)
if err != nil {
return err
}
data, err := store.AddGlobalFields(inst)
if err != nil {
return err
}
var b bytes.Buffer
if err := t.Execute(&b, data); err != nil {
return err
}
scanner := bufio.NewScanner(&b)
for scanner.Scan() {
logrus.Info(scanner.Text())
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
33 changes: 33 additions & 0 deletions pkg/store/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/docker/go-units"
hostagentclient "github.com/lima-vm/lima/pkg/hostagent/api/client"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/store/dirnames"
"github.com/lima-vm/lima/pkg/store/filenames"
)

Expand All @@ -34,6 +36,7 @@ type Instance struct {
CPUs int `json:"cpus,omitempty"`
Memory int64 `json:"memory,omitempty"` // bytes
Disk int64 `json:"disk,omitempty"` // bytes
Message string `json:"message,omitempty"`
Networks []limayaml.Network `json:"network,omitempty"`
SSHLocalPort int `json:"sshLocalPort,omitempty"`
HostAgentPID int `json:"hostAgentPID,omitempty"`
Expand Down Expand Up @@ -81,6 +84,7 @@ func Inspect(instName string) (*Instance, error) {
if err == nil {
inst.Disk = disk
}
inst.Message = y.Message
inst.Networks = y.Networks
inst.SSHLocalPort = *y.SSH.LocalPort // maybe 0

Expand Down Expand Up @@ -164,3 +168,32 @@ func ReadPIDFile(path string) (int, error) {
}
return pid, nil
}

type FormatData struct {
Instance
HostOS string
HostArch string
LimaHome string
IdentityFile string
}

func AddGlobalFields(inst *Instance) (FormatData, error) {
var data FormatData
data.Instance = *inst
// Add HostOS
data.HostOS = runtime.GOOS
// Add HostArch
data.HostArch = limayaml.NewArch(runtime.GOARCH)
// Add IdentityFile
configDir, err := dirnames.LimaConfigDir()
if err != nil {
return FormatData{}, err
}
data.IdentityFile = filepath.Join(configDir, filenames.UserPrivateKey)
// Add LimaHome
data.LimaHome, err = dirnames.LimaDir()
if err != nil {
return FormatData{}, err
}
return data, nil
}

0 comments on commit ecc4f8b

Please sign in to comment.