forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy_cmd.go
424 lines (371 loc) · 14.2 KB
/
deploy_cmd.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package cmd
import (
"errors"
"path/filepath"
"strings"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshlog "github.com/cloudfoundry/bosh-agent/logger"
boshsys "github.com/cloudfoundry/bosh-agent/system"
"github.com/cloudfoundry/bosh-agent/uuid"
biblobstore "github.com/cloudfoundry/bosh-init/blobstore"
bicloud "github.com/cloudfoundry/bosh-init/cloud"
biconfig "github.com/cloudfoundry/bosh-init/config"
bicpirel "github.com/cloudfoundry/bosh-init/cpi/release"
bidepl "github.com/cloudfoundry/bosh-init/deployment"
bihttpagent "github.com/cloudfoundry/bosh-init/deployment/agentclient/http"
bideplmanifest "github.com/cloudfoundry/bosh-init/deployment/manifest"
bivm "github.com/cloudfoundry/bosh-init/deployment/vm"
biinstall "github.com/cloudfoundry/bosh-init/installation"
biinstallmanifest "github.com/cloudfoundry/bosh-init/installation/manifest"
birel "github.com/cloudfoundry/bosh-init/release"
birelset "github.com/cloudfoundry/bosh-init/release/set"
birelsetmanifest "github.com/cloudfoundry/bosh-init/release/set/manifest"
bistemcell "github.com/cloudfoundry/bosh-init/stemcell"
biui "github.com/cloudfoundry/bosh-init/ui"
)
type deployCmd struct {
ui biui.UI
userConfig biconfig.UserConfig
fs boshsys.FileSystem
releaseSetParser birelsetmanifest.Parser
installationParser biinstallmanifest.Parser
deploymentParser bideplmanifest.Parser
legacyDeploymentConfigMigrator biconfig.LegacyDeploymentConfigMigrator
deploymentConfigService biconfig.DeploymentConfigService
releaseSetValidator birelsetmanifest.Validator
installationValidator biinstallmanifest.Validator
deploymentValidator bideplmanifest.Validator
installerFactory biinstall.InstallerFactory
releaseExtractor birel.Extractor
releaseManager birel.Manager
releaseResolver birelset.Resolver
cloudFactory bicloud.Factory
agentClientFactory bihttpagent.AgentClientFactory
vmManagerFactory bivm.ManagerFactory
stemcellExtractor bistemcell.Extractor
stemcellManagerFactory bistemcell.ManagerFactory
deploymentRecord bidepl.Record
blobstoreFactory biblobstore.Factory
deployer bidepl.Deployer
eventLogger biui.Stage
uuidGenerator uuid.Generator
logger boshlog.Logger
logTag string
}
func NewDeployCmd(
ui biui.UI,
userConfig biconfig.UserConfig,
fs boshsys.FileSystem,
releaseSetParser birelsetmanifest.Parser,
installationParser biinstallmanifest.Parser,
deploymentParser bideplmanifest.Parser,
legacyDeploymentConfigMigrator biconfig.LegacyDeploymentConfigMigrator,
deploymentConfigService biconfig.DeploymentConfigService,
releaseSetValidator birelsetmanifest.Validator,
installationValidator biinstallmanifest.Validator,
deploymentValidator bideplmanifest.Validator,
installerFactory biinstall.InstallerFactory,
releaseExtractor birel.Extractor,
releaseManager birel.Manager,
releaseResolver birelset.Resolver,
cloudFactory bicloud.Factory,
agentClientFactory bihttpagent.AgentClientFactory,
vmManagerFactory bivm.ManagerFactory,
stemcellExtractor bistemcell.Extractor,
stemcellManagerFactory bistemcell.ManagerFactory,
deploymentRecord bidepl.Record,
blobstoreFactory biblobstore.Factory,
deployer bidepl.Deployer,
uuidGenerator uuid.Generator,
logger boshlog.Logger,
) Cmd {
return &deployCmd{
ui: ui,
userConfig: userConfig,
fs: fs,
releaseSetParser: releaseSetParser,
installationParser: installationParser,
deploymentParser: deploymentParser,
legacyDeploymentConfigMigrator: legacyDeploymentConfigMigrator,
deploymentConfigService: deploymentConfigService,
releaseSetValidator: releaseSetValidator,
installationValidator: installationValidator,
deploymentValidator: deploymentValidator,
installerFactory: installerFactory,
releaseExtractor: releaseExtractor,
releaseManager: releaseManager,
releaseResolver: releaseResolver,
cloudFactory: cloudFactory,
agentClientFactory: agentClientFactory,
vmManagerFactory: vmManagerFactory,
stemcellExtractor: stemcellExtractor,
stemcellManagerFactory: stemcellManagerFactory,
deploymentRecord: deploymentRecord,
blobstoreFactory: blobstoreFactory,
deployer: deployer,
uuidGenerator: uuidGenerator,
logger: logger,
logTag: "deployCmd",
}
}
func (c *deployCmd) Name() string {
return "deploy"
}
func (c *deployCmd) Meta() Meta {
return Meta{
Synopsis: "Create or update a deployment",
Usage: "<deployment_manifest_path> <stemcell_path> <cpi_release_path> [<release_paths...>]",
Env: genericEnv,
}
}
func (c *deployCmd) Run(stage biui.Stage, args []string) error {
deploymentManifestPath, stemcellTarballPath, releaseTarballPaths, err := c.parseCmdInputs(args)
if err != nil {
return err
}
manifestAbsFilePath, err := filepath.Abs(deploymentManifestPath)
if err != nil {
c.ui.ErrorLinef("Failed getting absolute path to deployment file '%s'", deploymentManifestPath)
return bosherr.WrapErrorf(err, "Getting absolute path to deployment file '%s'", deploymentManifestPath)
}
if !c.fs.FileExists(manifestAbsFilePath) {
c.ui.ErrorLinef("Deployment '%s' does not exist", manifestAbsFilePath)
return bosherr.Errorf("Deployment manifest does not exist at '%s'", manifestAbsFilePath)
}
c.userConfig.DeploymentManifestPath = manifestAbsFilePath
c.ui.PrintLinef("Deployment manifest: '%s'", manifestAbsFilePath)
deploymentConfigPath := c.userConfig.DeploymentConfigPath()
c.deploymentConfigService.SetConfigPath(deploymentConfigPath)
c.ui.PrintLinef("Deployment state: '%s'", deploymentConfigPath)
if !c.deploymentConfigService.Exists() {
migrated, err := c.legacyDeploymentConfigMigrator.MigrateIfExists(c.userConfig.LegacyDeploymentConfigPath())
if err != nil {
return bosherr.WrapError(err, "Migrating legacy deployment config file")
}
if migrated {
c.ui.PrintLinef("Migrated legacy deployments file: '%s'", c.userConfig.LegacyDeploymentConfigPath())
}
}
deploymentConfig, err := c.deploymentConfigService.Load()
if err != nil {
return bosherr.WrapError(err, "Loading deployment config")
}
var (
extractedStemcell bistemcell.ExtractedStemcell
deploymentManifest bideplmanifest.Manifest
installationManifest biinstallmanifest.Manifest
)
err = stage.PerformComplex("validating", func(stage biui.Stage) error {
extractedStemcell, deploymentManifest, installationManifest, err = c.validate(stage, stemcellTarballPath, releaseTarballPaths, deploymentManifestPath)
return err
})
if err != nil {
return err
}
defer func() {
deleteErr := extractedStemcell.Delete()
if deleteErr != nil {
c.logger.Warn(c.logTag, "Failed to delete extracted stemcell: %s", deleteErr.Error())
}
}()
defer func() {
err := c.releaseManager.DeleteAll()
if err != nil {
c.logger.Warn(c.logTag, "Deleting all extracted releases: %s", err.Error())
}
}()
isDeployed, err := c.deploymentRecord.IsDeployed(deploymentManifestPath, c.releaseManager.List(), extractedStemcell)
if err != nil {
return bosherr.WrapError(err, "Checking if deployment has changed")
}
if isDeployed {
c.ui.PrintLinef("No deployment, stemcell or release changes. Skipping deploy.")
return nil
}
installer, err := c.installerFactory.NewInstaller()
if err != nil {
return bosherr.WrapError(err, "Creating CPI Installer")
}
var installation biinstall.Installation
err = stage.PerformComplex("installing CPI", func(installStage biui.Stage) error {
installation, err = installer.Install(installationManifest, installStage)
return err
})
if err != nil {
return err
}
err = stage.Perform("Starting registry", func() error {
return installation.StartRegistry()
})
if err != nil {
return err
}
defer func() {
//TODO: wrap stopping registry in stage?
err := installation.StopRegistry()
if err != nil {
c.logger.Warn(c.logTag, "Registry failed to stop: %s", err)
}
}()
cloud, err := c.cloudFactory.NewCloud(installation, deploymentConfig.DirectorID)
if err != nil {
return bosherr.WrapError(err, "Creating CPI client from CPI installation")
}
stemcellManager := c.stemcellManagerFactory.NewManager(cloud)
cloudStemcell, err := stemcellManager.Upload(extractedStemcell, stage)
if err != nil {
return err
}
agentClient := c.agentClientFactory.NewAgentClient(deploymentConfig.DirectorID, installationManifest.Mbus)
vmManager := c.vmManagerFactory.NewManager(cloud, agentClient)
blobstore, err := c.blobstoreFactory.Create(installationManifest.Mbus)
if err != nil {
return bosherr.WrapError(err, "Creating blobstore client")
}
err = stage.PerformComplex("deploying", func(deployStage biui.Stage) error {
_, err = c.deployer.Deploy(
cloud,
deploymentManifest,
cloudStemcell,
installationManifest.Registry,
installationManifest.SSHTunnel,
vmManager,
blobstore,
deployStage,
)
if err != nil {
return bosherr.WrapError(err, "Deploying")
}
err = c.deploymentRecord.Update(deploymentManifestPath, c.releaseManager.List())
if err != nil {
return bosherr.WrapError(err, "Updating deployment record")
}
return nil
})
if err != nil {
return err
}
// TODO: cleanup unused disks here?
err = stemcellManager.DeleteUnused(stage)
if err != nil {
return err
}
return nil
}
type Deployment struct{}
func (c *deployCmd) parseCmdInputs(args []string) (string, string, []string, error) {
if len(args) < 3 {
c.ui.ErrorLinef("Invalid usage - deploy command requires at least 3 arguments")
c.ui.PrintLinef("Expected usage: bosh-init deploy <deployment-manifest> <stemcell-tarball> <cpi-release-tarball> [release-2-tarball [release-3-tarball...]]")
c.logger.Error(c.logTag, "Invalid arguments: %#v", args)
return "", "", []string{}, errors.New("Invalid usage - deploy command requires at least 3 arguments")
}
return args[0], args[1], args[2:], nil
}
func (c *deployCmd) isBlank(str string) bool {
return str == "" || strings.TrimSpace(str) == ""
}
func (c *deployCmd) validate(
validationStage biui.Stage,
stemcellTarballPath string,
releaseTarballPaths []string,
deploymentManifestPath string,
) (
extractedStemcell bistemcell.ExtractedStemcell,
deploymentManifest bideplmanifest.Manifest,
installationManifest biinstallmanifest.Manifest,
err error,
) {
err = validationStage.Perform("Validating stemcell", func() error {
if !c.fs.FileExists(stemcellTarballPath) {
return bosherr.Errorf("Verifying that the stemcell '%s' exists", stemcellTarballPath)
}
extractedStemcell, err = c.stemcellExtractor.Extract(stemcellTarballPath)
if err != nil {
return bosherr.WrapErrorf(err, "Extracting stemcell from '%s'", stemcellTarballPath)
}
return nil
})
if err != nil {
return extractedStemcell, deploymentManifest, installationManifest, err
}
defer func() {
if err != nil {
deleteErr := extractedStemcell.Delete()
if deleteErr != nil {
c.logger.Warn(c.logTag, "Failed to delete extracted stemcell: %s", deleteErr.Error())
}
}
}()
err = validationStage.Perform("Validating releases", func() error {
for _, releaseTarballPath := range releaseTarballPaths {
if !c.fs.FileExists(releaseTarballPath) {
return bosherr.Errorf("Verifying that the release '%s' exists", releaseTarballPath)
}
release, err := c.releaseExtractor.Extract(releaseTarballPath)
if err != nil {
return bosherr.WrapErrorf(err, "Extracting release '%s'", releaseTarballPath)
}
c.releaseManager.Add(release)
}
return nil
})
if err != nil {
return extractedStemcell, deploymentManifest, installationManifest, err
}
defer func() {
if err != nil {
err := c.releaseManager.DeleteAll()
if err != nil {
c.logger.Warn(c.logTag, "Deleting all extracted releases: %s", err.Error())
}
}
}()
err = validationStage.Perform("Validating deployment manifest", func() error {
releaseSetManifest, err := c.releaseSetParser.Parse(deploymentManifestPath)
if err != nil {
return bosherr.WrapErrorf(err, "Parsing release set manifest '%s'", deploymentManifestPath)
}
err = c.releaseSetValidator.Validate(releaseSetManifest)
if err != nil {
return bosherr.WrapError(err, "Validating release set manifest")
}
//TODO: this seems to be a naming smell indicating a deeper issue
c.releaseResolver.Filter(releaseSetManifest.Releases)
deploymentManifest, err = c.deploymentParser.Parse(deploymentManifestPath)
if err != nil {
return bosherr.WrapErrorf(err, "Parsing deployment manifest '%s'", deploymentManifestPath)
}
err = c.deploymentValidator.Validate(deploymentManifest)
if err != nil {
return bosherr.WrapError(err, "Validating deployment manifest")
}
installationManifest, err = c.installationParser.Parse(deploymentManifestPath)
if err != nil {
return bosherr.WrapErrorf(err, "Parsing installation manifest '%s'", deploymentManifestPath)
}
err = c.installationValidator.Validate(installationManifest)
if err != nil {
return bosherr.WrapError(err, "Validating installation manifest")
}
return nil
})
if err != nil {
return extractedStemcell, deploymentManifest, installationManifest, err
}
err = validationStage.Perform("Validating cpi release", func() error {
cpiReleaseName := installationManifest.Template.Release
cpiRelease, err := c.releaseResolver.Find(cpiReleaseName)
if err != nil {
// should never happen, due to prior manifest validation
return bosherr.WrapErrorf(err, "installation release '%s' must refer to a provided release", cpiReleaseName)
}
cpiReleaseJobName := installationManifest.Template.Name
err = bicpirel.NewValidator().Validate(cpiRelease, cpiReleaseJobName)
if err != nil {
return bosherr.WrapErrorf(err, "Invalid CPI release '%s'", cpiReleaseName)
}
return nil
})
return extractedStemcell, deploymentManifest, installationManifest, err
}