/
ssh.go
141 lines (116 loc) · 3.2 KB
/
ssh.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package diagnostics
import (
"context"
"crypto/rsa"
"fmt"
"io/ioutil"
"net"
"strconv"
"time"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"github.com/openshift/openshift-azure/pkg/api"
"github.com/openshift/openshift-azure/pkg/cluster/names"
"github.com/openshift/openshift-azure/pkg/util/azureclient"
"github.com/openshift/openshift-azure/pkg/util/azureclient/network"
)
type ssher struct {
pipcli network.PublicIPAddressesClient
masterIPs []string
sshkey *rsa.PrivateKey
}
func NewSSHer(ctx context.Context, log *logrus.Entry, subscriptionID, resourceGroup string, sshkey *rsa.PrivateKey) (*ssher, error) {
authorizer, err := azureclient.GetAuthorizerFromContext(ctx, api.ContextKeyClientAuthorizer)
if err != nil {
return nil, err
}
s := &ssher{
pipcli: network.NewPublicIPAddressesClient(ctx, log, subscriptionID, authorizer),
sshkey: sshkey,
}
ips, err := s.pipcli.ListVirtualMachineScaleSetPublicIPAddressesComplete(ctx, resourceGroup, "ss-master")
if err != nil {
return nil, err
}
for ips.NotDone() {
s.masterIPs = append(s.masterIPs, *ips.Value().IPAddress)
err = ips.Next()
if err != nil {
return nil, err
}
}
return s, nil
}
func (s *ssher) dialViaMaster(ctx context.Context, config *ssh.ClientConfig, addr string) (net.Conn, error) {
for _, ip := range s.masterIPs {
client, err := ssh.Dial("tcp", ip+":22", config)
if err != nil {
continue
}
return client.Dial("tcp", addr)
}
return nil, fmt.Errorf("couldn't dial any master")
}
func (s *ssher) Dial(ctx context.Context, hostname string) (*ssh.Client, error) {
signer, err := ssh.NewSignerFromKey(s.sshkey)
if err != nil {
return nil, err
}
config := &ssh.ClientConfig{
User: "cloud-user",
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil },
Timeout: 10 * time.Second,
}
switch names.GetAgentRole(hostname) {
case api.AgentPoolProfileRoleMaster:
_, instanceID, err := names.GetScaleSetNameAndInstanceID(hostname)
if err != nil {
return nil, err
}
instance, err := strconv.ParseUint(instanceID, 36, 64)
if err != nil {
return nil, err
}
if int(instance) > len(s.masterIPs) {
return nil, fmt.Errorf("couldn't find IP for master")
}
return ssh.Dial("tcp", s.masterIPs[int(instance)]+":22", config)
default:
conn, err := s.dialViaMaster(ctx, config, hostname+":22")
if err != nil {
return nil, err
}
c, chans, reqs, err := ssh.NewClientConn(conn, hostname, config)
if err != nil {
return nil, err
}
return ssh.NewClient(c, chans, reqs), nil
}
}
func (s *ssher) RunRemoteCommandAndSaveToFile(client *ssh.Client, cmd, filename string) error {
result, err := s.RunRemoteCommand(client, cmd)
if err != nil {
return err
}
return ioutil.WriteFile(filename, result, 0666)
}
func (s *ssher) RunRemoteCommand(client *ssh.Client, cmd string) ([]byte, error) {
session, err := client.NewSession()
if err != nil {
return nil, err
}
defer session.Close()
stdout, err := session.StdoutPipe()
if err != nil {
return nil, err
}
err = session.Start(cmd)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(stdout)
return b, err
}