/
factory.go
180 lines (146 loc) · 4.23 KB
/
factory.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
package vm
import (
"path/filepath"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
apiv1 "github.com/cppforlife/bosh-cpi-go/apiv1"
bdisk "github.com/cppforlife/bosh-virtualbox-cpi/disk"
"github.com/cppforlife/bosh-virtualbox-cpi/driver"
bstem "github.com/cppforlife/bosh-virtualbox-cpi/stemcell"
bnet "github.com/cppforlife/bosh-virtualbox-cpi/vm/network"
bpds "github.com/cppforlife/bosh-virtualbox-cpi/vm/portdevices"
)
type FactoryOpts struct {
DirPath string
StorageController string
AutoEnableNetworks bool
}
type Factory struct {
opts FactoryOpts
uuidGen boshuuid.Generator
driver driver.Driver
runner driver.Runner
diskFactory bdisk.Factory
agentOptions apiv1.AgentOptions
stemcellAPIVersion apiv1.StemcellAPIVersion
logTag string
logger boshlog.Logger
}
func NewFactory(
opts FactoryOpts,
uuidGen boshuuid.Generator,
driver driver.Driver,
runner driver.Runner,
diskFactory bdisk.Factory,
agentOptions apiv1.AgentOptions,
stemcellAPIVersion apiv1.StemcellAPIVersion,
logger boshlog.Logger,
) Factory {
return Factory{
opts: opts,
uuidGen: uuidGen,
driver: driver,
runner: runner,
diskFactory: diskFactory,
agentOptions: agentOptions,
stemcellAPIVersion: stemcellAPIVersion,
logTag: "vm.Factory",
logger: logger,
}
}
func (f Factory) Create(
agentID apiv1.AgentID,
stemcell bstem.Stemcell,
props apiv1.VMCloudProps,
networks apiv1.Networks,
env apiv1.VMEnv,
) (VM, error) {
host := Host{bnet.NewNetworks(f.driver, f.logger)}
vmProps, err := NewVMProps(props)
if err != nil {
return nil, err
}
vmNetworks, err := NewNetworks(networks)
if err != nil {
return nil, err
}
if f.opts.AutoEnableNetworks {
err := host.EnableNetworks(vmNetworks)
if err != nil {
return nil, bosherr.WrapError(err, "Enabling networks")
}
}
vm, err := f.newClonedVM(stemcell)
if err != nil {
return nil, err
}
err = vm.SetProps(vmProps)
if err != nil {
f.cleanUpPartialCreate(vm)
return nil, err
}
err = vm.ConfigureNICs(vmNetworks, host)
if err != nil {
f.cleanUpPartialCreate(vm)
return nil, bosherr.WrapError(err, "Configuring NICs")
}
initialAgentEnv := apiv1.NewAgentEnvFactory().ForVM(
agentID, vm.ID(), vmNetworks.AsNetworks(), env, f.agentOptions)
initialAgentEnv.AttachSystemDisk(apiv1.NewDiskHintFromString("0"))
err = vm.ConfigureAgent(initialAgentEnv)
if err != nil {
f.cleanUpPartialCreate(vm)
return nil, bosherr.WrapError(err, "Initial agent configuration")
}
ephemeralDisk, err := f.diskFactory.Create(vmProps.EphemeralDisk)
if err != nil {
f.cleanUpPartialCreate(vm)
return nil, bosherr.WrapError(err, "Creating ephemeral disk")
}
err = vm.AttachEphemeralDisk(ephemeralDisk)
if err != nil {
f.cleanUpPartialCreate(vm)
return nil, bosherr.WrapError(err, "Attaching ephemeral disk")
}
err = vm.Start(vmProps.GUI)
if err != nil {
f.cleanUpPartialCreate(vm)
return nil, bosherr.WrapError(err, "Starting VM")
}
return vm, nil
}
func (f Factory) cleanUpPartialCreate(vm VM) {
err := vm.Delete()
if err != nil {
f.logger.Error(f.logTag, "Failed to clean up partially created VM: %s", err)
}
}
func (f Factory) newVM(cid apiv1.VMCID) VMImpl {
pdsOpts := bpds.PortDevicesOpts{Controller: f.opts.StorageController}
portDevices := bpds.NewPortDevices(cid, pdsOpts, f.driver, f.logger)
store := NewStore(filepath.Join(f.opts.DirPath, cid.AsString()), f.runner)
return NewVMImpl(cid, portDevices, store, f.stemcellAPIVersion, f.driver, f.logger)
}
func (f Factory) Find(cid apiv1.VMCID) (VM, error) {
return f.newVM(cid), nil
}
func (f Factory) newClonedVM(stemcell bstem.Stemcell) (VMImpl, error) {
cloneIDInternal, err := f.uuidGen.Generate()
if err != nil {
return VMImpl{}, bosherr.WrapError(err, "Generating clone VM id")
}
cloneID := "vm-" + cloneIDInternal
_, err = f.driver.Execute(
"clonevm", stemcell.ID().AsString(),
"--snapshot", stemcell.SnapshotName(),
"--options", "link",
"--name", cloneID, // extra non-conflicting
"--uuid", cloneIDInternal,
"--register",
)
if err != nil {
return VMImpl{}, bosherr.WrapError(err, "Cloning VM")
}
return f.newVM(apiv1.NewVMCID(cloneID)), nil
}