2022-12-15 14:12:47 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
2023-03-21 15:25:18 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/deploy/terraform"
|
|
|
|
"github.com/databricks/cli/bundle/phases"
|
|
|
|
"github.com/databricks/cli/bundle/run"
|
2024-10-22 14:59:17 +00:00
|
|
|
"github.com/databricks/cli/bundle/run/output"
|
2024-02-14 18:04:45 +00:00
|
|
|
"github.com/databricks/cli/cmd/bundle/utils"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/cmd/root"
|
2023-09-11 18:03:12 +00:00
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/flags"
|
2022-12-15 14:12:47 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
func newRunCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2024-03-28 10:32:34 +00:00
|
|
|
Use: "run [flags] KEY",
|
2024-04-22 11:50:13 +00:00
|
|
|
Short: "Run a job or pipeline update",
|
|
|
|
Long: `Run the job or pipeline identified by KEY.
|
|
|
|
|
|
|
|
The KEY is the unique identifier of the resource to run. In addition to
|
|
|
|
customizing the run using any of the available flags, you can also specify
|
|
|
|
keyword or positional arguments as shown in these examples:
|
|
|
|
|
|
|
|
databricks bundle run my_job -- --key1 value1 --key2 value2
|
|
|
|
|
|
|
|
Or:
|
|
|
|
|
|
|
|
databricks bundle run my_job -- value1 value2 value3
|
|
|
|
|
|
|
|
If the specified job uses job parameters or the job has a notebook task with
|
|
|
|
parameters, the first example applies and flag names are mapped to the
|
|
|
|
parameter names.
|
|
|
|
|
|
|
|
If the specified job does not use job parameters and the job has a Python file
|
|
|
|
task or a Python wheel task, the second example applies.
|
|
|
|
`,
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
2022-12-15 14:12:47 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
var runOptions run.Options
|
2024-02-06 14:51:02 +00:00
|
|
|
runOptions.Define(cmd)
|
2023-07-27 10:03:08 +00:00
|
|
|
|
|
|
|
var noWait bool
|
2024-02-09 14:33:14 +00:00
|
|
|
var restart bool
|
2023-07-27 10:03:08 +00:00
|
|
|
cmd.Flags().BoolVar(&noWait, "no-wait", false, "Don't wait for the run to complete.")
|
2024-02-09 14:33:14 +00:00
|
|
|
cmd.Flags().BoolVar(&restart, "restart", false, "Restart the run if it is already running.")
|
2023-07-27 10:03:08 +00:00
|
|
|
|
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
2023-09-11 18:03:12 +00:00
|
|
|
ctx := cmd.Context()
|
2024-03-28 10:32:34 +00:00
|
|
|
b, diags := utils.ConfigureBundleWithVariables(cmd)
|
|
|
|
if err := diags.Error(); err != nil {
|
|
|
|
return diags.Error()
|
|
|
|
}
|
2023-07-12 06:51:54 +00:00
|
|
|
|
2024-09-21 06:36:47 +00:00
|
|
|
diags = bundle.Apply(ctx, b, phases.Initialize())
|
2024-03-25 14:18:47 +00:00
|
|
|
if err := diags.Error(); err != nil {
|
2022-12-15 14:12:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-11 18:03:12 +00:00
|
|
|
// If no arguments are specified, prompt the user to select something to run.
|
2023-12-18 15:01:59 +00:00
|
|
|
if len(args) == 0 && cmdio.IsPromptSupported(ctx) {
|
2023-09-11 18:03:12 +00:00
|
|
|
// Invert completions from KEY -> NAME, to NAME -> KEY.
|
|
|
|
inv := make(map[string]string)
|
|
|
|
for k, v := range run.ResourceCompletionMap(b) {
|
|
|
|
inv[v] = k
|
|
|
|
}
|
|
|
|
id, err := cmdio.Select(ctx, inv, "Resource to run")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args = append(args, id)
|
|
|
|
}
|
|
|
|
|
2024-04-22 11:50:13 +00:00
|
|
|
if len(args) < 1 {
|
2023-09-11 18:03:12 +00:00
|
|
|
return fmt.Errorf("expected a KEY of the resource to run")
|
|
|
|
}
|
|
|
|
|
2024-09-21 06:36:47 +00:00
|
|
|
diags = bundle.Apply(ctx, b, bundle.Seq(
|
|
|
|
terraform.Interpolate(),
|
|
|
|
terraform.Write(),
|
|
|
|
terraform.StatePull(),
|
|
|
|
terraform.Load(terraform.ErrorOnEmptyState),
|
|
|
|
))
|
|
|
|
if err := diags.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:19:38 +00:00
|
|
|
runner, err := run.Find(b, args[0])
|
2022-12-15 14:12:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-22 11:50:13 +00:00
|
|
|
// Parse additional positional arguments.
|
|
|
|
err = runner.ParseArgs(args[1:], &runOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-12 06:51:54 +00:00
|
|
|
runOptions.NoWait = noWait
|
2024-10-22 14:59:17 +00:00
|
|
|
var output output.RunOutput
|
2024-02-09 14:33:14 +00:00
|
|
|
if restart {
|
2024-10-22 14:59:17 +00:00
|
|
|
output, err = runner.Restart(ctx, &runOptions)
|
|
|
|
} else {
|
|
|
|
output, err = runner.Run(ctx, &runOptions)
|
2024-02-09 14:33:14 +00:00
|
|
|
}
|
2022-12-22 15:19:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|
2024-10-22 14:59:17 +00:00
|
|
|
|
2023-03-21 15:25:18 +00:00
|
|
|
if output != nil {
|
2023-07-26 11:17:09 +00:00
|
|
|
switch root.OutputType(cmd) {
|
2023-03-21 15:25:18 +00:00
|
|
|
case flags.OutputText:
|
|
|
|
resultString, err := output.String()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.OutOrStdout().Write([]byte(resultString))
|
|
|
|
case flags.OutputJSON:
|
|
|
|
b, err := json.MarshalIndent(output, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.OutOrStdout().Write(b)
|
|
|
|
default:
|
2023-07-26 11:17:09 +00:00
|
|
|
return fmt.Errorf("unknown output type %s", root.OutputType(cmd))
|
2023-03-21 15:25:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-15 14:12:47 +00:00
|
|
|
return nil
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
2023-02-20 20:55:06 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
2024-03-28 10:32:34 +00:00
|
|
|
b, diags := root.MustConfigureBundle(cmd)
|
|
|
|
if err := diags.Error(); err != nil {
|
2023-02-20 20:55:06 +00:00
|
|
|
cobra.CompErrorln(err.Error())
|
|
|
|
return nil, cobra.ShellCompDirectiveError
|
|
|
|
}
|
|
|
|
|
|
|
|
// No completion in the context of a bundle.
|
|
|
|
// Source and destination paths are taken from bundle configuration.
|
|
|
|
if b == nil {
|
|
|
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
}
|
|
|
|
|
2024-04-22 11:50:13 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
return run.ResourceCompletions(b), cobra.ShellCompDirectiveNoFileComp
|
|
|
|
} else {
|
|
|
|
// If we know the resource to run, we can complete additional positional arguments.
|
|
|
|
runner, err := run.Find(b, args[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, cobra.ShellCompDirectiveError
|
|
|
|
}
|
|
|
|
return runner.CompleteArgs(args[1:], toComplete)
|
|
|
|
}
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
2022-12-15 14:12:47 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
return cmd
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|