2022-12-05 23:40:45 +00:00
|
|
|
package deployer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2023-03-22 15:37:26 +00:00
|
|
|
"github.com/databricks/bricks/libs/locker"
|
2023-03-17 14:17:31 +00:00
|
|
|
"github.com/databricks/bricks/libs/log"
|
2022-12-05 23:40:45 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go"
|
|
|
|
"github.com/hashicorp/terraform-exec/tfexec"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DeploymentStatus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Empty plan produced on terraform plan. No changes need to be applied
|
|
|
|
NoChanges DeploymentStatus = iota
|
|
|
|
|
|
|
|
// Deployment failed. No databricks assets were deployed
|
|
|
|
Failed
|
|
|
|
|
|
|
|
// Deployment failed/partially succeeded. failed to update remote terraform
|
|
|
|
// state file.
|
|
|
|
// The partially deployed resources are thus untracked and in most cases
|
|
|
|
// will need to be cleaned up manually
|
|
|
|
PartialButUntracked
|
|
|
|
|
|
|
|
// Deployment failed/partially succeeded. Remote terraform state file is
|
|
|
|
// updated with any partially deployed resources
|
|
|
|
Partial
|
|
|
|
|
|
|
|
// Deployment succeeded however the remote terraform state was not updated.
|
|
|
|
// The deployed resources are thus untracked and in most cases will need to
|
|
|
|
// be cleaned up manually
|
|
|
|
CompleteButUntracked
|
|
|
|
|
|
|
|
// Deployment succeeeded with remote terraform state file updated
|
|
|
|
Complete
|
|
|
|
)
|
|
|
|
|
|
|
|
// Deployer is a struct to deploy a DAB to a databricks workspace
|
|
|
|
//
|
|
|
|
// Here's a high level description of what a deploy looks like:
|
|
|
|
//
|
|
|
|
// 1. Client compiles the bundle configuration to a terraform HCL config file
|
|
|
|
//
|
2022-12-14 14:59:47 +00:00
|
|
|
// 2. Client tries to acquire a lock on the remote root of the project.
|
|
|
|
// -- If FAIL: print details about current holder of the deployment lock on
|
|
|
|
// remote root and terminate deployment
|
2022-12-05 23:40:45 +00:00
|
|
|
//
|
|
|
|
// 3. Client reads terraform state from remote root
|
|
|
|
//
|
|
|
|
// 4. Client applies the diff in terraform config to the databricks workspace
|
|
|
|
//
|
|
|
|
// 5. Client updates terraform state file in remote root
|
|
|
|
//
|
|
|
|
// 6. Client releases the deploy lock on remote root
|
|
|
|
type Deployer struct {
|
|
|
|
localRoot string
|
|
|
|
remoteRoot string
|
|
|
|
env string
|
2023-03-22 15:37:26 +00:00
|
|
|
locker *locker.Locker
|
2022-12-05 23:40:45 +00:00
|
|
|
wsc *databricks.WorkspaceClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func Create(ctx context.Context, env, localRoot, remoteRoot string, wsc *databricks.WorkspaceClient) (*Deployer, error) {
|
|
|
|
user, err := wsc.CurrentUser.Me(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-22 15:37:26 +00:00
|
|
|
newLocker, err := locker.CreateLocker(user.UserName, remoteRoot, wsc)
|
2022-12-05 23:40:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Deployer{
|
|
|
|
localRoot: localRoot,
|
|
|
|
remoteRoot: remoteRoot,
|
|
|
|
env: env,
|
|
|
|
locker: newLocker,
|
|
|
|
wsc: wsc,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Deployer) DefaultTerraformRoot() string {
|
|
|
|
return filepath.Join(b.localRoot, ".databricks/bundle", b.env)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Deployer) tfStateRemotePath() string {
|
2022-12-15 16:16:07 +00:00
|
|
|
// Note: remote paths are scoped to `remoteRoot` through the locker. Also see [Create].
|
|
|
|
return ".bundle/terraform.tfstate"
|
2022-12-05 23:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Deployer) tfStateLocalPath() string {
|
|
|
|
return filepath.Join(b.DefaultTerraformRoot(), "terraform.tfstate")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Deployer) LoadTerraformState(ctx context.Context) error {
|
2022-12-15 16:16:07 +00:00
|
|
|
bytes, err := b.locker.GetRawJsonFileContent(ctx, b.tfStateRemotePath())
|
2022-12-05 23:40:45 +00:00
|
|
|
if err != nil {
|
|
|
|
// If remote tf state is absent, use local tf state
|
|
|
|
if strings.Contains(err.Error(), "File not found.") {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = os.MkdirAll(b.DefaultTerraformRoot(), os.ModeDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.WriteFile(b.tfStateLocalPath(), bytes, os.ModePerm)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Deployer) SaveTerraformState(ctx context.Context) error {
|
|
|
|
bytes, err := os.ReadFile(b.tfStateLocalPath())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-15 16:16:07 +00:00
|
|
|
return b.locker.PutFile(ctx, b.tfStateRemotePath(), bytes)
|
2022-12-05 23:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Deployer) Lock(ctx context.Context, isForced bool) error {
|
2022-12-15 16:16:07 +00:00
|
|
|
return d.locker.Lock(ctx, isForced)
|
2022-12-05 23:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Deployer) Unlock(ctx context.Context) error {
|
2022-12-15 16:16:07 +00:00
|
|
|
return d.locker.Unlock(ctx)
|
2022-12-05 23:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Deployer) ApplyTerraformConfig(ctx context.Context, configPath, terraformBinaryPath string, isForced bool) (DeploymentStatus, error) {
|
|
|
|
applyErr := d.Lock(ctx, isForced)
|
|
|
|
if applyErr != nil {
|
|
|
|
return Failed, applyErr
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
applyErr = d.Unlock(ctx)
|
|
|
|
if applyErr != nil {
|
2023-03-17 14:17:31 +00:00
|
|
|
log.Errorf(ctx, "failed to unlock deployment mutex: %s", applyErr)
|
2022-12-05 23:40:45 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
applyErr = d.LoadTerraformState(ctx)
|
|
|
|
if applyErr != nil {
|
2023-03-17 14:17:31 +00:00
|
|
|
log.Debugf(ctx, "failed to load terraform state from workspace: %s", applyErr)
|
2022-12-05 23:40:45 +00:00
|
|
|
return Failed, applyErr
|
|
|
|
}
|
|
|
|
|
|
|
|
tf, applyErr := tfexec.NewTerraform(configPath, terraformBinaryPath)
|
|
|
|
if applyErr != nil {
|
2023-03-17 14:17:31 +00:00
|
|
|
log.Debugf(ctx, "failed to construct terraform object: %s", applyErr)
|
2022-12-05 23:40:45 +00:00
|
|
|
return Failed, applyErr
|
|
|
|
}
|
|
|
|
|
|
|
|
isPlanNotEmpty, applyErr := tf.Plan(ctx)
|
|
|
|
if applyErr != nil {
|
2023-03-17 14:17:31 +00:00
|
|
|
log.Debugf(ctx, "failed to compute terraform plan: %s", applyErr)
|
2022-12-05 23:40:45 +00:00
|
|
|
return Failed, applyErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isPlanNotEmpty {
|
2023-03-17 14:17:31 +00:00
|
|
|
log.Debugf(ctx, "terraform plan returned a empty diff")
|
2022-12-05 23:40:45 +00:00
|
|
|
return NoChanges, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
applyErr = tf.Apply(ctx)
|
|
|
|
// upload state even if apply fails to handle partial deployments
|
|
|
|
saveStateErr := d.SaveTerraformState(ctx)
|
|
|
|
|
|
|
|
if applyErr != nil && saveStateErr != nil {
|
2023-03-17 14:17:31 +00:00
|
|
|
log.Errorf(ctx, "terraform apply failed: %s", applyErr)
|
|
|
|
log.Errorf(ctx, "failed to upload terraform state after partial terraform apply: %s", saveStateErr)
|
2022-12-05 23:40:45 +00:00
|
|
|
return PartialButUntracked, fmt.Errorf("deploymented failed: %s", applyErr)
|
|
|
|
}
|
|
|
|
if applyErr != nil {
|
2023-03-17 14:17:31 +00:00
|
|
|
log.Errorf(ctx, "terraform apply failed: %s", applyErr)
|
2022-12-05 23:40:45 +00:00
|
|
|
return Partial, fmt.Errorf("deploymented failed: %s", applyErr)
|
|
|
|
}
|
|
|
|
if saveStateErr != nil {
|
2023-03-17 14:17:31 +00:00
|
|
|
log.Errorf(ctx, "failed to upload terraform state after completing terraform apply: %s", saveStateErr)
|
2022-12-05 23:40:45 +00:00
|
|
|
return CompleteButUntracked, fmt.Errorf("failed to upload terraform state file: %s", saveStateErr)
|
|
|
|
}
|
|
|
|
return Complete, nil
|
|
|
|
}
|