-
Notifications
You must be signed in to change notification settings - Fork 233
/
ssh.go
86 lines (69 loc) · 2.35 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
package ssh
import (
"encoding/base64"
"fmt"
"os"
"github.com/code-ready/crc/pkg/crc/constants"
"github.com/code-ready/crc/pkg/crc/logging"
"github.com/code-ready/machine/libmachine/drivers"
)
type SSHRunner struct {
driver drivers.Driver
privateSSHKey string
}
func CreateRunner(driver drivers.Driver) *SSHRunner {
return CreateRunnerWithPrivateKey(driver, constants.GetPrivateKeyPath())
}
func CreateRunnerWithPrivateKey(driver drivers.Driver, privateKey string) *SSHRunner {
return &SSHRunner{driver: driver, privateSSHKey: privateKey}
}
// Create a host using the driver's config
func (runner *SSHRunner) Run(command string) (string, error) {
return runner.runSSHCommandFromDriver(command, false)
}
func (runner *SSHRunner) SetTextContentAsRoot(destFilename string, content string, mode os.FileMode) error {
logging.Debugf("Creating %s with permissions 0%o in the CRC VM", destFilename, mode)
command := fmt.Sprintf("sudo install -m 0%o /dev/null %s && cat <<EOF | sudo tee %s\n%s\nEOF", mode, destFilename, destFilename, content)
_, err := runner.RunPrivate(command)
return err
}
func (runner *SSHRunner) RunPrivate(command string) (string, error) {
return runner.runSSHCommandFromDriver(command, true)
}
func (runner *SSHRunner) SetPrivateKeyPath(path string) {
runner.privateSSHKey = path
}
func (runner *SSHRunner) CopyData(data []byte, destFilename string) error {
base64Data := base64.StdEncoding.EncodeToString(data)
command := fmt.Sprintf("echo %s | base64 --decode | sudo tee %s > /dev/null", base64Data, destFilename)
_, err := runner.Run(command)
return err
}
func (runner *SSHRunner) runSSHCommandFromDriver(command string, runPrivate bool) (string, error) {
client, err := drivers.GetSSHClientFromDriver(runner.driver, runner.privateSSHKey)
if err != nil {
return "", err
}
if runPrivate {
logging.Debugf("About to run SSH command with hidden output")
} else {
logging.Debugf("About to run SSH command:\n%s", command)
}
output, err := client.Output(command)
if runPrivate {
if err != nil {
logging.Debugf("SSH command failed")
} else {
logging.Debugf("SSH command succeeded")
}
} else {
logging.Debugf("SSH command results: err: %v, output: %s", err, output)
}
if err != nil {
return "", fmt.Errorf(`ssh command error:
command : %s
err : %v
output : %s`, command, err, output)
}
return output, nil
}