-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
170 lines (148 loc) · 3.55 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package ssh
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/soundtrackyourbrand/ssh"
utilsRun "github.com/zond/sybutils/utils/run"
)
func ParseCreds(user string, b []byte) (result Creds, err error) {
k, err := ssh.ParsePrivateKey(b)
if err != nil {
return
}
result = Creds{
keys: []ssh.Signer{k},
user: user,
}
return
}
type Creds struct {
keys []ssh.Signer
user string
}
func (self Creds) Key(i int) (key ssh.PublicKey, err error) {
if i < len(self.keys) {
key = self.keys[i].PublicKey()
}
return
}
func (self Creds) Sign(i int, rand io.Reader, data []byte) (sig []byte, err error) {
return self.keys[i].Sign(rand, data)
}
func Rsync(user string, key []byte, addr, src, dst string, excludes ...string) (err error) {
creds, err := ParseCreds(user, key)
if err != nil {
return
}
if err = Run(creds, fmt.Sprintf("%v:22", addr), fmt.Sprintf("mkdir -p %#v", dst)); err != nil {
return
}
tmpdir, err := ioutil.TempDir("", "utilsSshRsync")
if err != nil {
return
}
defer func() {
if err == nil {
os.RemoveAll(tmpdir)
}
}()
outfilename := filepath.Join(tmpdir, "identity")
outfile, err := os.Create(outfilename)
if err != nil {
return
}
if _, err = outfile.Write(key); err != nil {
return
}
if err = outfile.Close(); err != nil {
return
}
if err = os.Chmod(outfilename, 0400); err != nil {
return
}
params := []string{"rsync", fmt.Sprintf("%#v", fmt.Sprintf("--rsh=ssh -i %#v -o \"StrictHostKeyChecking no\"", outfilename)), "--progress", "-avzc", "-C", "--delete"}
for _, excl := range excludes {
params = append(params, "--exclude", excl)
}
params = append(params, fmt.Sprintf("%v", src), fmt.Sprintf("%v@%v:%v", user, addr, dst))
if err = utilsRun.Run("sh", "-c", strings.Join(params, " ")+" 2>&1"); err != nil {
return
}
return
}
func TarCopy(creds Creds, addr, src, dst string, excludes ...string) (err error) {
sess, err := New(creds, addr)
if err != nil {
return
}
params := []string{}
for _, exclude := range excludes {
params = append(params, "--exclude", exclude)
}
params = append(params, "-c", "-z", "-C", filepath.Dir(src), filepath.Base(src))
tar := exec.Command("tar", params...)
pipein, pipeout := io.Pipe()
sess.Stdin, sess.Stdout, sess.Stderr = pipein, os.Stdout, os.Stderr
tar.Stdin, tar.Stdout, tar.Stderr = os.Stdin, pipeout, os.Stderr
remoteDone := make(chan struct{})
go func() {
cmd := fmt.Sprintf("mkdir -p %#v && tar -x -v -z -C %#v", dst, dst)
fmt.Printf(" *** ( %v ) %#v\n", addr, cmd)
if err := sess.Run(cmd); err != nil {
panic(err)
}
close(remoteDone)
}()
fmt.Printf(" ( *** %v ) %v", utilsRun.Host, "tar")
for _, bit := range params {
fmt.Printf(" %#v", bit)
}
fmt.Println("")
if err = tar.Run(); err != nil {
return
}
if err = pipeout.Close(); err != nil {
return
}
<-remoteDone
return
}
func Run(creds Creds, addr, cmd string) (err error) {
sess, err := New(creds, addr)
if err != nil {
return
}
in, out := io.Pipe()
sess.Stdin, sess.Stdout, sess.Stderr = in, os.Stdout, os.Stderr
remoteDone := make(chan struct{})
go func() {
fmt.Printf(" *** ( %v ) %#v\n", addr, cmd)
if err = sess.Run(cmd); err != nil {
return
}
close(remoteDone)
}()
if err = out.Close(); err != nil {
return
}
<-remoteDone
return
}
func New(creds Creds, addr string) (result *ssh.Session, err error) {
sshConn, err := ssh.Dial("tcp", addr, &ssh.ClientConfig{
User: creds.user,
Auth: []ssh.ClientAuth{
ssh.ClientAuthKeyring(creds),
},
})
if err != nil {
return
}
result, err = sshConn.NewSession()
return
}