forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvsphere_util.go
114 lines (96 loc) · 3.14 KB
/
vsphere_util.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
// +build go1.7
package cloud
import (
"context"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/types"
)
// Powered on means the instance is powered on and running.
// Powered off means the instance is completely powered off.
// Suspended means the instance is in a hibernated/sleep state.
func vsphereToEvgStatus(state types.VirtualMachinePowerState) CloudStatus {
switch state {
case types.VirtualMachinePowerStatePoweredOn:
return StatusRunning
case types.VirtualMachinePowerStatePoweredOff:
return StatusStopped
case types.VirtualMachinePowerStateSuspended:
return StatusStopped
default:
return StatusUnknown
}
}
// getInstance gets a reference to this instance from the vCenter.
// If this method errors, it is possible the instance does not exist.
func (c *vsphereClientImpl) getInstance(ctx context.Context, name string) (*object.VirtualMachine, error) {
vm, err := c.Finder.VirtualMachine(ctx, name)
if err != nil {
return nil, errors.Wrapf(err, "could not find vm for host %s", name)
}
return vm, err
}
// relocateSpec creates a spec for where the new machine will be located.
func (c *vsphereClientImpl) relocateSpec(ctx context.Context, s *vsphereSettings) (types.VirtualMachineRelocateSpec, error) {
var spec types.VirtualMachineRelocateSpec
var morRP types.ManagedObjectReference
var morDS types.ManagedObjectReference
// resource pool is required
rp, err := c.Finder.ResourcePool(ctx, s.ResourcePool)
if err != nil {
err = errors.Wrapf(err, "error finding pool %s", s.ResourcePool)
grip.Error(err)
return spec, err
}
morRP = rp.Common.Reference()
spec.Pool = &morRP
if s.Datastore != "" {
ds, err := c.Finder.Datastore(ctx, s.Datastore)
if err != nil {
err = errors.Wrapf(err, "error finding datastore %s", s.Datastore)
grip.Error(err)
return spec, err
}
morDS = ds.Common.Reference()
spec.Datastore = &morDS
}
grip.Info(message.Fields{
"message": "created spec to relocate clone",
"resource_pool": morRP,
"datastore": morDS,
})
return spec, nil
}
// configSpec creates a spec for hardware configuration of the new machine.
func (c *vsphereClientImpl) configSpec(s *vsphereSettings) types.VirtualMachineConfigSpec {
spec := types.VirtualMachineConfigSpec{
NumCPUs: s.NumCPUs,
MemoryMB: s.MemoryMB,
}
// the new vm will default to the clone's original values if either option is 0
grip.Info(message.Fields{
"message": "created spec to reconfigure clone",
"num_cpus": spec.NumCPUs,
"memory_mb": spec.MemoryMB,
})
return spec
}
// cloneSpec creates a spec for a new virtual machine, specifying
// where it will start up and its hardware configurations.
func (c *vsphereClientImpl) cloneSpec(ctx context.Context, s *vsphereSettings) (types.VirtualMachineCloneSpec, error) {
var spec types.VirtualMachineCloneSpec
cs := c.configSpec(s)
rs, err := c.relocateSpec(ctx, s)
if err != nil {
return spec, errors.Wrap(err, "error making relocate spec")
}
spec = types.VirtualMachineCloneSpec{
Config: &cs,
Location: rs,
Template: false,
PowerOn: true,
}
return spec, nil
}