Skip to content

Commit 0a7e018

Browse files
committed
Fix template initialization when running on Databricks
1 parent 75b09ff commit 0a7e018

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

cmd/bundle/init.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bundle
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"io/fs"
@@ -11,6 +12,8 @@ import (
1112

1213
"github.com/databricks/cli/cmd/root"
1314
"github.com/databricks/cli/libs/cmdio"
15+
"github.com/databricks/cli/libs/dbr"
16+
"github.com/databricks/cli/libs/filer"
1417
"github.com/databricks/cli/libs/git"
1518
"github.com/databricks/cli/libs/template"
1619
"github.com/spf13/cobra"
@@ -147,6 +150,26 @@ func repoName(url string) string {
147150
return parts[len(parts)-1]
148151
}
149152

153+
func constructOutputFiler(ctx context.Context, outputDir string) (filer.Filer, error) {
154+
outputDir, err := filepath.Abs(outputDir)
155+
if err != nil {
156+
return nil, err
157+
}
158+
159+
// If the CLI is running on DBR and we're writing to the workspace file system,
160+
// use the extension-aware workspace filesystem filer to instantiate the template.
161+
//
162+
// It is not possible to write notebooks through the workspace filesystem's FUSE mount.
163+
// Therefore this is the only way we can initialize templates that contain notebooks
164+
// when running the CLI on DBR and initializing a template to the workspace.
165+
//
166+
if strings.HasPrefix(outputDir, "/Workspace/") && dbr.RunsOnRuntime(ctx) {
167+
return filer.NewWorkspaceFilesExtensionsClient(root.WorkspaceClient(ctx), outputDir)
168+
}
169+
170+
return filer.NewLocalClient(outputDir)
171+
}
172+
150173
func newInitCommand() *cobra.Command {
151174
cmd := &cobra.Command{
152175
Use: "init [TEMPLATE_PATH]",
@@ -201,6 +224,11 @@ See https://docs.databricks.com/en/dev-tools/bundles/templates.html for more inf
201224
templatePath = getNativeTemplateByDescription(description)
202225
}
203226

227+
outputFiler, err := constructOutputFiler(ctx, outputDir)
228+
if err != nil {
229+
return err
230+
}
231+
204232
if templatePath == customTemplate {
205233
cmdio.LogString(ctx, "Please specify a path or Git repository to use a custom template.")
206234
cmdio.LogString(ctx, "See https://docs.databricks.com/en/dev-tools/bundles/templates.html to learn more about custom templates.")
@@ -230,7 +258,7 @@ See https://docs.databricks.com/en/dev-tools/bundles/templates.html for more inf
230258

231259
// skip downloading the repo because input arg is not a URL. We assume
232260
// it's a path on the local file system in that case
233-
return template.Materialize(ctx, configFile, templateFS, outputDir)
261+
return template.Materialize(ctx, configFile, templateFS, outputFiler)
234262
}
235263

236264
// Create a temporary directory with the name of the repository. The '*'
@@ -255,7 +283,7 @@ See https://docs.databricks.com/en/dev-tools/bundles/templates.html for more inf
255283
// Clean up downloaded repository once the template is materialized.
256284
defer os.RemoveAll(repoDir)
257285
templateFS := os.DirFS(filepath.Join(repoDir, templateDir))
258-
return template.Materialize(ctx, configFile, templateFS, outputDir)
286+
return template.Materialize(ctx, configFile, templateFS, outputFiler)
259287
}
260288
return cmd
261289
}

internal/bundle/helpers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/databricks/cli/internal"
1717
"github.com/databricks/cli/libs/cmdio"
1818
"github.com/databricks/cli/libs/env"
19+
"github.com/databricks/cli/libs/filer"
1920
"github.com/databricks/cli/libs/flags"
2021
"github.com/databricks/cli/libs/template"
2122
"github.com/databricks/cli/libs/vfs"
@@ -42,7 +43,9 @@ func initTestTemplateWithBundleRoot(t *testing.T, ctx context.Context, templateN
4243
cmd := cmdio.NewIO(flags.OutputJSON, strings.NewReader(""), os.Stdout, os.Stderr, "", "bundles")
4344
ctx = cmdio.InContext(ctx, cmd)
4445

45-
err = template.Materialize(ctx, configFilePath, os.DirFS(templateRoot), bundleRoot)
46+
out, err := filer.NewLocalClient(bundleRoot)
47+
require.NoError(t, err)
48+
err = template.Materialize(ctx, configFilePath, os.DirFS(templateRoot), out)
4649
return bundleRoot, err
4750
}
4851

libs/template/materialize.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const schemaFileName = "databricks_template_schema.json"
2121
// ctx: context containing a cmdio object. This is used to prompt the user
2222
// configFilePath: file path containing user defined config values
2323
// templateFS: root of the template definition
24-
// outputDir: root of directory where to initialize the template
25-
func Materialize(ctx context.Context, configFilePath string, templateFS fs.FS, outputDir string) error {
24+
// outputFiler: filer to use for writing the initialized template
25+
func Materialize(ctx context.Context, configFilePath string, templateFS fs.FS, outputFiler filer.Filer) error {
2626
if _, err := fs.Stat(templateFS, schemaFileName); errors.Is(err, fs.ErrNotExist) {
2727
return fmt.Errorf("not a bundle template: expected to find a template schema file at %s", schemaFileName)
2828
}
@@ -73,12 +73,7 @@ func Materialize(ctx context.Context, configFilePath string, templateFS fs.FS, o
7373
return err
7474
}
7575

76-
out, err := filer.NewLocalClient(outputDir)
77-
if err != nil {
78-
return err
79-
}
80-
81-
err = r.persistToDisk(ctx, out)
76+
err = r.persistToDisk(ctx, outputFiler)
8277
if err != nil {
8378
return err
8479
}

libs/template/materialize_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ func TestMaterializeForNonTemplateDirectory(t *testing.T) {
1919
ctx := root.SetWorkspaceClient(context.Background(), w)
2020

2121
// Try to materialize a non-template directory.
22-
err = Materialize(ctx, "", os.DirFS(tmpDir), "")
22+
err = Materialize(ctx, "", os.DirFS(tmpDir), nil)
2323
assert.EqualError(t, err, fmt.Sprintf("not a bundle template: expected to find a template schema file at %s", schemaFileName))
2424
}

0 commit comments

Comments
 (0)