forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_cmd.go
308 lines (266 loc) · 9.98 KB
/
delete_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
package cmd
import (
"errors"
"path/filepath"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshlog "github.com/cloudfoundry/bosh-agent/logger"
boshsys "github.com/cloudfoundry/bosh-agent/system"
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"
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"
biui "github.com/cloudfoundry/bosh-init/ui"
)
type deleteCmd struct {
ui biui.UI
userConfig biconfig.UserConfig
fs boshsys.FileSystem
releaseSetParser birelsetmanifest.Parser
installationParser biinstallmanifest.Parser
deploymentConfigService biconfig.DeploymentConfigService
releaseSetValidator birelsetmanifest.Validator
installationValidator biinstallmanifest.Validator
installerFactory biinstall.InstallerFactory
releaseExtractor birel.Extractor
releaseManager birel.Manager
releaseResolver birelset.Resolver
cloudFactory bicloud.Factory
agentClientFactory bihttpagent.AgentClientFactory
blobstoreFactory biblobstore.Factory
deploymentManagerFactory bidepl.ManagerFactory
logger boshlog.Logger
logTag string
}
func NewDeleteCmd(
ui biui.UI,
userConfig biconfig.UserConfig,
fs boshsys.FileSystem,
releaseSetParser birelsetmanifest.Parser,
installationParser biinstallmanifest.Parser,
deploymentConfigService biconfig.DeploymentConfigService,
releaseSetValidator birelsetmanifest.Validator,
installationValidator biinstallmanifest.Validator,
installerFactory biinstall.InstallerFactory,
releaseExtractor birel.Extractor,
releaseManager birel.Manager,
releaseResolver birelset.Resolver,
cloudFactory bicloud.Factory,
agentClientFactory bihttpagent.AgentClientFactory,
blobstoreFactory biblobstore.Factory,
deploymentManagerFactory bidepl.ManagerFactory,
logger boshlog.Logger,
) Cmd {
return &deleteCmd{
ui: ui,
userConfig: userConfig,
fs: fs,
releaseSetParser: releaseSetParser,
installationParser: installationParser,
deploymentConfigService: deploymentConfigService,
releaseSetValidator: releaseSetValidator,
installationValidator: installationValidator,
installerFactory: installerFactory,
releaseExtractor: releaseExtractor,
releaseManager: releaseManager,
releaseResolver: releaseResolver,
cloudFactory: cloudFactory,
agentClientFactory: agentClientFactory,
blobstoreFactory: blobstoreFactory,
deploymentManagerFactory: deploymentManagerFactory,
logger: logger,
logTag: "deleteCmd",
}
}
func (c *deleteCmd) Name() string {
return "delete"
}
func (c *deleteCmd) Meta() Meta {
return Meta{
Synopsis: "Delete existing deployment",
Usage: "<deployment_manifest_path> <cpi_release_path>",
Env: genericEnv,
}
}
func (c *deleteCmd) Run(stage biui.Stage, args []string) error {
deploymentManifestPath, releaseTarballPath, 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() {
c.ui.PrintLinef("No deployment config file found.")
return nil
}
deploymentConfig, err := c.deploymentConfigService.Load()
if err != nil {
return bosherr.WrapError(err, "Loading deployment config")
}
var installationManifest biinstallmanifest.Manifest
err = stage.PerformComplex("validating", func(stage biui.Stage) error {
installationManifest, err = c.validate(stage, releaseTarballPath, deploymentManifestPath)
return err
})
if err != nil {
return err
}
defer func() {
err := c.releaseManager.DeleteAll()
if err != nil {
c.logger.Warn(c.logTag, "Deleting all extracted releases: %s", err.Error())
}
}()
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)
}
}()
c.logger.Debug(c.logTag, "Creating cloud client...")
cloud, err := c.cloudFactory.NewCloud(installation, deploymentConfig.DirectorID)
if err != nil {
return bosherr.WrapError(err, "Creating CPI client from CPI installation")
}
c.logger.Debug(c.logTag, "Creating agent client...")
agentClient := c.agentClientFactory.NewAgentClient(deploymentConfig.DirectorID, installationManifest.Mbus)
c.logger.Debug(c.logTag, "Creating blobstore client...")
blobstore, err := c.blobstoreFactory.Create(installationManifest.Mbus)
if err != nil {
return bosherr.WrapError(err, "Creating blobstore client")
}
c.logger.Debug(c.logTag, "Creating deployment manager...")
deploymentManager := c.deploymentManagerFactory.NewManager(cloud, agentClient, blobstore)
c.logger.Debug(c.logTag, "Finding current deployment...")
deployment, found, err := deploymentManager.FindCurrent()
if err != nil {
return bosherr.WrapError(err, "Finding current deployment")
}
err = stage.PerformComplex("deleting deployment", func(deleteStage biui.Stage) error {
if !found {
//TODO: skip? would require adding skip support to PerformComplex
c.logger.Debug(c.logTag, "No current deployment found...")
return nil
}
err = deployment.Delete(deleteStage)
if err != nil {
return bosherr.WrapError(err, "Deleting deployment")
}
return nil
})
if err = deploymentManager.Cleanup(stage); err != nil {
return err
}
return err
}
func (c *deleteCmd) parseCmdInputs(args []string) (string, string, error) {
if len(args) != 2 {
c.ui.ErrorLinef("Invalid usage - delete command requires exactly 2 arguments")
c.ui.PrintLinef("Expected usage: bosh-init delete <deployment-manifest> <cpi-release-tarball>")
c.logger.Error(c.logTag, "Invalid arguments: %#v", args)
return "", "", errors.New("Invalid usage - delete command requires exactly 2 arguments")
}
return args[0], args[1], nil
}
func (c *deleteCmd) validate(validationStage biui.Stage, releaseTarballPath, deploymentManifestPath string) (
installationManifest biinstallmanifest.Manifest,
err error,
) {
err = validationStage.Perform("Validating releases", func() error {
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 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")
}
c.releaseResolver.Filter(releaseSetManifest.Releases)
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 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 installationManifest, err
}