diff --git a/bundle/config/bundle.go b/bundle/config/bundle.go index 87515957..49c8626d 100644 --- a/bundle/config/bundle.go +++ b/bundle/config/bundle.go @@ -33,4 +33,7 @@ type Bundle struct { // For example, 'mode: development' can be used for deployments for // development purposes. Mode Mode `json:"mode,omitempty"` + + // Overrides the compute used for jobs and other supported assets. + Compute string `json:"override_compute,omitempty" bundle:"readonly"` } diff --git a/bundle/config/mutator/override_compute.go b/bundle/config/mutator/override_compute.go index fbca2832..3ceea4de 100644 --- a/bundle/config/mutator/override_compute.go +++ b/bundle/config/mutator/override_compute.go @@ -9,32 +9,30 @@ import ( "github.com/databricks/cli/bundle/config/resources" ) -type overrideCompute struct { - compute string -} +type overrideCompute struct{} -func OverrideCompute(compute string) bundle.Mutator { - return &overrideCompute{compute: compute} +func OverrideCompute() bundle.Mutator { + return &overrideCompute{} } func (m *overrideCompute) Name() string { return "OverrideCompute" } -func (m *overrideCompute) overrideJobCompute(j *resources.Job) { +func overrideJobCompute(j *resources.Job, compute string) { for i := range j.Tasks { task := &j.Tasks[i] if task.NewCluster != nil { task.NewCluster = nil - task.ExistingClusterId = m.compute + task.ExistingClusterId = compute } else if task.ExistingClusterId != "" { - task.ExistingClusterId = m.compute + task.ExistingClusterId = compute } } } func (m *overrideCompute) Apply(ctx context.Context, b *bundle.Bundle) error { - if m.compute == "" { + if b.Config.Bundle.Compute == "" { return nil } if b.Config.Bundle.Mode != config.Development { @@ -43,7 +41,7 @@ func (m *overrideCompute) Apply(ctx context.Context, b *bundle.Bundle) error { r := b.Config.Resources for i := range r.Jobs { - m.overrideJobCompute(r.Jobs[i]) + overrideJobCompute(r.Jobs[i], b.Config.Bundle.Compute) } return nil diff --git a/bundle/config/mutator/override_compute_test.go b/bundle/config/mutator/override_compute_test.go index 07a8578b..e41c4225 100644 --- a/bundle/config/mutator/override_compute_test.go +++ b/bundle/config/mutator/override_compute_test.go @@ -18,7 +18,8 @@ func TestOverrideCompute(t *testing.T) { bundle := &bundle.Bundle{ Config: config.Root{ Bundle: config.Bundle{ - Mode: config.Development, + Mode: config.Development, + Compute: "newClusterID", }, Resources: config.Resources{ Jobs: map[string]*resources.Job{ @@ -38,7 +39,7 @@ func TestOverrideCompute(t *testing.T) { }, } - m := mutator.OverrideCompute("newClusterID") + m := mutator.OverrideCompute() err := m.Apply(context.Background(), bundle) require.NoError(t, err) assert.Nil(t, bundle.Config.Resources.Jobs["job1"].Tasks[0].NewCluster) diff --git a/bundle/config/mutator/process_environment_mode_test.go b/bundle/config/mutator/process_environment_mode_test.go index d3ecbd55..238e206b 100644 --- a/bundle/config/mutator/process_environment_mode_test.go +++ b/bundle/config/mutator/process_environment_mode_test.go @@ -53,7 +53,7 @@ func TestProcessEnvironmentModeApplyDefault(t *testing.T) { bundle := &bundle.Bundle{ Config: config.Root{ Bundle: config.Bundle{ - Mode: config.Default, + Mode: "", }, Resources: config.Resources{ Jobs: map[string]*resources.Job{ diff --git a/bundle/phases/initialize.go b/bundle/phases/initialize.go index 2d71c887..fc5056f6 100644 --- a/bundle/phases/initialize.go +++ b/bundle/phases/initialize.go @@ -11,7 +11,7 @@ import ( // The initialize phase fills in defaults and connects to the workspace. // Interpolation of fields referring to the "bundle" and "workspace" keys // happens upon completion of this phase. -func Initialize(overrideCompute string) bundle.Mutator { +func Initialize() bundle.Mutator { return newPhase( "initialize", []bundle.Mutator{ @@ -25,7 +25,7 @@ func Initialize(overrideCompute string) bundle.Mutator { interpolation.IncludeLookupsInPath("workspace"), interpolation.IncludeLookupsInPath(variable.VariableReferencePrefix), ), - mutator.OverrideCompute(overrideCompute), + mutator.OverrideCompute(), mutator.ProcessEnvironmentMode(), mutator.TranslatePaths(), terraform.Initialize(), diff --git a/cmd/bundle/deploy.go b/cmd/bundle/deploy.go index 3418886d..0cf96118 100644 --- a/cmd/bundle/deploy.go +++ b/cmd/bundle/deploy.go @@ -21,15 +21,16 @@ var deployCmd = &cobra.Command{ } func deploy(cmd *cobra.Command, b *bundle.Bundle) error { - // If `--force` is specified, force acquisition of the deployment lock. - b.Config.Bundle.Lock.Force = forceDeploy - if computeID == "" { computeID = os.Getenv("DATABRICKS_CLUSTER_ID") } + // If `--force` is specified, force acquisition of the deployment lock. + b.Config.Bundle.Lock.Force = forceDeploy + b.Config.Bundle.Compute = computeID + return bundle.Apply(cmd.Context(), b, bundle.Seq( - phases.Initialize(computeID), + phases.Initialize(), phases.Build(), phases.Deploy(), )) diff --git a/cmd/bundle/destroy.go b/cmd/bundle/destroy.go index cd4fbad3..d0fe699a 100644 --- a/cmd/bundle/destroy.go +++ b/cmd/bundle/destroy.go @@ -43,7 +43,7 @@ var destroyCmd = &cobra.Command{ } return bundle.Apply(ctx, b, bundle.Seq( - phases.Initialize(""), + phases.Initialize(), phases.Build(), phases.Destroy(), )) diff --git a/cmd/bundle/run.go b/cmd/bundle/run.go index 4058b2da..1c5413f3 100644 --- a/cmd/bundle/run.go +++ b/cmd/bundle/run.go @@ -38,7 +38,7 @@ var runCmd = &cobra.Command{ } err := bundle.Apply(cmd.Context(), b, bundle.Seq( - phases.Initialize(computeID), + phases.Initialize(), terraform.Interpolate(), terraform.Write(), terraform.StatePull(), diff --git a/cmd/bundle/sync.go b/cmd/bundle/sync.go index 2b7c29a3..19adc2dd 100644 --- a/cmd/bundle/sync.go +++ b/cmd/bundle/sync.go @@ -39,7 +39,7 @@ var syncCmd = &cobra.Command{ b := bundle.Get(cmd.Context()) // Run initialize phase to make sure paths are set. - err := bundle.Apply(cmd.Context(), b, phases.Initialize("")) + err := bundle.Apply(cmd.Context(), b, phases.Initialize()) if err != nil { return err } diff --git a/cmd/bundle/validate.go b/cmd/bundle/validate.go index 35896991..65ab3890 100644 --- a/cmd/bundle/validate.go +++ b/cmd/bundle/validate.go @@ -16,7 +16,7 @@ var validateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { b := bundle.Get(cmd.Context()) - err := bundle.Apply(cmd.Context(), b, phases.Initialize("")) + err := bundle.Apply(cmd.Context(), b, phases.Initialize()) if err != nil { return err }