Skip to content
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

Fix "bundle init" when run from Databricks #1744

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Conversation

fjakobs
Copy link
Contributor

@fjakobs fjakobs commented Sep 3, 2024

Changes

When started from a Databricks cluster use import API when creating notebooks from bundle init.

Tests

Manually tested by executing databricks bundle init from a web terminal on a Databricks cluster.

libs/template/file.go Outdated Show resolved Hide resolved
libs/template/materialize.go Outdated Show resolved Hide resolved
libs/template/file.go Outdated Show resolved Hide resolved
Copy link
Contributor

@shreyas-goenka shreyas-goenka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This also needs some unit tests. You could use a mocked workspace client and set it in the context for the unit test. You can use t.Setenv to set the DATABRICKS_RUNTIME_VERSION env var for the tests.

See TestResolveClusterReference for a reference of a mocked workplace client.

libs/template/file.go Outdated Show resolved Hide resolved
libs/template/file.go Outdated Show resolved Hide resolved
libs/template/file.go Outdated Show resolved Hide resolved
libs/template/file.go Outdated Show resolved Hide resolved
libs/template/file.go Outdated Show resolved Hide resolved
libs/template/file.go Outdated Show resolved Hide resolved
libs/template/materialize.go Outdated Show resolved Hide resolved
libs/template/file.go Outdated Show resolved Hide resolved
libs/template/file.go Outdated Show resolved Hide resolved
libs/template/file.go Outdated Show resolved Hide resolved
libs/template/file.go Outdated Show resolved Hide resolved
libs/template/file.go Outdated Show resolved Hide resolved
libs/template/file.go Outdated Show resolved Hide resolved
Copy link
Contributor

@lennartkats-db lennartkats-db left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking at this! This was much less involved than I had expected. Just one remaining comment + one nit.


const envDatabricksRuntimeVersion = "DATABRICKS_RUNTIME_VERSION"

func RunsOnDatabricks(ctx context.Context) bool {
Copy link
Contributor

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.

Copy link
Contributor Author

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 /Workspace as it makes the code using it harder to test

@@ -313,8 +319,7 @@ func (r *renderer) persistToDisk() error {
_, err := os.Stat(path)
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) {
} else if !errors.Is(err, fs.ErrNotExist) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: isn't this easier to read without the else if?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it is

Copy link
Contributor

@shreyas-goenka shreyas-goenka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good to me other than one comment. Please TAL!

libs/template/file.go Outdated Show resolved Hide resolved
if strings.HasPrefix(path, "/Workspace/") && runtime.RunsOnDatabricks(ctx) {
isNotebook, _, _ := notebook.DetectWithContent(path, content)
return isNotebook
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style nit: remove else block

libs/template/file.go Show resolved Hide resolved
libs/runtime/detect.go Show resolved Hide resolved
libs/template/file.go Show resolved Hide resolved
Copy link
Contributor

@pietern pietern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple small comments remaining.

FWIW, in hindsight it would have been neater to use a filer for writing everything. Then we could have swapped out the writing filer for the extension-aware workspace filer and things would have worked transparently (and all writes would use the API as opposed to only notebooks).

libs/notebook/detect.go Outdated Show resolved Hide resolved
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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use env.Set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference? I see t.Setenv used in tests. If I change it to env.Set then my tests break.

libs/template/renderer.go Show resolved Hide resolved
@fjakobs fjakobs added this pull request to the merge queue Sep 17, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants