This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
/
vms.go
282 lines (255 loc) · 7.34 KB
/
vms.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
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
// Copyright (c) 2016 VMware, Inc. All Rights Reserved.
//
// This product is licensed to you under the Apache License, Version 2.0 (the "License").
// You may not use this product except in compliance with the License.
//
// This product may include a number of subcomponents with separate copyright notices and
// license terms. Your use of these subcomponents is subject to the terms and conditions
// of the subcomponent's license, as noted in the LICENSE file.
package main
import (
"errors"
"net/http"
"github.com/vmware/bosh-photon-cpi/cpi"
ec "github.com/vmware/photon-controller-go-sdk/photon"
)
type CloudProps struct {
VMFlavor string
DiskFlavor string
VMAttachedDiskSizeGB int
}
const (
VMAttachedDiskSizeGBDefault = 16
DiskFlavorElement = "disk_flavor"
VMFlavorElement = "vm_flavor"
VMAttachedDiskSizeGBElement = "vm_attached_disk_size_gb"
)
var ErrCloudPropsValues = errors.New("error in cloud props properties")
func ParseCloudProps(cloudPropsMap map[string]interface{}) (cloudProps CloudProps, err error) {
var (
diskOk bool
vmOk bool
)
cloudProps.DiskFlavor, diskOk = cloudPropsMap[DiskFlavorElement].(string)
cloudProps.VMFlavor, vmOk = cloudPropsMap[VMFlavorElement].(string)
cloudProps.VMAttachedDiskSizeGB = VMAttachedDiskSizeGBDefault
if _, ok := cloudPropsMap[VMAttachedDiskSizeGBElement]; ok {
cloudProps.VMAttachedDiskSizeGB = int(cloudPropsMap[VMAttachedDiskSizeGBElement].(float64))
}
if !diskOk || !vmOk {
err = ErrCloudPropsValues
}
return
}
func CreateVM(ctx *cpi.Context, args []interface{}) (result interface{}, err error) {
if len(args) < 6 {
return nil, errors.New("Expected at least 6 arguments")
}
agentID, ok := args[0].(string)
if !ok {
return nil, errors.New("Unexpected argument where agent_id should be")
}
stemcellCID, ok := args[1].(string)
if !ok {
return nil, errors.New("Unexpected argument where stemcell_cid should be")
}
cloudPropsMap, ok := args[2].(map[string]interface{})
if !ok {
return nil, errors.New("Unexpected argument where cloud_properties should be")
}
cloudProps, err := ParseCloudProps(cloudPropsMap)
if err != nil {
return nil, err
}
networks, ok := args[3].(map[string]interface{})
if !ok {
return nil, errors.New("Unexpected argument where networks should be")
}
// Ignore args[4] for now, which is disk_cids
env, ok := args[5].(map[string]interface{})
if !ok {
return nil, errors.New("Unexpected argument where env should be")
}
ctx.Logger.Infof(
"CreateVM with agent_id: '%v', stemcell_cid: '%v', cloud_properties: '%v', networks: '%v', env: '%v'",
agentID, stemcellCID, cloudProps, networks, env)
ephDiskName := "bosh-ephemeral-disk"
spec := &ec.VmCreateSpec{
Name: "bosh-vm",
Flavor: cloudProps.VMFlavor,
SourceImageID: stemcellCID,
AttachedDisks: []ec.AttachedDisk{
ec.AttachedDisk{
CapacityGB: 50, // Ignored
Flavor: cloudProps.DiskFlavor,
Kind: "ephemeral-disk",
Name: "boot-disk",
State: "STARTED",
BootDisk: true,
},
ec.AttachedDisk{
CapacityGB: cloudProps.VMAttachedDiskSizeGB,
Flavor: cloudProps.DiskFlavor,
Kind: "ephemeral-disk",
Name: ephDiskName,
State: "STARTED",
BootDisk: false,
},
},
}
ctx.Logger.Infof("Creating VM with spec: %#v", spec)
vmTask, err := ctx.Client.Projects.CreateVM(ctx.Config.Photon.ProjectID, spec)
if err != nil {
return
}
ctx.Logger.Infof("Waiting on task: %#v", vmTask)
vmTask, err = ctx.Client.Tasks.Wait(vmTask.ID)
if err != nil {
return
}
// Get disk details of VM
ctx.Logger.Infof("Getting details of VM: %s", vmTask.Entity.ID)
vm, err := ctx.Client.VMs.Get(vmTask.Entity.ID)
if err != nil {
return
}
diskID := ""
for _, disk := range vm.AttachedDisks {
if disk.Name == ephDiskName {
diskID = disk.ID
break
}
}
if diskID == "" {
err = cpi.NewBoshError(
cpi.CloudError, false, "Could not find ID for ephemeral disk of new VM %s", vm.ID)
return
}
// Create agent config
agentEnv := &cpi.AgentEnv{
AgentID: agentID,
VM: cpi.VMSpec{Name: vm.Name, ID: vm.ID},
Networks: networks,
Env: env,
Mbus: ctx.Config.Agent.Mbus,
NTP: ctx.Config.Agent.NTP,
Disks: map[string]interface{}{
"ephemeral": map[string]interface{}{
"id": diskID,
"path": "/dev/sdb"},
},
Blobstore: cpi.BlobstoreSpec{
Provider: ctx.Config.Agent.Blobstore.Provider,
Options: ctx.Config.Agent.Blobstore.Options,
},
}
// Create and attach agent env ISO file
err = updateAgentEnv(ctx, vmTask.Entity.ID, agentEnv)
if err != nil {
return
}
ctx.Logger.Info("Starting VM")
onTask, err := ctx.Client.VMs.Start(vmTask.Entity.ID)
if err != nil {
return
}
ctx.Logger.Infof("Waiting on task: %#v", onTask)
onTask, err = ctx.Client.Tasks.Wait(onTask.ID)
if err != nil {
return
}
return vmTask.Entity.ID, nil
}
func DeleteVM(ctx *cpi.Context, args []interface{}) (result interface{}, err error) {
if len(args) < 1 {
return nil, errors.New("Expected at least 1 argument")
}
vmCID, ok := args[0].(string)
if !ok {
return nil, errors.New("Unexpected argument where vm_cid should be")
}
ctx.Logger.Infof("Deleting VM: %s", vmCID)
ctx.Logger.Info("Detaching disks")
// Detach any attached disks first
disks, err := ctx.Client.Projects.GetDisks(ctx.Config.Photon.ProjectID, nil)
if err != nil {
return
}
for _, disk := range disks.Items {
for _, vmID := range disk.VMs {
if vmID == vmCID {
ctx.Logger.Infof("Detaching disk: %s", disk.ID)
detachOp := &ec.VmDiskOperation{DiskID: disk.ID}
detachTask, err := ctx.Client.VMs.DetachDisk(vmCID, detachOp)
if err != nil {
return nil, err
}
ctx.Logger.Infof("Waiting on task: %#v", detachTask)
detachTask, err = ctx.Client.Tasks.Wait(detachTask.ID)
if err != nil {
return nil, err
}
}
}
}
ctx.Logger.Info("Stopping VM")
offTask, err := ctx.Client.VMs.Stop(vmCID)
if err != nil {
return
}
ctx.Logger.Infof("Waiting on task: %#v", offTask)
offTask, err = ctx.Client.Tasks.Wait(offTask.ID)
if err != nil {
return
}
ctx.Logger.Info("Deleting VM")
task, err := ctx.Client.VMs.Delete(vmCID)
if err != nil {
return
}
ctx.Logger.Infof("Waiting on task: %#v", task)
_, err = ctx.Client.Tasks.Wait(task.ID)
if err != nil {
return
}
return nil, nil
}
func HasVM(ctx *cpi.Context, args []interface{}) (result interface{}, err error) {
if len(args) < 1 {
return nil, errors.New("Expected at least 1 argument")
}
vmCID, ok := args[0].(string)
if !ok {
return nil, errors.New("Unexpected argument where vm_cid should be")
}
ctx.Logger.Infof("Determining if VM exists: %s", vmCID)
_, err = ctx.Client.VMs.Get(vmCID)
if err != nil {
apiErr, ok := err.(ec.ApiError)
if ok && apiErr.HttpStatusCode == http.StatusNotFound {
return false, nil
}
return nil, err
}
return true, nil
}
func RestartVM(ctx *cpi.Context, args []interface{}) (result interface{}, err error) {
if len(args) < 1 {
return nil, errors.New("Expected at least 1 argument")
}
vmCID, ok := args[0].(string)
if !ok {
return nil, errors.New("Unexpected argument where vm_cid should be")
}
ctx.Logger.Infof("Restarting VM: %s", vmCID)
task, err := ctx.Client.VMs.Restart(vmCID)
if err != nil {
return
}
ctx.Logger.Infof("Waiting on task: %#v", task)
_, err = ctx.Client.Tasks.Wait(task.ID)
if err != nil {
return
}
return nil, nil
}