2023-03-22 15:37:26 +00:00
package lock
import (
"context"
2023-12-28 13:15:21 +00:00
"errors"
"fmt"
2023-03-22 15:37:26 +00:00
2023-05-16 16:35:39 +00:00
"github.com/databricks/cli/bundle"
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
}
2023-05-24 12:45:19 +00:00
func ( m * acquire ) 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
}
err := m . init ( b )
if err != nil {
2023-05-24 12:45:19 +00:00
return 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
notExistsError := filer . NoSuchDirectoryError { }
if errors . As ( err , & notExistsError ) {
// If we get a "doesn't exist" error from the API this indicates
// we either don't have permissions or the path is invalid.
return fmt . Errorf ( "cannot write to deployment root (this can indicate a previous deploy was done with a different identity): %s" , b . Config . Workspace . RootPath )
}
2023-05-24 12:45:19 +00:00
return 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
}