This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
/
ssh_node.go
125 lines (102 loc) 路 3.21 KB
/
ssh_node.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
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
/*
Copyright The Pharmer Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cloud
import (
"fmt"
"os"
"pharmer.dev/pharmer/cloud"
"pharmer.dev/pharmer/cmds/cloud/options"
"pharmer.dev/pharmer/store"
"github.com/appscode/go/term"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/terminal"
)
func NewCmdSSH() *cobra.Command {
opts := options.NewNodeSSHConfig()
cmd := &cobra.Command{
Use: "node",
Short: "SSH into a Kubernetes cluster instance",
Long: `SSH into a cluster instance.`,
Example: `pharmer ssh node -k cluster-name node-name`,
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
if err := opts.ValidateFlags(cmd, args); err != nil {
term.Fatalln(err)
}
storeProvider, err := store.GetStoreProvider(cmd)
term.ExitOnError(err)
sshConfig, err := cloud.GetSSHConfig(storeProvider, opts.ClusterName, opts.NodeName)
term.ExitOnError(err)
err = OpenShell(sshConfig.PrivateKey, sshConfig.HostIP, sshConfig.HostPort, sshConfig.User)
if err != nil {
term.Fatalln(err)
}
},
}
opts.AddFlags(cmd.Flags())
return cmd
}
// http://stackoverflow.com/questions/26315572/ssh-executing-nsenter-as-remote-command-with-interactive-shell-in-golang-to-debu
func OpenShell(privateKey []byte, addr string, port int32, user string) error {
keySigner, err := ssh.ParsePrivateKey(privateKey)
term.ExitOnError(err)
// Create client config
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(keySigner),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
// Connect to ssh server
conn, err := ssh.Dial("tcp", fmt.Sprintf("%v:%v", addr, port), config)
term.ExitOnError(err)
defer conn.Close()
// Create a session
session, err := conn.NewSession()
term.ExitOnError(err)
// The following two lines makes the terminal work properly because of
// side-effects I don't understand.
fd := int(os.Stdin.Fd())
oldState, err := terminal.MakeRaw(fd)
term.ExitOnError(err)
session.Stdout = os.Stdout
session.Stderr = os.Stderr
session.Stdin = os.Stdin
termWidth, termHeight, err := terminal.GetSize(fd)
term.ExitOnError(err)
// Set up terminal modes
modes := ssh.TerminalModes{
ssh.ECHO: 1, // enable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
// Request pseudo terminal
if err := session.RequestPty("xterm-256color", termHeight, termWidth, modes); err != nil {
term.ExitOnError(err)
}
err = session.Shell()
if err != nil {
return err
}
err = session.Wait()
if err != nil {
return err
}
err = terminal.Restore(fd, oldState)
if err != nil {
return err
}
return nil
}