-
Notifications
You must be signed in to change notification settings - Fork 18
/
vm.go
154 lines (125 loc) · 3.37 KB
/
vm.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package vetu
import (
"context"
"github.com/cirruslabs/echelon"
"github.com/getsentry/sentry-go"
"strconv"
"strings"
"sync"
)
type VM struct {
ident string
env map[string]string
runningVMCtx context.Context
runningVMCtxCancel context.CancelFunc
wg sync.WaitGroup
errChan chan error
}
func NewVMClonedFrom(
ctx context.Context,
from string,
to string,
cpu uint32,
memory uint32,
lazyPull bool,
env map[string]string,
logger *echelon.Logger,
) (*VM, error) {
runningVMCtx, runningVMCtxCancel := context.WithCancel(context.Background())
vm := &VM{
ident: to,
env: env,
runningVMCtx: runningVMCtx,
runningVMCtxCancel: runningVMCtxCancel,
errChan: make(chan error, 1),
}
pullLogger := logger.Scoped("pull virtual machine")
if !lazyPull {
pullLogger.Infof("Pulling virtual machine %s...", from)
if _, _, err := CmdWithLogger(ctx, vm.env, pullLogger, "pull", from); err != nil {
pullLogger.Errorf("Ignoring pull failure: %v", err)
pullLogger.FinishWithType(echelon.FinishTypeFailed)
} else {
pullLogger.FinishWithType(echelon.FinishTypeSucceeded)
}
} else {
pullLogger.FinishWithType(echelon.FinishTypeSkipped)
}
cloneLogger := logger.Scoped("clone virtual machine")
cloneLogger.Infof("Cloning virtual machine %s...", from)
if _, _, err := CmdWithLogger(ctx, vm.env, cloneLogger, "clone", from, vm.ident); err != nil {
cloneLogger.Finish(false)
return nil, err
}
if cpu != 0 {
cpuStr := strconv.FormatUint(uint64(cpu), 10)
_, _, err := CmdWithLogger(ctx, vm.env, cloneLogger, "set", vm.ident, "--cpu", cpuStr)
if err != nil {
cloneLogger.Finish(false)
return nil, err
}
}
if memory != 0 {
memoryStr := strconv.FormatUint(uint64(memory), 10)
_, _, err := CmdWithLogger(ctx, vm.env, cloneLogger, "set", vm.ident, "--memory", memoryStr)
if err != nil {
cloneLogger.Finish(false)
return nil, err
}
}
cloneLogger.Finish(true)
return vm, nil
}
func (vm *VM) Ident() string {
return vm.ident
}
func (vm *VM) Start(
ctx context.Context,
bridgedInterface string,
hostNetworking bool,
) {
vm.wg.Add(1)
go func() {
defer vm.wg.Done()
args := []string{"run"}
if bridgedInterface != "" {
args = append(args, "--net-bridged", bridgedInterface)
} else if hostNetworking {
args = append(args, "--net-host")
}
args = append(args, vm.ident)
stdout, stderr, err := Cmd(vm.runningVMCtx, vm.env, args...)
if localHub := sentry.GetHubFromContext(ctx); localHub != nil {
localHub.AddBreadcrumb(&sentry.Breadcrumb{
Message: "\"vetu run\" finished",
Data: map[string]interface{}{
"err": err,
"stdout": stdout,
"stderr": stderr,
},
}, nil)
}
vm.errChan <- err
}()
}
func (vm *VM) ErrChan() chan error {
return vm.errChan
}
func (vm *VM) RetrieveIP(ctx context.Context) (string, error) {
// wait 30 seconds since usually a VM boots in 15
stdout, _, err := Cmd(ctx, vm.env, "ip", "--wait", "30", vm.ident)
if err != nil {
return "", err
}
return strings.TrimSpace(stdout), nil
}
func (vm *VM) Close() error {
ctx := context.Background()
// Try to gracefully terminate the VM
//nolint:dogsled // not interested in the output for now
_, _, _ = Cmd(ctx, vm.env, "stop", "--timeout", "5", vm.ident)
vm.runningVMCtxCancel()
vm.wg.Wait()
_, _, err := Cmd(ctx, vm.env, "delete", vm.ident)
return err
}