-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathpython_mutator.go
More file actions
553 lines (453 loc) · 17.5 KB
/
python_mutator.go
File metadata and controls
553 lines (453 loc) · 17.5 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package python
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/databricks/cli/bundle/config/mutator/resourcemutator"
"github.com/databricks/cli/libs/log"
"github.com/databricks/cli/libs/logdiag"
"github.com/databricks/databricks-sdk-go/logger"
"github.com/fatih/color"
"github.com/databricks/cli/libs/python"
"github.com/databricks/cli/bundle/env"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/dyn"
"github.com/databricks/cli/libs/dyn/convert"
"github.com/databricks/cli/libs/dyn/yamlloader"
"github.com/databricks/cli/libs/process"
)
type phase string
const (
// PythonMutatorPhaseLoadResources is the phase in which YAML configuration was loaded.
//
// At this stage, we execute Python code to load resources defined in Python.
//
// During this process, Python code can access:
// - selected deployment target
// - bundle variable values
// - variables provided through CLI argument or environment variables
//
// The following is not available:
// - variables referencing other variables are in unresolved format
//
// Python code can output YAML referencing variables, and CLI should resolve them.
//
// Existing resources can't be removed or modified, and CLI rejects such changes.
// While it's called 'load_resources', this phase is executed in 'init' phase of mutator pipeline.
PythonMutatorPhaseLoadResources phase = "load_resources"
// PythonMutatorPhaseApplyMutators is the phase in which resources defined in YAML or Python
// are already loaded.
//
// At this stage, we execute Python code to mutate resources defined in YAML or Python.
//
// During this process, Python code can access:
// - selected deployment target
// - bundle variable values
// - variables provided through CLI argument or environment variables
//
// The following is not available:
// - variables referencing other variables are in unresolved format
//
// Python code can output YAML referencing variables, and CLI should resolve them.
//
// Resources can't be added or removed, and CLI rejects such changes. Python code is
// allowed to modify existing resources, but not other parts of bundle configuration.
PythonMutatorPhaseApplyMutators phase = "apply_mutators"
)
type pythonMutator struct {
phase phase
}
func PythonMutator(phase phase) bundle.Mutator {
return &pythonMutator{
phase: phase,
}
}
func (m *pythonMutator) Name() string {
return fmt.Sprintf("PythonMutator(%s)", m.phase)
}
// opts is a common structure for deprecated PyDABs and upcoming Python
// configuration sections
type opts struct {
enabled bool
venvPath string
loadLocations bool
}
type runPythonMutatorOpts struct {
cacheDir string
bundleRootPath string
pythonPath string
loadLocations bool
}
// getOpts adapts deprecated PyDABs and upcoming Python configuration
// into a common structure.
func getOpts(b *bundle.Bundle, phase phase) (opts, error) {
experimental := b.Config.Experimental
if experimental == nil {
experimental = &config.Experimental{}
}
// using reflect.DeepEquals in case we add more fields
pydabsEnabled := !reflect.DeepEqual(experimental.PyDABs, config.PyDABs{})
experimentalPythonEnabled := !reflect.DeepEqual(experimental.Python, config.Python{})
pythonEnabled := !reflect.DeepEqual(b.Config.Python, config.Python{})
if pydabsEnabled {
return opts{}, errors.New("experimental/pydabs is deprecated, use python instead (https://docs.databricks.com/dev-tools/bundles/python)")
}
if experimentalPythonEnabled && pythonEnabled {
if !reflect.DeepEqual(experimental.Python, b.Config.Python) {
return opts{}, errors.New("'experimental/python' and 'python' configuration properties are mutually exclusive, use only 'python'")
} else {
return opts{
enabled: true,
venvPath: b.Config.Python.VEnvPath,
loadLocations: true,
}, nil
}
} else if pythonEnabled {
return opts{
enabled: true,
venvPath: b.Config.Python.VEnvPath,
loadLocations: true,
}, nil
} else if experimentalPythonEnabled {
return opts{
enabled: true,
venvPath: experimental.Python.VEnvPath,
loadLocations: true,
}, nil
} else {
return opts{}, nil
}
}
// applyBackwardsCompatibilityFixes applies fixes to bundle configuration
// so that older version of databricks-bundles Python library continue to see
// 'experimental.python' section even if bundle configuration uses 'python' section.
//
// This way upgrades are less risky if CLI version doesn't match Python package version.
//
// Later version of Python package should reject 'experimental.python' unless 'python' section
// is set to equivalent value, and after that reject 'experimental.python' altogether.
func applyBackwardsCompatibilityFixes(b *bundle.Bundle) error {
return b.Config.Mutate(func(value dyn.Value) (dyn.Value, error) {
outValue := value
pythonValue, _ := dyn.Get(outValue, "python")
if !pythonValue.IsValid() {
// if 'python' section doesn't exist, nothing to do
return value, nil
}
experimentalPythonValue, _ := dyn.Get(outValue, "experimental.python")
if experimentalPythonValue.IsValid() {
// if 'experimental.python' section exists, nothing to do
return value, nil
}
experimentalValue, _ := dyn.Get(outValue, "experimental")
if !experimentalValue.IsValid() {
updated, err := dyn.Set(outValue, "experimental", dyn.NewValue(map[string]dyn.Value{}, nil))
if err != nil {
return dyn.InvalidValue, fmt.Errorf("failed to create 'experimental' section: %w", err)
} else {
outValue = updated
}
}
// move 'python' section to 'experimental.python'
updated, err := dyn.Set(outValue, "experimental.python", pythonValue)
if err != nil {
return dyn.InvalidValue, fmt.Errorf("failed to set 'experimental.python' section: %w", err)
} else {
outValue = updated
}
return outValue, nil
})
}
func (m *pythonMutator) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
err := applyBackwardsCompatibilityFixes(b)
if err != nil {
return diag.FromErr(fmt.Errorf("failed to apply backwards compatibility fixes: %w", err))
}
opts, err := getOpts(b, m.phase)
if err != nil {
return diag.Errorf("failed to apply python mutator: %s", err)
}
if !opts.enabled {
return nil
}
// Don't run any arbitrary code when restricted execution is enabled.
if _, ok := env.RestrictedExecution(ctx); ok {
return diag.Errorf("Running Python code is not allowed when DATABRICKS_BUNDLE_RESTRICTED_CODE_EXECUTION is set")
}
// mutateDiags is used because Mutate returns 'error' instead of 'diag.Diagnostics'
var mutateDiags diag.Diagnostics
var result applyPythonOutputResult
mutateDiagsHasError := errors.New("unexpected error")
err = b.Config.Mutate(func(leftRoot dyn.Value) (dyn.Value, error) {
pythonPath, err := detectExecutable(ctx, opts.venvPath)
if err != nil {
return dyn.InvalidValue, fmt.Errorf("failed to get Python interpreter path: %w", err)
}
cacheDir, err := createCacheDir(ctx)
if err != nil {
return dyn.InvalidValue, fmt.Errorf("failed to create cache dir: %w", err)
}
rightRoot, diags := m.runPythonMutator(ctx, leftRoot, runPythonMutatorOpts{
cacheDir: cacheDir,
bundleRootPath: b.BundleRootPath,
pythonPath: pythonPath,
loadLocations: opts.loadLocations,
})
mutateDiags = diags
if diags.HasError() {
return dyn.InvalidValue, mutateDiagsHasError
}
newRoot, result0, err := applyPythonOutput(leftRoot, rightRoot)
result = result0
if err != nil {
return dyn.InvalidValue, fmt.Errorf("internal error when merging output of Python mutator: %w", err)
}
for _, resourceKey := range result.AddedResources.ToArray() {
log.Debugf(ctx, "added resource at 'resources.%s.%s'", resourceKey.Type, resourceKey.Name)
}
for _, resourceKey := range result.UpdatedResources.ToArray() {
log.Debugf(ctx, "updated resource at 'resources.%s.%s'", resourceKey.Type, resourceKey.Name)
}
for _, resourceKey := range result.DeletedResources.ToArray() {
log.Debugf(ctx, "deleted resource at 'resources.%s.%s'", resourceKey.Type, resourceKey.Name)
}
if !result.DeletedResources.IsEmpty() {
return dyn.InvalidValue, fmt.Errorf("unexpected deleted resources: %s", result.DeletedResources.ToArray())
}
if !result.AddedResources.IsEmpty() && m.phase == PythonMutatorPhaseApplyMutators {
return dyn.InvalidValue, fmt.Errorf("unexpected added resources: %s", result.AddedResources.ToArray())
}
if !result.UpdatedResources.IsEmpty() && m.phase == PythonMutatorPhaseLoadResources {
return dyn.InvalidValue, fmt.Errorf("unexpected updated resources: %s", result.UpdatedResources.ToArray())
}
return newRoot, nil
})
// we can precisely track resources that are added/updated, so sum doesn't double-count
b.Metrics.PythonUpdatedResourcesCount += int64(result.UpdatedResources.Size())
b.Metrics.PythonAddedResourcesCount += int64(result.AddedResources.Size())
if err == mutateDiagsHasError {
if !mutateDiags.HasError() {
panic("mutateDiags has no error, but error is expected")
}
return mutateDiags
} else {
mutateDiags = mutateDiags.Extend(diag.FromErr(err))
}
if mutateDiags.HasError() {
return mutateDiags
}
resourcemutator.NormalizeAndInitializeResources(ctx, b, result.AddedResources)
if logdiag.HasError(ctx) {
return mutateDiags
}
resourcemutator.NormalizeResources(ctx, b, result.UpdatedResources)
return mutateDiags
}
func createCacheDir(ctx context.Context) (string, error) {
// b.LocalStateDir doesn't work because target isn't yet selected
// support the same env variable as in b.LocalStateDir
if tempDir, exists := env.TempDir(ctx); exists {
// use 'default' as target name
cacheDir := filepath.Join(tempDir, "default", "python")
err := os.MkdirAll(cacheDir, 0o700)
if err != nil {
return "", err
}
return cacheDir, nil
}
return os.MkdirTemp("", "-python")
}
func (m *pythonMutator) runPythonMutator(ctx context.Context, root dyn.Value, opts runPythonMutatorOpts) (dyn.Value, diag.Diagnostics) {
inputPath := filepath.Join(opts.cacheDir, "input.json")
outputPath := filepath.Join(opts.cacheDir, "output.json")
diagnosticsPath := filepath.Join(opts.cacheDir, "diagnostics.json")
locationsPath := filepath.Join(opts.cacheDir, "locations.json")
args := []string{
opts.pythonPath,
"-m",
"databricks.bundles.build",
"--phase",
string(m.phase),
"--input",
inputPath,
"--output",
outputPath,
"--diagnostics",
diagnosticsPath,
}
if opts.loadLocations {
args = append(args, "--locations", locationsPath)
}
if err := writeInputFile(inputPath, root); err != nil {
return dyn.InvalidValue, diag.Errorf("failed to write input file: %s", err)
}
stderrBuf := bytes.Buffer{}
stderrWriter := io.MultiWriter(
newLogWriter(ctx, "stderr: "),
&stderrBuf,
)
stdoutWriter := newLogWriter(ctx, "stdout: ")
_, processErr := process.Background(
ctx,
args,
process.WithDir(opts.bundleRootPath),
process.WithStderrWriter(stderrWriter),
process.WithStdoutWriter(stdoutWriter),
)
if processErr != nil {
logger.Debugf(ctx, "python mutator process failed: %s", processErr)
}
pythonDiagnostics, pythonDiagnosticsErr := loadDiagnosticsFile(diagnosticsPath)
if pythonDiagnosticsErr != nil {
logger.Debugf(ctx, "failed to load diagnostics: %s", pythonDiagnosticsErr)
}
// if diagnostics file exists, it gives the most descriptive errors
// if there is any error, we treat it as fatal error, and stop processing
if pythonDiagnostics.HasError() {
return dyn.InvalidValue, pythonDiagnostics
}
// process can fail without reporting errors in diagnostics file or creating it, for instance,
// venv doesn't have 'databricks-bundles' library installed
if processErr != nil {
diagnostic := diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("python mutator process failed: %q, use --debug to enable logging", processErr),
Detail: explainProcessErr(stderrBuf.String()),
}
return dyn.InvalidValue, diag.Diagnostics{diagnostic}
}
// or we can fail to read diagnostics file, that should always be created
if pythonDiagnosticsErr != nil {
return dyn.InvalidValue, diag.Errorf("failed to load diagnostics: %s", pythonDiagnosticsErr)
}
locations, err := loadLocationsFile(opts.bundleRootPath, locationsPath)
if err != nil {
return dyn.InvalidValue, diag.Errorf("failed to load locations: %s", err)
}
output, outputDiags := loadOutputFile(opts.bundleRootPath, outputPath, locations)
pythonDiagnostics = pythonDiagnostics.Extend(outputDiags)
// we pass through pythonDiagnostic because it contains warnings
return output, pythonDiagnostics
}
const pythonInstallExplanation = `Ensure that 'databricks-bundles' is installed in Python environment:
$ .venv/bin/pip install databricks-bundles
If using a virtual environment, ensure it is specified as the venv_path property in databricks.yml,
or activate the environment before running CLI commands:
experimental:
python:
venv_path: .venv
`
// explainProcessErr provides additional explanation for common errors.
// It's meant to be the best effort, and not all errors are covered.
// Output should be used only used for error reporting.
func explainProcessErr(stderr string) string {
// implemented in cpython/Lib/runpy.py and portable across Python 3.x, including pypy
if strings.Contains(stderr, "Error while finding module specification for 'databricks.bundles.build'") {
summary := color.CyanString("Explanation: ") + "'databricks-bundles' library is not installed in the Python environment.\n"
return stderr + "\n" + summary + "\n" + pythonInstallExplanation
}
return stderr
}
func writeInputFile(inputPath string, input dyn.Value) error {
// we need to marshal dyn.Value instead of bundle.Config to JSON to support
// non-string fields assigned with bundle variables
rootConfigJson, err := json.Marshal(input.AsAny())
if err != nil {
return fmt.Errorf("failed to marshal input: %w", err)
}
return os.WriteFile(inputPath, rootConfigJson, 0o600)
}
// loadLocationsFile loads locations.json containing source locations for generated YAML.
func loadLocationsFile(bundleRoot, locationsPath string) (*pythonLocations, error) {
locationsFile, err := os.Open(locationsPath)
if errors.Is(err, fs.ErrNotExist) {
return newPythonLocations(), nil
} else if err != nil {
return nil, fmt.Errorf("failed to open locations file: %w", err)
}
defer locationsFile.Close()
return parsePythonLocations(bundleRoot, locationsFile)
}
func loadOutputFile(rootPath, outputPath string, locations *pythonLocations) (dyn.Value, diag.Diagnostics) {
outputFile, err := os.Open(outputPath)
if err != nil {
return dyn.InvalidValue, diag.FromErr(fmt.Errorf("failed to open output file: %w", err))
}
defer outputFile.Close()
return loadOutput(rootPath, outputFile, locations)
}
func loadOutput(rootPath string, outputFile io.Reader, locations *pythonLocations) (dyn.Value, diag.Diagnostics) {
// we need absolute path because later parts of pipeline assume all paths are absolute
// and this file will be used as location to resolve relative paths.
//
// virtualPath has to stay in bundleRootPath, because locations outside root path are not allowed:
//
// Error: path /var/folders/.../python/dist/*.whl is not contained in bundle root path
//
// for that, we pass virtualPath instead of outputPath as file location
virtualPath, err := filepath.Abs(filepath.Join(rootPath, generatedFileName))
if err != nil {
return dyn.InvalidValue, diag.FromErr(fmt.Errorf("failed to get absolute path: %w", err))
}
generated, err := yamlloader.LoadYAML(virtualPath, outputFile)
if err != nil {
return dyn.InvalidValue, diag.FromErr(fmt.Errorf("failed to parse output file: %w", err))
}
// generated has dyn.Location as if it comes from generated YAML file
// earlier we loaded locations.json with source locations in Python code
generatedWithLocations, err := mergePythonLocations(generated, locations)
if err != nil {
return dyn.InvalidValue, diag.FromErr(fmt.Errorf("failed to update locations: %w", err))
}
return strictNormalize(config.Root{}, generatedWithLocations)
}
func strictNormalize(dst any, generated dyn.Value) (dyn.Value, diag.Diagnostics) {
normalized, diags := convert.Normalize(dst, generated)
// warnings shouldn't happen because output should be already normalized
// when it happens, it's a bug in the mutator, and should be treated as an error
strictDiags := diag.Diagnostics{}
for _, d := range diags {
if d.Severity == diag.Warning {
d.Severity = diag.Error
}
strictDiags = strictDiags.Append(d)
}
return normalized, strictDiags
}
// loadDiagnosticsFile loads diagnostics from a file.
//
// It contains a list of warnings and errors that we should print to users.
//
// If the file doesn't exist, we return an error. We expect the file to always be
// created by the Python mutator, and it's absence means there are integration problems,
// and the diagnostics file was lost. If we treat non-existence as an empty diag.Diagnostics
// we risk loosing errors and warnings.
func loadDiagnosticsFile(path string) (diag.Diagnostics, error) {
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open diagnostics file: %w", err)
}
defer file.Close()
return parsePythonDiagnostics(file)
}
// detectExecutable lookups Python interpreter in virtual environment, or if not set, in PATH.
func detectExecutable(ctx context.Context, venvPath string) (string, error) {
if venvPath == "" {
interpreter, err := python.DetectExecutable(ctx)
if err != nil {
return "", err
}
return interpreter, nil
}
return python.DetectVEnvExecutable(venvPath)
}