@@ -2,6 +2,8 @@ package cmd
2
2
3
3
import (
4
4
"fmt"
5
+ "os"
6
+ "path/filepath"
5
7
6
8
"github.com/manifoldco/promptui"
7
9
"github.com/pkg/errors"
@@ -10,7 +12,10 @@ import (
10
12
)
11
13
12
14
func (r * runners ) InitVMSSH (parent * cobra.Command ) * cobra.Command {
13
- var sshUser string
15
+ var (
16
+ sshUser string
17
+ sshIdentityFile string
18
+ )
14
19
15
20
cmd := & cobra.Command {
16
21
Use : "ssh [VM_ID]" ,
@@ -29,6 +34,9 @@ Note: Only running VMs can be connected to via SSH.`,
29
34
Example : `# SSH into a specific VM by ID
30
35
replicated vm ssh <id>
31
36
37
+ # SSH with a specified identity file
38
+ replicated vm ssh <id> -i ~/.ssh/id_rsa
39
+
32
40
# SSH into a VM with a specific user
33
41
replicated vm ssh <id> -u myuser
34
42
@@ -41,7 +49,7 @@ replicated vm ssh`,
41
49
parent .AddCommand (cmd )
42
50
43
51
cmd .Flags ().StringVarP (& sshUser , "user" , "u" , "" , "SSH user to connect with" )
44
-
52
+ cmd . Flags (). StringVarP ( & sshIdentityFile , "identity-file" , "i" , "" , "SSH identity file to use" )
45
53
return cmd
46
54
}
47
55
@@ -56,6 +64,18 @@ func (r *runners) sshVM(cmd *cobra.Command, args []string) error {
56
64
}
57
65
58
66
sshUser , _ := cmd .Flags ().GetString ("user" )
67
+ sshIdentityFile , _ := cmd .Flags ().GetString ("identity-file" )
68
+
69
+ if sshIdentityFile != "" {
70
+ sshIdentityFile , err := filepath .Abs (sshIdentityFile )
71
+ if err != nil {
72
+ return errors .Wrap (err , "failed to get absolute path for identity file" )
73
+ }
74
+ _ , err = os .Stat (sshIdentityFile )
75
+ if err != nil {
76
+ return errors .Wrap (err , "identity file does not exist or is not accessible" )
77
+ }
78
+ }
59
79
60
80
// Get VM ID - either directly provided or selected
61
81
var vmID string
@@ -96,7 +116,7 @@ func (r *runners) sshVM(cmd *cobra.Command, args []string) error {
96
116
vmID = selectedVM .ID
97
117
}
98
118
99
- return r .kotsAPI .SSHIntoVM (vmID , sshUser )
119
+ return r .kotsAPI .SSHIntoVM (vmID , sshUser , sshIdentityFile )
100
120
}
101
121
102
122
// handleNoRunningVMs handles the case when no running VMs are found
0 commit comments