2024-02-05 15:29:45 +00:00
|
|
|
package libraries
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-22 11:44:34 +00:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2024-02-05 15:29:45 +00:00
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2024-04-22 11:44:34 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/compute"
|
2024-02-05 15:29:45 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/jobs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type match struct {
|
|
|
|
}
|
|
|
|
|
2024-04-22 11:44:34 +00:00
|
|
|
func ValidateLocalLibrariesExist() bundle.Mutator {
|
2024-02-05 15:29:45 +00:00
|
|
|
return &match{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *match) Name() string {
|
2024-04-22 11:44:34 +00:00
|
|
|
return "libraries.ValidateLocalLibrariesExist"
|
2024-02-05 15:29:45 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (a *match) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2024-04-22 11:44:34 +00:00
|
|
|
for _, job := range b.Config.Resources.Jobs {
|
|
|
|
err := validateEnvironments(job.Environments, b)
|
|
|
|
if err != nil {
|
|
|
|
return diag.FromErr(err)
|
2024-02-05 15:29:45 +00:00
|
|
|
}
|
2024-04-22 11:44:34 +00:00
|
|
|
|
|
|
|
for _, task := range job.JobSettings.Tasks {
|
|
|
|
err := validateTaskLibraries(task.Libraries, b)
|
2024-02-05 15:29:45 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2024-02-05 15:29:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 11:44:34 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateTaskLibraries(libs []compute.Library, b *bundle.Bundle) error {
|
|
|
|
for _, lib := range libs {
|
|
|
|
path := libraryPath(&lib)
|
|
|
|
if path == "" || !IsLocalPath(path) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
matches, err := filepath.Glob(filepath.Join(b.RootPath, path))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return fmt.Errorf("file %s is referenced in libraries section but doesn't exist on the local file system", libraryPath(&lib))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-05 15:29:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-22 11:44:34 +00:00
|
|
|
func validateEnvironments(envs []jobs.JobEnvironment, b *bundle.Bundle) error {
|
|
|
|
for _, env := range envs {
|
2024-05-21 10:00:04 +00:00
|
|
|
if env.Spec == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-22 11:44:34 +00:00
|
|
|
for _, dep := range env.Spec.Dependencies {
|
|
|
|
matches, err := filepath.Glob(filepath.Join(b.RootPath, dep))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) == 0 && IsEnvironmentDependencyLocal(dep) {
|
|
|
|
return fmt.Errorf("file %s is referenced in environments section but doesn't exist on the local file system", dep)
|
|
|
|
}
|
|
|
|
}
|
2024-02-05 15:29:45 +00:00
|
|
|
}
|
|
|
|
|
2024-04-22 11:44:34 +00:00
|
|
|
return nil
|
2024-02-05 15:29:45 +00:00
|
|
|
}
|