2023-07-25 11:35:08 +00:00
|
|
|
package artifacts
|
|
|
|
|
|
|
|
import (
|
2023-10-18 10:20:43 +00:00
|
|
|
"bytes"
|
2023-07-25 11:35:08 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
2023-08-31 14:10:32 +00:00
|
|
|
"path/filepath"
|
2023-07-25 11:35:08 +00:00
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/artifacts/whl"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-10-18 10:20:43 +00:00
|
|
|
"github.com/databricks/cli/libs/filer"
|
2023-12-21 08:00:37 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
2023-07-25 11:35:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mutatorFactory = func(name string) bundle.Mutator
|
|
|
|
|
|
|
|
var buildMutators map[config.ArtifactType]mutatorFactory = map[config.ArtifactType]mutatorFactory{
|
|
|
|
config.ArtifactPythonWheel: whl.Build,
|
|
|
|
}
|
|
|
|
|
|
|
|
var uploadMutators map[config.ArtifactType]mutatorFactory = map[config.ArtifactType]mutatorFactory{}
|
|
|
|
|
|
|
|
func getBuildMutator(t config.ArtifactType, name string) bundle.Mutator {
|
|
|
|
mutatorFactory, ok := buildMutators[t]
|
|
|
|
if !ok {
|
|
|
|
mutatorFactory = BasicBuild
|
|
|
|
}
|
|
|
|
|
|
|
|
return mutatorFactory(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUploadMutator(t config.ArtifactType, name string) bundle.Mutator {
|
|
|
|
mutatorFactory, ok := uploadMutators[t]
|
|
|
|
if !ok {
|
|
|
|
mutatorFactory = BasicUpload
|
|
|
|
}
|
|
|
|
|
|
|
|
return mutatorFactory(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Basic Build defines a general build mutator which builds artifact based on artifact.BuildCommand
|
|
|
|
type basicBuild struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func BasicBuild(name string) bundle.Mutator {
|
|
|
|
return &basicBuild{name: name}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *basicBuild) Name() string {
|
|
|
|
return fmt.Sprintf("artifacts.Build(%s)", m.name)
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *basicBuild) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2023-07-25 11:35:08 +00:00
|
|
|
artifact, ok := b.Config.Artifacts[m.name]
|
|
|
|
if !ok {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("artifact doesn't exist: %s", m.name)
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 10:40:12 +00:00
|
|
|
cmdio.LogString(ctx, fmt.Sprintf("Building %s...", m.name))
|
2023-07-25 11:35:08 +00:00
|
|
|
|
|
|
|
out, err := artifact.Build(ctx)
|
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("build for %s failed, error: %v, output: %s", m.name, err, out)
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
2023-12-21 08:00:37 +00:00
|
|
|
log.Infof(ctx, "Build succeeded")
|
2023-07-25 11:35:08 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Basic Upload defines a general upload mutator which uploads artifact as a library to workspace
|
|
|
|
type basicUpload struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func BasicUpload(name string) bundle.Mutator {
|
|
|
|
return &basicUpload{name: name}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *basicUpload) Name() string {
|
2023-10-18 10:20:43 +00:00
|
|
|
return fmt.Sprintf("artifacts.Upload(%s)", m.name)
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *basicUpload) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2023-07-25 11:35:08 +00:00
|
|
|
artifact, ok := b.Config.Artifacts[m.name]
|
|
|
|
if !ok {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("artifact doesn't exist: %s", m.name)
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(artifact.Files) == 0 {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("artifact source is not configured: %s", m.name)
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 10:20:43 +00:00
|
|
|
uploadPath, err := getUploadBasePath(b)
|
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-10-18 10:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err := filer.NewWorkspaceFilesClient(b.WorkspaceClient(), uploadPath)
|
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-10-18 10:20:43 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 15:29:45 +00:00
|
|
|
err = uploadArtifact(ctx, b, artifact, uploadPath, client)
|
2023-07-25 11:35:08 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("upload for %s failed, error: %v", m.name, err)
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-05 15:29:45 +00:00
|
|
|
func uploadArtifact(ctx context.Context, b *bundle.Bundle, a *config.Artifact, uploadPath string, client filer.Filer) error {
|
2023-07-25 11:35:08 +00:00
|
|
|
for i := range a.Files {
|
|
|
|
f := &a.Files[i]
|
2023-10-18 10:20:43 +00:00
|
|
|
|
2024-02-05 15:29:45 +00:00
|
|
|
filename := filepath.Base(f.Source)
|
|
|
|
cmdio.LogString(ctx, fmt.Sprintf("Uploading %s...", filename))
|
|
|
|
|
|
|
|
err := uploadArtifactFile(ctx, f.Source, client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof(ctx, "Upload succeeded")
|
|
|
|
f.RemotePath = path.Join(uploadPath, filepath.Base(f.Source))
|
|
|
|
|
2024-04-22 11:44:34 +00:00
|
|
|
// TODO: confirm if we still need to update the remote path to start with /Workspace
|
|
|
|
wsfsBase := "/Workspace"
|
|
|
|
remotePath := path.Join(wsfsBase, f.RemotePath)
|
|
|
|
|
|
|
|
for _, job := range b.Config.Resources.Jobs {
|
|
|
|
for i := range job.Tasks {
|
|
|
|
task := &job.Tasks[i]
|
|
|
|
for j := range task.Libraries {
|
|
|
|
lib := &task.Libraries[j]
|
|
|
|
if lib.Whl != "" && isArtifactMatchLibrary(f, lib.Whl, b) {
|
|
|
|
lib.Whl = remotePath
|
|
|
|
}
|
|
|
|
if lib.Jar != "" && isArtifactMatchLibrary(f, lib.Jar, b) {
|
|
|
|
lib.Jar = remotePath
|
|
|
|
}
|
|
|
|
}
|
2024-02-05 15:29:45 +00:00
|
|
|
}
|
2024-04-22 11:44:34 +00:00
|
|
|
|
|
|
|
for i := range job.Environments {
|
|
|
|
env := &job.Environments[i]
|
2024-05-21 10:00:04 +00:00
|
|
|
if env.Spec == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-22 11:44:34 +00:00
|
|
|
for j := range env.Spec.Dependencies {
|
|
|
|
lib := env.Spec.Dependencies[j]
|
|
|
|
if isArtifactMatchLibrary(f, lib, b) {
|
|
|
|
env.Spec.Dependencies[j] = remotePath
|
|
|
|
}
|
|
|
|
}
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-22 11:44:34 +00:00
|
|
|
func isArtifactMatchLibrary(f *config.ArtifactFile, libPath string, b *bundle.Bundle) bool {
|
|
|
|
if !filepath.IsAbs(libPath) {
|
|
|
|
libPath = filepath.Join(b.RootPath, libPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// libPath can be a glob pattern, so do the match first
|
|
|
|
matches, err := filepath.Glob(libPath)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range matches {
|
|
|
|
if m == f.Source {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-07-25 11:35:08 +00:00
|
|
|
// Function to upload artifact file to Workspace
|
2023-11-29 10:40:12 +00:00
|
|
|
func uploadArtifactFile(ctx context.Context, file string, client filer.Filer) error {
|
2023-07-25 11:35:08 +00:00
|
|
|
raw, err := os.ReadFile(file)
|
|
|
|
if err != nil {
|
2023-11-29 10:40:12 +00:00
|
|
|
return fmt.Errorf("unable to read %s: %w", file, errors.Unwrap(err))
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 10:40:12 +00:00
|
|
|
filename := filepath.Base(file)
|
|
|
|
err = client.Write(ctx, filename, bytes.NewReader(raw), filer.OverwriteIfExists, filer.CreateParentDirectories)
|
2023-07-25 11:35:08 +00:00
|
|
|
if err != nil {
|
2023-11-29 10:40:12 +00:00
|
|
|
return fmt.Errorf("unable to import %s: %w", filename, err)
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 10:40:12 +00:00
|
|
|
return nil
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getUploadBasePath(b *bundle.Bundle) (string, error) {
|
2023-11-15 13:37:26 +00:00
|
|
|
artifactPath := b.Config.Workspace.ArtifactPath
|
2023-07-25 11:35:08 +00:00
|
|
|
if artifactPath == "" {
|
|
|
|
return "", fmt.Errorf("remote artifact path not configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.Join(artifactPath, ".internal"), nil
|
|
|
|
}
|