forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
122 lines (106 loc) · 3.2 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
package vm
import (
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshlog "github.com/cloudfoundry/bosh-agent/logger"
boshsys "github.com/cloudfoundry/bosh-agent/system"
boshuuid "github.com/cloudfoundry/bosh-agent/uuid"
bicloud "github.com/cloudfoundry/bosh-init/cloud"
biconfig "github.com/cloudfoundry/bosh-init/config"
biagentclient "github.com/cloudfoundry/bosh-init/deployment/agentclient"
bihttpagent "github.com/cloudfoundry/bosh-init/deployment/agentclient/http"
bideplmanifest "github.com/cloudfoundry/bosh-init/deployment/manifest"
bistemcell "github.com/cloudfoundry/bosh-init/stemcell"
)
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,
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.cloud.CreateVM(agentID, stemcell.CID(), resourcePool.CloudProperties, networkInterfaces, resourcePool.Env)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Creating vm with stemcell cid '%s'", stemcell.CID())
}
err = m.vmRepo.UpdateCurrent(cid)
if err != nil {
return nil, bosherr.WrapError(err, "Updating current vm record")
}
vm := NewVM(
cid,
m.vmRepo,
m.stemcellRepo,
m.diskDeployer,
m.agentClient,
m.cloud,
m.fs,
m.logger,
)
return vm, nil
}