-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.go
141 lines (112 loc) · 3.17 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
package yeoman
import (
"context"
"fmt"
"log/slog"
)
type VM struct {
// Name of the VM.
Name string `json:"name"`
// MachineType in the cloud provider.
MachineType string `json:"machineType"`
// Disk size in GB.
Disk int `json:"disk"`
// GPU to attach to the machine.
GPU *GPU `json:"gpu,omitempty"`
// Image installed on the disk.
Image string `json:"image,omitempty"`
// ContainerImage to be used if configured.
ContainerImage string `json:"containerImage,omitempty"`
// Tags on the box applied during creation to indicate the services to
// be deployed.
Tags []string `json:"-"`
// IPs assigned to the box.
IPs StaticVMIPs `json:"ips,omitempty"`
// AllowHTTP will disable any cloud-based firewall on ports 80 and 443.
// This is useful for reverse-proxy boxes that need to be exposed to
// the public internet.
AllowHTTP bool `json:"allowHTTP"`
// Seccomp allows overwriting the default seccomp profile in Docker.
// This is useful for running headless chromium, among other things.
Seccomp string `json:"seccomp"`
// Running is true when the VM is live and false in all other
// circumstances.
Running bool `json:"running"`
}
type StaticVMIPs struct {
Internal IP `json:"internal"`
External IP `json:"external"`
}
type GPU struct {
Type string `json:"type"`
Count int `json:"count"`
}
type VMStore interface {
// GetAllVMs on the cloud provider.
GetAllVMs(context.Context, *slog.Logger) ([]VM, error)
// GetVM by name on the cloud provider.
GetVM(context.Context, *slog.Logger, string) (VM, error)
// DeleteVM. The implementation should shutdown the VM properly prior
// to delete and hang until the box is completely deleted.
DeleteVM(ctx context.Context, log *slog.Logger, name string) error
// CreateVM. This must hang until boot completes.
CreateVM(context.Context, *slog.Logger, VM) error
// RestartVM. Hang until boot completes.
RestartVM(context.Context, *slog.Logger, VM) error
}
type MockVMStore struct{ vals []string }
func (m *MockVMStore) GetAll(ctx context.Context) ([]*VM, error) {
const img = "tf-image-s6hkplzzvvp67nv7xsbkdricgskl3lpc"
m.vals = append(m.vals, "GetAll")
// Keep
vm1 := &VM{
Name: "ym-vm-box-1",
Tags: []string{"a", "box", img},
Disk: 1,
MachineType: "type",
}
// Delete, no bin
vm2 := &VM{
Name: "ym-vm-box-2",
Tags: []string{"a", "c", "box", img},
Disk: 1,
MachineType: "type",
}
// Delete, no tag
vm3 := &VM{
Name: "ym-vm-box-3",
Tags: []string{},
Disk: 1,
MachineType: "type",
}
// Delete, no machine type
vm4 := &VM{
Name: "ym-vm-box-4",
Disk: 1,
Tags: []string{},
}
// Delete, no disk
vm5 := &VM{
Name: "ym-vm-box-5",
Tags: []string{"a", "box", img},
MachineType: "type",
}
// Ignore, no prefix
vm6 := &VM{
Name: "x",
Tags: []string{"a"},
}
vms := []*VM{vm1, vm2, vm3, vm4, vm5, vm6}
return vms, nil
}
func (m *MockVMStore) Create(ctx context.Context, vm *VM) error {
m.vals = append(m.vals, fmt.Sprintf("Create %s", vm.Name))
return nil
}
func (m *MockVMStore) Delete(
ctx context.Context,
name string,
) error {
m.vals = append(m.vals, fmt.Sprintf("Delete %s", name))
return nil
}