2023-07-25 11:35:08 +00:00
|
|
|
package libraries
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2024-02-05 15:29:45 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
2023-07-25 11:35:08 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/compute"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/jobs"
|
|
|
|
)
|
|
|
|
|
2023-08-17 09:11:39 +00:00
|
|
|
func findAllTasks(b *bundle.Bundle) []*jobs.Task {
|
2023-07-25 11:35:08 +00:00
|
|
|
r := b.Config.Resources
|
2023-08-17 09:11:39 +00:00
|
|
|
result := make([]*jobs.Task, 0)
|
2023-07-25 11:35:08 +00:00
|
|
|
for k := range b.Config.Resources.Jobs {
|
|
|
|
tasks := r.Jobs[k].JobSettings.Tasks
|
|
|
|
for i := range tasks {
|
|
|
|
task := &tasks[i]
|
2023-08-17 09:11:39 +00:00
|
|
|
result = append(result, task)
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-17 09:11:39 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-09-08 11:08:21 +00:00
|
|
|
func FindAllWheelTasksWithLocalLibraries(b *bundle.Bundle) []*jobs.Task {
|
2023-08-17 09:11:39 +00:00
|
|
|
tasks := findAllTasks(b)
|
|
|
|
wheelTasks := make([]*jobs.Task, 0)
|
|
|
|
for _, task := range tasks {
|
2023-09-08 11:08:21 +00:00
|
|
|
if task.PythonWheelTask != nil && IsTaskWithLocalLibraries(task) {
|
2023-08-17 09:11:39 +00:00
|
|
|
wheelTasks = append(wheelTasks, task)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return wheelTasks
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 11:08:21 +00:00
|
|
|
func IsTaskWithLocalLibraries(task *jobs.Task) bool {
|
|
|
|
for _, l := range task.Libraries {
|
2024-02-05 15:29:45 +00:00
|
|
|
if IsLocalLibrary(&l) {
|
2023-09-08 11:08:21 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-08 13:45:21 +00:00
|
|
|
func IsTaskWithWorkspaceLibraries(task *jobs.Task) bool {
|
|
|
|
for _, l := range task.Libraries {
|
2024-02-05 15:29:45 +00:00
|
|
|
if IsWorkspaceLibrary(&l) {
|
2023-09-08 13:45:21 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-07-25 11:35:08 +00:00
|
|
|
func findLibraryMatches(lib *compute.Library, b *bundle.Bundle) ([]string, error) {
|
2024-02-05 15:29:45 +00:00
|
|
|
path := libraryPath(lib)
|
2023-07-25 11:35:08 +00:00
|
|
|
if path == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fullPath := filepath.Join(b.Config.Path, path)
|
|
|
|
return filepath.Glob(fullPath)
|
|
|
|
}
|
|
|
|
|
2024-02-05 15:29:45 +00:00
|
|
|
func findArtifactFiles(ctx context.Context, lib *compute.Library, b *bundle.Bundle) ([]*config.ArtifactFile, error) {
|
2023-07-25 11:35:08 +00:00
|
|
|
matches, err := findLibraryMatches(lib, b)
|
|
|
|
if err != nil {
|
2024-02-05 15:29:45 +00:00
|
|
|
return nil, err
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 15:29:45 +00:00
|
|
|
if len(matches) == 0 && IsLocalLibrary(lib) {
|
|
|
|
return nil, fmt.Errorf("file %s is referenced in libraries section but doesn't exist on the local file system", libraryPath(lib))
|
2023-07-26 12:58:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 15:29:45 +00:00
|
|
|
var out []*config.ArtifactFile
|
2023-07-25 11:35:08 +00:00
|
|
|
for _, match := range matches {
|
|
|
|
af, err := findArtifactFileByLocalPath(match, b)
|
|
|
|
if err != nil {
|
2023-08-29 08:26:09 +00:00
|
|
|
cmdio.LogString(ctx, fmt.Sprintf("%s. Skipping uploading. In order to use the define 'artifacts' section", err.Error()))
|
2023-07-25 11:35:08 +00:00
|
|
|
} else {
|
2024-02-05 15:29:45 +00:00
|
|
|
out = append(out, af)
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-05 15:29:45 +00:00
|
|
|
return out, nil
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func findArtifactFileByLocalPath(path string, b *bundle.Bundle) (*config.ArtifactFile, error) {
|
|
|
|
for _, a := range b.Config.Artifacts {
|
|
|
|
for k := range a.Files {
|
|
|
|
if a.Files[k].Source == path {
|
|
|
|
return &a.Files[k], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 08:26:09 +00:00
|
|
|
return nil, fmt.Errorf("artifact section is not defined for file at %s", path)
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 15:29:45 +00:00
|
|
|
func MapFilesToTaskLibraries(ctx context.Context, b *bundle.Bundle) map[string][]*compute.Library {
|
|
|
|
tasks := findAllTasks(b)
|
|
|
|
out := make(map[string][]*compute.Library)
|
|
|
|
for _, task := range tasks {
|
|
|
|
for j := range task.Libraries {
|
|
|
|
lib := &task.Libraries[j]
|
|
|
|
if !IsLocalLibrary(lib) {
|
|
|
|
continue
|
|
|
|
}
|
2023-08-07 09:55:30 +00:00
|
|
|
|
2024-02-05 15:29:45 +00:00
|
|
|
matches, err := findLibraryMatches(lib, b)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf(ctx, "Error matching library to files: %s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2023-08-29 08:26:09 +00:00
|
|
|
|
2024-02-05 15:29:45 +00:00
|
|
|
for _, match := range matches {
|
|
|
|
out[match] = append(out[match], lib)
|
|
|
|
}
|
|
|
|
}
|
2023-08-29 08:26:09 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 15:29:45 +00:00
|
|
|
return out
|
2023-10-09 10:10:28 +00:00
|
|
|
}
|