-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathprocess.go
More file actions
240 lines (201 loc) · 6.56 KB
/
process.go
File metadata and controls
240 lines (201 loc) · 6.56 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
package utils
import (
"context"
"errors"
"path/filepath"
"time"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config/mutator"
"github.com/databricks/cli/bundle/config/validate"
"github.com/databricks/cli/bundle/phases"
"github.com/databricks/cli/bundle/statemgmt"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/dyn"
"github.com/databricks/cli/libs/logdiag"
"github.com/databricks/cli/libs/sync"
"github.com/databricks/cli/libs/telemetry/protos"
"github.com/spf13/cobra"
)
type ProcessOptions struct {
// If true, do not call logdiag.InitContext(); will panic if logdiag context is not initialized
SkipInitContext bool
// Function to call after bundle is loaded but before phases.Initialize() is called
InitFunc func(b *bundle.Bundle)
// If true, phases.Initialize() is not called
SkipInitialize bool
// If true, call PopulateLocations()
IncludeLocations bool
// Function to call after phases.Initialize()
PostInitFunc func(context context.Context, b *bundle.Bundle) error
// If true, call PullResourcesState() to read state
ReadState bool
// AlwaysPull parameter to PullResourcesState()
// Implies ReadState
AlwaysPull bool
// If true, calls statemgmt.Load() to read the state and update resources with IDs; also calls InitializeURLs()
// Implies ReadState
InitIDs bool
// if true, pass ErrorOnEmptyState to statemgmt.Load
// Implies ReadState
ErrorOnEmptyState bool
// If true, configure outputHandler for phases.Deploy
Verbose bool
// If true, call corresponding phase:
FastValidate bool
Validate bool
Build bool
Deploy bool
// Indicate whether the bundle operation originates from the pipelines CLI
IsPipelinesCLI bool
}
func ProcessBundle(cmd *cobra.Command, opts ProcessOptions) (*bundle.Bundle, error) {
b, _, err := ProcessBundleRet(cmd, opts)
return b, err
}
func ProcessBundleRet(cmd *cobra.Command, opts ProcessOptions) (*bundle.Bundle, bool, error) {
isDirectEngine := false
ctx := cmd.Context()
if opts.SkipInitContext {
if !logdiag.IsSetup(ctx) {
panic("SkipInitContext=true but InitContext was not called")
}
} else {
ctx = logdiag.InitContext(ctx)
cmd.SetContext(ctx)
}
// Load bundle config and apply target
b := root.MustConfigureBundle(cmd)
if logdiag.HasError(ctx) {
return b, isDirectEngine, root.ErrAlreadyPrinted
}
variables, err := cmd.Flags().GetStringSlice("var")
if err != nil {
logdiag.LogDiag(ctx, diag.FromErr(err)[0])
return b, isDirectEngine, err
}
// Initialize variables by assigning them values passed as command line flags
configureVariables(cmd, b, variables)
if b == nil || logdiag.HasError(ctx) {
return b, isDirectEngine, root.ErrAlreadyPrinted
}
ctx = cmd.Context()
if opts.InitFunc != nil {
bundle.ApplyFuncContext(ctx, b, func(context.Context, *bundle.Bundle) { opts.InitFunc(b) })
}
if !opts.SkipInitialize {
t0 := time.Now()
phases.Initialize(ctx, b)
b.Metrics.ExecutionTimes = append(b.Metrics.ExecutionTimes, protos.IntMapEntry{
Key: "phases.Initialize",
Value: time.Since(t0).Milliseconds(),
})
// not checking error right away here, add locations first
}
if b != nil {
// Include location information in the output if the flag is set.
if opts.IncludeLocations {
bundle.ApplyContext(ctx, b, mutator.PopulateLocations())
if logdiag.HasError(ctx) {
return b, isDirectEngine, root.ErrAlreadyPrinted
}
}
}
if logdiag.HasError(ctx) {
return b, isDirectEngine, root.ErrAlreadyPrinted
}
if opts.PostInitFunc != nil {
err := opts.PostInitFunc(ctx, b)
if err != nil {
return b, isDirectEngine, err
}
}
if opts.ReadState || opts.AlwaysPull || opts.InitIDs || opts.ErrorOnEmptyState {
// PullResourcesState depends on stateFiler which needs b.Config.Workspace.StatePath which is set in phases.Initialize
ctx, isDirectEngine = statemgmt.PullResourcesState(ctx, b, statemgmt.AlwaysPull(opts.AlwaysPull))
if logdiag.HasError(ctx) {
return b, isDirectEngine, root.ErrAlreadyPrinted
}
cmd.SetContext(ctx)
// These are not safe in plan/deploy because they insert empty config settings for deleted resources.
if opts.InitIDs || opts.ErrorOnEmptyState {
var modes []statemgmt.LoadMode
if opts.ErrorOnEmptyState {
modes = append(modes, statemgmt.ErrorOnEmptyState)
}
bundle.ApplySeqContext(ctx, b,
statemgmt.Load(isDirectEngine, modes...),
mutator.InitializeURLs(),
)
if logdiag.HasError(ctx) {
return b, isDirectEngine, root.ErrAlreadyPrinted
}
}
}
if opts.FastValidate {
t1 := time.Now()
bundle.ApplyContext(ctx, b, validate.FastValidate())
b.Metrics.ExecutionTimes = append(b.Metrics.ExecutionTimes, protos.IntMapEntry{
Key: "validate.FastValidate",
Value: time.Since(t1).Milliseconds(),
})
if logdiag.HasError(ctx) {
return b, isDirectEngine, root.ErrAlreadyPrinted
}
// Pipeline CLI only validation.
if opts.IsPipelinesCLI {
rejectDefinitions(ctx, b)
if logdiag.HasError(ctx) {
return b, isDirectEngine, root.ErrAlreadyPrinted
}
}
}
if opts.Validate {
validate.Validate(ctx, b)
if logdiag.HasError(ctx) {
return b, isDirectEngine, root.ErrAlreadyPrinted
}
}
if opts.Build {
t2 := time.Now()
phases.Build(ctx, b)
b.Metrics.ExecutionTimes = append(b.Metrics.ExecutionTimes, protos.IntMapEntry{
Key: "phases.Build",
Value: time.Since(t2).Milliseconds(),
})
if logdiag.HasError(ctx) {
return b, isDirectEngine, root.ErrAlreadyPrinted
}
}
if opts.Deploy {
var outputHandler sync.OutputHandler
if opts.Verbose {
outputHandler = func(ctx context.Context, c <-chan sync.Event) {
sync.TextOutput(ctx, c, cmd.OutOrStdout())
}
}
t3 := time.Now()
phases.Deploy(ctx, b, outputHandler, isDirectEngine)
b.Metrics.ExecutionTimes = append(b.Metrics.ExecutionTimes, protos.IntMapEntry{
Key: "phases.Deploy",
Value: time.Since(t3).Milliseconds(),
})
if logdiag.HasError(ctx) {
return b, isDirectEngine, root.ErrAlreadyPrinted
}
}
return b, isDirectEngine, nil
}
func rejectDefinitions(ctx context.Context, b *bundle.Bundle) {
if b.Config.Definitions != nil {
v := dyn.GetValue(b.Config.Value(), "definitions")
loc := v.Locations()
filename := "input yaml"
if len(loc) > 0 {
filename = filepath.ToSlash(loc[0].File)
}
logdiag.LogError(ctx, errors.New(filename+` seems to be formatted for open-source Spark Declarative Pipelines.
Pipelines CLI currently only supports Lakeflow Declarative Pipelines development.
To see an example of a supported pipelines template, create a new Pipelines CLI project with "pipelines init".`))
}
}