2022-11-30 13:15:22 +00:00
|
|
|
package artifacts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2024-07-16 08:57:04 +00:00
|
|
|
"github.com/databricks/cli/libs/filer"
|
|
|
|
"github.com/databricks/cli/libs/log"
|
2022-11-30 13:15:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func UploadAll() bundle.Mutator {
|
|
|
|
return &all{
|
|
|
|
name: "Upload",
|
|
|
|
fn: uploadArtifactByName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-25 11:35:08 +00:00
|
|
|
func CleanUp() bundle.Mutator {
|
|
|
|
return &cleanUp{}
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:15:22 +00:00
|
|
|
type upload struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func uploadArtifactByName(name string) (bundle.Mutator, error) {
|
|
|
|
return &upload{name}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *upload) Name() string {
|
|
|
|
return fmt.Sprintf("artifacts.Upload(%s)", m.name)
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *upload) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2022-11-30 13:15:22 +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)
|
2022-11-30 13:15:22 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return bundle.Apply(ctx, b, getUploadMutator(artifact.Type, m.name))
|
|
|
|
}
|
|
|
|
|
|
|
|
type cleanUp struct{}
|
|
|
|
|
|
|
|
func (m *cleanUp) Name() string {
|
|
|
|
return "artifacts.CleanUp"
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *cleanUp) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2023-07-25 11:35:08 +00:00
|
|
|
uploadPath, err := getUploadBasePath(b)
|
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 08:57:04 +00:00
|
|
|
client, err := getFilerForArtifacts(b.WorkspaceClient(), uploadPath)
|
|
|
|
if err != nil {
|
|
|
|
return diag.FromErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We intentionally ignore the error because it is not critical to the deployment
|
|
|
|
err = client.Delete(ctx, ".", filer.DeleteRecursively)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf(ctx, "failed to delete %s: %v", uploadPath, err)
|
|
|
|
}
|
2023-07-25 11:35:08 +00:00
|
|
|
|
2024-07-16 08:57:04 +00:00
|
|
|
err = client.Mkdir(ctx, ".")
|
2023-07-25 11:35:08 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("unable to create directory for %s: %v", uploadPath, err)
|
2022-11-30 13:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2022-11-30 13:15:22 +00:00
|
|
|
}
|