Skip to content

Commit c5f822f

Browse files
committed
feat: add crc ssh and crc vm stats commands
1 parent 9fe03b5 commit c5f822f

File tree

5 files changed

+592
-0
lines changed

5 files changed

+592
-0
lines changed

cmd/crc/cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
cmdBundle "github.com/crc-org/crc/v2/cmd/crc/cmd/bundle"
1616
cmdConfig "github.com/crc-org/crc/v2/cmd/crc/cmd/config"
17+
cmdVM "github.com/crc-org/crc/v2/cmd/crc/cmd/vm"
1718
crcConfig "github.com/crc-org/crc/v2/pkg/crc/config"
1819
"github.com/crc-org/crc/v2/pkg/crc/constants"
1920
crcErr "github.com/crc-org/crc/v2/pkg/crc/errors"
@@ -71,6 +72,7 @@ func init() {
7172
// subcommands
7273
rootCmd.AddCommand(cmdConfig.GetConfigCmd(config))
7374
rootCmd.AddCommand(cmdBundle.GetBundleCmd(config))
75+
rootCmd.AddCommand(cmdVM.GetVMCmd(config))
7476

7577
logging.AddLogLevelFlag(rootCmd.PersistentFlags())
7678
}

cmd/crc/cmd/root_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ func TestCrcManPageGenerator_WhenInvoked_GeneratesManPagesForAllCrcSubCommands(t
3838
"crc-oc-env.1",
3939
"crc-podman-env.1",
4040
"crc-setup.1",
41+
"crc-ssh.1",
4142
"crc-start.1",
4243
"crc-status.1",
4344
"crc-stop.1",
4445
"crc-version.1",
46+
"crc-vm-stats.1",
47+
"crc-vm.1",
4548
"crc.1",
4649
}, manPagesFiles)
4750
}

cmd/crc/cmd/ssh.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"strconv"
8+
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func init() {
13+
rootCmd.AddCommand(sshCmd)
14+
}
15+
16+
var sshCmd = &cobra.Command{
17+
Use: "ssh [-- COMMAND...]",
18+
Short: "Open an SSH connection to the OpenShift cluster node",
19+
Long: "Open an SSH connection to the OpenShift cluster node. Pass commands after -- to execute them remotely.",
20+
RunE: func(_ *cobra.Command, args []string) error {
21+
return runSSH(args)
22+
},
23+
}
24+
25+
func runSSH(args []string) error {
26+
client := newMachine()
27+
if err := checkIfMachineMissing(client); err != nil {
28+
return err
29+
}
30+
31+
connectionDetails, err := client.ConnectionDetails()
32+
if err != nil {
33+
return err
34+
}
35+
36+
sshPath, err := exec.LookPath("ssh")
37+
if err != nil {
38+
return fmt.Errorf("cannot find ssh binary: %w", err)
39+
}
40+
41+
var sshKey string
42+
for _, key := range connectionDetails.SSHKeys {
43+
if _, err := os.Stat(key); err == nil {
44+
sshKey = key
45+
break
46+
}
47+
}
48+
if sshKey == "" {
49+
return fmt.Errorf("no SSH key found")
50+
}
51+
52+
sshArgs := []string{
53+
"ssh",
54+
"-o", "StrictHostKeyChecking=no",
55+
"-o", "UserKnownHostsFile=/dev/null",
56+
"-o", "LogLevel=ERROR",
57+
"-i", sshKey,
58+
"-p", strconv.Itoa(connectionDetails.SSHPort),
59+
fmt.Sprintf("%s@%s", connectionDetails.SSHUsername, connectionDetails.IP),
60+
}
61+
62+
if len(args) > 0 {
63+
sshArgs = append(sshArgs, args...)
64+
}
65+
66+
cmd := exec.Command(sshPath, sshArgs[1:]...)
67+
cmd.Stdin = os.Stdin
68+
cmd.Stdout = os.Stdout
69+
cmd.Stderr = os.Stderr
70+
return cmd.Run()
71+
}

0 commit comments

Comments
 (0)