Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nextchanges/bundles/bundle-file-origin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Tag the files and libraries that bundle commands upload to the workspace as deployment copies, so the workspace can tell them apart from a bundle's source files. ([#7893](https://github.com/databricks/cli/pull/7893))
1 change: 1 addition & 0 deletions acceptance/bundle/apps/app_yaml/out.app.yml.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"method": "POST",
"path": "/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/app/app.yml",
"q": {
"bundleFileOrigin": "DEPLOYMENT_COPY",
"overwrite": "true"
},
"raw_body": "command:\n - python\n - app.py\n"
Expand Down
2 changes: 2 additions & 0 deletions acceptance/bundle/deploy/readplan/whl-upload/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged
"method": "POST",
"path": "/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/whl-upload/default/artifacts/.internal/my_test_code-0.0.1+[UNIX_TIME_NANOS]-py3-none-any.whl",
"q": {
"bundleFileOrigin": "DEPLOYMENT_COPY",
"overwrite": "true"
}
}
Expand All @@ -39,6 +40,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged
"method": "POST",
"path": "/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/whl-upload/default/artifacts/.internal/my_test_code-0.0.1+[UNIX_TIME_NANOS]-py3-none-any.whl",
"q": {
"bundleFileOrigin": "DEPLOYMENT_COPY",
"overwrite": "true"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Resources: 2 created, 0 changed, 0 deleted, 0 unchanged
"method": "POST",
"path": "/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/outside_of_bundle_root/default/artifacts/.internal/test.whl",
"q": {
"bundleFileOrigin": "DEPLOYMENT_COPY",
"overwrite": "true"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ GET /api/2.0/workspace/get-status path=/Workspace/Users/[USERNAME]/.bundle/reque
GET /api/2.0/workspace/get-status path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock return_export_info=true
GET /api/2.0/workspace/get-status path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock return_export_info=true
GET /api/2.0/workspace/get-status path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock return_export_info=true
POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/request-summary/default/files/databricks.yml overwrite=true
POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/request-summary/default/files/databricks.yml bundleFileOrigin=DEPLOYMENT_COPY overwrite=true
POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock overwrite=false
POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock overwrite=false
POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deployment.json overwrite=true
Expand Down
2 changes: 2 additions & 0 deletions acceptance/bundle/sync/bundle_file_origin/databricks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bundle:
name: test-bundle
1 change: 1 addition & 0 deletions acceptance/bundle/sync/bundle_file_origin/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("hello")
2 changes: 2 additions & 0 deletions acceptance/bundle/sync/bundle_file_origin/out.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cloud = false
EnvMatrix.DMS = ["", "true"]
36 changes: 36 additions & 0 deletions acceptance/bundle/sync/bundle_file_origin/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

=== Deploy tags the bundle files it uploads, not its state files

>>> [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
Files: 2 uploaded, 0 deleted
Resources: 0 created, 0 changed, 0 deleted, 0 unchanged

>>> print_requests.py --summary --sort //default/files/ //state/metadata.json
POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/databricks.yml bundleFileOrigin=DEPLOYMENT_COPY overwrite=true
POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/hello.py bundleFileOrigin=DEPLOYMENT_COPY overwrite=true
POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/metadata.json overwrite=true

=== Bundle sync writes to the same file_path, so it tags them too

>>> [CLI] bundle sync --full
Action: PUT: databricks.yml, hello.py
Initial Sync Complete
Uploaded databricks.yml
Uploaded hello.py

>>> print_requests.py --summary --sort //api/2.0/workspace-files/import-file
POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/databricks.yml bundleFileOrigin=DEPLOYMENT_COPY overwrite=true
POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/hello.py bundleFileOrigin=DEPLOYMENT_COPY overwrite=true

=== A plain sync does not tag its uploads

>>> MSYS_NO_PATHCONV=1 [CLI] sync . /Users/[USERNAME]/plain-sync
Action: PUT: databricks.yml, hello.py
Initial Sync Complete
Uploaded databricks.yml
Uploaded hello.py

>>> print_requests.py --summary --sort //api/2.0/workspace-files/import-file
POST /api/2.0/workspace-files/import-file/Users/[USERNAME]/plain-sync/databricks.yml overwrite=true
POST /api/2.0/workspace-files/import-file/Users/[USERNAME]/plain-sync/hello.py overwrite=true
13 changes: 13 additions & 0 deletions acceptance/bundle/sync/bundle_file_origin/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
title "Deploy tags the bundle files it uploads, not its state files"
trace $CLI bundle deploy
# Deployment history (DMS=true) also uploads resources.json, so only metadata.json stands for the state files.
trace print_requests.py --summary --sort //default/files/ //state/metadata.json

title "Bundle sync writes to the same file_path, so it tags them too"
trace $CLI bundle sync --full | sort
trace print_requests.py --summary --sort //api/2.0/workspace-files/import-file

title "A plain sync does not tag its uploads"
# Prevent Git Bash on Windows from converting the /Users/... sync target into a Windows path.
trace MSYS_NO_PATHCONV=1 $CLI sync . /Users/$CURRENT_USER_NAME/plain-sync | sort
trace print_requests.py --summary --sort //api/2.0/workspace-files/import-file
1 change: 1 addition & 0 deletions acceptance/bundle/sync/bundle_file_origin/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RecordRequests = true
6 changes: 5 additions & 1 deletion acceptance/bundle/sync/output.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@

>>> [CLI] bundle sync --output text
Action: PUT: .gitignore, databricks.yml, dryrun/databricks.yml, dryrun/out.test.toml, ignored-folder/folder1/script.py, ignored-folder/script.py, replace-node-type/bom.sql, replace-node-type/databricks.yml.tmpl, replace-node-type/out.test.toml, replace-node-type/plain.sql, upload-504/databricks.yml, upload-504/out.test.toml, zip-extract/archive.zip, zip-extract/archive_v2.zip, zip-extract/databricks.yml.tmpl, zip-extract/out.test.toml
Action: PUT: .gitignore, bundle_file_origin/databricks.yml, bundle_file_origin/hello.py, bundle_file_origin/out.test.toml, databricks.yml, dryrun/databricks.yml, dryrun/out.test.toml, ignored-folder/folder1/script.py, ignored-folder/script.py, replace-node-type/bom.sql, replace-node-type/databricks.yml.tmpl, replace-node-type/out.test.toml, replace-node-type/plain.sql, upload-504/databricks.yml, upload-504/out.test.toml, zip-extract/archive.zip, zip-extract/archive_v2.zip, zip-extract/databricks.yml.tmpl, zip-extract/out.test.toml
Initial Sync Complete
Uploaded .gitignore
Uploaded bundle_file_origin
Uploaded bundle_file_origin/databricks.yml
Uploaded bundle_file_origin/hello.py
Uploaded bundle_file_origin/out.test.toml
Uploaded databricks.yml
Uploaded dryrun
Uploaded dryrun/databricks.yml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Warn: retrying after 500 Internal Server Error from POST /api/2.0/workspace-file
Warn: retrying after 500 Internal Server Error from POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/file_to_upload.txt
Error: Fault injected by test. (500 INJECTED)

Endpoint: POST [DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true
Endpoint: POST [DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true&bundleFileOrigin=DEPLOYMENT_COPY
HTTP Status: 500 Internal Server Error
API error_code: INJECTED
API message: Fault injected by test.
Expand Down
6 changes: 3 additions & 3 deletions acceptance/bundle/upload/timeout/output.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
Warn: retrying after Post "[DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true": request timed out after 5s of inactivity
Warn: retrying after Post "[DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true": request timed out after 5s of inactivity
Error: Post "[DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true": request timed out after 5s of inactivity
Warn: retrying after Post "[DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true&bundleFileOrigin=DEPLOYMENT_COPY": request timed out after 5s of inactivity
Warn: retrying after Post "[DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true&bundleFileOrigin=DEPLOYMENT_COPY": request timed out after 5s of inactivity
Error: Post "[DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true&bundleFileOrigin=DEPLOYMENT_COPY": request timed out after 5s of inactivity

/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/file_to_upload.txt
/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/file_to_upload.txt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
"method": "POST",
"path": "/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/empty.py",
"q": {
"bundleFileOrigin": "DEPLOYMENT_COPY",
"overwrite": "true"
}
}
Expand Down
1 change: 1 addition & 0 deletions acceptance/bundle/validate/sync_patterns/out.sync.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"method": "POST",
"path": "/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/conf/dir/test.yml",
"q": {
"bundleFileOrigin": "DEPLOYMENT_COPY",
"overwrite": "true"
}
}
21 changes: 21 additions & 0 deletions bundle/deploy/files/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package files
import (
"context"
"fmt"
"path"
"strings"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/dbr"
"github.com/databricks/cli/libs/sync"
)

Expand Down Expand Up @@ -55,6 +58,8 @@ func GetSyncOptions(ctx context.Context, b *bundle.Bundle) (*sync.SyncOptions, e

SnapshotBasePath: cacheDir,
WorkspaceClient: b.WorkspaceClient(ctx),

DeploymentCopy: !writesOntoSource(ctx, b),
}

if b.Config.Workspace.CurrentUser != nil {
Expand All @@ -63,3 +68,19 @@ func GetSyncOptions(ctx context.Context, b *bundle.Bundle) (*sync.SyncOptions, e

return opts, nil
}

// writesOntoSource reports whether file_path is the sync root itself, as in a
// deploy from a workspace folder that points file_path at that folder. Those
// files are the bundle's source, not a deployment copy.
func writesOntoSource(ctx context.Context, b *bundle.Bundle) bool {
if !dbr.HasDetection(ctx) || !dbr.RunsOnRuntime(ctx) {
return false
}
// A file_path set from a variable can still lack the /Workspace prefix,
// because PrependWorkspacePrefix runs before variables are resolved.
filePath := b.Config.Workspace.FilePath
if !strings.HasPrefix(filePath, "/Workspace/") {
filePath = path.Join("/Workspace", filePath)
}
return path.Clean(filePath) == path.Clean(b.SyncRootPath)
}
64 changes: 64 additions & 0 deletions bundle/deploy/files/sync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package files

import (
"testing"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/libs/dbr"
"github.com/stretchr/testify/assert"
)

func TestWritesOntoSource(t *testing.T) {
for _, tc := range []struct {
name string
isDbr bool
syncRootPath string
filePath string
expect bool
}{
{
name: "file_path is the workspace sync root",
isDbr: true,
syncRootPath: "/Workspace/Users/me/my_bundle",
filePath: "/Workspace/Users/me/my_bundle/",
expect: true,
},
{
name: "file_path from a variable without the /Workspace prefix",
isDbr: true,
syncRootPath: "/Workspace/Users/me/my_bundle",
filePath: "/Users/me/my_bundle",
expect: true,
},
{
name: "default file_path from the workspace",
isDbr: true,
syncRootPath: "/Workspace/Users/me/my_bundle",
filePath: "/Workspace/Users/me/.bundle/my_bundle/default/files",
expect: false,
},
{
name: "same path outside the runtime",
isDbr: false,
syncRootPath: "/Workspace/Users/me/my_bundle",
filePath: "/Workspace/Users/me/my_bundle",
expect: false,
},
} {
t.Run(tc.name, func(t *testing.T) {
ctx := dbr.MockRuntime(t.Context(), dbr.Environment{IsDbr: tc.isDbr})
b := &bundle.Bundle{
SyncRootPath: tc.syncRootPath,
Config: config.Root{
Workspace: config.Workspace{FilePath: tc.filePath},
},
}
assert.Equal(t, tc.expect, writesOntoSource(ctx, b))
})
}

t.Run("no runtime detection", func(t *testing.T) {
assert.False(t, writesOntoSource(t.Context(), &bundle.Bundle{}))
})
}
2 changes: 1 addition & 1 deletion bundle/libraries/filer_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import (
)

func filerForWorkspace(ctx context.Context, b *bundle.Bundle, uploadPath string) (filer.Filer, string, diag.Diagnostics) {
f, err := filer.NewWorkspaceFilesClient(b.WorkspaceClient(ctx), uploadPath)
f, err := filer.NewWorkspaceFilesClientForBundleDeploy(b.WorkspaceClient(ctx), uploadPath)
return f, uploadPath, diag.FromErr(err)
}
20 changes: 20 additions & 0 deletions libs/filer/workspace_files_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ type WorkspaceFilesClient struct {

// File operations will be relative to this path.
root WorkspaceRootPath

// Tag every upload as written by a bundle deployment, so the workspace
// does not treat a deployed databricks.yml as a bundle's source.
deploymentCopy bool
}

// workspaceIDHeaders returns the workspace routing header map for outbound
Expand All @@ -137,6 +141,16 @@ func (w *WorkspaceFilesClient) workspaceIDHeaders() map[string]string {
}

func NewWorkspaceFilesClient(w *databricks.WorkspaceClient, root string) (Filer, error) {
return newWorkspaceFilesClient(w, root, false)
}

// NewWorkspaceFilesClientForBundleDeploy returns a client that tags every
// upload as a bundle deployment copy.
func NewWorkspaceFilesClientForBundleDeploy(w *databricks.WorkspaceClient, root string) (Filer, error) {
return newWorkspaceFilesClient(w, root, true)
}

func newWorkspaceFilesClient(w *databricks.WorkspaceClient, root string, deploymentCopy bool) (Filer, error) {
apiClient, err := client.New(w.Config)
if err != nil {
return nil, err
Expand All @@ -147,6 +161,8 @@ func NewWorkspaceFilesClient(w *databricks.WorkspaceClient, root string) (Filer,
apiClient: apiClient,

root: NewWorkspaceRootPath(root),

deploymentCopy: deploymentCopy,
}, nil
}

Expand All @@ -163,6 +179,10 @@ func (w *WorkspaceFilesClient) Write(ctx context.Context, name string, reader io
url.PathEscape(strings.TrimLeft(absPath, "/")),
overwrite,
)
// Workspaces that do not know the parameter ignore it.
if w.deploymentCopy {
urlPath += "&bundleFileOrigin=DEPLOYMENT_COPY"
}

// Buffer the file contents because we may need to retry below and we cannot read twice.
body, err := io.ReadAll(reader)
Expand Down
43 changes: 43 additions & 0 deletions libs/filer/workspace_files_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package filer
import (
"encoding/json"
"io/fs"
"net/url"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -181,3 +183,44 @@ func TestWorkspaceFilesClientStatNotFound(t *testing.T) {
err := statWithError(t, 404, "RESOURCE_DOES_NOT_EXIST")
assert.ErrorIs(t, err, fs.ErrNotExist)
}

func TestWorkspaceFilesClientWriteBundleFileOrigin(t *testing.T) {
for _, tc := range []struct {
name string
newFn func(*databricks.WorkspaceClient, string) (Filer, error)
expect url.Values
}{
{
name: "default",
newFn: NewWorkspaceFilesClient,
expect: url.Values{"overwrite": {"true"}},
},
{
name: "bundle deploy",
newFn: NewWorkspaceFilesClientForBundleDeploy,
expect: url.Values{"overwrite": {"true"}, "bundleFileOrigin": {"DEPLOYMENT_COPY"}},
},
} {
t.Run(tc.name, func(t *testing.T) {
var query url.Values
server := testserver.New(t)
server.Handle("POST", "/api/2.0/workspace-files/import-file/{path...}", func(req testserver.Request) any {
query = req.URL.Query()
return ""
})

client, err := databricks.NewWorkspaceClient(&databricks.Config{
Host: server.URL,
Token: "testtoken",
})
require.NoError(t, err)

f, err := tc.newFn(client, "/test")
require.NoError(t, err)

err = f.Write(t.Context(), "databricks.yml", strings.NewReader("bundle: {}"), OverwriteIfExists)
require.NoError(t, err)
assert.Equal(t, tc.expect, query)
})
}
}
9 changes: 8 additions & 1 deletion libs/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type SyncOptions struct {
DryRun bool

NoValidateRemotePath bool

// Tag uploads as bundle deployment copies.
DeploymentCopy bool
}

type Sync struct {
Expand Down Expand Up @@ -115,7 +118,11 @@ func New(ctx context.Context, opts SyncOptions) (*Sync, error) {
}
}

workspaceFiler, err := filer.NewWorkspaceFilesClient(opts.WorkspaceClient, opts.RemotePath)
newWorkspaceFiler := filer.NewWorkspaceFilesClient
if opts.DeploymentCopy {
newWorkspaceFiler = filer.NewWorkspaceFilesClientForBundleDeploy
}
workspaceFiler, err := newWorkspaceFiler(opts.WorkspaceClient, opts.RemotePath)
if err != nil {
return nil, err
}
Expand Down
Loading