Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 213
Persist deployment metadata in WSFS#845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
ad183331b7dcf9fa70d49464e51d2d4c00b671395e8f1fe9811237ac99453d543281a2ffb9a7960971c06f4e0d16489663cbeaf25a214973580e9f9a2daa26cfb16db407967b6b15720c391d50d51c0a66f5f6ac6e7d6ea5dde3c42c496c581cd79c6c37d16720429de5f9bf41fe854b5625c37c613f452cdbf575119ac2File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,6 +2,7 @@ package mutator | ||
| import ( | ||
| "context" | ||
| "path/filepath" | ||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/libs/git" | ||
| @@ -52,5 +53,17 @@ func (m *loadGitDetails) Apply(ctx context.Context, b *bundle.Bundle) error { | ||
| remoteUrl := repo.OriginUrl() | ||
| b.Config.Bundle.Git.OriginURL = remoteUrl | ||
| } | ||
| // Compute relative path of the bundle root from the Git repo root. | ||
| absBundlePath, err := filepath.Abs(b.Config.Path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // repo.Root() returns the absolute path of the repo | ||
| relBundlePath, err := filepath.Rel(repo.Root(), absBundlePath) | ||
shreyas-goenka marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if err != nil { | ||
| return err | ||
| } | ||
| b.Config.Bundle.Git.BundleRootPath = filepath.ToSlash(relBundlePath) | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package metadata | ||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "path/filepath" | ||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/metadata" | ||
| ) | ||
| type compute struct{} | ||
| func Compute() bundle.Mutator { | ||
| return &compute{} | ||
| } | ||
| func (m *compute) Name() string { | ||
| return "metadata.Compute" | ||
| } | ||
| func (m *compute) Apply(_ context.Context, b *bundle.Bundle) error { | ||
| b.Metadata = metadata.Metadata{ | ||
| Version: metadata.Version, | ||
| Config: metadata.Config{}, | ||
| } | ||
| // Set git details in metadata | ||
| b.Metadata.Config.Bundle.Git = b.Config.Bundle.Git | ||
| // Set job config paths in metadata | ||
| jobsMetadata := make(map[string]*metadata.Job) | ||
| for name, job := range b.Config.Resources.Jobs { | ||
| // Compute config file path the job is defined in, relative to the bundle | ||
| // root | ||
| relativePath, err := filepath.Rel(b.Config.Path, job.ConfigFilePath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to compute relative path for job %s: %w", name, err) | ||
| } | ||
| // Metadata for the job | ||
| jobsMetadata[name] = &metadata.Job{ | ||
| ID: job.ID, | ||
| RelativePath: filepath.ToSlash(relativePath), | ||
| } | ||
| } | ||
| b.Metadata.Config.Resources.Jobs = jobsMetadata | ||
shreyas-goenka marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // Set file upload destination of the bundle in metadata | ||
| b.Metadata.Config.Workspace.FilesPath = b.Config.Workspace.FilesPath | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package metadata | ||
| import ( | ||
| "context" | ||
| "testing" | ||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/config" | ||
| "github.com/databricks/cli/bundle/config/paths" | ||
| "github.com/databricks/cli/bundle/config/resources" | ||
| "github.com/databricks/cli/bundle/metadata" | ||
| "github.com/databricks/databricks-sdk-go/service/jobs" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
| func TestComputeMetadataMutator(t *testing.T) { | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Workspace: config.Workspace{ | ||
| RootPath: "/Users/shreyas.goenka@databricks.com", | ||
| ArtifactsPath: "/Users/shreyas.goenka@databricks.com/artifacts", | ||
| FilesPath: "/Users/shreyas.goenka@databricks.com/files", | ||
| }, | ||
| Bundle: config.Bundle{ | ||
| Name: "my-bundle", | ||
| Target: "development", | ||
| Git: config.Git{ | ||
| Branch: "my-branch", | ||
| OriginURL: "www.host.com", | ||
| Commit: "abcd", | ||
| BundleRootPath: "a/b/c/d", | ||
| }, | ||
| }, | ||
| Resources: config.Resources{ | ||
| Jobs: map[string]*resources.Job{ | ||
| "my-job-1": { | ||
| Paths: paths.Paths{ | ||
| ConfigFilePath: "a/b/c", | ||
| }, | ||
| ID: "1111", | ||
| JobSettings: &jobs.JobSettings{ | ||
| Name: "My Job One", | ||
| }, | ||
| }, | ||
| "my-job-2": { | ||
| Paths: paths.Paths{ | ||
| ConfigFilePath: "d/e/f", | ||
| }, | ||
| ID: "2222", | ||
| JobSettings: &jobs.JobSettings{ | ||
| Name: "My Job Two", | ||
| }, | ||
| }, | ||
| }, | ||
| Pipelines: map[string]*resources.Pipeline{ | ||
| "my-pipeline": { | ||
| Paths: paths.Paths{ | ||
| ConfigFilePath: "abc", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| expectedMetadata := metadata.Metadata{ | ||
| Version: metadata.Version, | ||
| Config: metadata.Config{ | ||
| Workspace: metadata.Workspace{ | ||
| FilesPath: "/Users/shreyas.goenka@databricks.com/files", | ||
| }, | ||
| Bundle: metadata.Bundle{ | ||
| Git: config.Git{ | ||
| Branch: "my-branch", | ||
| OriginURL: "www.host.com", | ||
| Commit: "abcd", | ||
| BundleRootPath: "a/b/c/d", | ||
| }, | ||
| }, | ||
| Resources: metadata.Resources{ | ||
| Jobs: map[string]*metadata.Job{ | ||
| "my-job-1": { | ||
| RelativePath: "a/b/c", | ||
| ID: "1111", | ||
| }, | ||
| "my-job-2": { | ||
| RelativePath: "d/e/f", | ||
| ID: "2222", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| err := Compute().Apply(context.Background(), b) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, expectedMetadata, b.Metadata) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package metadata | ||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/libs/filer" | ||
| ) | ||
| const MetadataFileName = "metadata.json" | ||
| type upload struct{} | ||
| func Upload() bundle.Mutator { | ||
| return &upload{} | ||
| } | ||
| func (m *upload) Name() string { | ||
| return "metadata.Upload" | ||
| } | ||
| func (m *upload) Apply(ctx context.Context, b *bundle.Bundle) error { | ||
| f, err := filer.NewWorkspaceFilesClient(b.WorkspaceClient(), b.Config.Workspace.StatePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| metadata, err := json.MarshalIndent(b.Metadata, "", " ") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return f.Write(ctx, MetadataFileName, bytes.NewReader(metadata), filer.CreateParentDirectories, filer.OverwriteIfExists) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package metadata | ||
| import ( | ||
| "github.com/databricks/cli/bundle/config" | ||
| ) | ||
| const Version = 1 | ||
| type Bundle struct { | ||
| Git config.Git `json:"git,omitempty"` | ||
| } | ||
| type Workspace struct { | ||
| FilesPath string `json:"file_path,omitempty"` | ||
| } | ||
| type Job struct { | ||
| ID string `json:"id,omitempty"` | ||
| // Relative path from the bundle root to the configuration file that holds | ||
| // the definition of this resource. | ||
| RelativePath string `json:"relative_path,omitempty"` | ||
| } | ||
| type Resources struct { | ||
| Jobs map[string]*Job `json:"jobs,omitempty"` | ||
| } | ||
| type Config struct { | ||
| Bundle Bundle `json:"bundle,omitempty"` | ||
| Workspace Workspace `json:"workspace,omitempty"` | ||
| Resources Resources `json:"resources,omitempty"` | ||
| } | ||
| // Metadata about the bundle deployment. This is the interface Databricks services | ||
| // rely on to integrate with bundles when they need additional information about | ||
| // a bundle deployment. | ||
| // | ||
| // After deploy, a file containing the metadata (metadata.json) can be found | ||
| // in the WSFS location containing the bundle state. | ||
| type Metadata struct { | ||
| Version int `json:"version"` | ||
| Config Config `json:"config"` | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,7 +2,7 @@ | ||
| "properties": { | ||
| "unique_id": { | ||
| "type": "string", | ||
| "description": "Unique ID for job name" | ||
| "description": "Unique ID for pipeline name" | ||
shreyas-goenka marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "properties": { | ||
| "unique_id": { | ||
| "type": "string", | ||
| "description": "Unique ID for job name" | ||
| }, | ||
| "spark_version": { | ||
| "type": "string", | ||
| "description": "Spark version used for job cluster" | ||
| }, | ||
| "node_type_id": { | ||
| "type": "string", | ||
| "description": "Node type id for job cluster" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Databricks notebook source | ||
| print("bye") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| resources: | ||
| jobs: | ||
| bar: | ||
| name: test-job-metadata-2-{{.unique_id}} | ||
| tasks: | ||
| - task_key: my_notebook_task | ||
| new_cluster: | ||
| num_workers: 1 | ||
| spark_version: "{{.spark_version}}" | ||
| node_type_id: "{{.node_type_id}}" | ||
| notebook_task: | ||
| notebook_path: "./bar.py" |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.