forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment_deleter.go
221 lines (184 loc) · 8.06 KB
/
deployment_deleter.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
package cmd
import (
bihttpagent "github.com/cloudfoundry/bosh-agent/agentclient/http"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
bihttpclient "github.com/cloudfoundry/bosh-utils/httpclient"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
biblobstore "github.com/cloudfoundry/bosh-cli/blobstore"
bicloud "github.com/cloudfoundry/bosh-cli/cloud"
biconfig "github.com/cloudfoundry/bosh-cli/config"
bicpirel "github.com/cloudfoundry/bosh-cli/cpi/release"
bidepl "github.com/cloudfoundry/bosh-cli/deployment"
boshtpl "github.com/cloudfoundry/bosh-cli/director/template"
biinstall "github.com/cloudfoundry/bosh-cli/installation"
boshinst "github.com/cloudfoundry/bosh-cli/installation"
biinstallmanifest "github.com/cloudfoundry/bosh-cli/installation/manifest"
birelsetmanifest "github.com/cloudfoundry/bosh-cli/release/set/manifest"
biui "github.com/cloudfoundry/bosh-cli/ui"
)
type DeploymentDeleter interface {
DeleteDeployment(stage biui.Stage) (err error)
}
func NewDeploymentDeleter(
ui biui.UI,
logTag string,
logger boshlog.Logger,
deploymentStateService biconfig.DeploymentStateService,
releaseManager boshinst.ReleaseManager,
cloudFactory bicloud.Factory,
agentClientFactory bihttpagent.AgentClientFactory,
blobstoreFactory biblobstore.Factory,
deploymentManagerFactory bidepl.ManagerFactory,
deploymentManifestPath string,
deploymentVars boshtpl.Variables,
cpiInstaller bicpirel.CpiInstaller,
cpiUninstaller biinstall.Uninstaller,
releaseFetcher boshinst.ReleaseFetcher,
releaseSetAndInstallationManifestParser ReleaseSetAndInstallationManifestParser,
tempRootConfigurator TempRootConfigurator,
targetProvider biinstall.TargetProvider,
) DeploymentDeleter {
return &deploymentDeleter{
ui: ui,
logTag: logTag,
logger: logger,
deploymentStateService: deploymentStateService,
releaseManager: releaseManager,
cloudFactory: cloudFactory,
agentClientFactory: agentClientFactory,
blobstoreFactory: blobstoreFactory,
deploymentManagerFactory: deploymentManagerFactory,
deploymentManifestPath: deploymentManifestPath,
deploymentVars: deploymentVars,
cpiInstaller: cpiInstaller,
cpiUninstaller: cpiUninstaller,
releaseFetcher: releaseFetcher,
releaseSetAndInstallationManifestParser: releaseSetAndInstallationManifestParser,
tempRootConfigurator: tempRootConfigurator,
targetProvider: targetProvider,
}
}
type deploymentDeleter struct {
ui biui.UI
logTag string
logger boshlog.Logger
deploymentStateService biconfig.DeploymentStateService
releaseManager boshinst.ReleaseManager
cloudFactory bicloud.Factory
agentClientFactory bihttpagent.AgentClientFactory
blobstoreFactory biblobstore.Factory
deploymentManagerFactory bidepl.ManagerFactory
deploymentManifestPath string
deploymentVars boshtpl.Variables
cpiInstaller bicpirel.CpiInstaller
cpiUninstaller biinstall.Uninstaller
releaseFetcher boshinst.ReleaseFetcher
releaseSetAndInstallationManifestParser ReleaseSetAndInstallationManifestParser
tempRootConfigurator TempRootConfigurator
targetProvider biinstall.TargetProvider
}
func (c *deploymentDeleter) DeleteDeployment(stage biui.Stage) (err error) {
c.ui.BeginLinef("Deployment state: '%s'\n", c.deploymentStateService.Path())
if !c.deploymentStateService.Exists() {
c.ui.BeginLinef("No deployment state file found.\n")
return nil
}
deploymentState, err := c.deploymentStateService.Load()
if err != nil {
return bosherr.WrapError(err, "Loading deployment state")
}
target, err := c.targetProvider.NewTarget()
if err != nil {
return bosherr.WrapError(err, "Determining installation target")
}
err = c.tempRootConfigurator.PrepareAndSetTempRoot(target.TmpPath(), c.logger)
if err != nil {
return bosherr.WrapError(err, "Setting temp root")
}
defer func() {
err := c.releaseManager.DeleteAll()
if err != nil {
c.logger.Warn(c.logTag, "Deleting all extracted releases: %s", err.Error())
}
}()
var installationManifest biinstallmanifest.Manifest
err = stage.PerformComplex("validating", func(stage biui.Stage) error {
var releaseSetManifest birelsetmanifest.Manifest
releaseSetManifest, installationManifest, err = c.releaseSetAndInstallationManifestParser.ReleaseSetAndInstallationManifest(c.deploymentManifestPath, c.deploymentVars)
if err != nil {
return err
}
cpiReleaseName := installationManifest.Template.Release
cpiReleaseRef, found := releaseSetManifest.FindByName(cpiReleaseName)
if !found {
return bosherr.Errorf("installation release '%s' must refer to a release in releases", cpiReleaseName)
}
err = c.releaseFetcher.DownloadAndExtract(cpiReleaseRef, stage)
if err != nil {
return err
}
err = c.cpiInstaller.ValidateCpiRelease(installationManifest, stage)
return err
})
if err != nil {
return err
}
err = c.cpiInstaller.WithInstalledCpiRelease(installationManifest, target, stage, func(localCpiInstallation biinstall.Installation) error {
return localCpiInstallation.WithRunningRegistry(c.logger, stage, func() error {
err = c.findAndDeleteDeployment(stage, localCpiInstallation, deploymentState.DirectorID, installationManifest.Mbus)
if err != nil {
return err
}
return stage.Perform("Uninstalling local artifacts for CPI and deployment", func() error {
err := c.cpiUninstaller.Uninstall(localCpiInstallation.Target())
if err != nil {
return err
}
return c.deploymentStateService.Cleanup()
})
})
})
return err
}
func (c *deploymentDeleter) findAndDeleteDeployment(stage biui.Stage, installation biinstall.Installation, directorID, installationMbus string) error {
deploymentManager, err := c.deploymentManager(installation, directorID, installationMbus)
if err != nil {
return err
}
err = c.findCurrentDeploymentAndDelete(stage, deploymentManager)
if err != nil {
return bosherr.WrapError(err, "Deleting deployment")
}
return deploymentManager.Cleanup(stage)
}
func (c *deploymentDeleter) findCurrentDeploymentAndDelete(stage biui.Stage, deploymentManager bidepl.Manager) error {
c.logger.Debug(c.logTag, "Finding current deployment...")
deployment, found, err := deploymentManager.FindCurrent()
if err != nil {
return bosherr.WrapError(err, "Finding current deployment")
}
return 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
}
return deployment.Delete(deleteStage)
})
}
func (c *deploymentDeleter) deploymentManager(installation biinstall.Installation, directorID, installationMbus string) (bidepl.Manager, error) {
c.logger.Debug(c.logTag, "Creating cloud client...")
cloud, err := c.cloudFactory.NewCloud(installation, directorID)
if err != nil {
return nil, bosherr.WrapError(err, "Creating CPI client from CPI installation")
}
c.logger.Debug(c.logTag, "Creating agent client...")
agentClient := c.agentClientFactory.NewAgentClient(directorID, installationMbus)
c.logger.Debug(c.logTag, "Creating blobstore client...")
blobstore, err := c.blobstoreFactory.Create(installationMbus, bihttpclient.CreateDefaultClientInsecureSkipVerify())
if err != nil {
return nil, bosherr.WrapError(err, "Creating blobstore client")
}
c.logger.Debug(c.logTag, "Creating deployment manager...")
return c.deploymentManagerFactory.NewManager(cloud, agentClient, blobstore), nil
}