-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathdeploy.go
More file actions
265 lines (225 loc) · 7.3 KB
/
deploy.go
File metadata and controls
265 lines (225 loc) · 7.3 KB
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
package phases
import (
"context"
"errors"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/artifacts"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/bundle/config/engine"
"github.com/databricks/cli/bundle/deploy"
"github.com/databricks/cli/bundle/deploy/files"
"github.com/databricks/cli/bundle/deploy/lock"
"github.com/databricks/cli/bundle/deploy/metadata"
"github.com/databricks/cli/bundle/deploy/terraform"
"github.com/databricks/cli/bundle/deployplan"
"github.com/databricks/cli/bundle/direct"
"github.com/databricks/cli/bundle/libraries"
"github.com/databricks/cli/bundle/metrics"
"github.com/databricks/cli/bundle/permissions"
"github.com/databricks/cli/bundle/scripts"
"github.com/databricks/cli/bundle/statemgmt"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/log"
"github.com/databricks/cli/libs/logdiag"
"github.com/databricks/cli/libs/sync"
)
func approvalForDeploy(ctx context.Context, b *bundle.Bundle, plan *deployplan.Plan) (bool, error) {
actions := plan.GetActions()
err := checkForPreventDestroy(b, actions)
if err != nil {
return false, err
}
types := []deployplan.ActionType{deployplan.Recreate, deployplan.Delete}
schemaActions := filterGroup(actions, "schemas", types...)
dltActions := filterGroup(actions, "pipelines", types...)
volumeActions := filterGroup(actions, "volumes", types...)
dashboardActions := filterGroup(actions, "dashboards", types...)
// We don't need to display any prompts in this case.
if len(schemaActions) == 0 && len(dltActions) == 0 && len(volumeActions) == 0 && len(dashboardActions) == 0 {
return true, nil
}
// One or more UC schema resources will be deleted or recreated.
if len(schemaActions) != 0 {
cmdio.LogString(ctx, deleteOrRecreateSchemaMessage)
for _, action := range schemaActions {
if action.IsChildResource() {
continue
}
cmdio.Log(ctx, action)
}
}
// One or more DLT pipelines is being recreated.
if len(dltActions) != 0 {
cmdio.LogString(ctx, deleteOrRecreatePipelineMessage)
for _, action := range dltActions {
cmdio.Log(ctx, action)
}
}
// One or more volumes is being recreated.
if len(volumeActions) != 0 {
cmdio.LogString(ctx, deleteOrRecreateVolumeMessage)
for _, action := range volumeActions {
cmdio.Log(ctx, action)
}
}
// One or more dashboards is being recreated.
if len(dashboardActions) != 0 {
cmdio.LogString(ctx, deleteOrRecreateDashboardMessage)
for _, action := range dashboardActions {
cmdio.Log(ctx, action)
}
}
if b.AutoApprove {
return true, nil
}
if !cmdio.IsPromptSupported(ctx) {
return false, errors.New("the deployment requires destructive actions, but current console does not support prompting. Please specify --auto-approve if you would like to skip prompts and proceed")
}
cmdio.LogString(ctx, "")
approved, err := cmdio.AskYesOrNo(ctx, "Would you like to proceed?")
if err != nil {
return false, err
}
return approved, nil
}
func deployCore(ctx context.Context, b *bundle.Bundle, plan *deployplan.Plan, targetEngine engine.EngineType) {
// Core mutators that CRUD resources and modify deployment state. These
// mutators need informed consent if they are potentially destructive.
cmdio.LogString(ctx, "Deploying resources...")
if targetEngine.IsDirect() {
b.DeploymentBundle.Apply(ctx, b.WorkspaceClient(), plan, direct.MigrateMode(false))
} else {
bundle.ApplyContext(ctx, b, terraform.Apply())
}
// Even if deployment failed, there might be updates in states that we need to upload
statemgmt.PushResourcesState(ctx, b, targetEngine)
if logdiag.HasError(ctx) {
return
}
bundle.ApplySeqContext(ctx, b,
statemgmt.Load(targetEngine),
metadata.Compute(),
metadata.Upload(),
statemgmt.UploadStateForYamlSync(targetEngine),
)
if !logdiag.HasError(ctx) {
cmdio.LogString(ctx, "Deployment complete!")
}
}
// uploadLibraries uploads libraries to the workspace.
// It also cleans up the artifacts directory and transforms wheel tasks.
// It is called by only "bundle deploy".
func uploadLibraries(ctx context.Context, b *bundle.Bundle, libs map[string][]libraries.LocationToUpdate) {
bundle.ApplySeqContext(ctx, b,
artifacts.CleanUp(),
libraries.Upload(libs),
)
}
// The deploy phase deploys artifacts and resources.
// If readPlanPath is provided, the plan is loaded from that file instead of being calculated.
func Deploy(ctx context.Context, b *bundle.Bundle, outputHandler sync.OutputHandler, engine engine.EngineType, libs map[string][]libraries.LocationToUpdate, plan *deployplan.Plan) {
log.Info(ctx, "Phase: deploy")
// Core mutators that CRUD resources and modify deployment state. These
// mutators need informed consent if they are potentially destructive.
bundle.ApplySeqContext(ctx, b,
scripts.Execute(config.ScriptPreDeploy),
lock.Acquire(),
)
if logdiag.HasError(ctx) {
// lock is not acquired here
return
}
// lock is acquired here
defer func() {
bundle.ApplyContext(ctx, b, lock.Release(lock.GoalDeploy))
}()
uploadLibraries(ctx, b, libs)
if logdiag.HasError(ctx) {
return
}
bundle.ApplySeqContext(ctx, b,
files.Upload(outputHandler),
deploy.StateUpdate(),
deploy.StatePush(),
permissions.ApplyWorkspaceRootPermissions(),
metrics.TrackUsedCompute(),
deploy.ResourcePathMkdir(),
)
if logdiag.HasError(ctx) {
return
}
if plan != nil {
// Initialize DeploymentBundle for applying the loaded plan
_, localPath := b.StateFilenameDirect(ctx)
err := b.DeploymentBundle.InitForApply(ctx, b.WorkspaceClient(), localPath, plan)
if err != nil {
logdiag.LogError(ctx, err)
return
}
} else {
plan = RunPlan(ctx, b, engine)
}
if logdiag.HasError(ctx) {
return
}
haveApproval, err := approvalForDeploy(ctx, b, plan)
if err != nil {
logdiag.LogError(ctx, err)
return
}
if haveApproval {
deployCore(ctx, b, plan, engine)
} else {
cmdio.LogString(ctx, "Deployment cancelled!")
return
}
if logdiag.HasError(ctx) {
return
}
bundle.ApplyContext(ctx, b, scripts.Execute(config.ScriptPostDeploy))
}
func RunPlan(ctx context.Context, b *bundle.Bundle, engine engine.EngineType) *deployplan.Plan {
if engine.IsDirect() {
_, localPath := b.StateFilenameDirect(ctx)
plan, err := b.DeploymentBundle.CalculatePlan(ctx, b.WorkspaceClient(), &b.Config, localPath)
if err != nil {
logdiag.LogError(ctx, err)
return nil
}
return plan
}
bundle.ApplySeqContext(ctx, b,
terraform.Interpolate(),
terraform.Write(),
terraform.Plan(terraform.PlanGoal("deploy")),
)
if logdiag.HasError(ctx) {
return nil
}
tf := b.Terraform
if tf == nil {
logdiag.LogError(ctx, errors.New("terraform not initialized"))
return nil
}
plan, err := terraform.ShowPlanFile(ctx, tf, b.TerraformPlanPath)
if err != nil {
logdiag.LogError(ctx, err)
return nil
}
for _, group := range b.Config.Resources.AllResources() {
for rKey := range group.Resources {
resourceKey := "resources." + group.Description.PluralName + "." + rKey
if _, ok := plan.Plan[resourceKey]; !ok {
plan.Plan[resourceKey] = &deployplan.PlanEntry{
Action: deployplan.Skip,
}
}
}
}
return plan
}
// If there are more than 1 thousand of a resource type, do not
// include more resources.
// Since we have a timeout of 3 seconds, we cap the maximum number of IDs
// we send in a single request to have reliable telemetry.
const ResourceIdLimit = 1000