2023-03-22 15:37:26 +00:00
|
|
|
package lock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-19 13:57:25 +00:00
|
|
|
"fmt"
|
2023-03-22 15:37:26 +00:00
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
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 (
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
func (m *release) Apply(ctx context.Context, b *bundle.Bundle) error {
|
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:
|
|
|
|
return b.Locker.Unlock(ctx)
|
|
|
|
case GoalDestroy:
|
|
|
|
return b.Locker.Unlock(ctx, locker.AllowLockFileNotExist)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown goal for lock release: %s", m.goal)
|
2023-03-22 15:37:26 +00:00
|
|
|
}
|
|
|
|
}
|