2022-12-12 11:49:25 +00:00
|
|
|
// Package bundle is the top level package for Databricks Application Bundles.
|
|
|
|
//
|
|
|
|
// A bundle is represented by the [Bundle] type. It consists of configuration
|
|
|
|
// and runtime state, such as a client to a Databricks workspace.
|
|
|
|
// Every mutation to a bundle's configuration or state is represented as a [Mutator].
|
|
|
|
// This interface makes every mutation observable and lets us reason about sequencing.
|
2022-11-18 09:57:31 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
2022-11-30 13:40:41 +00:00
|
|
|
"os"
|
2022-11-18 09:57:31 +00:00
|
|
|
"path/filepath"
|
2022-11-23 14:20:03 +00:00
|
|
|
"sync"
|
2022-11-18 09:57:31 +00:00
|
|
|
|
|
|
|
"github.com/databricks/bricks/bundle/config"
|
2022-11-24 20:41:57 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go"
|
2022-12-15 14:12:47 +00:00
|
|
|
"github.com/hashicorp/terraform-exec/tfexec"
|
2022-11-18 09:57:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Bundle struct {
|
|
|
|
Config config.Root
|
2022-11-23 14:20:03 +00:00
|
|
|
|
|
|
|
// Store a pointer to the workspace client.
|
|
|
|
// It can be initialized on demand after loading the configuration.
|
|
|
|
clientOnce sync.Once
|
2022-11-24 20:41:57 +00:00
|
|
|
client *databricks.WorkspaceClient
|
2022-12-15 14:12:47 +00:00
|
|
|
|
|
|
|
// Stores an initialized copy of this bundle's Terraform wrapper.
|
|
|
|
Terraform *tfexec.Terraform
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Load(path string) (*Bundle, error) {
|
|
|
|
bundle := &Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: path,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := bundle.Config.Load(filepath.Join(path, config.FileName))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bundle, nil
|
|
|
|
}
|
2022-11-21 14:39:53 +00:00
|
|
|
|
|
|
|
func LoadFromRoot() (*Bundle, error) {
|
|
|
|
root, err := getRoot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Load(root)
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:41:57 +00:00
|
|
|
func (b *Bundle) WorkspaceClient() *databricks.WorkspaceClient {
|
2022-11-23 14:20:03 +00:00
|
|
|
b.clientOnce.Do(func() {
|
2022-11-24 20:41:57 +00:00
|
|
|
var err error
|
|
|
|
b.client, err = b.Config.Workspace.Client()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-11-23 14:20:03 +00:00
|
|
|
})
|
|
|
|
return b.client
|
|
|
|
}
|
2022-11-30 13:40:41 +00:00
|
|
|
|
|
|
|
var cacheDirName = filepath.Join(".databricks", "bundle")
|
|
|
|
|
|
|
|
// CacheDir returns directory to use for temporary files for this bundle.
|
|
|
|
// Scoped to the bundle's environment.
|
2022-12-15 16:30:33 +00:00
|
|
|
func (b *Bundle) CacheDir(paths ...string) (string, error) {
|
2022-11-30 13:40:41 +00:00
|
|
|
if b.Config.Bundle.Environment == "" {
|
|
|
|
panic("environment not set")
|
|
|
|
}
|
|
|
|
|
2022-12-15 16:30:33 +00:00
|
|
|
// Fixed components of the result path.
|
|
|
|
parts := []string{
|
|
|
|
// Anchor at bundle root directory.
|
|
|
|
b.Config.Path,
|
|
|
|
// Static cache directory.
|
|
|
|
cacheDirName,
|
|
|
|
// Scope with environment name.
|
|
|
|
b.Config.Bundle.Environment,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append dynamic components of the result path.
|
|
|
|
parts = append(parts, paths...)
|
|
|
|
|
2022-11-30 13:40:41 +00:00
|
|
|
// Make directory if it doesn't exist yet.
|
2022-12-15 16:30:33 +00:00
|
|
|
dir := filepath.Join(parts...)
|
2022-11-30 13:40:41 +00:00
|
|
|
err := os.MkdirAll(dir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir, nil
|
|
|
|
}
|