-
Notifications
You must be signed in to change notification settings - Fork 23
/
factory.go
154 lines (125 loc) · 3.67 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
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"
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 AgentOptions
logTag string
logger boshlog.Logger
}
func NewFactory(
opts FactoryOpts,
uuidGen boshuuid.Generator,
driver driver.Driver,
runner driver.Runner,
diskFactory bdisk.Factory,
agentOptions AgentOptions,
logger boshlog.Logger,
) Factory {
return Factory{
opts: opts,
uuidGen: uuidGen,
driver: driver,
runner: runner,
diskFactory: diskFactory,
agentOptions: agentOptions,
logTag: "vm.Factory",
logger: logger,
}
}
func (f Factory) Create(agentID string, stemcell bstem.Stemcell, props VMProps, networks Networks, env Environment) (VM, error) {
if f.opts.AutoEnableNetworks {
err := Host{bnet.NewNetworks(f.driver, f.logger)}.EnableNetworks(networks)
if err != nil {
return nil, bosherr.WrapError(err, "Enabling networks")
}
}
vm, err := f.newClonedVM(stemcell)
if err != nil {
return nil, err
}
err = vm.SetProps(props)
if err != nil {
f.cleanUpPartialCreate(vm)
return nil, err
}
networksWithMACs, err := vm.ConfigureNICs(networks)
if err != nil {
f.cleanUpPartialCreate(vm)
return nil, bosherr.WrapError(err, "Configuring NICs")
}
initialAgentEnv := NewAgentEnvForVM(agentID, vm.ID(), networksWithMACs, env, f.agentOptions)
err = vm.ConfigureAgent(initialAgentEnv)
if err != nil {
f.cleanUpPartialCreate(vm)
return nil, bosherr.WrapError(err, "Initial agent configuration")
}
ephemeralDisk, err := f.diskFactory.Create(props.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(props.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(id string) VMImpl {
pdsOpts := bpds.PortDevicesOpts{Controller: f.opts.StorageController}
portDevices := bpds.NewPortDevices(id, pdsOpts, f.driver, f.logger)
store := NewStore(filepath.Join(f.opts.DirPath, id), f.runner)
return NewVMImpl(id, portDevices, store, f.driver, f.logger)
}
func (f Factory) Find(id string) (VM, error) {
return f.newVM(id), 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(),
"--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(cloneID), nil
}