forked from docker-archive/deploykit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.go
More file actions
403 lines (332 loc) · 9.47 KB
/
Copy pathinstance.go
File metadata and controls
403 lines (332 loc) · 9.47 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package hyperkit
import (
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"os/user"
"path"
"path/filepath"
"time"
logutil "github.com/docker/infrakit/pkg/log"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/docker/infrakit/pkg/types"
"github.com/moby/hyperkit/go"
"github.com/rneugeba/iso9660wrap"
"github.com/satori/go.uuid"
)
var log = logutil.New("module", "instance/hyperkit")
// NewPlugin creates an instance plugin for hyperkit.
func NewPlugin(vmDir, hyperkit, vpnkitSock string) instance.Plugin {
return &hyperkitPlugin{VMDir: vmDir,
HyperKit: hyperkit,
VPNKitSock: vpnkitSock,
DiskDir: path.Join(vmDir, "disks"),
}
}
type hyperkitPlugin struct {
// VMDir is the path to a directory where per VM state is kept
VMDir string
// Hyperkit is the path to the hyperkit executable
HyperKit string
// VPNKitSock is the path to the VPNKit Unix domain socket.
VPNKitSock string
// DiskDir is the path to persistent (across reboots) disk images
DiskDir string
}
// Properties is the struct that holds the input
type Properties struct {
hyperkit.HyperKit
// Checksum is a checksum for the image
Checksum string
}
// Validate performs local validation on a provision request.
func (p hyperkitPlugin) Validate(req *types.Any) error {
properties := Properties{}
if err := req.Decode(&properties); err != nil {
return fmt.Errorf("error decoding guest configuration: %s, err=%v", req.String(), err)
}
// The guest is just the same data structure used by hyperkit for full fidelity config
guest := properties.HyperKit
for key, check := range map[string]int{
"CPUs": guest.CPUs,
"Memory": guest.Memory,
"DiskSize": guest.DiskSize,
} {
if check == 0 {
return fmt.Errorf("no %s specified", key)
}
}
for key, check := range map[string]string{
"Kernel": guest.Kernel,
"Initrd": guest.Initrd,
} {
if check == "" {
return fmt.Errorf("no %s specified", key)
}
}
return nil
}
// Provision creates a new instance.
func (p hyperkitPlugin) Provision(spec instance.Spec) (*instance.ID, error) {
if spec.Properties == nil {
return nil, fmt.Errorf("missing properties in spec")
}
properties := Properties{
HyperKit: hyperkit.HyperKit{
HyperKit: p.HyperKit,
VPNKitSock: p.VPNKitSock,
},
}
if err := spec.Properties.Decode(&properties); err != nil {
return nil, fmt.Errorf("error decoding guest configuration: err=%v", err)
}
// The guest is just the same data structure used by hyperkit for full fidelity config
guest := properties.HyperKit
// directory for instance state
instanceDir, err := ioutil.TempDir(p.VMDir, "infrakit-")
if err != nil {
return nil, err
}
guest.StateDir = instanceDir
// instance id
id := instance.ID(path.Base(instanceDir))
log.Info("new instance", "id", id)
logicalID := string(id)
if spec.LogicalID != nil {
// Assume IP address is the format of the LogicalID
logicalID = string(*spec.LogicalID)
// The LogicalID may be a IP address. If so, translate
// it into a magic UUID which cause VPNKit to assign a
// fixed IP address
if ip := net.ParseIP(logicalID); len(ip) > 0 {
vpnkitkey := make([]byte, 16)
vpnkitkey[12] = ip.To4()[0]
vpnkitkey[13] = ip.To4()[1]
vpnkitkey[14] = ip.To4()[2]
vpnkitkey[15] = ip.To4()[3]
guest.VPNKitKey = fmt.Sprintf("%x-%x-%x-%x-%x",
vpnkitkey[0:4],
vpnkitkey[4:6],
vpnkitkey[6:8],
vpnkitkey[8:10],
vpnkitkey[10:])
}
// If a LogicalID is supplied and the Disk size is
// non-zero, we place the disk in a special directory
// so it persists across reboots.
if guest.DiskSize > 0 {
guest.DiskImage = path.Join(p.DiskDir, logicalID+".img")
}
}
// if there's init then build an iso image of that
if spec.Init != "" {
isoImage := path.Join(instanceDir, "data.iso")
outfh, err := os.OpenFile(isoImage, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Crit("Cannot create user data ISOs", "err", err)
}
err = iso9660wrap.WriteBuffer(outfh, []byte(spec.Init), "config")
if err != nil {
log.Crit("Cannot write user data ISO", "err", err)
}
outfh.Close()
guest.ISOImage = isoImage
}
// Generate new UUID, otherwise /sys/class/dmi/id/product_uuid is identical on all VMs
guest.UUID = uuid.NewV4().String()
guest.VPNKitSock, err = checkVPNKitSock(p.VPNKitSock)
if err != nil {
return nil, err
}
log.Info("Starting guest", "id", id, "guest", guest, "uuid", guest.UUID,
"kernel", guest.Kernel, "initrd", guest.Initrd,
"cpus", guest.CPUs, "memory", guest.Memory, "disksize", guest.DiskSize,
"image", guest.DiskImage, "isoimage", guest.ISOImage,
"cmdline", guest.CmdLine)
if err := ioutil.WriteFile(path.Join(instanceDir, "logical.id"), []byte(logicalID), 0644); err != nil {
return nil, err
}
// inject additional tags
spec.Tags["infrakit.id"] = string(id)
spec.Tags["infrakit.logicalID"] = logicalID
tagData, err := types.AnyValue(spec.Tags)
if err != nil {
return nil, err
}
log.Debug("tags", "id", id, "tags", tagData)
if err := ioutil.WriteFile(path.Join(instanceDir, "tags"), tagData.Bytes(), 0644); err != nil {
return nil, err
}
guest.Console = hyperkit.ConsoleFile
err = guest.Start(guest.CmdLine)
if err != nil {
return nil, err
}
log.Info("Started", "id", id)
return &id, nil
}
// Label labels the instance
func (p hyperkitPlugin) Label(instance instance.ID, labels map[string]string) error {
instanceDir := path.Join(p.VMDir, string(instance))
tagFile := path.Join(instanceDir, "tags")
buff, err := ioutil.ReadFile(tagFile)
if err != nil {
return err
}
tags := map[string]string{}
err = types.AnyBytes(buff).Decode(&tags)
if err != nil {
return err
}
for k, v := range labels {
tags[k] = v
}
encoded, err := types.AnyValue(tags)
if err != nil {
return err
}
return ioutil.WriteFile(tagFile, encoded.Bytes(), 0644)
}
// Destroy terminates an existing instance.
func (p hyperkitPlugin) Destroy(id instance.ID, ctx instance.Context) error {
log.Info("Destroying VM", "id", id)
instanceDir := path.Join(p.VMDir, string(id))
_, err := os.Stat(instanceDir)
if err != nil {
if os.IsNotExist(err) {
return errors.New("Instance does not exist")
}
}
h, err := hyperkit.FromState(instanceDir)
if err != nil {
return err
}
err = h.Stop()
if err != nil {
return err
}
if h.IsRunning() {
timeout := time.After(30 * time.Second)
tick := time.Tick(1 * time.Second)
go func() {
check:
for {
select {
case <-timeout:
log.Warn("timeout trying to stop instance", "id", id)
return
case <-tick:
if !h.IsRunning() {
log.Debug("hyperkit stopped", "id", id)
break check
}
}
}
log.Debug("removing on-disk state", "id", id)
err := h.Remove(false)
if err != nil {
log.Warn("cannot remove state", "id", id, "err", err)
}
log.Info("removed on-disk state", "id", id)
}()
}
log.Debug("removing on-disk state", "id", id)
return h.Remove(false)
}
// DescribeInstances returns descriptions of all instances matching all of the provided tags.
func (p hyperkitPlugin) DescribeInstances(tags map[string]string, properties bool) ([]instance.Description, error) {
files, err := ioutil.ReadDir(p.VMDir)
if err != nil {
return nil, err
}
descriptions := []instance.Description{}
for _, file := range files {
if !file.IsDir() {
continue
}
instanceDir := path.Join(p.VMDir, file.Name())
tagData, err := ioutil.ReadFile(path.Join(instanceDir, "tags"))
if err != nil {
if os.IsNotExist(err) {
continue
}
return nil, err
}
instanceTags := map[string]string{}
if err := types.AnyBytes(tagData).Decode(&instanceTags); err != nil {
return nil, err
}
allMatched := true
for k, v := range tags {
value, exists := instanceTags[k]
if !exists || v != value {
allMatched = false
break
}
}
if allMatched {
var logicalID *instance.LogicalID
id := instance.ID(file.Name())
h, err := hyperkit.FromState(instanceDir)
if err != nil {
log.Warn("Could not get instance data", "id", id)
p.Destroy(id, instance.Termination)
continue
}
// Some extra information about the instance
// TODO - explore the hyperkit api and see what it can expose
lidData, err := ioutil.ReadFile(path.Join(instanceDir, "logical.id"))
if err != nil {
log.Warn("Could not get logical ID", "id", id)
continue
}
lid := instance.LogicalID(lidData)
logicalID = &lid
desc := instance.Description{
ID: id,
LogicalID: logicalID,
Tags: instanceTags,
}
if properties {
extra := map[string]interface{}{
"running": h.IsRunning(),
}
jsonData, err := ioutil.ReadFile(path.Join(instanceDir, "hyperkit.json"))
if err != nil {
log.Warn("Could not load hyperkit.json", "id", id)
continue
}
if err := types.AnyBytes(jsonData).Decode(&extra); err != nil {
log.Warn("Could not decode hyperkit.json", "id", id)
continue
}
desc.Properties = types.AnyValueMust(extra)
}
descriptions = append(descriptions, desc)
}
}
return descriptions, nil
}
const (
defaultVPNKitSock = "Library/Containers/com.docker.docker/Data/s50"
)
// checkVPNKitSock tries to find and/or validate the path of the VPNKit socket
func checkVPNKitSock(vpnkitsock string) (string, error) {
if vpnkitsock == "auto" {
vpnkitsock = filepath.Join(getHome(), defaultVPNKitSock)
}
vpnkitsock = filepath.Clean(vpnkitsock)
_, err := os.Stat(vpnkitsock)
if err != nil {
return "", err
}
return vpnkitsock, nil
}
func getHome() string {
if usr, err := user.Current(); err == nil {
return usr.HomeDir
}
return os.Getenv("HOME")
}