Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 214
Fix "bundle init" when run from Databricks#1744
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
662234f8d788096585a7562b245108b6b1090d6490e51037f02745de4c9bf79694413b4cd9b1784d1bbf6bf59ff49c6ed6File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,14 @@ | ||||||||||||||
| package runtime | ||||||||||||||
| import ( | ||||||||||||||
| "context" | ||||||||||||||
| "github.com/databricks/cli/libs/env" | ||||||||||||||
| ) | ||||||||||||||
| const envDatabricksRuntimeVersion = "DATABRICKS_RUNTIME_VERSION" | ||||||||||||||
| func RunsOnDatabricks(ctx context.Context) bool { | ||||||||||||||
| value, ok := env.Lookup(ctx, envDatabricksRuntimeVersion) | ||||||||||||||
| return value != "" && ok | ||||||||||||||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need the value to be non-empty, then you don't need to check if it exists (the OK). You can use Lines 51 to 56 in a4ba0bb
| ||||||||||||||
| } | ||||||||||||||
fjakobs marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package runtime | ||
| import ( | ||
| "context" | ||
| "testing" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
| func TestRunsOnDatabricks(t *testing.T) { | ||
| ctx := context.Background() | ||
| t.Setenv("DATABRICKS_RUNTIME_VERSION", "") | ||
| assert.False(t, RunsOnDatabricks(ctx)) | ||
| t.Setenv("DATABRICKS_RUNTIME_VERSION", "14.3") | ||
| assert.True(t, RunsOnDatabricks(ctx)) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,12 +2,19 @@ package template | ||
| import ( | ||
| "context" | ||
| "encoding/base64" | ||
| "io" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "github.com/databricks/cli/cmd/root" | ||
| "github.com/databricks/cli/libs/filer" | ||
| "github.com/databricks/cli/libs/log" | ||
| "github.com/databricks/cli/libs/notebook" | ||
| "github.com/databricks/cli/libs/runtime" | ||
| "github.com/databricks/databricks-sdk-go/service/workspace" | ||
| ) | ||
| // Interface representing a file to be materialized from a template into a project | ||
| @@ -68,16 +75,20 @@ func (f *copyFile) PersistToDisk() error { | ||
| return err | ||
| } | ||
| defer srcFile.Close() | ||
| dstFile, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, f.perm) | ||
| // we read the full file into memory because we need to inspect the content | ||
| // in order to determine if it is a notebook | ||
| // Once we stop using the workspace API, we can remove this and write in a streaming fashion | ||
| content, err := io.ReadAll(srcFile) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer dstFile.Close() | ||
| _, err = io.Copy(dstFile, srcFile) | ||
| return err | ||
| return writeFile(f.ctx, path, content, f.perm) | ||
| } | ||
| type inMemoryFile struct { | ||
pietern marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ctx context.Context | ||
| dstPath *destinationPath | ||
| content []byte | ||
| @@ -97,5 +108,37 @@ func (f *inMemoryFile) PersistToDisk() error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return os.WriteFile(path, f.content, f.perm) | ||
| return writeFile(f.ctx, path, f.content, f.perm) | ||
| } | ||
| func shouldUseImportNotebook(ctx context.Context, path string, content []byte) bool { | ||
| if strings.HasPrefix(path, "/Workspace/") && runtime.RunsOnDatabricks(ctx) { | ||
| isNotebook, _, err := notebook.DetectWithContent(path, content) | ||
| if err != nil { | ||
| log.Debugf(ctx, "Error detecting notebook: %v", err) | ||
| } | ||
| return isNotebook && err == nil | ||
| } | ||
| return false | ||
| } | ||
| func writeFile(ctx context.Context, path string, content []byte, perm fs.FileMode) error { | ||
| if shouldUseImportNotebook(ctx, path, content) { | ||
| return importNotebook(ctx, path, content) | ||
| } else { | ||
fjakobs marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return os.WriteFile(path, content, perm) | ||
| } | ||
| } | ||
| func importNotebook(ctx context.Context, path string, content []byte) error { | ||
| w := root.WorkspaceClient(ctx) | ||
| return w.Workspace.Import(ctx, workspace.Import{ | ||
| Format: "AUTO", | ||
| Overwrite: false, | ||
| Path: path, | ||
| Content: base64.StdEncoding.EncodeToString(content), | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -8,6 +8,11 @@ import ( | ||
| "runtime" | ||
| "testing" | ||
| "github.com/databricks/databricks-sdk-go/experimental/mocks" | ||
| "github.com/databricks/databricks-sdk-go/service/workspace" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/databricks/cli/cmd/root" | ||
| "github.com/databricks/cli/libs/filer" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| @@ -17,6 +22,7 @@ func testInMemoryFile(t *testing.T, perm fs.FileMode) { | ||
| tmpDir := t.TempDir() | ||
| f := &inMemoryFile{ | ||
| ctx: context.Background(), | ||
| dstPath: &destinationPath{ | ||
| root: tmpDir, | ||
| relPath: "a/b/c", | ||
| @@ -109,3 +115,37 @@ func TestTemplateCopyFilePersistToDiskForWindows(t *testing.T) { | ||
| // fs.FileMode values we can use for different operating systems. | ||
| testCopyFile(t, 0666) | ||
| } | ||
| func TestShouldUseImportNotebook(t *testing.T) { | ||
| ctx := context.Background() | ||
| data := []byte("# Databricks notebook source\n print('hello')") | ||
| assert.False(t, shouldUseImportNotebook(ctx, "./foo/bar", data)) | ||
| assert.False(t, shouldUseImportNotebook(ctx, "./foo/bar.ipynb", data)) | ||
| assert.False(t, shouldUseImportNotebook(ctx, "/Workspace/foo/bar", data)) | ||
| assert.False(t, shouldUseImportNotebook(ctx, "/Workspace/foo/bar.ipynb", data)) | ||
| t.Setenv("DATABRICKS_RUNTIME_VERSION", "14.3") | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the difference? I see Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With If it broke then either 1) you weren't capturing the returned | ||
| assert.False(t, shouldUseImportNotebook(ctx, "./foo/bar", data)) | ||
| assert.False(t, shouldUseImportNotebook(ctx, "./foo/bar.ipynb", data)) | ||
| assert.False(t, shouldUseImportNotebook(ctx, "/Workspace/foo/bar", data)) | ||
| assert.True(t, shouldUseImportNotebook(ctx, "/Workspace/foo/bar.py", data)) | ||
| } | ||
| func TestImportNotebook(t *testing.T) { | ||
| ctx := context.Background() | ||
| m := mocks.NewMockWorkspaceClient(t) | ||
| ctx = root.SetWorkspaceClient(ctx, m.WorkspaceClient) | ||
| workspaceApi := m.GetMockWorkspaceAPI() | ||
| workspaceApi.EXPECT().Import(mock.Anything, workspace.Import{ | ||
| Content: "cXdlcnR5", // base64 of "qwerty" | ||
| Format: "AUTO", | ||
| Overwrite: false, | ||
| Path: "/Workspace/foo/bar.ipynb", | ||
| }).Return(nil) | ||
| err := importNotebook(ctx, "/Workspace/foo/bar.ipynb", []byte("qwerty")) | ||
| assert.NoError(t, err) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -153,12 +153,18 @@ func (r *renderer) computeFile(relPathTemplate string) (file, error) { | ||
| return nil, err | ||
| } | ||
| // we need the absolute path in case we need to write notebooks using the REST API | ||
| rootPath, err := filepath.Abs(r.instanceRoot) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
fjakobs marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // If file name does not specify the `.tmpl` extension, then it is copied | ||
| // over as is, without treating it as a template | ||
| if !strings.HasSuffix(relPathTemplate, templateExtension) { | ||
| return ©File{ | ||
| dstPath: &destinationPath{ | ||
| root: r.instanceRoot, | ||
| root: rootPath, | ||
| relPath: relPath, | ||
| }, | ||
| perm: perm, | ||
| @@ -194,8 +200,9 @@ func (r *renderer) computeFile(relPathTemplate string) (file, error) { | ||
| } | ||
| return &inMemoryFile{ | ||
| ctx: r.ctx, | ||
| dstPath: &destinationPath{ | ||
| root: r.instanceRoot, | ||
| root: rootPath, | ||
| relPath: relPath, | ||
| }, | ||
| perm: perm, | ||
| @@ -314,7 +321,7 @@ func (r *renderer) persistToDisk() error { | ||
| if err == nil { | ||
| return fmt.Errorf("failed to initialize template, one or more files already exist: %s", path) | ||
| } | ||
| if err != nil && !errors.Is(err, fs.ErrNotExist) { | ||
| if !errors.Is(err, fs.ErrNotExist) { | ||
| return fmt.Errorf("error while verifying file %s does not already exist: %w", path, err) | ||
| } | ||
| } | ||
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.
+1 for a common lib function. I'd expect this to also check for the existence of /Workspace though to avoid false positives. There may be all kinds of reasons why customers set the env var locally.
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.
I'd prefer not to test for
/Workspaceas it makes the code using it harder to testThere 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.
Yeah, testing is a pain :( We can leave it out, but then we should at least emit a debug log message whenever there's a match. There will be false positives.
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.
Testing is not an issue as long as you don't inline these expectations.
You can store a bool on the context that stores whether or not we're running on DBR (see
bundle/context.gofor inspiration). In tests, you mark it as always true or always false depending on what you want to test. For the real CLI run, you run a routine that performs the actual detection and stores it in the context.For the check itself, it can look at
/proc/mountsfor the workspace fuse mount, it can check for/.fuse-mounts, it can check for/databricks, and I'm sure there are a couple other stable ways to determine this.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.
Implemented this approach in #1889.
Besides the environment variable, it also checks for the presence of a
/databricksdirectory.