Skip to content

Commit 02dea48

Browse files
authored
Merge pull request #2494 from rancher-sandbox/create
Move instance creation from cmd/limactl/start.go → pkg/instance/create.go
2 parents 495db7a + 54182f8 commit 02dea48

File tree

3 files changed

+86
-81
lines changed

3 files changed

+86
-81
lines changed

cmd/limactl/start.go

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"errors"
65
"fmt"
76
"io"
@@ -15,16 +14,14 @@ import (
1514
"github.com/lima-vm/lima/cmd/limactl/editflags"
1615
"github.com/lima-vm/lima/cmd/limactl/guessarg"
1716
"github.com/lima-vm/lima/pkg/editutil"
17+
"github.com/lima-vm/lima/pkg/instance"
1818
"github.com/lima-vm/lima/pkg/ioutilx"
19-
"github.com/lima-vm/lima/pkg/limayaml"
2019
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
21-
"github.com/lima-vm/lima/pkg/osutil"
2220
"github.com/lima-vm/lima/pkg/start"
2321
"github.com/lima-vm/lima/pkg/store"
2422
"github.com/lima-vm/lima/pkg/store/filenames"
2523
"github.com/lima-vm/lima/pkg/templatestore"
2624
"github.com/lima-vm/lima/pkg/uiutil"
27-
"github.com/lima-vm/lima/pkg/version"
2825
"github.com/lima-vm/lima/pkg/yqutil"
2926
"github.com/sirupsen/logrus"
3027
"github.com/spf13/cobra"
@@ -229,7 +226,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
229226
inst, err := store.Inspect(st.instName)
230227
if err == nil {
231228
if createOnly {
232-
return nil, fmt.Errorf("Instance %q already exists", st.instName)
229+
return nil, fmt.Errorf("instance %q already exists", st.instName)
233230
}
234231
logrus.Infof("Using the existing instance %q", st.instName)
235232
yqExprs, err := editflags.YQExpressions(flags, false)
@@ -276,8 +273,8 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
276273
return nil, err
277274
}
278275
}
279-
saveBrokenEditorBuffer := tty
280-
return createInstance(cmd.Context(), st, saveBrokenEditorBuffer)
276+
saveBrokenYAML := tty
277+
return instance.Create(cmd.Context(), st.instName, st.yBytes, saveBrokenYAML)
281278
}
282279

283280
func applyYQExpressionToExistingInstance(inst *store.Instance, yq string) (*store.Instance, error) {
@@ -313,66 +310,6 @@ func applyYQExpressionToExistingInstance(inst *store.Instance, yq string) (*stor
313310
return store.Inspect(inst.Name)
314311
}
315312

316-
func createInstance(ctx context.Context, st *creatorState, saveBrokenEditorBuffer bool) (*store.Instance, error) {
317-
if st.instName == "" {
318-
return nil, errors.New("got empty st.instName")
319-
}
320-
if len(st.yBytes) == 0 {
321-
return nil, errors.New("got empty st.yBytes")
322-
}
323-
324-
instDir, err := store.InstanceDir(st.instName)
325-
if err != nil {
326-
return nil, err
327-
}
328-
329-
// the full path of the socket name must be less than UNIX_PATH_MAX chars.
330-
maxSockName := filepath.Join(instDir, filenames.LongestSock)
331-
if len(maxSockName) >= osutil.UnixPathMax {
332-
return nil, fmt.Errorf("instance name %q too long: %q must be less than UNIX_PATH_MAX=%d characters, but is %d",
333-
st.instName, maxSockName, osutil.UnixPathMax, len(maxSockName))
334-
}
335-
if _, err := os.Stat(instDir); !errors.Is(err, os.ErrNotExist) {
336-
return nil, fmt.Errorf("instance %q already exists (%q)", st.instName, instDir)
337-
}
338-
// limayaml.Load() needs to pass the store file path to limayaml.FillDefault() to calculate default MAC addresses
339-
filePath := filepath.Join(instDir, filenames.LimaYAML)
340-
y, err := limayaml.Load(st.yBytes, filePath)
341-
if err != nil {
342-
return nil, err
343-
}
344-
if err := limayaml.Validate(y, true); err != nil {
345-
if !saveBrokenEditorBuffer {
346-
return nil, err
347-
}
348-
rejectedYAML := "lima.REJECTED.yaml"
349-
if writeErr := os.WriteFile(rejectedYAML, st.yBytes, 0o644); writeErr != nil {
350-
return nil, fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %w: %w", rejectedYAML, writeErr, err)
351-
}
352-
return nil, fmt.Errorf("the YAML is invalid, saved the buffer as %q: %w", rejectedYAML, err)
353-
}
354-
if err := os.MkdirAll(instDir, 0o700); err != nil {
355-
return nil, err
356-
}
357-
if err := os.WriteFile(filePath, st.yBytes, 0o644); err != nil {
358-
return nil, err
359-
}
360-
if err := os.WriteFile(filepath.Join(instDir, filenames.LimaVersion), []byte(version.Version), 0o444); err != nil {
361-
return nil, err
362-
}
363-
364-
inst, err := store.Inspect(st.instName)
365-
if err != nil {
366-
return nil, err
367-
}
368-
369-
if err := start.Register(ctx, inst); err != nil {
370-
return nil, err
371-
}
372-
373-
return inst, nil
374-
}
375-
376313
type creatorState struct {
377314
instName string // instance name
378315
yBytes []byte // yaml bytes

pkg/instance/create.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package instance
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"os"
8+
"path/filepath"
9+
10+
"github.com/lima-vm/lima/pkg/driver"
11+
"github.com/lima-vm/lima/pkg/driverutil"
12+
"github.com/lima-vm/lima/pkg/limayaml"
13+
"github.com/lima-vm/lima/pkg/osutil"
14+
"github.com/lima-vm/lima/pkg/store"
15+
"github.com/lima-vm/lima/pkg/store/filenames"
16+
"github.com/lima-vm/lima/pkg/version"
17+
)
18+
19+
func Create(ctx context.Context, instName string, yBytes []byte, saveBrokenYAML bool) (*store.Instance, error) {
20+
if instName == "" {
21+
return nil, errors.New("got empty instName")
22+
}
23+
if len(yBytes) == 0 {
24+
return nil, errors.New("got empty yBytes")
25+
}
26+
27+
instDir, err := store.InstanceDir(instName)
28+
if err != nil {
29+
return nil, err
30+
}
31+
32+
// the full path of the socket name must be less than UNIX_PATH_MAX chars.
33+
maxSockName := filepath.Join(instDir, filenames.LongestSock)
34+
if len(maxSockName) >= osutil.UnixPathMax {
35+
return nil, fmt.Errorf("instance name %q too long: %q must be less than UNIX_PATH_MAX=%d characters, but is %d",
36+
instName, maxSockName, osutil.UnixPathMax, len(maxSockName))
37+
}
38+
if _, err := os.Stat(instDir); !errors.Is(err, os.ErrNotExist) {
39+
return nil, fmt.Errorf("instance %q already exists (%q)", instName, instDir)
40+
}
41+
// limayaml.Load() needs to pass the store file path to limayaml.FillDefault() to calculate default MAC addresses
42+
filePath := filepath.Join(instDir, filenames.LimaYAML)
43+
y, err := limayaml.Load(yBytes, filePath)
44+
if err != nil {
45+
return nil, err
46+
}
47+
if err := limayaml.Validate(y, true); err != nil {
48+
if !saveBrokenYAML {
49+
return nil, err
50+
}
51+
rejectedYAML := "lima.REJECTED.yaml"
52+
if writeErr := os.WriteFile(rejectedYAML, yBytes, 0o644); writeErr != nil {
53+
return nil, fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %w: %w", rejectedYAML, writeErr, err)
54+
}
55+
return nil, fmt.Errorf("the YAML is invalid, saved the buffer as %q: %w", rejectedYAML, err)
56+
}
57+
if err := os.MkdirAll(instDir, 0o700); err != nil {
58+
return nil, err
59+
}
60+
if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
61+
return nil, err
62+
}
63+
if err := os.WriteFile(filepath.Join(instDir, filenames.LimaVersion), []byte(version.Version), 0o444); err != nil {
64+
return nil, err
65+
}
66+
67+
inst, err := store.Inspect(instName)
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
73+
Instance: inst,
74+
Yaml: y,
75+
})
76+
77+
if err := limaDriver.Register(ctx); err != nil {
78+
return nil, err
79+
}
80+
81+
return inst, nil
82+
}

pkg/start/start.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -371,17 +371,3 @@ func ShowMessage(inst *store.Instance) error {
371371
}
372372
return scanner.Err()
373373
}
374-
375-
func Register(ctx context.Context, inst *store.Instance) error {
376-
y, err := inst.LoadYAML()
377-
if err != nil {
378-
return err
379-
}
380-
381-
limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
382-
Instance: inst,
383-
Yaml: y,
384-
})
385-
386-
return limaDriver.Register(ctx)
387-
}

0 commit comments

Comments
 (0)