|
| 1 | +package apps |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "os/signal" |
| 10 | + "strings" |
| 11 | + "syscall" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/databricks/cli/cmd/root" |
| 15 | + "github.com/databricks/cli/libs/appproxy" |
| 16 | + "github.com/databricks/cli/libs/apps" |
| 17 | + "github.com/databricks/cli/libs/auth" |
| 18 | + "github.com/databricks/cli/libs/cmdctx" |
| 19 | + "github.com/databricks/cli/libs/cmdio" |
| 20 | + "github.com/databricks/databricks-sdk-go" |
| 21 | + "github.com/spf13/cobra" |
| 22 | +) |
| 23 | + |
| 24 | +// Databricks Apps send a SIGKILL signal 15 seconds after a SIGTERM |
| 25 | +// https://docs.databricks.com/aws/en/dev-tools/databricks-apps/app-development#important-guidelines-for-implementing-databricks-apps |
| 26 | +const SHUTDOWN_TIMEOUT = 15 * time.Second |
| 27 | + |
| 28 | +func setupWorkspaceAndConfig(cmd *cobra.Command, entryPoint string) (*apps.Config, *apps.AppSpec, error) { |
| 29 | + ctx := cmd.Context() |
| 30 | + w := cmdctx.WorkspaceClient(ctx) |
| 31 | + workspaceId, err := w.CurrentWorkspaceID(ctx) |
| 32 | + if err != nil { |
| 33 | + return nil, nil, err |
| 34 | + } |
| 35 | + |
| 36 | + cwd, err := os.Getwd() |
| 37 | + if err != nil { |
| 38 | + return nil, nil, err |
| 39 | + } |
| 40 | + |
| 41 | + config := apps.NewConfig(w.Config.Host, workspaceId, cwd) |
| 42 | + if entryPoint != "" { |
| 43 | + config.AppSpecFiles = []string{entryPoint} |
| 44 | + } |
| 45 | + spec, err := apps.ReadAppSpecFile(config) |
| 46 | + if err != nil { |
| 47 | + return nil, nil, err |
| 48 | + } |
| 49 | + |
| 50 | + return config, spec, nil |
| 51 | +} |
| 52 | + |
| 53 | +func setupApp(cmd *cobra.Command, config *apps.Config, spec *apps.AppSpec, customEnv []string, prepareEnvironment bool) (apps.App, []string, error) { |
| 54 | + ctx := cmd.Context() |
| 55 | + cfg := cmdctx.ConfigUsed(ctx) |
| 56 | + app := apps.NewApp(config, spec) |
| 57 | + env := auth.ProcessEnv(cfg) |
| 58 | + if cfg.Profile != "" { |
| 59 | + env = append(env, "DATABRICKS_CONFIG_PROFILE="+cfg.Profile) |
| 60 | + } |
| 61 | + |
| 62 | + appEnv, err := spec.LoadEnvVars(ctx, customEnv) |
| 63 | + if err != nil { |
| 64 | + return app, nil, err |
| 65 | + } |
| 66 | + env = append(env, appEnv...) |
| 67 | + |
| 68 | + if prepareEnvironment { |
| 69 | + err := app.PrepareEnvironment() |
| 70 | + if err != nil { |
| 71 | + return app, nil, err |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return app, env, nil |
| 76 | +} |
| 77 | + |
| 78 | +func startAppProcess(cmd *cobra.Command, config *apps.Config, app apps.App, env []string, debug bool) (*exec.Cmd, error) { |
| 79 | + ctx := cmd.Context() |
| 80 | + specCommand, err := app.GetCommand(debug) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + cmdio.LogString(ctx, "Running command: "+strings.Join(specCommand, " ")) |
| 86 | + appCmd := exec.Command(specCommand[0], specCommand[1:]...) |
| 87 | + appCmd.Stdin = cmd.InOrStdin() |
| 88 | + appCmd.Stdout = cmd.OutOrStdout() |
| 89 | + appCmd.Stderr = cmd.ErrOrStderr() |
| 90 | + |
| 91 | + appEnvs := apps.GetBaseEnvVars(config) |
| 92 | + for _, envVar := range appEnvs { |
| 93 | + env = append(env, envVar.String()) |
| 94 | + } |
| 95 | + |
| 96 | + appCmd.Env = env |
| 97 | + appCmd.Dir = config.AppPath |
| 98 | + |
| 99 | + err = appCmd.Start() |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + return appCmd, nil |
| 105 | +} |
| 106 | + |
| 107 | +func setupProxy(ctx context.Context, cmd *cobra.Command, config *apps.Config, w *databricks.WorkspaceClient, port int, debug bool) error { |
| 108 | + proxy, err := appproxy.New(ctx, config.AppURL) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + me, err := w.CurrentUser.Me(ctx) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + for key, value := range apps.GetXHeaders(me) { |
| 119 | + proxy.InjectHeader(key, value) |
| 120 | + } |
| 121 | + |
| 122 | + proxyAddr := fmt.Sprintf("localhost:%d", port) |
| 123 | + go func() { |
| 124 | + cmdio.LogString(ctx, "To access your app go to http://"+proxyAddr) |
| 125 | + err := proxy.ListenAndServe(proxyAddr) |
| 126 | + if err != nil { |
| 127 | + cmd.PrintErrln(err) |
| 128 | + } |
| 129 | + }() |
| 130 | + |
| 131 | + if debug { |
| 132 | + cmdio.LogString(ctx, "To debug your app, attach a debugger to port "+apps.DEBUG_PORT) |
| 133 | + } |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +// SIGTERM (not supported on Windows) and SIGINT (Ctrl+C, supported cross-platform) |
| 139 | +// are caught to enable graceful shutdown of the app process. |
| 140 | +func handleGracefulShutdown(appCmd *exec.Cmd) error { |
| 141 | + done := make(chan error, 1) |
| 142 | + sigChan := make(chan os.Signal, 1) |
| 143 | + signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) |
| 144 | + |
| 145 | + go func() { |
| 146 | + done <- appCmd.Wait() |
| 147 | + }() |
| 148 | + |
| 149 | + select { |
| 150 | + case err := <-done: |
| 151 | + return err |
| 152 | + case <-sigChan: |
| 153 | + if err := appCmd.Process.Signal(os.Interrupt); err != nil { |
| 154 | + return fmt.Errorf("failed to send interrupt signal: %w", err) |
| 155 | + } |
| 156 | + |
| 157 | + select { |
| 158 | + case err := <-done: |
| 159 | + return err |
| 160 | + case <-time.After(SHUTDOWN_TIMEOUT): |
| 161 | + if err := appCmd.Process.Kill(); err != nil { |
| 162 | + return fmt.Errorf("failed to kill process: %w", err) |
| 163 | + } |
| 164 | + return errors.New("process killed after timeout") |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func newRunLocal() *cobra.Command { |
| 170 | + var ( |
| 171 | + port int |
| 172 | + debug bool |
| 173 | + prepareEnvironment bool |
| 174 | + entryPoint string |
| 175 | + customEnv []string |
| 176 | + ) |
| 177 | + |
| 178 | + cmd := &cobra.Command{} |
| 179 | + |
| 180 | + cmd.Use = "run-local" |
| 181 | + cmd.Short = `Run an app locally` |
| 182 | + cmd.Long = `Run an app locally. |
| 183 | +
|
| 184 | + This command starts an app locally.` |
| 185 | + |
| 186 | + cmd.Flags().IntVar(&port, "port", 8001, "Port on which to run the app") |
| 187 | + cmd.Flags().BoolVar(&debug, "debug", false, "Enable debug mode") |
| 188 | + cmd.Flags().BoolVar(&prepareEnvironment, "prepare-environment", false, "Prepares the environment for running the app. Requires 'uv' to be installed.") |
| 189 | + cmd.Flags().StringSliceVar(&customEnv, "env", nil, "Set environment variables") |
| 190 | + cmd.Flags().StringVar(&entryPoint, "entry-point", "", "Specify the custom entry point with configuration (.yml file) for the app. Defaults to app.yml") |
| 191 | + |
| 192 | + cmd.PreRunE = root.MustWorkspaceClient |
| 193 | + |
| 194 | + cmd.RunE = func(cmd *cobra.Command, args []string) error { |
| 195 | + ctx := cmd.Context() |
| 196 | + w := cmdctx.WorkspaceClient(ctx) |
| 197 | + |
| 198 | + config, spec, err := setupWorkspaceAndConfig(cmd, entryPoint) |
| 199 | + if err != nil { |
| 200 | + return err |
| 201 | + } |
| 202 | + |
| 203 | + app, env, err := setupApp(cmd, config, spec, customEnv, prepareEnvironment) |
| 204 | + if err != nil { |
| 205 | + return err |
| 206 | + } |
| 207 | + |
| 208 | + appCmd, err := startAppProcess(cmd, config, app, env, debug) |
| 209 | + if err != nil { |
| 210 | + return err |
| 211 | + } |
| 212 | + |
| 213 | + err = setupProxy(ctx, cmd, config, w, port, debug) |
| 214 | + if err != nil { |
| 215 | + return err |
| 216 | + } |
| 217 | + |
| 218 | + return handleGracefulShutdown(appCmd) |
| 219 | + } |
| 220 | + |
| 221 | + cmd.ValidArgsFunction = cobra.NoFileCompletions |
| 222 | + |
| 223 | + return cmd |
| 224 | +} |
| 225 | + |
| 226 | +func init() { |
| 227 | + cmdOverrides = append(cmdOverrides, func(cmd *cobra.Command) { |
| 228 | + cmd.AddCommand(newRunLocal()) |
| 229 | + }) |
| 230 | +} |
0 commit comments