mirror of https://github.com/databricks/cli.git
107 lines
2.4 KiB
Go
107 lines
2.4 KiB
Go
|
package apps
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"path"
|
||
|
"path/filepath"
|
||
|
|
||
|
"github.com/databricks/cli/bundle"
|
||
|
"github.com/databricks/cli/bundle/config/resources"
|
||
|
"github.com/databricks/cli/bundle/deploy"
|
||
|
"github.com/databricks/cli/libs/cmdio"
|
||
|
"github.com/databricks/cli/libs/diag"
|
||
|
"github.com/databricks/cli/libs/filer"
|
||
|
"github.com/databricks/databricks-sdk-go/service/apps"
|
||
|
"golang.org/x/sync/errgroup"
|
||
|
|
||
|
"gopkg.in/yaml.v3"
|
||
|
)
|
||
|
|
||
|
type appsDeploy struct {
|
||
|
filerFactory deploy.FilerFactory
|
||
|
}
|
||
|
|
||
|
func Deploy() bundle.Mutator {
|
||
|
return appsDeploy{deploy.AppFiler}
|
||
|
}
|
||
|
|
||
|
func (a appsDeploy) Name() string {
|
||
|
return "apps.Deploy"
|
||
|
}
|
||
|
|
||
|
func (a appsDeploy) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
||
|
if len(b.Config.Resources.Apps) == 0 {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
errGrp, ctx := errgroup.WithContext(ctx)
|
||
|
w := b.WorkspaceClient()
|
||
|
f, err := a.filerFactory(b)
|
||
|
if err != nil {
|
||
|
return diag.FromErr(err)
|
||
|
}
|
||
|
|
||
|
for _, app := range b.Config.Resources.Apps {
|
||
|
cmdio.LogString(ctx, fmt.Sprintf("Deploying app %s...", app.Name))
|
||
|
errGrp.Go(func() error {
|
||
|
// If the app has a config, we need to deploy it first.
|
||
|
// It means we need to write app.yml file with the content of the config field
|
||
|
// to the remote source code path of the app.
|
||
|
if app.Config != nil {
|
||
|
appPath, err := filepath.Rel(b.Config.Workspace.FilePath, app.SourceCodePath)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("failed to get relative path of app source code path: %w", err)
|
||
|
}
|
||
|
|
||
|
buf, err := configToYaml(app)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
err = f.Write(ctx, path.Join(appPath, "app.yml"), buf, filer.OverwriteIfExists)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("failed to write %s file: %w", path.Join(app.SourceCodePath, "app.yml"), err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
wait, err := w.Apps.Deploy(ctx, apps.CreateAppDeploymentRequest{
|
||
|
AppName: app.Name,
|
||
|
AppDeployment: &apps.AppDeployment{
|
||
|
Mode: apps.AppDeploymentModeSnapshot,
|
||
|
SourceCodePath: app.SourceCodePath,
|
||
|
},
|
||
|
})
|
||
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
_, err = wait.Get()
|
||
|
return err
|
||
|
})
|
||
|
}
|
||
|
|
||
|
if err := errGrp.Wait(); err != nil {
|
||
|
return diag.FromErr(err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func configToYaml(app *resources.App) (*bytes.Buffer, error) {
|
||
|
buf := bytes.NewBuffer(nil)
|
||
|
enc := yaml.NewEncoder(buf)
|
||
|
enc.SetIndent(2)
|
||
|
|
||
|
err := enc.Encode(app.Config)
|
||
|
defer enc.Close()
|
||
|
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("failed to encode app config to yaml: %w", err)
|
||
|
}
|
||
|
|
||
|
return buf, nil
|
||
|
}
|