/
remote.go
67 lines (51 loc) · 1.55 KB
/
remote.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
package remote
import (
"fmt"
"os/exec"
"github.com/sirupsen/logrus"
)
// Client - wrapper to run bash commands over ssh
type Client struct {
// ConnectionString - user@host for ssh command
ConnectionString string
log *logrus.Entry
}
func (c *Client) String() string {
return c.ConnectionString
}
// Exec - run command over ssh
func (c *Client) Exec(cmd string) (string, error) {
l := c.log.WithField("func", "Exec()")
l.Info(cmd)
out, err := exec.Command("ssh", c.ConnectionString, cmd).CombinedOutput()
if err != nil {
return "", fmt.Errorf("Command 'ssh %s %s' error: %s; out: %s", c.ConnectionString, cmd, err, out)
}
return fmt.Sprintf("%s", out), nil
}
// CopyFiles - copy local files to remote server
func (c *Client) CopyFiles(from, to string) error {
l := c.log.WithField("func", "CopyFiles()")
toAddress := fmt.Sprintf("%s:%s", c.ConnectionString, to)
l.Infof("scp %s %s\n", from, toAddress)
if out, err := exec.Command("scp", from, toAddress).CombinedOutput(); err != nil {
return fmt.Errorf("Command 'scp %s %s' error: %s; out: %s", from, toAddress, err, out)
}
return nil
}
// NewClient - create new SSH remote client
func NewClient(connectionString string, log *logrus.Entry) (*Client, error) {
l := log.WithFields(logrus.Fields{
"address": connectionString,
"cmp": "remote",
})
client := &Client{
ConnectionString: connectionString,
log: l,
}
_, err := client.Exec("date")
if err != nil {
return nil, fmt.Errorf("Failed to validate %s connection: %s", connectionString, err)
}
return client, nil
}