2022-07-07 18:56:59 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2023-03-16 16:48:17 +00:00
|
|
|
"context"
|
2023-02-20 10:33:30 +00:00
|
|
|
"flag"
|
2022-07-07 18:56:59 +00:00
|
|
|
"fmt"
|
2023-03-16 16:48:17 +00:00
|
|
|
"io"
|
2023-02-20 10:33:30 +00:00
|
|
|
"path/filepath"
|
2022-07-07 18:56:59 +00:00
|
|
|
"time"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2024-03-18 14:41:58 +00:00
|
|
|
"github.com/databricks/cli/bundle/deploy/files"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/cmd/root"
|
|
|
|
"github.com/databricks/cli/libs/flags"
|
|
|
|
"github.com/databricks/cli/libs/sync"
|
2024-05-30 07:41:50 +00:00
|
|
|
"github.com/databricks/cli/libs/vfs"
|
2022-07-07 18:56:59 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
type syncFlags struct {
|
|
|
|
// project files polling interval
|
|
|
|
interval time.Duration
|
|
|
|
full bool
|
|
|
|
watch bool
|
|
|
|
output flags.Output
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *syncFlags) syncOptionsFromBundle(cmd *cobra.Command, args []string, b *bundle.Bundle) (*sync.SyncOptions, error) {
|
2023-02-20 10:33:30 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
return nil, fmt.Errorf("SRC and DST are not configurable in the context of a bundle")
|
|
|
|
}
|
|
|
|
|
2024-04-18 15:13:16 +00:00
|
|
|
opts, err := files.GetSyncOptions(cmd.Context(), bundle.ReadOnly(b))
|
2023-02-20 10:33:30 +00:00
|
|
|
if err != nil {
|
2024-03-18 14:41:58 +00:00
|
|
|
return nil, fmt.Errorf("cannot get sync options: %w", err)
|
2023-02-20 10:33:30 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 14:41:58 +00:00
|
|
|
opts.Full = f.full
|
|
|
|
opts.PollInterval = f.interval
|
|
|
|
return opts, nil
|
2023-02-20 10:33:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
func (f *syncFlags) syncOptionsFromArgs(cmd *cobra.Command, args []string) (*sync.SyncOptions, error) {
|
2023-02-20 10:33:30 +00:00
|
|
|
if len(args) != 2 {
|
|
|
|
return nil, flag.ErrHelp
|
|
|
|
}
|
|
|
|
|
Add verbose flag to the "bundle deploy" command (#1774)
## Changes
- Extract sync output logic from `cmd/sync` into `lib/sync`
- Add hidden `verbose` flag to the `bundle deploy` command, it's false
by default and hidden from the `--help` output
- Pass output handler to the `deploy/files/upload` mutator if the
verbose option is true
The was an idea to use in-place output overriding each past file sync
event in the output, bit that wont work for the extension, since it
doesn't display deploy logs in the terminal.
Example output:
```
~/tmp/defpy: ~/cli/cli bundle deploy --sync-progress
Building defpy...
Uploading defpy-0.0.1+20240917.112755-py3-none-any.whl...
Uploading bundle files to /Users/ilia.babanov@databricks.com/.bundle/defpy/dev/files...
Action: PUT: requirements-dev.txt, resources/defpy_pipeline.yml, pytest.ini, src/defpy/main.py, src/defpy/__init__.py, src/dlt_pipeline.ipynb, tests/main_test.py, src/notebook.ipynb, setup.py, resources/defpy_job.yml, .vscode/extensions.json, .vscode/settings.json, fixtures/.gitkeep, .vscode/__builtins__.pyi, README.md, .gitignore, databricks.yml
Uploaded tests
Uploaded resources
Uploaded fixtures
Uploaded .vscode
Uploaded src/defpy
Uploaded requirements-dev.txt
Uploaded .gitignore
Uploaded fixtures/.gitkeep
Uploaded src/defpy/__init__.py
Uploaded databricks.yml
Uploaded README.md
Uploaded setup.py
Uploaded .vscode/__builtins__.pyi
Uploaded .vscode/extensions.json
Uploaded src/dlt_pipeline.ipynb
Uploaded .vscode/settings.json
Uploaded resources/defpy_job.yml
Uploaded pytest.ini
Uploaded src/defpy/main.py
Uploaded tests/main_test.py
Uploaded resources/defpy_pipeline.yml
Uploaded src/notebook.ipynb
Initial Sync Complete
Deploying resources...
Updating deployment state...
Deployment complete!
```
Output example in the extension:
<img width="1843" alt="Screenshot 2024-09-19 at 11 07 48"
src="https://github.com/user-attachments/assets/0fafd095-cdc6-44b8-b482-27a38ada0330">
## Tests
Manually for the `sync` and `bundle deploy` commands + vscode extension
sync and deploy flows
2024-09-23 10:09:11 +00:00
|
|
|
var outputFunc func(context.Context, <-chan sync.Event, io.Writer)
|
|
|
|
switch f.output {
|
|
|
|
case flags.OutputText:
|
|
|
|
outputFunc = sync.TextOutput
|
|
|
|
case flags.OutputJSON:
|
|
|
|
outputFunc = sync.JsonOutput
|
|
|
|
}
|
|
|
|
|
|
|
|
var outputHandler sync.OutputHandler
|
|
|
|
if outputFunc != nil {
|
|
|
|
outputHandler = func(ctx context.Context, events <-chan sync.Event) {
|
|
|
|
outputFunc(ctx, events, cmd.OutOrStdout())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 10:33:30 +00:00
|
|
|
opts := sync.SyncOptions{
|
2024-08-19 15:41:02 +00:00
|
|
|
LocalRoot: vfs.MustNew(args[0]),
|
|
|
|
Paths: []string{"."},
|
|
|
|
Include: nil,
|
|
|
|
Exclude: nil,
|
|
|
|
|
2023-02-20 10:33:30 +00:00
|
|
|
RemotePath: args[1],
|
2023-07-27 10:03:08 +00:00
|
|
|
Full: f.full,
|
|
|
|
PollInterval: f.interval,
|
2023-02-20 10:33:30 +00:00
|
|
|
|
|
|
|
// We keep existing behavior for VS Code extension where if there is
|
|
|
|
// no bundle defined, we store the snapshots in `.databricks`.
|
|
|
|
// The sync code will automatically create this directory if it doesn't
|
|
|
|
// exist and add it to the `.gitignore` file in the root.
|
|
|
|
SnapshotBasePath: filepath.Join(args[0], ".databricks"),
|
2023-10-13 13:04:15 +00:00
|
|
|
WorkspaceClient: root.WorkspaceClient(cmd.Context()),
|
Add verbose flag to the "bundle deploy" command (#1774)
## Changes
- Extract sync output logic from `cmd/sync` into `lib/sync`
- Add hidden `verbose` flag to the `bundle deploy` command, it's false
by default and hidden from the `--help` output
- Pass output handler to the `deploy/files/upload` mutator if the
verbose option is true
The was an idea to use in-place output overriding each past file sync
event in the output, bit that wont work for the extension, since it
doesn't display deploy logs in the terminal.
Example output:
```
~/tmp/defpy: ~/cli/cli bundle deploy --sync-progress
Building defpy...
Uploading defpy-0.0.1+20240917.112755-py3-none-any.whl...
Uploading bundle files to /Users/ilia.babanov@databricks.com/.bundle/defpy/dev/files...
Action: PUT: requirements-dev.txt, resources/defpy_pipeline.yml, pytest.ini, src/defpy/main.py, src/defpy/__init__.py, src/dlt_pipeline.ipynb, tests/main_test.py, src/notebook.ipynb, setup.py, resources/defpy_job.yml, .vscode/extensions.json, .vscode/settings.json, fixtures/.gitkeep, .vscode/__builtins__.pyi, README.md, .gitignore, databricks.yml
Uploaded tests
Uploaded resources
Uploaded fixtures
Uploaded .vscode
Uploaded src/defpy
Uploaded requirements-dev.txt
Uploaded .gitignore
Uploaded fixtures/.gitkeep
Uploaded src/defpy/__init__.py
Uploaded databricks.yml
Uploaded README.md
Uploaded setup.py
Uploaded .vscode/__builtins__.pyi
Uploaded .vscode/extensions.json
Uploaded src/dlt_pipeline.ipynb
Uploaded .vscode/settings.json
Uploaded resources/defpy_job.yml
Uploaded pytest.ini
Uploaded src/defpy/main.py
Uploaded tests/main_test.py
Uploaded resources/defpy_pipeline.yml
Uploaded src/notebook.ipynb
Initial Sync Complete
Deploying resources...
Updating deployment state...
Deployment complete!
```
Output example in the extension:
<img width="1843" alt="Screenshot 2024-09-19 at 11 07 48"
src="https://github.com/user-attachments/assets/0fafd095-cdc6-44b8-b482-27a38ada0330">
## Tests
Manually for the `sync` and `bundle deploy` commands + vscode extension
sync and deploy flows
2024-09-23 10:09:11 +00:00
|
|
|
|
|
|
|
OutputHandler: outputHandler,
|
2023-02-20 10:33:30 +00:00
|
|
|
}
|
|
|
|
return &opts, nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
func New() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2023-12-28 13:14:55 +00:00
|
|
|
Use: "sync [flags] SRC DST",
|
|
|
|
Short: "Synchronize a local directory to a workspace directory",
|
2024-03-12 14:12:34 +00:00
|
|
|
Args: root.MaximumNArgs(2),
|
2023-12-28 13:14:55 +00:00
|
|
|
GroupID: "development",
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f := syncFlags{
|
|
|
|
output: flags.OutputText,
|
|
|
|
}
|
|
|
|
cmd.Flags().DurationVar(&f.interval, "interval", 1*time.Second, "file system polling interval (for --watch)")
|
|
|
|
cmd.Flags().BoolVar(&f.full, "full", false, "perform full synchronization (default is incremental)")
|
|
|
|
cmd.Flags().BoolVar(&f.watch, "watch", false, "watch local file system for changes")
|
|
|
|
cmd.Flags().Var(&f.output, "output", "type of output format")
|
2022-09-16 09:06:58 +00:00
|
|
|
|
2023-10-19 12:50:46 +00:00
|
|
|
// Wrapper for [root.MustWorkspaceClient] that disables loading authentication configuration from a bundle.
|
|
|
|
mustWorkspaceClient := func(cmd *cobra.Command, args []string) error {
|
|
|
|
cmd.SetContext(root.SkipLoadBundle(cmd.Context()))
|
|
|
|
return root.MustWorkspaceClient(cmd, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.PreRunE = mustWorkspaceClient
|
2023-07-27 10:03:08 +00:00
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
2023-02-20 10:33:30 +00:00
|
|
|
var opts *sync.SyncOptions
|
|
|
|
var err error
|
|
|
|
|
|
|
|
//
|
|
|
|
// To be uncommented and used once our VS Code extension is bundle aware.
|
2023-07-18 10:16:34 +00:00
|
|
|
// Until then, this could interfere with extension usage where a `databricks.yml` file is present.
|
2023-05-16 16:35:39 +00:00
|
|
|
// See https://github.com/databricks/cli/pull/207.
|
2023-02-20 10:33:30 +00:00
|
|
|
//
|
|
|
|
// b := bundle.GetOrNil(cmd.Context())
|
|
|
|
// if b != nil {
|
|
|
|
// // Run initialize phase to make sure paths are set.
|
2023-05-24 12:45:19 +00:00
|
|
|
// err = bundle.Apply(cmd.Context(), b, phases.Initialize())
|
2023-02-20 10:33:30 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// opts, err = syncOptionsFromBundle(cmd, args, b)
|
|
|
|
// } else {
|
2023-07-27 10:03:08 +00:00
|
|
|
opts, err = f.syncOptionsFromArgs(cmd, args)
|
2023-02-20 10:33:30 +00:00
|
|
|
// }
|
2023-01-19 14:57:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-20 10:33:30 +00:00
|
|
|
ctx := cmd.Context()
|
|
|
|
s, err := sync.New(ctx, *opts)
|
2023-01-24 07:30:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
Add verbose flag to the "bundle deploy" command (#1774)
## Changes
- Extract sync output logic from `cmd/sync` into `lib/sync`
- Add hidden `verbose` flag to the `bundle deploy` command, it's false
by default and hidden from the `--help` output
- Pass output handler to the `deploy/files/upload` mutator if the
verbose option is true
The was an idea to use in-place output overriding each past file sync
event in the output, bit that wont work for the extension, since it
doesn't display deploy logs in the terminal.
Example output:
```
~/tmp/defpy: ~/cli/cli bundle deploy --sync-progress
Building defpy...
Uploading defpy-0.0.1+20240917.112755-py3-none-any.whl...
Uploading bundle files to /Users/ilia.babanov@databricks.com/.bundle/defpy/dev/files...
Action: PUT: requirements-dev.txt, resources/defpy_pipeline.yml, pytest.ini, src/defpy/main.py, src/defpy/__init__.py, src/dlt_pipeline.ipynb, tests/main_test.py, src/notebook.ipynb, setup.py, resources/defpy_job.yml, .vscode/extensions.json, .vscode/settings.json, fixtures/.gitkeep, .vscode/__builtins__.pyi, README.md, .gitignore, databricks.yml
Uploaded tests
Uploaded resources
Uploaded fixtures
Uploaded .vscode
Uploaded src/defpy
Uploaded requirements-dev.txt
Uploaded .gitignore
Uploaded fixtures/.gitkeep
Uploaded src/defpy/__init__.py
Uploaded databricks.yml
Uploaded README.md
Uploaded setup.py
Uploaded .vscode/__builtins__.pyi
Uploaded .vscode/extensions.json
Uploaded src/dlt_pipeline.ipynb
Uploaded .vscode/settings.json
Uploaded resources/defpy_job.yml
Uploaded pytest.ini
Uploaded src/defpy/main.py
Uploaded tests/main_test.py
Uploaded resources/defpy_pipeline.yml
Uploaded src/notebook.ipynb
Initial Sync Complete
Deploying resources...
Updating deployment state...
Deployment complete!
```
Output example in the extension:
<img width="1843" alt="Screenshot 2024-09-19 at 11 07 48"
src="https://github.com/user-attachments/assets/0fafd095-cdc6-44b8-b482-27a38ada0330">
## Tests
Manually for the `sync` and `bundle deploy` commands + vscode extension
sync and deploy flows
2024-09-23 10:09:11 +00:00
|
|
|
defer s.Close()
|
2023-01-23 12:52:39 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
if f.watch {
|
2023-03-16 16:48:17 +00:00
|
|
|
err = s.RunContinuous(ctx)
|
|
|
|
} else {
|
2024-06-17 09:48:52 +00:00
|
|
|
_, err = s.RunOnce(ctx)
|
2023-01-24 14:06:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 16:48:17 +00:00
|
|
|
return err
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
2023-02-20 13:31:59 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
2023-10-19 12:34:20 +00:00
|
|
|
cmd.SetContext(root.SkipPrompt(cmd.Context()))
|
2023-10-09 10:37:18 +00:00
|
|
|
|
2023-10-19 12:50:46 +00:00
|
|
|
err := mustWorkspaceClient(cmd, args)
|
2023-02-20 13:31:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, cobra.ShellCompDirectiveError
|
|
|
|
}
|
|
|
|
|
|
|
|
// No completion in the context of a bundle.
|
|
|
|
// Source and destination paths are taken from bundle configuration.
|
|
|
|
b := bundle.GetOrNil(cmd.Context())
|
|
|
|
if b != nil {
|
|
|
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
}
|
|
|
|
|
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
return nil, cobra.ShellCompDirectiveFilterDirs
|
|
|
|
case 1:
|
2023-10-09 10:37:18 +00:00
|
|
|
wsc := root.WorkspaceClient(cmd.Context())
|
2023-02-20 13:31:59 +00:00
|
|
|
return completeRemotePath(cmd.Context(), wsc, toComplete)
|
|
|
|
default:
|
|
|
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
}
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
2022-07-07 18:56:59 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
return cmd
|
2022-07-07 18:56:59 +00:00
|
|
|
}
|