|
| 1 | +package cfgpickers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/databricks/cli/libs/cmdio" |
| 11 | + "github.com/databricks/databricks-sdk-go" |
| 12 | + "github.com/databricks/databricks-sdk-go/service/compute" |
| 13 | + "github.com/databricks/databricks-sdk-go/service/iam" |
| 14 | + "github.com/fatih/color" |
| 15 | + "github.com/manifoldco/promptui" |
| 16 | + "golang.org/x/mod/semver" |
| 17 | +) |
| 18 | + |
| 19 | +var minUcRuntime = canonicalVersion("v12.0") |
| 20 | + |
| 21 | +var dbrVersionRegex = regexp.MustCompile(`^(\d+\.\d+)\.x-.*`) |
| 22 | +var dbrSnapshotVersionRegex = regexp.MustCompile(`^(\d+)\.x-snapshot.*`) |
| 23 | + |
| 24 | +func canonicalVersion(v string) string { |
| 25 | + return semver.Canonical("v" + strings.TrimPrefix(v, "v")) |
| 26 | +} |
| 27 | + |
| 28 | +func GetRuntimeVersion(cluster compute.ClusterDetails) (string, bool) { |
| 29 | + match := dbrVersionRegex.FindStringSubmatch(cluster.SparkVersion) |
| 30 | + if len(match) < 1 { |
| 31 | + match = dbrSnapshotVersionRegex.FindStringSubmatch(cluster.SparkVersion) |
| 32 | + if len(match) > 1 { |
| 33 | + // we return 14.999 for 14.x-snapshot for semver.Compare() to work properly |
| 34 | + return fmt.Sprintf("%s.999", match[1]), true |
| 35 | + } |
| 36 | + return "", false |
| 37 | + } |
| 38 | + return match[1], true |
| 39 | +} |
| 40 | + |
| 41 | +func IsCompatibleWithUC(cluster compute.ClusterDetails, minVersion string) bool { |
| 42 | + minVersion = canonicalVersion(minVersion) |
| 43 | + if semver.Compare(minUcRuntime, minVersion) >= 0 { |
| 44 | + return false |
| 45 | + } |
| 46 | + runtimeVersion, ok := GetRuntimeVersion(cluster) |
| 47 | + if !ok { |
| 48 | + return false |
| 49 | + } |
| 50 | + clusterRuntime := canonicalVersion(runtimeVersion) |
| 51 | + if semver.Compare(minVersion, clusterRuntime) > 0 { |
| 52 | + return false |
| 53 | + } |
| 54 | + switch cluster.DataSecurityMode { |
| 55 | + case compute.DataSecurityModeUserIsolation, compute.DataSecurityModeSingleUser: |
| 56 | + return true |
| 57 | + default: |
| 58 | + return false |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +var ErrNoCompatibleClusters = errors.New("no compatible clusters found") |
| 63 | + |
| 64 | +type compatibleCluster struct { |
| 65 | + compute.ClusterDetails |
| 66 | + versionName string |
| 67 | +} |
| 68 | + |
| 69 | +func (v compatibleCluster) Access() string { |
| 70 | + switch v.DataSecurityMode { |
| 71 | + case compute.DataSecurityModeUserIsolation: |
| 72 | + return "Shared" |
| 73 | + case compute.DataSecurityModeSingleUser: |
| 74 | + return "Assigned" |
| 75 | + default: |
| 76 | + return "Unknown" |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func (v compatibleCluster) Runtime() string { |
| 81 | + runtime, _, _ := strings.Cut(v.versionName, " (") |
| 82 | + return runtime |
| 83 | +} |
| 84 | + |
| 85 | +func (v compatibleCluster) State() string { |
| 86 | + state := v.ClusterDetails.State |
| 87 | + switch state { |
| 88 | + case compute.StateRunning, compute.StateResizing: |
| 89 | + return color.GreenString(state.String()) |
| 90 | + case compute.StateError, compute.StateTerminated, compute.StateTerminating, compute.StateUnknown: |
| 91 | + return color.RedString(state.String()) |
| 92 | + default: |
| 93 | + return color.BlueString(state.String()) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +type clusterFilter func(cluster *compute.ClusterDetails, me *iam.User) bool |
| 98 | + |
| 99 | +func WithDatabricksConnect(minVersion string) func(*compute.ClusterDetails, *iam.User) bool { |
| 100 | + return func(cluster *compute.ClusterDetails, me *iam.User) bool { |
| 101 | + if !IsCompatibleWithUC(*cluster, minVersion) { |
| 102 | + return false |
| 103 | + } |
| 104 | + switch cluster.ClusterSource { |
| 105 | + case compute.ClusterSourceJob, |
| 106 | + compute.ClusterSourceModels, |
| 107 | + compute.ClusterSourcePipeline, |
| 108 | + compute.ClusterSourcePipelineMaintenance, |
| 109 | + compute.ClusterSourceSql: |
| 110 | + // only UI and API clusters are usable for DBConnect. |
| 111 | + // `CanUseClient: "NOTEBOOKS"`` didn't seem to have an effect. |
| 112 | + return false |
| 113 | + } |
| 114 | + if cluster.SingleUserName != "" && cluster.SingleUserName != me.UserName { |
| 115 | + return false |
| 116 | + } |
| 117 | + return true |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func loadInteractiveClusters(ctx context.Context, w *databricks.WorkspaceClient, filters []clusterFilter) ([]compatibleCluster, error) { |
| 122 | + promptSpinner := cmdio.Spinner(ctx) |
| 123 | + promptSpinner <- "Loading list of clusters to select from" |
| 124 | + defer close(promptSpinner) |
| 125 | + all, err := w.Clusters.ListAll(ctx, compute.ListClustersRequest{ |
| 126 | + CanUseClient: "NOTEBOOKS", |
| 127 | + }) |
| 128 | + if err != nil { |
| 129 | + return nil, fmt.Errorf("list clusters: %w", err) |
| 130 | + } |
| 131 | + me, err := w.CurrentUser.Me(ctx) |
| 132 | + if err != nil { |
| 133 | + return nil, fmt.Errorf("current user: %w", err) |
| 134 | + } |
| 135 | + versions := map[string]string{} |
| 136 | + sv, err := w.Clusters.SparkVersions(ctx) |
| 137 | + if err != nil { |
| 138 | + return nil, fmt.Errorf("list runtime versions: %w", err) |
| 139 | + } |
| 140 | + for _, v := range sv.Versions { |
| 141 | + versions[v.Key] = v.Name |
| 142 | + } |
| 143 | + var compatible []compatibleCluster |
| 144 | + for _, cluster := range all { |
| 145 | + var skip bool |
| 146 | + for _, filter := range filters { |
| 147 | + if !filter(&cluster, me) { |
| 148 | + skip = true |
| 149 | + } |
| 150 | + } |
| 151 | + if skip { |
| 152 | + continue |
| 153 | + } |
| 154 | + compatible = append(compatible, compatibleCluster{ |
| 155 | + ClusterDetails: cluster, |
| 156 | + versionName: versions[cluster.SparkVersion], |
| 157 | + }) |
| 158 | + } |
| 159 | + return compatible, nil |
| 160 | +} |
| 161 | + |
| 162 | +func AskForCluster(ctx context.Context, w *databricks.WorkspaceClient, filters ...clusterFilter) (string, error) { |
| 163 | + compatible, err := loadInteractiveClusters(ctx, w, filters) |
| 164 | + if err != nil { |
| 165 | + return "", fmt.Errorf("load: %w", err) |
| 166 | + } |
| 167 | + if len(compatible) == 0 { |
| 168 | + return "", ErrNoCompatibleClusters |
| 169 | + } |
| 170 | + if len(compatible) == 1 { |
| 171 | + return compatible[0].ClusterId, nil |
| 172 | + } |
| 173 | + i, _, err := cmdio.RunSelect(ctx, &promptui.Select{ |
| 174 | + Label: "Choose compatible cluster", |
| 175 | + Items: compatible, |
| 176 | + Searcher: func(input string, idx int) bool { |
| 177 | + lower := strings.ToLower(compatible[idx].ClusterName) |
| 178 | + return strings.Contains(lower, input) |
| 179 | + }, |
| 180 | + StartInSearchMode: true, |
| 181 | + Templates: &promptui.SelectTemplates{ |
| 182 | + Label: "{{.ClusterName | faint}}", |
| 183 | + Active: `{{.ClusterName | bold}} ({{.State}} {{.Access}} Runtime {{.Runtime}}) ({{.ClusterId | faint}})`, |
| 184 | + Inactive: `{{.ClusterName}} ({{.State}} {{.Access}} Runtime {{.Runtime}})`, |
| 185 | + Selected: `{{ "Configured cluster" | faint }}: {{ .ClusterName | bold }} ({{.ClusterId | faint}})`, |
| 186 | + }, |
| 187 | + }) |
| 188 | + if err != nil { |
| 189 | + return "", err |
| 190 | + } |
| 191 | + return compatible[i].ClusterId, nil |
| 192 | +} |
0 commit comments