Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/limainfo/limainfo.go
2614 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package limainfo
5
6
import (
7
"context"
8
"errors"
9
"io/fs"
10
"path/filepath"
11
"runtime"
12
13
"github.com/sirupsen/logrus"
14
15
"github.com/lima-vm/lima/v2/pkg/envutil"
16
"github.com/lima-vm/lima/v2/pkg/limatype"
17
"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
18
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
19
"github.com/lima-vm/lima/v2/pkg/limayaml"
20
"github.com/lima-vm/lima/v2/pkg/plugins"
21
"github.com/lima-vm/lima/v2/pkg/registry"
22
"github.com/lima-vm/lima/v2/pkg/templatestore"
23
"github.com/lima-vm/lima/v2/pkg/usrlocal"
24
"github.com/lima-vm/lima/v2/pkg/version"
25
)
26
27
type LimaInfo struct {
28
Version string `json:"version"`
29
Templates []templatestore.Template `json:"templates"`
30
DefaultTemplate *limatype.LimaYAML `json:"defaultTemplate"`
31
LimaHome string `json:"limaHome"`
32
VMTypes []string `json:"vmTypes"` // since Lima v0.14.2
33
VMTypesEx map[string]DriverExt `json:"vmTypesEx"` // since Lima v2.0.0
34
GuestAgents map[limatype.Arch]GuestAgent `json:"guestAgents"` // since Lima v1.1.0
35
ShellEnvBlock []string `json:"shellEnvBlock"` // since Lima v2.0.0
36
HostOS string `json:"hostOS"` // since Lima v2.0.0
37
HostArch string `json:"hostArch"` // since Lima v2.0.0
38
IdentityFile string `json:"identityFile"` // since Lima v2.0.0
39
Plugins []plugins.Plugin `json:"plugins"` // since Lima v2.0.0
40
LibexecPaths []string `json:"libexecPaths"` // since Lima v2.0.0
41
SharePaths []string `json:"sharePaths"` // since Lima v2.0.0
42
}
43
44
type DriverExt struct {
45
Location string `json:"location,omitempty"` // since Lima v2.0.0
46
}
47
48
type GuestAgent struct {
49
Location string `json:"location"` // since Lima v1.1.0
50
}
51
52
// New returns a LimaInfo object with the Lima version, a list of all Templates and their location,
53
// the DefaultTemplate corresponding to template:default with all defaults filled in, the
54
// LimaHome location, a list of all supported VMTypes, and a map of GuestAgents for each architecture.
55
func New(ctx context.Context) (*LimaInfo, error) {
56
b, err := templatestore.Read(templatestore.Default)
57
if err != nil {
58
return nil, err
59
}
60
y, err := limayaml.Load(ctx, b, "")
61
if err != nil {
62
return nil, err
63
}
64
65
reg := registry.List()
66
if len(reg) == 0 {
67
return nil, errors.New("no VM types found; ensure that the drivers are properly registered")
68
}
69
vmTypesEx := make(map[string]DriverExt)
70
var vmTypes []string
71
for name, path := range reg {
72
vmTypesEx[name] = DriverExt{
73
Location: path,
74
}
75
vmTypes = append(vmTypes, name)
76
}
77
78
info := &LimaInfo{
79
Version: version.Version,
80
DefaultTemplate: y,
81
VMTypes: vmTypes,
82
VMTypesEx: vmTypesEx,
83
GuestAgents: make(map[limatype.Arch]GuestAgent),
84
ShellEnvBlock: envutil.GetDefaultBlockList(),
85
HostOS: runtime.GOOS,
86
HostArch: limatype.NewArch(runtime.GOARCH),
87
}
88
info.Templates, err = templatestore.Templates()
89
if err != nil {
90
return nil, err
91
}
92
info.LimaHome, err = dirnames.LimaDir()
93
if err != nil {
94
return nil, err
95
}
96
configDir, err := dirnames.LimaConfigDir()
97
if err != nil {
98
return nil, err
99
}
100
info.LibexecPaths, err = usrlocal.LibexecLima()
101
if err != nil {
102
return nil, err
103
}
104
info.SharePaths, err = usrlocal.ShareLima()
105
if err != nil {
106
return nil, err
107
}
108
info.IdentityFile = filepath.Join(configDir, filenames.UserPrivateKey)
109
for _, arch := range limatype.ArchTypes {
110
bin, err := usrlocal.GuestAgentBinary(limatype.LINUX, arch)
111
if err != nil {
112
if errors.Is(err, fs.ErrNotExist) {
113
logrus.WithError(err).Debugf("Failed to resolve the guest agent binary for %q", arch)
114
} else {
115
logrus.WithError(err).Warnf("Failed to resolve the guest agent binary for %q", arch)
116
}
117
continue
118
}
119
info.GuestAgents[arch] = GuestAgent{
120
Location: bin,
121
}
122
}
123
124
info.Plugins, err = plugins.Discover()
125
if err != nil {
126
// Don't fail the entire info command if plugin discovery fails.
127
logrus.WithError(err).Warn("Failed to discover plugins")
128
}
129
130
return info, nil
131
}
132
133