This repository has been archived by the owner on Jun 22, 2021. It is now read-only.
/
nodessh.go
60 lines (49 loc) · 1.5 KB
/
nodessh.go
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
package cmd
import (
"fmt"
"github.com/rajch/kutti/cmd/kutti/defaults"
"github.com/rajch/kutti/internal/pkg/kuttilog"
"github.com/rajch/kutti/pkg/sshclient"
"github.com/spf13/cobra"
)
// nodesshCmd represents the nodessh command
var nodesshCmd = &cobra.Command{
Use: "ssh NODENAME",
Aliases: []string{"connect", "attach"},
Short: "Open an SSH connection to the node",
Long: `Open an SSH connection to the node.`,
Run: nodesshCommand,
Args: nodenameonlyargs,
SilenceErrors: true,
DisableFlagsInUseLine: true,
}
func init() {
nodeCmd.AddCommand(nodesshCmd)
nodesshCmd.Flags().StringP("cluster", "c", defaults.Getdefault("cluster"), "cluster name")
}
func nodesshCommand(cmd *cobra.Command, args []string) {
cluster, err := getCluster(cmd)
if err != nil {
kuttilog.Printf(0, "Error: %v.", err)
return
}
nodename := args[0]
node, ok := cluster.Nodes[nodename]
if !ok {
kuttilog.Printf(0, "Error: Node '%s' not found.\n", nodename)
return
}
if node.Status() != "Running" {
kuttilog.Printf(0, "Error: Node '%s' is not running.\n", nodename)
return
}
sshport, ok := node.Ports[22]
if !ok {
kuttilog.Printf(0, "Error: The SSH port of node '%s' has not been forwarded.\n", nodename)
return
}
kuttilog.Printf(2, "Connecting to node %s...", nodename)
address := fmt.Sprintf("localhost:%v", sshport)
client := sshclient.New("user1", "Pass@word1")
client.RunInterativeShell(address)
}