2023-03-22 15:37:26 +00:00
|
|
|
package lock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-06-19 13:57:25 +00:00
|
|
|
"github.com/databricks/cli/libs/locker"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
2023-03-22 15:37:26 +00:00
|
|
|
)
|
|
|
|
|
2023-06-19 13:57:25 +00:00
|
|
|
type Goal string
|
2023-03-22 15:37:26 +00:00
|
|
|
|
2023-06-19 13:57:25 +00:00
|
|
|
const (
|
2024-02-14 18:04:45 +00:00
|
|
|
GoalBind = Goal("bind")
|
|
|
|
GoalUnbind = Goal("unbind")
|
2023-06-19 13:57:25 +00:00
|
|
|
GoalDeploy = Goal("deploy")
|
|
|
|
GoalDestroy = Goal("destroy")
|
|
|
|
)
|
|
|
|
|
|
|
|
type release struct {
|
|
|
|
goal Goal
|
|
|
|
}
|
|
|
|
|
|
|
|
func Release(goal Goal) bundle.Mutator {
|
|
|
|
return &release{goal}
|
2023-03-22 15:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *release) Name() string {
|
|
|
|
return "lock:release"
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *release) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2023-03-22 15:37:26 +00:00
|
|
|
// Return early if locking is disabled.
|
2024-02-07 11:17:17 +00:00
|
|
|
if !b.Config.Bundle.Deployment.Lock.IsEnabled() {
|
2023-03-22 15:37:26 +00:00
|
|
|
log.Infof(ctx, "Skipping; locking is disabled")
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-03-22 15:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return early if the locker is not set.
|
|
|
|
// It is likely an error occurred prior to initialization of the locker instance.
|
|
|
|
if b.Locker == nil {
|
|
|
|
log.Warnf(ctx, "Unable to release lock if locker is not configured")
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-03-22 15:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof(ctx, "Releasing deployment lock")
|
2023-06-19 13:57:25 +00:00
|
|
|
switch m.goal {
|
|
|
|
case GoalDeploy:
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(b.Locker.Unlock(ctx))
|
2024-02-14 18:04:45 +00:00
|
|
|
case GoalBind, GoalUnbind:
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(b.Locker.Unlock(ctx))
|
2023-06-19 13:57:25 +00:00
|
|
|
case GoalDestroy:
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(b.Locker.Unlock(ctx, locker.AllowLockFileNotExist))
|
2023-06-19 13:57:25 +00:00
|
|
|
default:
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("unknown goal for lock release: %s", m.goal)
|
2023-03-22 15:37:26 +00:00
|
|
|
}
|
|
|
|
}
|