2023-03-22 15:37:26 +00:00
|
|
|
package lock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-28 13:15:21 +00:00
|
|
|
"errors"
|
2024-10-10 11:18:23 +00:00
|
|
|
"io/fs"
|
2023-03-22 15:37:26 +00:00
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2024-10-10 11:18:23 +00:00
|
|
|
"github.com/databricks/cli/bundle/permissions"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-12-28 13:15:21 +00:00
|
|
|
"github.com/databricks/cli/libs/filer"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/locker"
|
|
|
|
"github.com/databricks/cli/libs/log"
|
2023-03-22 15:37:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type acquire struct{}
|
|
|
|
|
|
|
|
func Acquire() bundle.Mutator {
|
|
|
|
return &acquire{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *acquire) Name() string {
|
|
|
|
return "lock:acquire"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *acquire) init(b *bundle.Bundle) error {
|
|
|
|
user := b.Config.Workspace.CurrentUser.UserName
|
2023-04-12 14:54:36 +00:00
|
|
|
dir := b.Config.Workspace.StatePath
|
2023-03-22 15:37:26 +00:00
|
|
|
l, err := locker.CreateLocker(user, dir, b.WorkspaceClient())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Locker = l
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *acquire) 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
|
|
|
}
|
|
|
|
|
|
|
|
err := m.init(b)
|
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-03-22 15:37:26 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 11:17:17 +00:00
|
|
|
force := b.Config.Bundle.Deployment.Lock.Force
|
2023-03-22 15:37:26 +00:00
|
|
|
log.Infof(ctx, "Acquiring deployment lock (force: %v)", force)
|
|
|
|
err = b.Locker.Lock(ctx, force)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf(ctx, "Failed to acquire deployment lock: %v", err)
|
2023-12-28 13:15:21 +00:00
|
|
|
|
2024-10-10 11:18:23 +00:00
|
|
|
if errors.Is(err, fs.ErrPermission) {
|
|
|
|
return permissions.ReportPossiblePermissionDenied(ctx, b, b.Config.Workspace.StatePath)
|
|
|
|
}
|
|
|
|
|
2023-12-28 13:15:21 +00:00
|
|
|
notExistsError := filer.NoSuchDirectoryError{}
|
|
|
|
if errors.As(err, ¬ExistsError) {
|
|
|
|
// If we get a "doesn't exist" error from the API this indicates
|
|
|
|
// we either don't have permissions or the path is invalid.
|
2024-10-10 11:18:23 +00:00
|
|
|
return permissions.ReportPossiblePermissionDenied(ctx, b, b.Config.Workspace.StatePath)
|
2023-12-28 13:15:21 +00:00
|
|
|
}
|
2024-10-10 11:18:23 +00:00
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-03-22 15:37:26 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-03-22 15:37:26 +00:00
|
|
|
}
|