-
Notifications
You must be signed in to change notification settings - Fork 173
bundle: extract DeploymentLock interface + workspace filesystem impl #5314
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
Open
shreyas-goenka
wants to merge
10
commits into
main
Choose a base branch
from
shreyas-goenka/bundle-lock-abstraction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
09f9090
bundle: extract DeploymentLock interface + workspace filesystem impl
shreyas-goenka 915430b
bundle/deploy/lock: own *locker.Locker on the lock object, not on bun…
shreyas-goenka 7ccf788
bundle/deploy/lock: decouple workspaceFilesystemLock from bundle.Bundle
shreyas-goenka 00a7e9a
bundle/deploy/lock: drop the permission-error callback indirection
shreyas-goenka 0433a83
bundle/deploy/lock: drop redundant Errorf on failed lock acquire
shreyas-goenka 8f541a0
bundle/phases: surface Release errors via logdiag (restore pre-refact…
shreyas-goenka 6cad232
bundle/deploy/lock: lift the *bundle.Bundle reference off workspaceFi…
shreyas-goenka 209c9a4
acceptance/pipelines: drop duplicate "Error: Failed to acquire deploy…
shreyas-goenka e5c90cb
Revert: restore "Failed to acquire deployment lock" log line + goldens
shreyas-goenka cab0dfe
bundle/deploy/lock: only init WorkspaceClient when locking is enabled
shreyas-goenka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package lock | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/permissions" | ||
| "github.com/databricks/cli/libs/diag" | ||
| ) | ||
|
|
||
| // Goal describes the purpose of a deployment operation. | ||
| type Goal string | ||
|
|
||
| const ( | ||
| GoalBind = Goal("bind") | ||
| GoalUnbind = Goal("unbind") | ||
| GoalDeploy = Goal("deploy") | ||
| GoalDestroy = Goal("destroy") | ||
| ) | ||
|
|
||
| // DeploymentStatus indicates whether the deployment operation succeeded or failed. | ||
| type DeploymentStatus int | ||
|
|
||
| const ( | ||
| DeploymentSuccess DeploymentStatus = iota | ||
| DeploymentFailure | ||
| ) | ||
|
|
||
| // DeploymentLock manages the deployment lock lifecycle. | ||
| type DeploymentLock interface { | ||
| // Acquire acquires the deployment lock. | ||
| Acquire(ctx context.Context) error | ||
|
|
||
| // Release releases the deployment lock with the given deployment status. | ||
| Release(ctx context.Context, status DeploymentStatus) error | ||
| } | ||
|
|
||
| // NewDeploymentLock returns a DeploymentLock backed by the workspace | ||
| // filesystem. Captures everything the lock needs from the bundle at | ||
| // construction time so the lock implementation itself does not retain a | ||
| // *bundle.Bundle reference. The workspace client is only initialized when | ||
| // locking is enabled to match the original lazy-init behavior. | ||
| func NewDeploymentLock(ctx context.Context, b *bundle.Bundle, goal Goal) DeploymentLock { | ||
| enabled := b.Config.Bundle.Deployment.Lock.IsEnabled() | ||
| l := &workspaceFilesystemLock{ | ||
| user: b.Config.Workspace.CurrentUser.UserName, | ||
| statePath: b.Config.Workspace.StatePath, | ||
| enabled: enabled, | ||
| force: b.Config.Bundle.Deployment.Lock.Force, | ||
| goal: goal, | ||
| reportPermissionError: func(ctx context.Context, path string) diag.Diagnostics { | ||
| return permissions.ReportPossiblePermissionDenied(ctx, b, path) | ||
| }, | ||
| } | ||
| if enabled { | ||
| l.client = b.WorkspaceClient(ctx) | ||
| } | ||
| return l | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package lock | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io/fs" | ||
|
|
||
| "github.com/databricks/cli/libs/diag" | ||
| "github.com/databricks/cli/libs/locker" | ||
| "github.com/databricks/cli/libs/log" | ||
| "github.com/databricks/databricks-sdk-go" | ||
| ) | ||
|
|
||
| // workspaceFilesystemLock implements DeploymentLock using a lock file in the | ||
| // bundle's workspace state path. Holds only the primitives it needs from the | ||
| // bundle. | ||
| type workspaceFilesystemLock struct { | ||
| client *databricks.WorkspaceClient | ||
| user string | ||
| statePath string | ||
| enabled bool | ||
| force bool | ||
|
|
||
| // reportPermissionError produces the user-facing permission diagnostic | ||
| // when the workspace API returns ErrPermission/ErrNotExist from Lock. | ||
| // Lifted to a callback so this struct does not pin a *bundle.Bundle. | ||
| reportPermissionError func(ctx context.Context, path string) diag.Diagnostics | ||
|
|
||
| locker *locker.Locker | ||
| goal Goal | ||
| } | ||
|
|
||
| func (l *workspaceFilesystemLock) Acquire(ctx context.Context) error { | ||
| // Return early if locking is disabled. | ||
| if !l.enabled { | ||
| log.Infof(ctx, "Skipping; locking is disabled") | ||
| return nil | ||
| } | ||
|
|
||
| lk, err := locker.CreateLocker(l.user, l.statePath, l.client) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| l.locker = lk | ||
|
|
||
| log.Infof(ctx, "Acquiring deployment lock (force: %v)", l.force) | ||
| err = lk.Lock(ctx, l.force) | ||
| if err != nil { | ||
| log.Errorf(ctx, "Failed to acquire deployment lock: %v", err) | ||
|
|
||
| // If we get a permission or "doesn't exist" error from the API this | ||
| // indicates we either don't have permissions or the path is invalid. | ||
| if errors.Is(err, fs.ErrPermission) || errors.Is(err, fs.ErrNotExist) { | ||
| return l.reportPermissionError(ctx, l.statePath).Error() | ||
| } | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (l *workspaceFilesystemLock) Release(ctx context.Context, _ DeploymentStatus) error { | ||
| // Return early if locking is disabled. | ||
| if !l.enabled { | ||
| log.Infof(ctx, "Skipping; locking is disabled") | ||
| return nil | ||
| } | ||
|
|
||
| // Return early if the locker is not set. | ||
| // It is likely an error occurred prior to initialization of the locker instance. | ||
| if l.locker == nil { | ||
| log.Warnf(ctx, "Unable to release lock if locker is not configured") | ||
| return nil | ||
| } | ||
|
|
||
| log.Infof(ctx, "Releasing deployment lock") | ||
| if l.goal == GoalDestroy { | ||
| return l.locker.Unlock(ctx, locker.AllowLockFileNotExist) | ||
| } | ||
| return l.locker.Unlock(ctx) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
status is not used for now - but will be used with deployment metadata service.