-
Notifications
You must be signed in to change notification settings - Fork 161
/
manager.go
151 lines (133 loc) · 4.11 KB
/
manager.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
package vm
import (
biagentclient "github.com/cloudfoundry/bosh-agent/agentclient"
bihttpagent "github.com/cloudfoundry/bosh-agent/agentclient/http"
bicloud "github.com/cloudfoundry/bosh-cli/cloud"
biconfig "github.com/cloudfoundry/bosh-cli/config"
bideplmanifest "github.com/cloudfoundry/bosh-cli/deployment/manifest"
bistemcell "github.com/cloudfoundry/bosh-cli/stemcell"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
biproperty "github.com/cloudfoundry/bosh-utils/property"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
"github.com/pivotal-golang/clock"
)
type Manager interface {
FindCurrent() (VM, bool, error)
Create(bistemcell.CloudStemcell, bideplmanifest.Manifest) (VM, error)
}
type manager struct {
vmRepo biconfig.VMRepo
stemcellRepo biconfig.StemcellRepo
diskDeployer DiskDeployer
agentClient biagentclient.AgentClient
agentClientFactory bihttpagent.AgentClientFactory
cloud bicloud.Cloud
uuidGenerator boshuuid.Generator
fs boshsys.FileSystem
logger boshlog.Logger
logTag string
}
func NewManager(
vmRepo biconfig.VMRepo,
stemcellRepo biconfig.StemcellRepo,
diskDeployer DiskDeployer,
agentClient biagentclient.AgentClient,
cloud bicloud.Cloud,
uuidGenerator boshuuid.Generator,
fs boshsys.FileSystem,
logger boshlog.Logger,
) Manager {
return &manager{
cloud: cloud,
agentClient: agentClient,
vmRepo: vmRepo,
stemcellRepo: stemcellRepo,
diskDeployer: diskDeployer,
uuidGenerator: uuidGenerator,
fs: fs,
logger: logger,
logTag: "vmManager",
}
}
func (m *manager) FindCurrent() (VM, bool, error) {
vmCID, found, err := m.vmRepo.FindCurrent()
if err != nil {
return nil, false, bosherr.WrapError(err, "Finding currently deployed vm")
}
if !found {
return nil, false, nil
}
vm := NewVM(
vmCID,
m.vmRepo,
m.stemcellRepo,
m.diskDeployer,
m.agentClient,
m.cloud,
clock.NewClock(),
m.fs,
m.logger,
)
return vm, true, err
}
func (m *manager) Create(stemcell bistemcell.CloudStemcell, deploymentManifest bideplmanifest.Manifest) (VM, error) {
jobName := deploymentManifest.JobName()
networkInterfaces, err := deploymentManifest.NetworkInterfaces(jobName)
m.logger.Debug(m.logTag, "Creating VM with network interfaces: %#v", networkInterfaces)
if err != nil {
return nil, bosherr.WrapError(err, "Getting network spec")
}
resourcePool, err := deploymentManifest.ResourcePool(jobName)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Getting resource pool for job '%s'", jobName)
}
agentID, err := m.uuidGenerator.Generate()
if err != nil {
return nil, bosherr.WrapError(err, "Generating agent ID")
}
cid, err := m.createAndRecordVM(agentID, stemcell, resourcePool, networkInterfaces)
if err != nil {
return nil, err
}
metadata := bicloud.VMMetadata{
Deployment: deploymentManifest.Name,
Job: deploymentManifest.JobName(),
Index: "0",
Director: "bosh-init",
}
err = m.cloud.SetVMMetadata(cid, metadata)
if err != nil {
cloudErr, ok := err.(bicloud.Error)
if ok && cloudErr.Type() == bicloud.NotImplementedError {
//ignore it
} else {
return nil, bosherr.WrapErrorf(err, "Setting VM metadata to %s", metadata)
}
}
vm := NewVM(
cid,
m.vmRepo,
m.stemcellRepo,
m.diskDeployer,
m.agentClient,
m.cloud,
clock.NewClock(),
m.fs,
m.logger,
)
return vm, nil
}
func (m *manager) createAndRecordVM(agentID string, stemcell bistemcell.CloudStemcell, resourcePool bideplmanifest.ResourcePool, networkInterfaces map[string]biproperty.Map) (string, error) {
cid, err := m.cloud.CreateVM(agentID, stemcell.CID(), resourcePool.CloudProperties, networkInterfaces, resourcePool.Env)
if err != nil {
return "", bosherr.WrapErrorf(err, "Creating vm with stemcell cid '%s'", stemcell.CID())
}
// Record vm info immediately so we don't leak it
err = m.vmRepo.UpdateCurrent(cid)
if err != nil {
return "", bosherr.WrapError(err, "Updating current vm record")
}
return cid, nil
}