/
zsync.go
318 lines (276 loc) · 7.47 KB
/
zsync.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2017, The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"os/user"
"strings"
"sync"
"time"
"github.com/dsnet/golib/cron"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
gomail "gopkg.in/gomail.v2"
)
type dataset struct {
name string
target execTarget
latestSnapshot *atomicString
}
// PoolPath returns the pool path (e.g., ""//host/pool").
func (ds dataset) PoolPath() string {
pool := ds.name
if i := strings.IndexByte(pool, '/'); i >= 0 {
pool = pool[:i]
}
return fmt.Sprintf("//%s/%s", ds.target.host, pool)
}
// DatasetPath returns the dataset path (e.g., "//host/pool/dataset").
func (ds dataset) DatasetPath() string {
return fmt.Sprintf("//%s/%s", ds.target.host, ds.name)
}
// SnapshotPath returns the snapshot path (e.g., "//host/pool/dataset@snap").
func (ds dataset) SnapshotPath(s string) string {
return fmt.Sprintf("%s@%s", ds.DatasetPath(), s)
}
// SnapshotName returns the snapshot name (e.g., "pool/dataset@snap").
func (ds dataset) SnapshotName(s string) string {
return fmt.Sprintf("%s@%s", ds.name, s)
}
type zsyncer struct {
log *log.Logger
replSema chan struct{}
ctx context.Context
cancel context.CancelFunc
smtp smtpConfig
http httpConfig
poolMonitors map[string]*poolMonitor
replicaManagers map[string]*replicaManager
snapshotManagers map[string]*snapshotManager
}
func newZSyncer(conf config, logger *log.Logger) *zsyncer {
ctx, cancel := context.WithCancel(context.Background())
zs := &zsyncer{
log: logger,
replSema: make(chan struct{}, conf.ConcurrentTransfers),
ctx: ctx,
cancel: cancel,
smtp: conf.SMTP,
http: conf.HTTP,
poolMonitors: make(map[string]*poolMonitor),
replicaManagers: make(map[string]*replicaManager),
snapshotManagers: make(map[string]*snapshotManager),
}
// Process SSH-related configuration options.
var (
auth []ssh.AuthMethod
hostKeys ssh.HostKeyCallback
keepAlive keepAliveConfig
localAliases []string
)
// Parse all of the private keys.
var keys []ssh.Signer
for _, kf := range conf.SSH.KeyFiles {
b, err := ioutil.ReadFile(kf)
if err != nil {
logger.Fatalf("private key error: %v", err)
}
k, err := ssh.ParsePrivateKey(b)
if err != nil {
logger.Fatalf("private key error: %v", err)
}
keys = append(keys, k)
}
if len(keys) > 0 {
auth = append(auth, ssh.PublicKeys(keys...))
}
// Parse all of the host public keys.
if len(conf.SSH.KnownHostFiles) == 0 && conf.SSH.KnownHostFiles != nil {
hostKeys = ssh.InsecureIgnoreHostKey()
} else if len(conf.SSH.KnownHostFiles) > 0 {
var err error
hostKeys, err = knownhosts.New(conf.SSH.KnownHostFiles...)
if err != nil {
logger.Fatalf("public key error: %v", err)
}
}
keepAlive = *conf.SSH.KeepAlive
localAliases = conf.SSH.LocalhostAliases
// Process each of the dataset sources and their mirrors.
for _, ds := range conf.Datasets {
makeDataset := func(dp datasetPath) dataset {
ds := dataset{
name: strings.Trim(dp.Path, "/"),
target: execTarget{host: dp.Hostname()},
latestSnapshot: new(atomicString),
}
isLocalhost := ds.target.host == "localhost"
for _, alias := range localAliases {
isLocalhost = isLocalhost || ds.target.host == alias
}
if isLocalhost {
ds.target.isLocalhost = true
return ds
}
// Setup all parameters for SSH.
ds.target.port = dp.Port()
if ds.target.port == "" {
ds.target.port = "22"
}
ds.target.auth = append([]ssh.AuthMethod{}, auth...)
ds.target.hostKeys = hostKeys
if dp.User != nil {
ds.target.user = dp.User.Username()
if p, ok := dp.User.Password(); ok {
ds.target.auth = append(ds.target.auth, ssh.Password(p))
}
} else {
u, err := user.Current()
if err != nil {
logger.Fatalf("unexpected error: %v", err)
}
ds.target.user = u.Username
}
ds.target.keepAlive = keepAlive
if len(ds.target.auth) == 0 {
logger.Fatal("no authentication methods specified")
}
if ds.target.hostKeys == nil {
logger.Fatal("no hostkey callback specified")
}
return ds
}
src := makeDataset(ds.Source)
var dsts []dataset
for _, dp := range ds.Mirrors {
dsts = append(dsts, makeDataset(dp))
}
// Parse replica manager options.
sendFlags, recvFlags := conf.SendFlags, conf.RecvFlags
if ds.SendFlags != nil {
sendFlags = ds.SendFlags
}
if ds.RecvFlags != nil {
recvFlags = ds.RecvFlags
}
// Parse snapshot manager options.
var ssOpts snapshotOptions
if as := ds.AutoSnapshot; as != nil {
ssOpts = *as
} else if as := conf.AutoSnapshot; as != nil {
ssOpts = *as
}
sched, err := cron.ParseSchedule(ssOpts.Cron)
if err != nil {
logger.Fatalf("could not parse: %v", err)
}
tz, err := time.LoadLocation(ssOpts.TimeZone)
if err != nil {
logger.Fatalf("invalid timezone: %v", err)
}
// Register the pool monitors, dataset replicator, and dataset manager.
zs.RegisterPoolMonitors(src, dsts)
zs.RegisterReplicaManager(src, dsts, sendFlags, recvFlags)
zs.RegisterSnapshotManager(src, dsts, sched, tz, ssOpts.Count, ssOpts.SkipEmpty)
}
return zs
}
func (zs *zsyncer) Run() {
if zs.http.Address != "" {
go zs.ServeHTTP()
}
var wg sync.WaitGroup
wg.Add(len(zs.poolMonitors) + len(zs.replicaManagers) + len(zs.snapshotManagers))
wrapFunc := func(f func()) {
defer wg.Done()
f()
}
for _, pm := range zs.poolMonitors {
go wrapFunc(pm.Run)
}
for _, rm := range zs.replicaManagers {
go wrapFunc(rm.Run)
}
for _, sm := range zs.snapshotManagers {
go wrapFunc(sm.Run)
}
wg.Wait()
}
func (zs *zsyncer) Close() error {
zs.cancel()
return nil
}
// zsyncError is a wrapper for only recovering panics from checkError.
type zsyncError struct{ error }
func recoverError(f func(error)) {
ex := recover()
if ze, ok := ex.(zsyncError); ok {
f(ze.error)
} else if ex != nil {
panic(ex)
}
}
func checkError(err error) {
if err != nil {
panic(zsyncError{err})
}
}
func trySignal(c chan<- struct{}) {
select {
case c <- struct{}{}:
default:
}
}
func indentLines(s string) string {
ss := strings.Split(strings.Trim(s, "\n"), "\n")
return "\t" + strings.Join(ss, "\n\t")
}
// timeoutAfter returns the timeout to wait before retrying an operation.
// It takes the previous timeout (starting at zero) as input.
func timeoutAfter(d time.Duration) time.Duration {
switch {
case d < 1*time.Minute:
return 1 * time.Minute
case d < 5*time.Minute:
return 5 * time.Minute
case d < 10*time.Minute:
return 10 * time.Minute
case d < 30*time.Minute:
return 30 * time.Minute
default:
return 60 * time.Minute
}
}
func sendEmail(smtp smtpConfig, subject, body string) error {
msg := gomail.NewMessage()
msg.SetAddressHeader("From", smtp.Username, "ZSync Daemon")
msg.SetAddressHeader("To", smtp.ToAddress, smtp.ToName)
msg.SetHeader("Subject", subject)
msg.SetBody("text/html", body)
d := gomail.NewDialer(smtp.Host, smtp.Port, smtp.Username, smtp.Password)
return d.DialAndSend(msg)
}
type (
funcReader func([]byte) (int, error)
funcWriter func([]byte) (int, error)
)
func (f funcReader) Read(b []byte) (int, error) { return f(b) }
func (f funcWriter) Write(b []byte) (int, error) { return f(b) }
type atomicString struct {
mu sync.Mutex
v string
}
func (p *atomicString) Load() string {
p.mu.Lock()
defer p.mu.Unlock()
return p.v
}
func (p *atomicString) Store(s string) {
p.mu.Lock()
defer p.mu.Unlock()
p.v = s
}