Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tune output of bundle deploy command #1047

Merged
merged 10 commits into from
Dec 21, 2023
5 changes: 3 additions & 2 deletions bundle/artifacts/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/filer"
"github.com/databricks/cli/libs/log"
)

type mutatorFactory = func(name string) bundle.Mutator
Expand Down Expand Up @@ -67,7 +68,7 @@ func (m *basicBuild) Apply(ctx context.Context, b *bundle.Bundle) error {
if err != nil {
return fmt.Errorf("build for %s failed, error: %w, output: %s", m.name, err, out)
}
cmdio.LogString(ctx, "Build succeeded")
log.Infof(ctx, "Build succeeded")

return nil
}
Expand Down Expand Up @@ -124,7 +125,7 @@ func uploadArtifact(ctx context.Context, a *config.Artifact, uploadPath string,
if err != nil {
return err
}
cmdio.LogString(ctx, "Upload succeeded")
log.Infof(ctx, "Upload succeeded")
f.RemotePath = path.Join(uploadPath, filepath.Base(f.Source))
}
}
Expand Down
7 changes: 3 additions & 4 deletions bundle/artifacts/whl/autodetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/bundle/libraries"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/log"
)

Expand All @@ -32,17 +31,17 @@ func (m *detectPkg) Apply(ctx context.Context, b *bundle.Bundle) error {
log.Infof(ctx, "No local wheel tasks in databricks.yml config, skipping auto detect")
return nil
}
cmdio.LogString(ctx, "Detecting Python wheel project...")
log.Infof(ctx, "Detecting Python wheel project...")

// checking if there is setup.py in the bundle root
setupPy := filepath.Join(b.Config.Path, "setup.py")
_, err := os.Stat(setupPy)
if err != nil {
cmdio.LogString(ctx, "No Python wheel project found at bundle root folder")
log.Infof(ctx, "No Python wheel project found at bundle root folder")
return nil
}

cmdio.LogString(ctx, fmt.Sprintf("Found Python wheel project at %s", b.Config.Path))
log.Infof(ctx, fmt.Sprintf("Found Python wheel project at %s", b.Config.Path))
module := extractModuleName(setupPy)

if b.Config.Artifacts == nil {
Expand Down
3 changes: 2 additions & 1 deletion bundle/artifacts/whl/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/log"
"github.com/databricks/cli/libs/python"
)

Expand Down Expand Up @@ -44,7 +45,7 @@ func (m *build) Apply(ctx context.Context, b *bundle.Bundle) error {
if err != nil {
return fmt.Errorf("build failed %s, error: %w, output: %s", m.name, err, out)
}
cmdio.LogString(ctx, "Build succeeded")
log.Infof(ctx, "Build succeeded")

wheels := python.FindFilesWithSuffixInPath(distPath, ".whl")
if len(wheels) == 0 {
Expand Down
13 changes: 13 additions & 0 deletions bundle/deferred.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type DeferredMutator struct {
mutator Mutator
finally Mutator
}
type contextKey string

const mainErrKey contextKey = "mainErr"

func (d *DeferredMutator) Name() string {
return "deferred"
Expand All @@ -24,10 +27,20 @@ func Defer(mutator Mutator, finally Mutator) Mutator {

func (d *DeferredMutator) Apply(ctx context.Context, b *Bundle) error {
mainErr := Apply(ctx, b, d.mutator)
if mainErr != nil {
ctx = context.WithValue(ctx, mainErrKey, mainErr)
}
errOnFinish := Apply(ctx, b, d.finally)
if mainErr != nil || errOnFinish != nil {
return errs.FromMany(mainErr, errOnFinish)
}

return nil
}

func ErrFromContext(ctx context.Context) error {
if err, ok := ctx.Value(mainErrKey).(error); ok {
return err
}
return nil
}
36 changes: 36 additions & 0 deletions bundle/deferred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ func (t *mutatorWithError) Apply(_ context.Context, b *Bundle) error {
return fmt.Errorf(t.errorMsg)
}

type checkErrorMutator struct {
applyCalled int
expectedError string
}

func (c *checkErrorMutator) Name() string {
return "checkError"
}

func (c *checkErrorMutator) Apply(ctx context.Context, b *Bundle) error {
c.applyCalled++
mainErr := ErrFromContext(ctx)
if mainErr != nil && mainErr.Error() == c.expectedError {
return nil // Correct error found
}
return fmt.Errorf("expected error '%s', but found '%v'", c.expectedError, mainErr)
}

func TestDeferredMutatorWhenAllMutatorsSucceed(t *testing.T) {
m1 := &testMutator{}
m2 := &testMutator{}
Expand Down Expand Up @@ -106,3 +124,21 @@ func TestDeferredMutatorCombinesErrorMessages(t *testing.T) {
assert.Equal(t, 1, mErr.applyCalled)
assert.Equal(t, 1, cleanupErr.applyCalled)
}

func TestDeferredMutatorPassesErrorToFinally(t *testing.T) {
mErr := &mutatorWithError{errorMsg: "mutator error occurred"}
finalCheck := &testMutator{
nestedMutators: []Mutator{
&checkErrorMutator{expectedError: "mutator error occurred"},
},
}
deferredMutator := Defer(mErr, finalCheck)

b := &Bundle{}
err := Apply(context.Background(), b, deferredMutator)

assert.ErrorContains(t, err, "mutator error occurred")
assert.Equal(t, 1, mErr.applyCalled)
assert.Equal(t, 1, finalCheck.applyCalled)
assert.Equal(t, 1, finalCheck.nestedMutators[0].(*checkErrorMutator).applyCalled)
}
5 changes: 3 additions & 2 deletions bundle/deploy/files/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/log"
)

type upload struct{}
Expand All @@ -15,7 +16,7 @@ func (m *upload) Name() string {
}

func (m *upload) Apply(ctx context.Context, b *bundle.Bundle) error {
cmdio.LogString(ctx, "Starting upload of bundle files")
cmdio.LogString(ctx, fmt.Sprintf("Uploading bundle files to %s...", b.Config.Workspace.FilePath))
sync, err := getSync(ctx, b)
if err != nil {
return err
Expand All @@ -26,7 +27,7 @@ func (m *upload) Apply(ctx context.Context, b *bundle.Bundle) error {
return err
}

cmdio.LogString(ctx, fmt.Sprintf("Uploaded bundle files at %s!\n", b.Config.Workspace.FilePath))
log.Infof(ctx, "Uploaded bundle files")
return nil
}

Expand Down
23 changes: 18 additions & 5 deletions bundle/deploy/lock/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/locker"
"github.com/databricks/cli/libs/log"
)
Expand All @@ -17,11 +18,12 @@ const (
)

type release struct {
goal Goal
goal Goal
successMessage string
}

func Release(goal Goal) bundle.Mutator {
return &release{goal}
func Release(goal Goal, successMessage string) bundle.Mutator {
return &release{goal, successMessage}
}

func (m *release) Name() string {
Expand All @@ -45,10 +47,21 @@ func (m *release) Apply(ctx context.Context, b *bundle.Bundle) error {
log.Infof(ctx, "Releasing deployment lock")
switch m.goal {
case GoalDeploy:
return b.Locker.Unlock(ctx)
err := b.Locker.Unlock(ctx)
if err != nil {
return err
}
case GoalDestroy:
return b.Locker.Unlock(ctx, locker.AllowLockFileNotExist)
err := b.Locker.Unlock(ctx, locker.AllowLockFileNotExist)
if err != nil {
return err
}
default:
return fmt.Errorf("unknown goal for lock release: %s", m.goal)
}

if m.successMessage != "" && bundle.ErrFromContext(ctx) == nil {
cmdio.LogString(ctx, m.successMessage)
}
return nil
}
5 changes: 3 additions & 2 deletions bundle/deploy/terraform/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/log"
"github.com/hashicorp/terraform-exec/tfexec"
)

Expand All @@ -21,7 +22,7 @@ func (w *apply) Apply(ctx context.Context, b *bundle.Bundle) error {
return fmt.Errorf("terraform not initialized")
}

cmdio.LogString(ctx, "Starting resource deployment")
cmdio.LogString(ctx, "Deploying resources...")

err := tf.Init(ctx, tfexec.Upgrade(true))
if err != nil {
Expand All @@ -33,7 +34,7 @@ func (w *apply) Apply(ctx context.Context, b *bundle.Bundle) error {
return fmt.Errorf("terraform apply: %w", err)
}

cmdio.LogString(ctx, "Resource deployment completed!")
log.Infof(ctx, "Resource deployment completed")
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions bundle/deploy/terraform/state_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/filer"
"github.com/databricks/cli/libs/log"
)
Expand Down Expand Up @@ -37,6 +38,7 @@ func (l *statePush) Apply(ctx context.Context, b *bundle.Bundle) error {
defer local.Close()

// Upload state file from local cache directory to filer.
cmdio.LogString(ctx, "Updating deployment state...")
log.Infof(ctx, "Writing local state file to remote state directory")
err = f.Write(ctx, TerraformStateFileName, local, filer.CreateParentDirectories, filer.OverwriteIfExists)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion bundle/phases/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Deploy() bundle.Mutator {
),
),
),
lock.Release(lock.GoalDeploy),
lock.Release(lock.GoalDeploy, "Deployment complete!"),
),
scripts.Execute(config.ScriptPostDeploy),
)
Expand Down
2 changes: 1 addition & 1 deletion bundle/phases/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Destroy() bundle.Mutator {
terraform.StatePush(),
files.Delete(),
),
lock.Release(lock.GoalDestroy),
lock.Release(lock.GoalDestroy, "Destroy complete!"),
),
)

Expand Down