-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathclient.go
More file actions
885 lines (765 loc) · 30.3 KB
/
client.go
File metadata and controls
885 lines (765 loc) · 30.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
package client
import (
"context"
_ "embed"
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"
"time"
"github.com/databricks/cli/experimental/ssh/internal/keys"
"github.com/databricks/cli/experimental/ssh/internal/proxy"
"github.com/databricks/cli/experimental/ssh/internal/sessions"
"github.com/databricks/cli/experimental/ssh/internal/sshconfig"
"github.com/databricks/cli/experimental/ssh/internal/vscode"
sshWorkspace "github.com/databricks/cli/experimental/ssh/internal/workspace"
"github.com/databricks/cli/internal/build"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/log"
"github.com/databricks/cli/libs/telemetry"
"github.com/databricks/cli/libs/telemetry/protos"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/retries"
"github.com/databricks/databricks-sdk-go/service/compute"
"github.com/databricks/databricks-sdk-go/service/jobs"
"github.com/databricks/databricks-sdk-go/service/workspace"
"github.com/gorilla/websocket"
)
//go:embed ssh-server-bootstrap.py
var sshServerBootstrapScript string
var errServerMetadata = errors.New("server metadata error")
var connectionNameRegex = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_-]*$`)
const (
sshServerTaskKey = "start_ssh_server"
serverlessEnvironmentKey = "ssh_tunnel_serverless"
minEnvironmentVersion = 4
)
type ClientOptions struct {
// Id of the cluster to connect to (for dedicated clusters)
ClusterID string
// Connection name (for serverless compute). Used as unique identifier instead of ClusterID.
ConnectionName string
// GPU accelerator type (for serverless compute)
Accelerator string
// Delay before shutting down the server after the last client disconnects
ShutdownDelay time.Duration
// Maximum number of SSH clients
MaxClients int
// Indicates that the CLI runs as a ProxyCommand - it should establish ws connection
// to the cluster and proxy all traffic through stdin/stdout.
// In the non proxy mode the CLI spawns an ssh client with the ProxyCommand config.
ProxyMode bool
// Open remote IDE window with a specific ssh config (empty, 'vscode', or 'cursor')
IDE string
// Expected format: "<user_name>,<port>,<cluster_id>".
// If present, the CLI won't attempt to start the server.
ServerMetadata string
// How often the CLI should reconnect to the server with new auth.
HandoverTimeout time.Duration
// Max amount of time the server process is allowed to live
ServerTimeout time.Duration
// Max amount of time to wait for the SSH server task to reach RUNNING state
TaskStartupTimeout time.Duration
// Directory for local SSH tunnel development releases.
// If not present, the CLI will use github releases with the current version.
ReleasesDir string
// Directory for local SSH keys. Defaults to ~/.databricks/ssh-tunnel-keys
SSHKeysDir string
// Client public key name located in the ssh-tunnel secrets scope.
ClientPublicKeyName string
// Client private key name located in the ssh-tunnel secrets scope.
ClientPrivateKeyName string
// If true, the CLI will attempt to start the cluster if it is not running.
AutoStartCluster bool
// Optional auth profile name. If present, will be added as --profile flag to the ProxyCommand while spawning ssh client.
Profile string
// Additional arguments to pass to the SSH client in the non proxy mode.
AdditionalArgs []string
// Optional path to the user known hosts file.
UserKnownHostsFile string
// Liteswap header value for traffic routing (dev/test only).
Liteswap string
// If true, skip checking and updating IDE settings.
SkipSettingsCheck bool
// Environment version for serverless compute.
EnvironmentVersion int
}
func (o *ClientOptions) Validate() error {
if !o.ProxyMode && o.ClusterID == "" && o.ConnectionName == "" && o.Accelerator == "" {
return errors.New("please provide --cluster or --accelerator flag")
}
if o.Accelerator != "" && o.ClusterID != "" {
return errors.New("--accelerator flag can only be used with serverless compute, not with --cluster")
}
// TODO: Remove when we add support for serverless CPU
if o.ConnectionName != "" && o.Accelerator == "" {
return errors.New("--name flag requires --accelerator to be set (for now we only support serverless GPU compute)")
}
if o.ConnectionName != "" && !connectionNameRegex.MatchString(o.ConnectionName) {
return fmt.Errorf("connection name %q must consist of letters, numbers, dashes, and underscores", o.ConnectionName)
}
if o.IDE != "" && o.IDE != vscode.VSCodeOption && o.IDE != vscode.CursorOption {
return fmt.Errorf("invalid IDE value: %q, expected %q or %q", o.IDE, vscode.VSCodeOption, vscode.CursorOption)
}
if o.EnvironmentVersion > 0 && o.EnvironmentVersion < minEnvironmentVersion {
return fmt.Errorf("environment version must be >= %d, got %d", minEnvironmentVersion, o.EnvironmentVersion)
}
return nil
}
func (o *ClientOptions) IsServerlessMode() bool {
return o.ClusterID == "" && (o.ConnectionName != "" || o.Accelerator != "")
}
// SessionIdentifier returns the unique identifier for the session.
// For dedicated clusters, this is the cluster ID. For serverless, this is the connection name.
func (o *ClientOptions) SessionIdentifier() string {
if o.IsServerlessMode() {
return o.ConnectionName
}
return o.ClusterID
}
// FormatMetadata formats the server metadata string for use in ProxyCommand.
// Returns empty string if userName is empty or serverPort is zero.
func FormatMetadata(userName string, serverPort int, clusterID string) string {
if userName == "" || serverPort == 0 {
return ""
}
if clusterID != "" {
return fmt.Sprintf("%s,%d,%s", userName, serverPort, clusterID)
}
return fmt.Sprintf("%s,%d", userName, serverPort)
}
// ToProxyCommand generates the ProxyCommand string for SSH config.
// This method serializes the ClientOptions into a command-line invocation that will
// be parsed back into ClientOptions when the SSH ProxyCommand is executed.
func (o *ClientOptions) ToProxyCommand() (string, error) {
executablePath, err := os.Executable()
if err != nil {
return "", fmt.Errorf("failed to get current executable path: %w", err)
}
var proxyCommand string
if o.IsServerlessMode() {
proxyCommand = fmt.Sprintf("%q ssh connect --proxy --name=%s --shutdown-delay=%s",
executablePath, o.ConnectionName, o.ShutdownDelay.String())
if o.Accelerator != "" {
proxyCommand += " --accelerator=" + o.Accelerator
}
} else {
proxyCommand = fmt.Sprintf("%q ssh connect --proxy --cluster=%s --auto-start-cluster=%t --shutdown-delay=%s",
executablePath, o.ClusterID, o.AutoStartCluster, o.ShutdownDelay.String())
}
if o.ServerMetadata != "" {
proxyCommand += " --metadata=" + o.ServerMetadata
}
if o.HandoverTimeout > 0 {
proxyCommand += " --handover-timeout=" + o.HandoverTimeout.String()
}
if o.Profile != "" {
proxyCommand += " --profile=" + o.Profile
}
if o.Liteswap != "" {
proxyCommand += " --liteswap=" + o.Liteswap
}
if o.EnvironmentVersion > 0 {
proxyCommand += " --environment-version=" + strconv.Itoa(o.EnvironmentVersion)
}
return proxyCommand, nil
}
func Run(ctx context.Context, client *databricks.WorkspaceClient, opts ClientOptions) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGHUP, syscall.SIGTERM)
go func() {
<-sigCh
cmdio.LogString(ctx, "Received termination signal, cleaning up...")
cancel()
}()
event := BuildTelemetryEvent(opts)
runErr := runConnect(ctx, client, opts, event)
if runErr != nil {
event.IsSuccess = false
} else {
event.IsSuccess = true
}
telemetry.Log(ctx, protos.DatabricksCliLog{
SshTunnelEvent: event,
})
return runErr
}
// BuildTelemetryEvent creates an SshTunnelEvent pre-populated with data from client options.
func BuildTelemetryEvent(opts ClientOptions) *protos.SshTunnelEvent {
event := &protos.SshTunnelEvent{
AcceleratorType: opts.Accelerator,
IdeType: opts.IDE,
AutoStartCluster: opts.AutoStartCluster,
}
if opts.IsServerlessMode() {
event.ComputeType = protos.SshTunnelComputeTypeServerless
} else {
event.ComputeType = protos.SshTunnelComputeTypeDedicated
}
switch {
case opts.ProxyMode:
event.ClientMode = protos.SshTunnelClientModeProxy
case opts.IDE != "":
event.ClientMode = protos.SshTunnelClientModeIDE
default:
event.ClientMode = protos.SshTunnelClientModeSSH
}
// If metadata is provided, the server is already running — this is a reconnect from ProxyCommand.
event.IsReconnect = opts.ServerMetadata != ""
return event
}
func runConnect(ctx context.Context, client *databricks.WorkspaceClient, opts ClientOptions, event *protos.SshTunnelEvent) error {
// For serverless without explicit --name: auto-generate or reconnect to existing session.
if opts.IsServerlessMode() && opts.ConnectionName == "" && !opts.ProxyMode {
err := resolveServerlessSession(ctx, client, &opts)
if err != nil {
return err
}
}
sessionID := opts.SessionIdentifier()
if sessionID == "" {
return errors.New("either --cluster or --accelerator must be provided")
}
if !opts.ProxyMode {
cmdio.LogString(ctx, fmt.Sprintf("Connecting to %s...", sessionID))
}
if opts.IDE != "" && !opts.ProxyMode {
if err := vscode.CheckIDECommand(opts.IDE); err != nil {
return err
}
if err := vscode.CheckIDESSHExtension(ctx, opts.IDE); err != nil {
return err
}
}
// Check and update IDE settings for serverless mode, where we must set up
// desired server ports (or socket connection mode) for the connection to go through
// (as the majority of the localhost ports on the remote side are blocked by iptable rules).
// Plus the platform (always linux), and extensions (python and jupyter), to make the initial experience smoother.
if opts.IDE != "" && opts.IsServerlessMode() && !opts.ProxyMode && !opts.SkipSettingsCheck && cmdio.IsPromptSupported(ctx) {
err := vscode.CheckAndUpdateSettings(ctx, opts.IDE, opts.ConnectionName)
if err != nil {
cmdio.LogString(ctx, fmt.Sprintf("Failed to update IDE settings: %v", err))
cmdio.LogString(ctx, vscode.GetManualInstructions(opts.IDE, opts.ConnectionName))
cmdio.LogString(ctx, "Use --skip-settings-check to bypass IDE settings verification.")
shouldProceed, promptErr := cmdio.AskYesOrNo(ctx, "Do you want to proceed with the connection?")
if promptErr != nil {
return fmt.Errorf("failed to prompt user: %w", promptErr)
}
if !shouldProceed {
return errors.New("aborted: IDE settings need to be updated manually, user declined to proceed")
}
}
}
// Only check cluster state for dedicated clusters
if !opts.IsServerlessMode() {
cmdio.LogString(ctx, "Checking cluster state...")
err := checkClusterState(ctx, client, opts.ClusterID, opts.AutoStartCluster)
if err != nil {
return err
}
}
secretScopeName, err := keys.CreateKeysSecretScope(ctx, client, sessionID)
if err != nil {
return fmt.Errorf("failed to create secret scope: %w", err)
}
privateKeyBytes, publicKeyBytes, err := keys.CheckAndGenerateSSHKeyPairFromSecrets(ctx, client, secretScopeName, opts.ClientPrivateKeyName, opts.ClientPublicKeyName)
if err != nil {
return fmt.Errorf("failed to get or generate SSH key pair from secrets: %w", err)
}
keyPath, err := keys.GetLocalSSHKeyPath(ctx, sessionID, opts.SSHKeysDir)
if err != nil {
return fmt.Errorf("failed to get local keys folder: %w", err)
}
err = keys.SaveSSHKeyPair(keyPath, privateKeyBytes, publicKeyBytes)
if err != nil {
return fmt.Errorf("failed to save SSH key pair locally: %w", err)
}
log.Infof(ctx, "Using SSH key: %s", keyPath)
log.Infof(ctx, "Secrets scope: %s, key name: %s", secretScopeName, opts.ClientPublicKeyName)
var userName string
var serverPort int
var clusterID string
version := build.GetInfo().Version
if opts.ServerMetadata == "" {
cmdio.LogString(ctx, "Uploading binaries...")
sp := cmdio.NewSpinner(ctx)
sp.TrackElapsedTime()
sp.Update("Uploading binaries...")
if err := UploadTunnelReleases(ctx, client, version, opts.ReleasesDir); err != nil {
sp.Close()
return fmt.Errorf("failed to upload ssh-tunnel binaries: %w", err)
}
sp.Close()
serverStartTime := time.Now()
userName, serverPort, clusterID, err = ensureSSHServerIsRunning(ctx, client, version, secretScopeName, opts)
if err != nil {
return fmt.Errorf("failed to ensure that ssh server is running: %w", err)
}
event.ServerStartTimeMs = time.Since(serverStartTime).Milliseconds()
} else {
// Metadata format: "<user_name>,<port>,<cluster_id>"
metadata := strings.Split(opts.ServerMetadata, ",")
if len(metadata) < 2 {
return fmt.Errorf("invalid metadata: %s, expected format: <user_name>,<port>[,<cluster_id>]", opts.ServerMetadata)
}
userName = metadata[0]
if userName == "" {
return fmt.Errorf("remote user name is empty in the metadata: %s", opts.ServerMetadata)
}
serverPort, err = strconv.Atoi(metadata[1])
if err != nil {
return fmt.Errorf("cannot parse port from metadata: %s, %w", opts.ServerMetadata, err)
}
if len(metadata) >= 3 {
clusterID = metadata[2]
} else {
clusterID = opts.ClusterID
}
}
// For serverless mode, we need the cluster ID from metadata for Driver Proxy connections
if opts.IsServerlessMode() && clusterID == "" {
return errors.New("cluster ID is required for serverless connections but was not found in metadata")
}
log.Infof(ctx, "Remote user name: %s", userName)
log.Infof(ctx, "Server port: %d", serverPort)
if opts.IsServerlessMode() {
log.Infof(ctx, "Cluster ID (from serverless job): %s", clusterID)
}
if !opts.ProxyMode {
cmdio.LogString(ctx, "Connected!")
}
// Persist the session for future reconnects.
if opts.IsServerlessMode() && !opts.ProxyMode {
err = sessions.Add(ctx, sessions.Session{
Name: opts.ConnectionName,
Accelerator: opts.Accelerator,
WorkspaceHost: client.Config.Host,
CreatedAt: time.Now(),
ClusterID: clusterID,
})
if err != nil {
log.Warnf(ctx, "Failed to save session state: %v", err)
}
}
if opts.ProxyMode {
return runSSHProxy(ctx, client, serverPort, clusterID, opts)
} else if opts.IDE != "" {
return runIDE(ctx, client, userName, keyPath, serverPort, clusterID, opts)
} else {
log.Infof(ctx, "Additional SSH arguments: %v", opts.AdditionalArgs)
return spawnSSHClient(ctx, userName, keyPath, serverPort, clusterID, opts)
}
}
func runIDE(ctx context.Context, client *databricks.WorkspaceClient, userName, keyPath string, serverPort int, clusterID string, opts ClientOptions) error {
connectionName := opts.SessionIdentifier()
if connectionName == "" {
return errors.New("connection name is required for IDE integration")
}
// Get Databricks user name for the workspace path
currentUser, err := client.CurrentUser.Me(ctx)
if err != nil {
return fmt.Errorf("failed to get current user: %w", err)
}
// Ensure SSH config entry exists
configPath, err := sshconfig.GetMainConfigPath(ctx)
if err != nil {
return fmt.Errorf("failed to get SSH config path: %w", err)
}
err = ensureSSHConfigEntry(ctx, configPath, connectionName, userName, keyPath, serverPort, clusterID, opts)
if err != nil {
return fmt.Errorf("failed to ensure SSH config entry: %w", err)
}
return vscode.LaunchIDE(ctx, opts.IDE, connectionName, userName, currentUser.UserName)
}
func ensureSSHConfigEntry(ctx context.Context, configPath, hostName, userName, keyPath string, serverPort int, clusterID string, opts ClientOptions) error {
// Ensure the Include directive exists in the main SSH config
err := sshconfig.EnsureIncludeDirective(ctx, configPath)
if err != nil {
return err
}
// Generate ProxyCommand with server metadata
optsWithMetadata := opts
optsWithMetadata.ServerMetadata = FormatMetadata(userName, serverPort, clusterID)
proxyCommand, err := optsWithMetadata.ToProxyCommand()
if err != nil {
return fmt.Errorf("failed to generate ProxyCommand: %w", err)
}
var hostConfig string
if opts.IsServerlessMode() {
hostConfig = sshconfig.GenerateServerlessHostConfig(hostName, userName, keyPath, proxyCommand)
} else {
hostConfig = sshconfig.GenerateHostConfig(hostName, userName, keyPath, proxyCommand)
}
_, err = sshconfig.CreateOrUpdateHostConfig(ctx, hostName, hostConfig, true)
if err != nil {
return err
}
log.Infof(ctx, "Updated SSH config entry for '%s'", hostName)
return nil
}
// getServerMetadata retrieves the server metadata from the workspace and validates it via Driver Proxy.
// sessionID is the unique identifier for the session (cluster ID for dedicated clusters, connection name for serverless).
// For dedicated clusters, clusterID should be the same as sessionID.
// For serverless, clusterID is read from the workspace metadata.
func getServerMetadata(ctx context.Context, client *databricks.WorkspaceClient, sessionID, clusterID, version, liteswap string) (int, string, string, error) {
wsMetadata, err := sshWorkspace.GetWorkspaceMetadata(ctx, client, version, sessionID)
if err != nil {
return 0, "", "", errors.Join(errServerMetadata, err)
}
log.Debugf(ctx, "Workspace metadata: %+v", wsMetadata)
// For serverless mode, the cluster ID comes from the metadata
effectiveClusterID := clusterID
if wsMetadata.ClusterID != "" {
effectiveClusterID = wsMetadata.ClusterID
}
if effectiveClusterID == "" {
return 0, "", "", errors.Join(errServerMetadata, errors.New("cluster ID not available in metadata"))
}
workspaceID, err := client.CurrentWorkspaceID(ctx)
if err != nil {
return 0, "", "", err
}
metadataURL := fmt.Sprintf("%s/driver-proxy-api/o/%d/%s/%d/metadata", client.Config.Host, workspaceID, effectiveClusterID, wsMetadata.Port)
log.Debugf(ctx, "Metadata URL: %s", metadataURL)
req, err := http.NewRequestWithContext(ctx, "GET", metadataURL, nil)
if err != nil {
return 0, "", "", err
}
if liteswap != "" {
req.Header.Set("x-databricks-traffic-id", "testenv://liteswap/"+liteswap)
}
if err := client.Config.Authenticate(req); err != nil {
return 0, "", "", err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return 0, "", "", err
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return 0, "", "", err
}
log.Debugf(ctx, "Metadata response: %s", string(bodyBytes))
log.Debugf(ctx, "Metadata response status code: %d", resp.StatusCode)
if resp.StatusCode != http.StatusOK {
return 0, "", "", errors.Join(errServerMetadata, fmt.Errorf("server is not ok, status code %d", resp.StatusCode))
}
return wsMetadata.Port, string(bodyBytes), effectiveClusterID, nil
}
func submitSSHTunnelJob(ctx context.Context, client *databricks.WorkspaceClient, version, secretScopeName string, opts ClientOptions) error {
sessionID := opts.SessionIdentifier()
contentDir, err := sshWorkspace.GetWorkspaceContentDir(ctx, client, version, sessionID)
if err != nil {
return fmt.Errorf("failed to get workspace content directory: %w", err)
}
err = client.Workspace.MkdirsByPath(ctx, contentDir)
if err != nil {
return fmt.Errorf("failed to create directory in the remote workspace: %w", err)
}
sshTunnelJobName := "ssh-server-bootstrap-" + sessionID
jobNotebookPath := filepath.ToSlash(filepath.Join(contentDir, "ssh-server-bootstrap"))
notebookContent := "# Databricks notebook source\n" + sshServerBootstrapScript
encodedContent := base64.StdEncoding.EncodeToString([]byte(notebookContent))
err = client.Workspace.Import(ctx, workspace.Import{
Path: jobNotebookPath,
Format: workspace.ImportFormatSource,
Content: encodedContent,
Language: workspace.LanguagePython,
Overwrite: true,
})
if err != nil {
return fmt.Errorf("failed to create ssh-tunnel notebook: %w", err)
}
baseParams := map[string]string{
"version": version,
"secretScopeName": secretScopeName,
"authorizedKeySecretName": opts.ClientPublicKeyName,
"shutdownDelay": opts.ShutdownDelay.String(),
"maxClients": strconv.Itoa(opts.MaxClients),
"sessionId": sessionID,
"serverless": strconv.FormatBool(opts.IsServerlessMode()),
}
log.Infof(ctx, "Submitting a job to start the ssh server...")
task := jobs.SubmitTask{
TaskKey: sshServerTaskKey,
NotebookTask: &jobs.NotebookTask{
NotebookPath: jobNotebookPath,
BaseParameters: baseParams,
},
TimeoutSeconds: int(opts.ServerTimeout.Seconds()),
}
if opts.IsServerlessMode() {
task.EnvironmentKey = serverlessEnvironmentKey
if opts.Accelerator != "" {
log.Infof(ctx, "Using accelerator: %s", opts.Accelerator)
task.Compute = &jobs.Compute{
HardwareAccelerator: compute.HardwareAcceleratorType(opts.Accelerator),
}
}
} else {
task.ExistingClusterId = opts.ClusterID
}
submitRequest := jobs.SubmitRun{
RunName: sshTunnelJobName,
TimeoutSeconds: int(opts.ServerTimeout.Seconds()),
Tasks: []jobs.SubmitTask{task},
}
if opts.IsServerlessMode() {
submitRequest.Environments = []jobs.JobEnvironment{
{
EnvironmentKey: serverlessEnvironmentKey,
Spec: &compute.Environment{
EnvironmentVersion: strconv.Itoa(max(opts.EnvironmentVersion, minEnvironmentVersion)),
},
},
}
}
waiter, err := client.Jobs.Submit(ctx, submitRequest)
if err != nil {
return fmt.Errorf("failed to submit job: %w", err)
}
log.Infof(ctx, "Job submitted successfully with run ID: %d", waiter.RunId)
return waitForJobToStart(ctx, client, waiter.RunId, opts.TaskStartupTimeout)
}
func spawnSSHClient(ctx context.Context, userName, privateKeyPath string, serverPort int, clusterID string, opts ClientOptions) error {
// Create a copy with metadata for the ProxyCommand
optsWithMetadata := opts
optsWithMetadata.ServerMetadata = FormatMetadata(userName, serverPort, clusterID)
proxyCommand, err := optsWithMetadata.ToProxyCommand()
if err != nil {
return fmt.Errorf("failed to generate ProxyCommand: %w", err)
}
hostName := opts.SessionIdentifier()
hostKeyChecking := "StrictHostKeyChecking=accept-new"
if opts.IsServerlessMode() {
hostKeyChecking = "StrictHostKeyChecking=no"
}
sshArgs := []string{
"-l", userName,
"-i", privateKeyPath,
"-o", "IdentitiesOnly=yes",
"-o", hostKeyChecking,
"-o", "ConnectTimeout=360",
"-o", "ProxyCommand=" + proxyCommand,
}
if opts.IsServerlessMode() {
sshArgs = append(sshArgs, "-o", "UserKnownHostsFile=/dev/null")
} else if opts.UserKnownHostsFile != "" {
sshArgs = append(sshArgs, "-o", "UserKnownHostsFile="+opts.UserKnownHostsFile)
}
sshArgs = append(sshArgs, hostName)
sshArgs = append(sshArgs, opts.AdditionalArgs...)
log.Debugf(ctx, "Launching SSH client: ssh %s", strings.Join(sshArgs, " "))
sshCmd := exec.CommandContext(ctx, "ssh", sshArgs...)
sshCmd.Stdin = os.Stdin
sshCmd.Stdout = os.Stdout
sshCmd.Stderr = os.Stderr
return sshCmd.Run()
}
func runSSHProxy(ctx context.Context, client *databricks.WorkspaceClient, serverPort int, clusterID string, opts ClientOptions) error {
createConn := func(ctx context.Context, connID string) (*websocket.Conn, error) {
return createWebsocketConnection(ctx, client, connID, clusterID, serverPort, opts.Liteswap)
}
requestHandoverTick := func() <-chan time.Time {
return time.After(opts.HandoverTimeout)
}
return proxy.RunClientProxy(ctx, os.Stdin, os.Stdout, requestHandoverTick, createConn)
}
func checkClusterState(ctx context.Context, client *databricks.WorkspaceClient, clusterID string, autoStart bool) error {
sp := cmdio.NewSpinner(ctx)
sp.TrackElapsedTime()
defer sp.Close()
if autoStart {
sp.Update("Ensuring the cluster is running...")
err := client.Clusters.EnsureClusterIsRunning(ctx, clusterID)
if err != nil {
return fmt.Errorf("failed to ensure that the cluster is running: %w", err)
}
} else {
sp.Update("Checking cluster state...")
cluster, err := client.Clusters.GetByClusterId(ctx, clusterID)
if err != nil {
return fmt.Errorf("failed to get cluster info: %w", err)
}
if cluster.State != compute.StateRunning {
return fmt.Errorf("cluster %s is not running, current state: %s. Use --auto-start-cluster to start it automatically", clusterID, cluster.State)
}
}
return nil
}
// waitForJobToStart polls the task status until the SSH server task is in RUNNING state or terminates.
// Returns an error if the task fails to start or if polling times out.
func waitForJobToStart(ctx context.Context, client *databricks.WorkspaceClient, runID int64, taskStartupTimeout time.Duration) error {
sp := cmdio.NewSpinner(ctx)
sp.TrackElapsedTime()
defer sp.Close()
sp.Update("Starting SSH server...")
var prevState jobs.RunLifecycleStateV2State
_, err := retries.Poll(ctx, taskStartupTimeout, func() (*jobs.RunTask, *retries.Err) {
run, err := client.Jobs.GetRun(ctx, jobs.GetRunRequest{
RunId: runID,
})
if err != nil {
return nil, retries.Halt(fmt.Errorf("failed to get job run status: %w", err))
}
// Find the SSH server task
var sshTask *jobs.RunTask
for i := range run.Tasks {
if run.Tasks[i].TaskKey == sshServerTaskKey {
sshTask = &run.Tasks[i]
break
}
}
if sshTask == nil {
return nil, retries.Halt(fmt.Errorf("SSH server task '%s' not found in job run", sshServerTaskKey))
}
if sshTask.Status == nil {
return nil, retries.Halt(errors.New("task status is nil"))
}
currentState := sshTask.Status.State
// Update spinner if state changed
if currentState != prevState {
sp.Update(fmt.Sprintf("Starting SSH server... (task: %s)", currentState))
prevState = currentState
}
// Check if task is running
if currentState == jobs.RunLifecycleStateV2StateRunning {
return sshTask, nil
}
// Check for terminal failure states
if currentState == jobs.RunLifecycleStateV2StateTerminated {
return nil, retries.Halt(errors.New("task terminated before reaching running state"))
}
// Continue polling for other states
return nil, retries.Continues(fmt.Sprintf("waiting for task to start (current state: %s)", currentState))
})
return err
}
func ensureSSHServerIsRunning(ctx context.Context, client *databricks.WorkspaceClient, version, secretScopeName string, opts ClientOptions) (string, int, string, error) {
sessionID := opts.SessionIdentifier()
// For dedicated clusters, use clusterID; for serverless, it will be read from metadata
clusterID := opts.ClusterID
serverPort, userName, effectiveClusterID, err := getServerMetadata(ctx, client, sessionID, clusterID, version, opts.Liteswap)
if errors.Is(err, errServerMetadata) {
cmdio.LogString(ctx, "Starting SSH server...")
err := submitSSHTunnelJob(ctx, client, version, secretScopeName, opts)
if err != nil {
return "", 0, "", fmt.Errorf("failed to submit and start ssh server job: %w", err)
}
sp := cmdio.NewSpinner(ctx)
sp.TrackElapsedTime()
sp.Update("Waiting for the SSH server to start...")
maxRetries := 30
for retries := range maxRetries {
if ctx.Err() != nil {
sp.Close()
return "", 0, "", ctx.Err()
}
serverPort, userName, effectiveClusterID, err = getServerMetadata(ctx, client, sessionID, clusterID, version, opts.Liteswap)
if err == nil {
sp.Close()
break
} else if retries < maxRetries-1 {
time.Sleep(2 * time.Second)
} else {
sp.Close()
return "", 0, "", fmt.Errorf("failed to start the ssh server: %w", err)
}
}
} else if err != nil {
return "", 0, "", err
}
return userName, serverPort, effectiveClusterID, nil
}
// resolveServerlessSession handles auto-generation and reconnection for serverless sessions.
// It checks local state for existing sessions matching the workspace and accelerator,
// probes them to see if they're still alive, and prompts the user to reconnect or create new.
func resolveServerlessSession(ctx context.Context, client *databricks.WorkspaceClient, opts *ClientOptions) error {
version := build.GetInfo().Version
matching, err := sessions.FindMatching(ctx, client.Config.Host, opts.Accelerator)
if err != nil {
log.Warnf(ctx, "Failed to load session state: %v", err)
}
// Probe sessions to find alive ones (limit to 5 most recent to avoid latency).
const maxProbe = 5
if len(matching) > maxProbe {
matching = matching[len(matching)-maxProbe:]
}
var alive []sessions.Session
for _, s := range matching {
_, _, _, probeErr := getServerMetadata(ctx, client, s.Name, s.ClusterID, version, opts.Liteswap)
if probeErr == nil {
alive = append(alive, s)
} else {
cleanupStaleSession(ctx, client, s, version)
}
}
if len(alive) > 0 && cmdio.IsPromptSupported(ctx) {
choices := make([]string, 0, len(alive)+1)
for _, s := range alive {
choices = append(choices, fmt.Sprintf("Reconnect to %s (started %s)", s.Name, s.CreatedAt.Format(time.RFC822)))
}
choices = append(choices, "Create new session")
choice, choiceErr := cmdio.AskSelect(ctx, "Found existing sessions:", choices)
if choiceErr != nil {
return fmt.Errorf("failed to prompt user: %w", choiceErr)
}
for i, s := range alive {
if choice == choices[i] {
opts.ConnectionName = s.Name
cmdio.LogString(ctx, "Reconnecting to session: "+s.Name)
return nil
}
}
}
// No alive session selected — generate a new name.
opts.ConnectionName = sessions.GenerateSessionName(opts.Accelerator)
cmdio.LogString(ctx, "Creating new session: "+opts.ConnectionName)
return nil
}
// cleanupStaleSession removes all local and remote artifacts for a stale session.
func cleanupStaleSession(ctx context.Context, client *databricks.WorkspaceClient, s sessions.Session, version string) {
// Remove local SSH keys.
keyPath, err := keys.GetLocalSSHKeyPath(ctx, s.Name, "")
if err == nil {
os.RemoveAll(filepath.Dir(keyPath))
}
// Remove SSH config entry.
if err := sshconfig.RemoveHostConfig(ctx, s.Name); err != nil {
log.Debugf(ctx, "Failed to remove SSH config for %s: %v", s.Name, err)
}
// Delete secret scope (best-effort).
me, err := client.CurrentUser.Me(ctx)
if err == nil {
scopeName := fmt.Sprintf("%s-%s-ssh-tunnel-keys", me.UserName, s.Name)
deleteErr := client.Secrets.DeleteScope(ctx, workspace.DeleteScope{Scope: scopeName})
if deleteErr != nil {
log.Debugf(ctx, "Failed to delete secret scope %s: %v", scopeName, deleteErr)
}
}
// Remove workspace content directory (best-effort).
contentDir, err := sshWorkspace.GetWorkspaceContentDir(ctx, client, version, s.Name)
if err == nil {
deleteErr := client.Workspace.Delete(ctx, workspace.Delete{Path: contentDir, Recursive: true})
if deleteErr != nil {
log.Debugf(ctx, "Failed to delete workspace content for %s: %v", s.Name, deleteErr)
}
}
// Remove from local state.
if err := sessions.Remove(ctx, s.Name); err != nil {
log.Debugf(ctx, "Failed to remove session %s from state: %v", s.Name, err)
}
log.Infof(ctx, "Cleaned up stale session: %s", s.Name)
}