2022-05-13 13:30:22 +00:00
|
|
|
package project
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-05-14 17:55:00 +00:00
|
|
|
"sync"
|
2022-05-13 13:30:22 +00:00
|
|
|
|
2022-09-08 13:50:00 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/databricks"
|
2022-09-07 09:55:59 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/clusters"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/commands"
|
2022-09-08 13:50:00 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/scim"
|
2022-09-07 09:55:59 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/workspaces"
|
2022-09-16 09:06:58 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-05-13 13:30:22 +00:00
|
|
|
)
|
|
|
|
|
2022-09-16 09:06:58 +00:00
|
|
|
type project struct {
|
|
|
|
mu sync.Mutex
|
2022-05-14 17:55:00 +00:00
|
|
|
|
2022-09-16 13:18:46 +00:00
|
|
|
root string
|
|
|
|
|
2022-09-16 09:06:58 +00:00
|
|
|
config *Config
|
|
|
|
wsc *workspaces.WorkspacesClient
|
|
|
|
me *scim.User
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure is used as a PreRunE function for all commands that
|
|
|
|
// require a project to be configured. If a project could successfully
|
|
|
|
// be found and loaded, it is set on the command's context object.
|
|
|
|
func Configure(cmd *cobra.Command, args []string) error {
|
|
|
|
root, err := getRoot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-14 17:55:00 +00:00
|
|
|
|
2022-09-16 09:06:58 +00:00
|
|
|
ctx, err := Initialize(cmd.Context(), root)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.SetContext(ctx)
|
|
|
|
return nil
|
2022-05-14 17:55:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 09:06:58 +00:00
|
|
|
// Placeholder to use as unique key in context.Context.
|
|
|
|
var projectKey int
|
|
|
|
|
|
|
|
// Initialize loads a project configuration given a root.
|
|
|
|
// It stores the project on a new context.
|
|
|
|
// The project is available through the `Get()` function.
|
|
|
|
func Initialize(ctx context.Context, root string) (context.Context, error) {
|
|
|
|
config, err := loadProjectConf(root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p := project{
|
2022-09-16 13:18:46 +00:00
|
|
|
root: root,
|
2022-09-16 09:06:58 +00:00
|
|
|
config: &config,
|
|
|
|
}
|
|
|
|
|
2022-09-21 11:55:37 +00:00
|
|
|
if config.Profile == "" {
|
|
|
|
// Bricks config doesn't define the profile to use, so go sdk will figure
|
|
|
|
// out the auth credentials based on the enviroment.
|
|
|
|
// eg. DATABRICKS_CONFIG_PROFILE can be used to select which profile to use or
|
|
|
|
// DATABRICKS_HOST and DATABRICKS_TOKEN can be used to set the workspace auth creds
|
|
|
|
p.wsc = workspaces.New()
|
|
|
|
} else {
|
|
|
|
p.wsc = workspaces.New(&databricks.Config{Profile: config.Profile})
|
|
|
|
}
|
|
|
|
|
2022-09-16 09:06:58 +00:00
|
|
|
return context.WithValue(ctx, &projectKey, &p), nil
|
2022-05-14 17:55:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 09:06:58 +00:00
|
|
|
// Get returns the project as configured on the context.
|
|
|
|
// It panics if it isn't configured.
|
|
|
|
func Get(ctx context.Context) *project {
|
|
|
|
project, ok := ctx.Value(&projectKey).(*project)
|
|
|
|
if !ok {
|
|
|
|
panic(`context not configured with project`)
|
|
|
|
}
|
|
|
|
return project
|
2022-05-13 13:30:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 09:55:59 +00:00
|
|
|
// Make sure to initialize the workspaces client on project init
|
2022-09-16 09:06:58 +00:00
|
|
|
func (p *project) WorkspacesClient() *workspaces.WorkspacesClient {
|
|
|
|
return p.wsc
|
2022-09-07 09:55:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 13:18:46 +00:00
|
|
|
func (p *project) Root() string {
|
|
|
|
return p.root
|
|
|
|
}
|
|
|
|
|
2022-09-16 09:06:58 +00:00
|
|
|
func (p *project) Me() (*scim.User, error) {
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
if p.me != nil {
|
|
|
|
return p.me, nil
|
2022-05-14 17:55:00 +00:00
|
|
|
}
|
2022-09-16 09:06:58 +00:00
|
|
|
me, err := p.wsc.CurrentUser.Me(context.Background())
|
2022-05-13 13:30:22 +00:00
|
|
|
if err != nil {
|
2022-09-14 15:50:29 +00:00
|
|
|
return nil, err
|
2022-05-14 17:55:00 +00:00
|
|
|
}
|
2022-09-16 09:06:58 +00:00
|
|
|
p.me = me
|
2022-09-14 15:50:29 +00:00
|
|
|
return me, nil
|
2022-05-14 17:55:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 09:06:58 +00:00
|
|
|
func (p *project) DeploymentIsolationPrefix() string {
|
|
|
|
if p.config.Isolation == None {
|
|
|
|
return p.config.Name
|
2022-05-21 13:23:37 +00:00
|
|
|
}
|
2022-09-16 09:06:58 +00:00
|
|
|
if p.config.Isolation == Soft {
|
|
|
|
me, err := p.Me()
|
2022-09-14 15:50:29 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-09-16 09:06:58 +00:00
|
|
|
return fmt.Sprintf("%s/%s", p.config.Name, me.UserName)
|
2022-05-21 13:23:37 +00:00
|
|
|
}
|
2022-09-16 09:06:58 +00:00
|
|
|
panic(fmt.Errorf("unknow project isolation: %s", p.config.Isolation))
|
2022-05-21 13:23:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 09:55:59 +00:00
|
|
|
func getClusterIdFromClusterName(ctx context.Context,
|
|
|
|
wsc *workspaces.WorkspacesClient,
|
|
|
|
clusterName string,
|
|
|
|
) (clusterId string, err error) {
|
|
|
|
clusterId = ""
|
|
|
|
clustersList, err := wsc.Clusters.List(ctx, clusters.ListRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, cluster := range clustersList.Clusters {
|
|
|
|
if cluster.ClusterName == clusterName {
|
|
|
|
clusterId = cluster.ClusterId
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = fmt.Errorf("could not find cluster with name: %s", clusterName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Old version of getting development cluster details with isolation implemented.
|
|
|
|
// Kept just for reference. Remove once isolation is implemented properly
|
|
|
|
/*
|
2022-09-16 09:06:58 +00:00
|
|
|
func (p *project) DevelopmentCluster(ctx context.Context) (cluster clusters.ClusterInfo, err error) {
|
|
|
|
api := clusters.NewClustersAPI(ctx, p.Client()) // TODO: rewrite with normal SDK
|
|
|
|
if p.project.DevCluster == nil {
|
|
|
|
p.project.DevCluster = &clusters.Cluster{}
|
|
|
|
}
|
|
|
|
dc := p.project.DevCluster
|
|
|
|
if p.project.Isolation == Soft {
|
|
|
|
if p.project.IsDevClusterJustReference() {
|
2022-05-14 17:55:00 +00:00
|
|
|
err = fmt.Errorf("projects with soft isolation cannot have named clusters")
|
|
|
|
return
|
|
|
|
}
|
2022-09-16 09:06:58 +00:00
|
|
|
dc.ClusterName = fmt.Sprintf("dev/%s", p.DeploymentIsolationPrefix())
|
2022-05-14 17:55:00 +00:00
|
|
|
}
|
|
|
|
if dc.ClusterName == "" {
|
|
|
|
err = fmt.Errorf("please either pick `isolation: soft` or specify a shared cluster name")
|
2022-05-13 13:30:22 +00:00
|
|
|
return
|
|
|
|
}
|
2022-09-16 09:06:58 +00:00
|
|
|
return app.GetOrCreateRunningCluster(dc.ClusterName, *dc)
|
2022-05-13 13:30:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func runCommandOnDev(ctx context.Context, language, command string) common.CommandResults {
|
2022-05-14 17:55:00 +00:00
|
|
|
cluster, err := Current.DevelopmentCluster(ctx)
|
|
|
|
exec := Current.Client().CommandExecutor(ctx)
|
2022-05-13 13:30:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.CommandResults{
|
|
|
|
ResultType: "error",
|
2022-05-14 17:55:00 +00:00
|
|
|
Summary: err.Error(),
|
2022-05-13 13:30:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return exec.Execute(cluster.ClusterID, language, command)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunPythonOnDev(ctx context.Context, command string) common.CommandResults {
|
|
|
|
return runCommandOnDev(ctx, "python", command)
|
2022-05-14 17:55:00 +00:00
|
|
|
}
|
2022-09-07 09:55:59 +00:00
|
|
|
*/
|
|
|
|
|
2022-09-16 09:06:58 +00:00
|
|
|
// TODO: Add safe access to p.project and p.project.DevCluster that throws errors if
|
2022-09-07 09:55:59 +00:00
|
|
|
// the fields are not defined properly
|
2022-09-16 09:06:58 +00:00
|
|
|
func (p *project) GetDevelopmentClusterId(ctx context.Context) (clusterId string, err error) {
|
|
|
|
clusterId = p.config.DevCluster.ClusterId
|
|
|
|
clusterName := p.config.DevCluster.ClusterName
|
2022-09-07 09:55:59 +00:00
|
|
|
if clusterId != "" {
|
|
|
|
return
|
|
|
|
} else if clusterName != "" {
|
|
|
|
// Add workspaces client on init
|
2022-09-16 09:06:58 +00:00
|
|
|
return getClusterIdFromClusterName(ctx, p.wsc, clusterName)
|
2022-09-07 09:55:59 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Add the project config file location used to error message
|
|
|
|
err = fmt.Errorf("please define either development cluster's cluster_id or cluster_name in your project config")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCommandOnDev(ctx context.Context, language, command string) commands.CommandResults {
|
2022-09-16 09:06:58 +00:00
|
|
|
clusterId, err := Get(ctx).GetDevelopmentClusterId(ctx)
|
2022-09-07 09:55:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return commands.CommandResults{
|
|
|
|
ResultType: "error",
|
|
|
|
Summary: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
2022-09-16 09:06:58 +00:00
|
|
|
return Get(ctx).wsc.Commands.Execute(ctx, clusterId, language, command)
|
2022-09-07 09:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RunPythonOnDev(ctx context.Context, command string) commands.CommandResults {
|
|
|
|
return runCommandOnDev(ctx, "python", command)
|
|
|
|
}
|