forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment_deleter.go
189 lines (165 loc) · 7.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
package cmd
import (
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"
birelsetmanifest "github.com/cloudfoundry/bosh-init/release/set/manifest"
biui "github.com/cloudfoundry/bosh-init/ui"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type DeploymentDeleter interface {
DeleteDeployment(stage biui.Stage) (err error)
}
func NewDeploymentDeleter(
ui biui.UI,
logTag string,
logger boshlog.Logger,
deploymentStateService biconfig.DeploymentStateService,
releaseManager birel.Manager,
cloudFactory bicloud.Factory,
agentClientFactory bihttpagent.AgentClientFactory,
blobstoreFactory biblobstore.Factory,
deploymentManagerFactory bidepl.ManagerFactory,
deploymentManifestPath string,
cpiInstaller bicpirel.CpiInstaller,
cpiUninstaller biinstall.Uninstaller,
releaseFetcher birel.Fetcher,
releaseSetAndInstallationManifestParser ReleaseSetAndInstallationManifestParser,
) DeploymentDeleter {
return &deploymentDeleter{
ui: ui,
logTag: logTag,
logger: logger,
deploymentStateService: deploymentStateService,
releaseManager: releaseManager,
cloudFactory: cloudFactory,
agentClientFactory: agentClientFactory,
blobstoreFactory: blobstoreFactory,
deploymentManagerFactory: deploymentManagerFactory,
deploymentManifestPath: deploymentManifestPath,
cpiInstaller: cpiInstaller,
cpiUninstaller: cpiUninstaller,
releaseFetcher: releaseFetcher,
releaseSetAndInstallationManifestParser: releaseSetAndInstallationManifestParser,
}
}
type deploymentDeleter struct {
ui biui.UI
logTag string
logger boshlog.Logger
deploymentStateService biconfig.DeploymentStateService
releaseManager birel.Manager
cloudFactory bicloud.Factory
agentClientFactory bihttpagent.AgentClientFactory
blobstoreFactory biblobstore.Factory
deploymentManagerFactory bidepl.ManagerFactory
deploymentManifestPath string
cpiInstaller bicpirel.CpiInstaller
cpiUninstaller biinstall.Uninstaller
releaseFetcher birel.Fetcher
releaseSetAndInstallationManifestParser ReleaseSetAndInstallationManifestParser
}
func (c *deploymentDeleter) DeleteDeployment(stage biui.Stage) (err error) {
c.ui.PrintLinef("Deployment state: '%s'", c.deploymentStateService.Path())
if !c.deploymentStateService.Exists() {
c.ui.PrintLinef("No deployment state file found.")
return nil
}
deploymentState, err := c.deploymentStateService.Load()
if err != nil {
return bosherr.WrapError(err, "Loading deployment state")
}
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)
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, 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)
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
}