/
ssh.go
111 lines (89 loc) · 2.83 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
/*
Copyright 2020 The Kubernetes Authors All rights reserved.
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 machine
import (
"fmt"
"os/exec"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/state"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/config"
)
// GetHost find node's host information by name in the given cluster.
func GetHost(api libmachine.API, cc config.ClusterConfig, n config.Node) (*host.Host, error) {
machineName := config.MachineName(cc, n)
host, err := LoadHost(api, machineName)
if err != nil {
return nil, errors.Wrap(err, "host exists and load")
}
currentState, err := host.Driver.GetState()
if err != nil {
return nil, errors.Wrap(err, "state")
}
if currentState != state.Running {
return nil, errors.Errorf("%q is not running", machineName)
}
return host, nil
}
// CreateSSHShell creates a new SSH shell / client
func CreateSSHShell(api libmachine.API, cc config.ClusterConfig, n config.Node, args []string, native bool) error {
host, err := GetHost(api, cc, n)
if err != nil {
return err
}
if native {
ssh.SetDefaultClient(ssh.Native)
} else {
ssh.SetDefaultClient(ssh.External)
}
client, err := host.CreateSSHClient()
if err != nil {
return errors.Wrap(err, "Creating ssh client")
}
return client.Shell(args...)
}
// GetSSHHostAddrPort returns the host address and port for ssh
func GetSSHHostAddrPort(api libmachine.API, cc config.ClusterConfig, n config.Node) (string, int, error) {
host, err := GetHost(api, cc, n)
if err != nil {
return "", 0, err
}
addr, err := host.Driver.GetSSHHostname()
if err != nil {
return "", 0, err
}
port, err := host.Driver.GetSSHPort()
if err != nil {
return "", 0, err
}
return addr, port, nil
}
// RunSSHHostCommand runs a command to the SSH host
func RunSSHHostCommand(api libmachine.API, cc config.ClusterConfig, n config.Node, command string, args []string) (string, error) {
addr, port, err := GetSSHHostAddrPort(api, cc, n)
if err != nil {
return "", err
}
cmdPath, err := exec.LookPath(command)
if err != nil {
return "", err
}
args = append(args, "-p")
args = append(args, fmt.Sprintf("%d", port))
args = append(args, addr)
cmd := exec.Command(cmdPath, args...)
output, err := cmd.Output()
return string(output), err
}