2023-08-07 13:14:25 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
2024-11-20 11:42:23 +00:00
|
|
|
"context"
|
2023-08-25 09:03:42 +00:00
|
|
|
"errors"
|
2023-11-28 09:04:06 +00:00
|
|
|
"fmt"
|
2023-08-07 13:14:25 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2023-08-25 09:03:42 +00:00
|
|
|
"github.com/databricks/cli/cmd/root"
|
2023-09-05 13:57:01 +00:00
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2024-11-20 11:42:23 +00:00
|
|
|
"github.com/databricks/cli/libs/dbr"
|
|
|
|
"github.com/databricks/cli/libs/filer"
|
2024-12-30 06:42:05 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
2024-12-27 07:57:24 +00:00
|
|
|
"github.com/databricks/cli/libs/telemetry"
|
2023-08-07 13:14:25 +00:00
|
|
|
"github.com/databricks/cli/libs/template"
|
2024-12-27 07:57:24 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/client"
|
2023-08-07 13:14:25 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2024-11-20 11:42:23 +00:00
|
|
|
func constructOutputFiler(ctx context.Context, outputDir string) (filer.Filer, error) {
|
|
|
|
outputDir, err := filepath.Abs(outputDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the CLI is running on DBR and we're writing to the workspace file system,
|
|
|
|
// use the extension-aware workspace filesystem filer to instantiate the template.
|
|
|
|
//
|
|
|
|
// It is not possible to write notebooks through the workspace filesystem's FUSE mount.
|
|
|
|
// Therefore this is the only way we can initialize templates that contain notebooks
|
|
|
|
// when running the CLI on DBR and initializing a template to the workspace.
|
|
|
|
//
|
|
|
|
if strings.HasPrefix(outputDir, "/Workspace/") && dbr.RunsOnRuntime(ctx) {
|
|
|
|
return filer.NewWorkspaceFilesExtensionsClient(root.WorkspaceClient(ctx), outputDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
return filer.NewLocalClient(outputDir)
|
|
|
|
}
|
|
|
|
|
2023-08-07 13:14:25 +00:00
|
|
|
func newInitCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2023-08-25 09:03:42 +00:00
|
|
|
Use: "init [TEMPLATE_PATH]",
|
2023-10-19 07:08:36 +00:00
|
|
|
Short: "Initialize using a bundle template",
|
2024-03-12 14:12:34 +00:00
|
|
|
Args: root.MaximumNArgs(1),
|
2023-11-28 09:04:06 +00:00
|
|
|
Long: fmt.Sprintf(`Initialize using a bundle template.
|
2023-10-19 07:08:36 +00:00
|
|
|
|
|
|
|
TEMPLATE_PATH optionally specifies which template to use. It can be one of the following:
|
2023-11-28 09:04:06 +00:00
|
|
|
%s
|
2023-10-19 07:08:36 +00:00
|
|
|
- a local file system path with a template directory
|
|
|
|
- a Git repository URL, e.g. https://github.com/my/repository
|
|
|
|
|
2025-01-03 10:43:53 +00:00
|
|
|
See https://docs.databricks.com/en/dev-tools/bundles/templates.html for more information on templates.`, template.HelpDescriptions()),
|
2023-08-07 13:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var configFile string
|
2023-08-17 20:32:30 +00:00
|
|
|
var outputDir string
|
2023-08-18 09:29:48 +00:00
|
|
|
var templateDir string
|
2023-11-14 22:27:58 +00:00
|
|
|
var tag string
|
|
|
|
var branch string
|
2024-08-05 12:24:22 +00:00
|
|
|
cmd.Flags().StringVar(&configFile, "config-file", "", "JSON file containing key value pairs of input parameters required for template initialization.")
|
2023-10-19 07:08:36 +00:00
|
|
|
cmd.Flags().StringVar(&templateDir, "template-dir", "", "Directory path within a Git repository containing the template.")
|
2023-08-17 20:32:30 +00:00
|
|
|
cmd.Flags().StringVar(&outputDir, "output-dir", "", "Directory to write the initialized template to.")
|
2023-11-14 22:27:58 +00:00
|
|
|
cmd.Flags().StringVar(&branch, "tag", "", "Git tag to use for template initialization")
|
|
|
|
cmd.Flags().StringVar(&tag, "branch", "", "Git branch to use for template initialization")
|
2023-08-07 13:14:25 +00:00
|
|
|
|
2024-12-27 07:57:24 +00:00
|
|
|
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
|
|
|
|
// Configure the logger to send telemetry to Databricks.
|
2025-01-03 06:01:22 +00:00
|
|
|
ctx := telemetry.WithDefaultLogger(cmd.Context())
|
2024-12-27 07:57:24 +00:00
|
|
|
cmd.SetContext(ctx)
|
|
|
|
|
|
|
|
return root.MustWorkspaceClient(cmd, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.PostRun = func(cmd *cobra.Command, args []string) {
|
|
|
|
ctx := cmd.Context()
|
|
|
|
w := root.WorkspaceClient(ctx)
|
|
|
|
apiClient, err := client.New(w.Config)
|
|
|
|
if err != nil {
|
|
|
|
// Uploading telemetry is best effort. Do not error.
|
2024-12-30 06:42:05 +00:00
|
|
|
log.Debugf(ctx, "Could not create API client to send telemetry using: %v", err)
|
2024-12-27 07:57:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
telemetry.Flush(cmd.Context(), apiClient)
|
|
|
|
}
|
|
|
|
|
2023-08-07 13:14:25 +00:00
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
2023-11-14 22:27:58 +00:00
|
|
|
if tag != "" && branch != "" {
|
|
|
|
return errors.New("only one of --tag or --branch can be specified")
|
|
|
|
}
|
2023-10-19 07:08:36 +00:00
|
|
|
|
2023-11-14 22:27:58 +00:00
|
|
|
// Git ref to use for template initialization
|
|
|
|
ref := branch
|
|
|
|
if tag != "" {
|
|
|
|
ref = tag
|
|
|
|
}
|
|
|
|
|
2025-01-03 10:43:53 +00:00
|
|
|
var tmpl *template.Template
|
|
|
|
var err error
|
2023-11-14 22:27:58 +00:00
|
|
|
ctx := cmd.Context()
|
2025-01-03 10:43:53 +00:00
|
|
|
|
2023-08-25 09:03:42 +00:00
|
|
|
if len(args) > 0 {
|
2025-01-03 10:43:53 +00:00
|
|
|
// User already specified a template local path or a Git URL. Use that
|
|
|
|
// information to configure a reader for the template
|
|
|
|
tmpl = template.Get(template.Custom)
|
|
|
|
// TODO: Get rid of the name arg.
|
|
|
|
if template.IsGitRepoUrl(args[0]) {
|
|
|
|
tmpl.SetReader(template.NewGitReader("", args[0], ref, templateDir))
|
|
|
|
} else {
|
|
|
|
tmpl.SetReader(template.NewLocalReader("", args[0]))
|
|
|
|
}
|
2023-08-25 09:03:42 +00:00
|
|
|
} else {
|
2025-01-03 10:43:53 +00:00
|
|
|
tmplId, err := template.PromptForTemplateId(cmd.Context(), ref, templateDir)
|
|
|
|
if tmplId == template.Custom {
|
|
|
|
// If a user selects custom during the prompt, ask them to provide a path or Git URL
|
|
|
|
// as a positional argument.
|
|
|
|
cmdio.LogString(ctx, "Please specify a path or Git repository to use a custom template.")
|
|
|
|
cmdio.LogString(ctx, "See https://docs.databricks.com/en/dev-tools/bundles/templates.html to learn more about custom templates.")
|
|
|
|
return nil
|
2023-08-25 09:03:42 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-27 12:03:08 +00:00
|
|
|
|
2025-01-03 10:43:53 +00:00
|
|
|
tmpl = template.Get(tmplId)
|
2024-11-20 11:42:23 +00:00
|
|
|
}
|
|
|
|
|
2025-01-03 10:43:53 +00:00
|
|
|
defer tmpl.Reader.Close()
|
2023-08-07 13:14:25 +00:00
|
|
|
|
2025-01-03 10:43:53 +00:00
|
|
|
outputFiler, err := constructOutputFiler(ctx, outputDir)
|
2023-08-07 13:14:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-08 12:52:53 +00:00
|
|
|
|
2025-01-03 10:43:53 +00:00
|
|
|
tmpl.Writer.Initialize(tmpl.Reader, configFile, outputFiler)
|
2024-02-08 12:52:53 +00:00
|
|
|
|
2025-01-03 10:43:53 +00:00
|
|
|
err = tmpl.Writer.Materialize(ctx)
|
2023-08-07 13:14:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-08 12:52:53 +00:00
|
|
|
|
2025-01-03 10:43:53 +00:00
|
|
|
return tmpl.Writer.LogTelemetry(ctx)
|
2023-08-07 13:14:25 +00:00
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|