2022-11-18 09:57:31 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-05-15 09:34:05 +00:00
|
|
|
"fmt"
|
2022-11-18 09:57:31 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-05-15 09:34:05 +00:00
|
|
|
"strings"
|
2022-11-18 09:57:31 +00:00
|
|
|
|
2023-11-13 11:29:40 +00:00
|
|
|
"github.com/databricks/cli/bundle/config/resources"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle/config/variable"
|
2023-08-23 16:47:07 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/jobs"
|
2022-11-18 09:57:31 +00:00
|
|
|
"github.com/ghodss/yaml"
|
|
|
|
"github.com/imdario/mergo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Root struct {
|
|
|
|
// Path contains the directory path to the root of the bundle.
|
2023-07-18 10:16:34 +00:00
|
|
|
// It is set when loading `databricks.yml`.
|
2023-04-12 14:17:13 +00:00
|
|
|
Path string `json:"-" bundle:"readonly"`
|
2022-11-18 09:57:31 +00:00
|
|
|
|
2023-05-15 09:34:05 +00:00
|
|
|
// Contains user defined variables
|
|
|
|
Variables map[string]*variable.Variable `json:"variables,omitempty"`
|
|
|
|
|
2022-11-18 09:57:31 +00:00
|
|
|
// Bundle contains details about this bundle, such as its name,
|
|
|
|
// version of the spec (TODO), default cluster, default warehouse, etc.
|
2023-09-11 08:16:22 +00:00
|
|
|
Bundle Bundle `json:"bundle,omitempty"`
|
2022-11-18 09:57:31 +00:00
|
|
|
|
|
|
|
// Include specifies a list of patterns of file names to load and
|
2023-07-25 08:00:46 +00:00
|
|
|
// merge into the this configuration. Only includes defined in the root
|
|
|
|
// `databricks.yml` are processed. Defaults to an empty list.
|
2022-11-18 09:57:31 +00:00
|
|
|
Include []string `json:"include,omitempty"`
|
|
|
|
|
|
|
|
// Workspace contains details about the workspace to connect to
|
|
|
|
// and paths in the workspace tree to use for this bundle.
|
2023-07-07 13:10:25 +00:00
|
|
|
Workspace Workspace `json:"workspace,omitempty"`
|
2022-11-18 09:57:31 +00:00
|
|
|
|
2022-11-30 13:15:22 +00:00
|
|
|
// Artifacts contains a description of all code artifacts in this bundle.
|
2023-09-04 09:55:01 +00:00
|
|
|
Artifacts Artifacts `json:"artifacts,omitempty"`
|
2022-11-30 13:15:22 +00:00
|
|
|
|
2022-11-18 09:57:31 +00:00
|
|
|
// Resources contains a description of all Databricks resources
|
2022-12-01 21:39:15 +00:00
|
|
|
// to deploy in this bundle (e.g. jobs, pipelines, etc.).
|
2023-01-20 15:55:44 +00:00
|
|
|
Resources Resources `json:"resources,omitempty"`
|
2022-11-18 09:57:31 +00:00
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
// Targets can be used to differentiate settings and resources between
|
|
|
|
// bundle deployment targets (e.g. development, staging, production).
|
2022-11-18 09:57:31 +00:00
|
|
|
// If not specified, the code below initializes this field with a
|
2023-08-17 15:22:32 +00:00
|
|
|
// single default-initialized target called "default".
|
|
|
|
Targets map[string]*Target `json:"targets,omitempty"`
|
|
|
|
|
|
|
|
// DEPRECATED. Left for backward compatibility with Targets
|
2023-12-06 10:45:18 +00:00
|
|
|
Environments map[string]*Target `json:"environments,omitempty" bundle:"deprecated"`
|
2023-08-18 08:07:25 +00:00
|
|
|
|
|
|
|
// Sync section specifies options for files synchronization
|
2023-09-11 08:16:22 +00:00
|
|
|
Sync Sync `json:"sync,omitempty"`
|
2023-08-23 16:47:07 +00:00
|
|
|
|
|
|
|
// RunAs section allows to define an execution identity for jobs and pipelines runs
|
|
|
|
RunAs *jobs.JobRunAs `json:"run_as,omitempty"`
|
2023-09-14 10:14:13 +00:00
|
|
|
|
|
|
|
Experimental *Experimental `json:"experimental,omitempty"`
|
2023-11-13 11:29:40 +00:00
|
|
|
|
|
|
|
// Permissions section allows to define permissions which will be
|
|
|
|
// applied to all resources defined in bundle
|
|
|
|
Permissions []resources.Permission `json:"permissions,omitempty"`
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 12:55:56 +00:00
|
|
|
// Load loads the bundle configuration file at the specified path.
|
2022-11-18 09:57:31 +00:00
|
|
|
func Load(path string) (*Root, error) {
|
2023-10-25 12:55:56 +00:00
|
|
|
raw, err := os.ReadFile(path)
|
2022-11-18 09:57:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-25 12:55:56 +00:00
|
|
|
var r Root
|
|
|
|
err = yaml.Unmarshal(raw, &r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load %s: %w", path, err)
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 12:55:56 +00:00
|
|
|
if r.Environments != nil && r.Targets != nil {
|
|
|
|
return nil, fmt.Errorf("both 'environments' and 'targets' are specified, only 'targets' should be used: %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Environments != nil {
|
|
|
|
//TODO: add a command line notice that this is a deprecated option.
|
|
|
|
r.Targets = r.Environments
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 12:55:56 +00:00
|
|
|
r.Path = filepath.Dir(path)
|
|
|
|
r.SetConfigFilePath(path)
|
|
|
|
|
|
|
|
_, err = r.Resources.VerifyUniqueResourceIdentifiers()
|
|
|
|
return &r, err
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:17:13 +00:00
|
|
|
// SetConfigFilePath configures the path that its configuration
|
|
|
|
// was loaded from in configuration leafs that require it.
|
|
|
|
func (r *Root) SetConfigFilePath(path string) {
|
|
|
|
r.Resources.SetConfigFilePath(path)
|
2023-09-04 09:55:01 +00:00
|
|
|
if r.Artifacts != nil {
|
|
|
|
r.Artifacts.SetConfigFilePath(path)
|
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
if r.Targets != nil {
|
|
|
|
for _, env := range r.Targets {
|
2023-06-08 20:55:49 +00:00
|
|
|
if env == nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-04-12 14:17:13 +00:00
|
|
|
if env.Resources != nil {
|
|
|
|
env.Resources.SetConfigFilePath(path)
|
|
|
|
}
|
2023-09-04 09:55:01 +00:00
|
|
|
if env.Artifacts != nil {
|
|
|
|
env.Artifacts.SetConfigFilePath(path)
|
|
|
|
}
|
2023-04-12 14:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 09:34:05 +00:00
|
|
|
// Initializes variables using values passed from the command line flag
|
|
|
|
// Input has to be a string of the form `foo=bar`. In this case the variable with
|
|
|
|
// name `foo` is assigned the value `bar`
|
|
|
|
func (r *Root) InitializeVariables(vars []string) error {
|
|
|
|
for _, variable := range vars {
|
|
|
|
parsedVariable := strings.SplitN(variable, "=", 2)
|
|
|
|
if len(parsedVariable) != 2 {
|
|
|
|
return fmt.Errorf("unexpected flag value for variable assignment: %s", variable)
|
|
|
|
}
|
|
|
|
name := parsedVariable[0]
|
|
|
|
val := parsedVariable[1]
|
|
|
|
|
|
|
|
if _, ok := r.Variables[name]; !ok {
|
|
|
|
return fmt.Errorf("variable %s has not been defined", name)
|
|
|
|
}
|
|
|
|
err := r.Variables[name].Set(val)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to assign %s to %s: %s", val, name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-18 09:57:31 +00:00
|
|
|
func (r *Root) Merge(other *Root) error {
|
2023-09-04 09:55:01 +00:00
|
|
|
err := r.Sync.Merge(r, other)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
other.Sync = Sync{}
|
|
|
|
|
2023-04-12 14:17:13 +00:00
|
|
|
// TODO: when hooking into merge semantics, disallow setting path on the target instance.
|
|
|
|
other.Path = ""
|
|
|
|
|
2023-04-17 10:21:21 +00:00
|
|
|
// Check for safe merge, protecting against duplicate resource identifiers
|
2023-09-04 09:55:01 +00:00
|
|
|
err = r.Resources.VerifySafeMerge(&other.Resources)
|
2023-04-17 10:21:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-18 09:57:31 +00:00
|
|
|
// TODO: define and test semantics for merging.
|
2023-08-15 09:58:54 +00:00
|
|
|
return mergo.Merge(r, other, mergo.WithOverride)
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
func (r *Root) MergeTargetOverrides(target *Target) error {
|
2022-11-18 09:57:31 +00:00
|
|
|
var err error
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
// Target may be nil if it's empty.
|
|
|
|
if target == nil {
|
2022-12-22 14:31:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
if target.Bundle != nil {
|
|
|
|
err = mergo.Merge(&r.Bundle, target.Bundle, mergo.WithOverride)
|
2022-11-18 09:57:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
if target.Workspace != nil {
|
|
|
|
err = mergo.Merge(&r.Workspace, target.Workspace, mergo.WithOverride)
|
2022-11-18 09:57:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
if target.Artifacts != nil {
|
|
|
|
err = mergo.Merge(&r.Artifacts, target.Artifacts, mergo.WithOverride, mergo.WithAppendSlice)
|
2022-11-30 13:15:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
if target.Resources != nil {
|
|
|
|
err = mergo.Merge(&r.Resources, target.Resources, mergo.WithOverride, mergo.WithAppendSlice)
|
2022-11-18 09:57:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-14 06:43:45 +00:00
|
|
|
|
2023-09-21 19:21:20 +00:00
|
|
|
err = r.Resources.Merge()
|
2023-09-18 14:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
if target.Variables != nil {
|
|
|
|
for k, v := range target.Variables {
|
2024-01-04 21:04:42 +00:00
|
|
|
rootVariable, ok := r.Variables[k]
|
2023-05-15 12:07:18 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("variable %s is not defined but is assigned a value", k)
|
|
|
|
}
|
2024-01-04 21:04:42 +00:00
|
|
|
|
|
|
|
if sv, ok := v.(string); ok {
|
|
|
|
// we allow overrides of the default value for a variable
|
|
|
|
defaultVal := sv
|
|
|
|
rootVariable.Default = &defaultVal
|
|
|
|
} else if vv, ok := v.(map[string]any); ok {
|
|
|
|
// we also allow overrides of the lookup value for a variable
|
|
|
|
lookup, ok := vv["lookup"]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("variable %s is incorrectly defined lookup override, no 'lookup' key defined", k)
|
|
|
|
}
|
|
|
|
rootVariable.Lookup = variable.LookupFromMap(lookup.(map[string]any))
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("variable %s is incorrectly defined in target override", k)
|
|
|
|
}
|
2023-05-15 12:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 16:47:07 +00:00
|
|
|
if target.RunAs != nil {
|
|
|
|
r.RunAs = target.RunAs
|
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
if target.Mode != "" {
|
|
|
|
r.Bundle.Mode = target.Mode
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
if target.ComputeID != "" {
|
|
|
|
r.Bundle.ComputeID = target.ComputeID
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 12:44:33 +00:00
|
|
|
git := &r.Bundle.Git
|
2023-08-17 15:22:32 +00:00
|
|
|
if target.Git.Branch != "" {
|
|
|
|
git.Branch = target.Git.Branch
|
2023-07-30 12:44:33 +00:00
|
|
|
git.Inferred = false
|
|
|
|
}
|
2023-08-17 15:22:32 +00:00
|
|
|
if target.Git.Commit != "" {
|
|
|
|
git.Commit = target.Git.Commit
|
2023-07-30 12:44:33 +00:00
|
|
|
}
|
2023-08-17 15:22:32 +00:00
|
|
|
if target.Git.OriginURL != "" {
|
|
|
|
git.OriginURL = target.Git.OriginURL
|
2023-07-30 12:44:33 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 15:18:18 +00:00
|
|
|
if target.Sync != nil {
|
|
|
|
err = mergo.Merge(&r.Sync, target.Sync, mergo.WithAppendSlice)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-13 11:29:40 +00:00
|
|
|
if target.Permissions != nil {
|
|
|
|
err = mergo.Merge(&r.Permissions, target.Permissions, mergo.WithAppendSlice)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 09:57:31 +00:00
|
|
|
return nil
|
|
|
|
}
|