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-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle/config/variable"
|
2022-11-18 09:57:31 +00:00
|
|
|
"github.com/ghodss/yaml"
|
|
|
|
"github.com/imdario/mergo"
|
|
|
|
)
|
|
|
|
|
2023-07-18 10:16:34 +00:00
|
|
|
type ConfigFileNames []string
|
|
|
|
|
|
|
|
// FileNames contains allowed names of bundle configuration files.
|
|
|
|
var FileNames = ConfigFileNames{"databricks.yml", "databricks.yaml", "bundle.yml", "bundle.yaml"}
|
|
|
|
|
|
|
|
func (c ConfigFileNames) FindInPath(path string) (string, error) {
|
|
|
|
result := ""
|
|
|
|
var firstErr error
|
|
|
|
|
|
|
|
for _, file := range c {
|
|
|
|
filePath := filepath.Join(path, file)
|
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
if err == nil {
|
|
|
|
if result != "" {
|
|
|
|
return "", fmt.Errorf("multiple bundle root configuration files found in %s", path)
|
|
|
|
}
|
|
|
|
result = filePath
|
|
|
|
} else {
|
|
|
|
if firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if result == "" {
|
|
|
|
return "", firstErr
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
2022-11-18 09:57:31 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
Bundle Bundle `json:"bundle"`
|
|
|
|
|
|
|
|
// Include specifies a list of patterns of file names to load and
|
2023-07-18 10:16:34 +00:00
|
|
|
// merge into the this configuration. If not set in `databricks.yml`,
|
2022-11-18 09:57:31 +00:00
|
|
|
// it defaults to loading `*.yml` and `*/*.yml`.
|
|
|
|
//
|
|
|
|
// Also see [mutator.DefineDefaultInclude].
|
|
|
|
//
|
|
|
|
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.
|
|
|
|
Artifacts map[string]*Artifact `json:"artifacts,omitempty"`
|
|
|
|
|
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
|
|
|
|
|
|
|
// Environments can be used to differentiate settings and resources between
|
|
|
|
// bundle deployment environments (e.g. development, staging, production).
|
|
|
|
// If not specified, the code below initializes this field with a
|
|
|
|
// single default-initialized environment called "default".
|
|
|
|
Environments map[string]*Environment `json:"environments,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func Load(path string) (*Root, error) {
|
|
|
|
var r Root
|
|
|
|
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were given a directory, assume this is the bundle root.
|
|
|
|
if stat.IsDir() {
|
2023-07-18 10:16:34 +00:00
|
|
|
path, err = FileNames.FindInPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.Load(path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &r, nil
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
if r.Environments != nil {
|
|
|
|
for _, env := range r.Environments {
|
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-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
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:17:13 +00:00
|
|
|
func (r *Root) Load(path string) error {
|
|
|
|
raw, err := os.ReadFile(path)
|
2022-11-18 09:57:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(raw, r)
|
|
|
|
if err != nil {
|
2023-07-07 10:22:58 +00:00
|
|
|
return fmt.Errorf("failed to load %s: %w", path, err)
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
2023-04-17 10:21:21 +00:00
|
|
|
|
2023-04-12 14:17:13 +00:00
|
|
|
r.Path = filepath.Dir(path)
|
|
|
|
r.SetConfigFilePath(path)
|
2023-04-17 10:21:21 +00:00
|
|
|
|
|
|
|
_, err = r.Resources.VerifyUniqueResourceIdentifiers()
|
|
|
|
return err
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Root) Merge(other *Root) error {
|
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
|
|
|
|
err := r.Resources.VerifySafeMerge(&other.Resources)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-18 09:57:31 +00:00
|
|
|
// TODO: define and test semantics for merging.
|
|
|
|
return mergo.MergeWithOverwrite(r, other)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Root) MergeEnvironment(env *Environment) error {
|
|
|
|
var err error
|
|
|
|
|
2022-12-22 14:31:32 +00:00
|
|
|
// Environment may be nil if it's empty.
|
|
|
|
if env == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-18 09:57:31 +00:00
|
|
|
if env.Bundle != nil {
|
|
|
|
err = mergo.MergeWithOverwrite(&r.Bundle, env.Bundle)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if env.Workspace != nil {
|
|
|
|
err = mergo.MergeWithOverwrite(&r.Workspace, env.Workspace)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:15:22 +00:00
|
|
|
if env.Artifacts != nil {
|
|
|
|
err = mergo.Merge(&r.Artifacts, env.Artifacts, mergo.WithAppendSlice)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 09:57:31 +00:00
|
|
|
if env.Resources != nil {
|
2022-11-18 10:12:24 +00:00
|
|
|
err = mergo.Merge(&r.Resources, env.Resources, mergo.WithAppendSlice)
|
2022-11-18 09:57:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 12:07:18 +00:00
|
|
|
if env.Variables != nil {
|
|
|
|
for k, v := range env.Variables {
|
|
|
|
variable, ok := r.Variables[k]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("variable %s is not defined but is assigned a value", k)
|
|
|
|
}
|
|
|
|
// we only allow overrides of the default value for a variable
|
|
|
|
defaultVal := v
|
|
|
|
variable.Default = &defaultVal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-12 06:51:54 +00:00
|
|
|
if env.Mode != "" {
|
|
|
|
r.Bundle.Mode = env.Mode
|
|
|
|
}
|
|
|
|
|
|
|
|
if env.ComputeID != "" {
|
|
|
|
r.Bundle.ComputeID = env.ComputeID
|
|
|
|
}
|
|
|
|
|
2022-11-18 09:57:31 +00:00
|
|
|
return nil
|
|
|
|
}
|