| 1 | |
| 2 | |
| 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"` |
| 33 | VMTypesEx map[string]DriverExt `json:"vmTypesEx"` |
| 34 | GuestAgents map[limatype.Arch]GuestAgent `json:"guestAgents"` |
| 35 | ShellEnvBlock []string `json:"shellEnvBlock"` |
| 36 | HostOS string `json:"hostOS"` |
| 37 | HostArch string `json:"hostArch"` |
| 38 | IdentityFile string `json:"identityFile"` |
| 39 | Plugins []plugins.Plugin `json:"plugins"` |
| 40 | LibexecPaths []string `json:"libexecPaths"` |
| 41 | SharePaths []string `json:"sharePaths"` |
| 42 | } |
| 43 | |
| 44 | type DriverExt struct { |
| 45 | Location string `json:"location,omitempty"` |
| 46 | } |
| 47 | |
| 48 | type GuestAgent struct { |
| 49 | Location string `json:"location"` |
| 50 | } |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 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 | for _, os := range limatype.OSTypes { |
| 111 | bin, err := usrlocal.GuestAgentBinary(os, arch) |
| 112 | if err != nil { |
| 113 | if errors.Is(err, fs.ErrNotExist) { |
| 114 | logrus.WithError(err).Debugf("Failed to resolve the guest agent binary for %#q-%#q", os, arch) |
| 115 | } else { |
| 116 | logrus.WithError(err).Warnf("Failed to resolve the guest agent binary for %#q-%#q", os, arch) |
| 117 | } |
| 118 | continue |
| 119 | } |
| 120 | key := arch |
| 121 | |
| 122 | if os != limatype.LINUX { |
| 123 | key = os + "-" + arch |
| 124 | } |
| 125 | info.GuestAgents[key] = GuestAgent{ |
| 126 | Location: bin, |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | info.Plugins, err = plugins.Discover() |
| 132 | if err != nil { |
| 133 | |
| 134 | logrus.WithError(err).Warn("Failed to discover plugins") |
| 135 | } |
| 136 | |
| 137 | return info, nil |
| 138 | } |