forked from arm/remoteproc-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimavm.go
More file actions
89 lines (70 loc) · 2.38 KB
/
Copy pathlimavm.go
File metadata and controls
89 lines (70 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package limavm
import (
"bytes"
"fmt"
"os/exec"
"path/filepath"
"strings"
"github.com/arm/remoteproc-runtime/e2e/repo"
"github.com/arm/remoteproc-runtime/e2e/runner"
)
var (
prepareLimaVMScript = filepath.Join(repo.MustFindRootDir(), "e2e", "limavm", "prepare-lima-vm.sh")
teardownLimaVMScript = filepath.Join(repo.MustFindRootDir(), "e2e", "limavm", "teardown-lima-vm.sh")
)
type LimaVM struct {
name string
}
var BinBuildEnv = map[string]string{
"GOOS": "linux",
"GOARCH": "amd64",
"CGO_ENABLED": "0",
}
func NewWithDocker(mountDir string, buildContext string, bins repo.Bins) (LimaVM, error) {
return New("docker", mountDir, buildContext, string(bins.Runtime), string(bins.Shim))
}
func NewWithPodman(mountDir string, buildContext string, runtimeBin repo.RuntimeBin) (LimaVM, error) {
return New("podman", mountDir, buildContext, string(runtimeBin))
}
func NewAlpine(mountDir string, runtimeBin repo.RuntimeBin) (LimaVM, error) {
return New("alpine", mountDir, "", string(runtimeBin))
}
func New(template string, mountDir string, buildContext string, binsToInstall ...string) (LimaVM, error) {
cmd := exec.Command(
prepareLimaVMScript,
append([]string{template, mountDir, buildContext}, binsToInstall...)...,
)
streamer := runner.NewStreamingCmd(cmd).WithPrefix("prepare-vm")
if err := streamer.Start(); err != nil {
return LimaVM{}, fmt.Errorf("failed to start prepare script: %w", err)
}
if err := streamer.Wait(); err != nil {
return LimaVM{}, fmt.Errorf("failed to prepare VM: %w", err)
}
vmName := strings.TrimSpace(streamer.Output())
if vmName == "" {
return LimaVM{}, fmt.Errorf("prepare script did not return VM name")
}
return LimaVM{name: vmName}, nil
}
func (vm LimaVM) Cleanup() {
cmd := exec.Command(teardownLimaVMScript, vm.name)
_ = cmd.Run()
}
func (vm LimaVM) cmd(name string, args ...string) *exec.Cmd {
allArgs := append([]string{"shell", vm.name, name}, args...)
return exec.Command("limactl", allArgs...)
}
func (vm LimaVM) RunCommand(name string, args ...string) (stdout, stderr string, err error) {
cmd := vm.cmd(name, args...)
var stdoutBuf, stderrBuf bytes.Buffer
cmd.Stdout = &stdoutBuf
cmd.Stderr = &stderrBuf
err = cmd.Run()
stdout = stdoutBuf.String()
stderr = stderrBuf.String()
if err != nil {
return stdout, stderr, fmt.Errorf("cmd failed: %w\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
}
return stdout, stderr, nil
}