forked from whyrusleeping/pinbot
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
586 lines (491 loc) · 13.2 KB
/
main.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package main
import (
"bufio"
"context"
"errors"
"flag"
"fmt"
"math/rand"
"os"
"strings"
"sync"
"time"
cid "github.com/ipfs/go-cid"
shell "github.com/ipfs/go-ipfs-api"
"github.com/ipfs/ipfs-cluster/api"
cluster "github.com/ipfs/ipfs-cluster/api/rest/client"
ma "github.com/multiformats/go-multiaddr"
hb "github.com/whyrusleeping/hellabot"
log "gopkg.in/inconshreveable/log15.v2"
)
var prefix string
var gateway string
var bot *hb.Bot
var msgs chan msgWrap
var retries = 5
type msgWrap struct {
message string
actor string
}
var (
cmdBotsnack = "botsnack"
cmdFriends = "friends"
cmdBefriend = "befriend"
cmdShun = "shun"
cmdPin = "pin"
cmdUnPin = "unpin"
cmdStatus = "status"
cmdOngoing = "ongoing"
cmdRecover = "recover"
cmdPinLegacy = "legacypin"
cmdUnpinLegacy = "legacyunpin"
)
var (
friends FriendsList
r *rand.Rand
)
func init() {
r = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
}
func messageQueueProcess() {
for mWrap := range msgs {
sendMsg(mWrap.actor, mWrap.message)
}
}
func botMsg(actor, msg string) {
msgs <- msgWrap{
message: msg,
actor: actor,
}
}
func sendMsg(actor, msg string) {
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
fmt.Println("recovered from panic, sleeping a bit")
time.Sleep(10 * time.Second)
}
}()
bot.Msg(actor, msg)
}
func formatError(action string, err error) error {
if strings.Contains(err.Error(), "504 Gateway Time-out") {
err = fmt.Errorf("504 Gateway Time-out")
}
if strings.Contains(err.Error(), "502 Bad Gateway") {
err = fmt.Errorf("502 Bad Gateway")
}
return fmt.Errorf("%s failed: %s", action, err.Error())
}
func tryPin(path string, sh *shell.Shell) error {
out, err := sh.Refs(path, true)
if err != nil {
return formatError("refs", err)
}
// throw away results
for range out {
}
err = sh.Pin(path)
if err != nil {
return formatError("pin", err)
}
return nil
}
func tryUnpin(path string, sh *shell.Shell) error {
out, err := sh.Refs(path, true)
if err != nil {
return formatError("refs", err)
}
// throw away results
for range out {
}
err = sh.Unpin(path)
if err != nil {
return formatError("unpin", err)
}
return nil
}
var pinfile = "pins.log"
func writePin(pin, label string) error {
fi, err := os.OpenFile(pinfile, os.O_APPEND|os.O_EXCL|os.O_WRONLY, 0660)
if err != nil {
return err
}
_, err = fmt.Fprintf(fi, "%s\t%s\n", pin, label)
if err != nil {
return err
}
return fi.Close()
}
func Pin(b *hb.Bot, actor, path, label string) {
if !strings.HasPrefix(path, "/ipfs") && !strings.HasPrefix(path, "/ipns") {
path = "/ipfs/" + path
}
errs := make(chan error, len(shs))
var wg sync.WaitGroup
botMsg(actor, fmt.Sprintf("now pinning on %d nodes", len(shs)))
// pin to every node concurrently.
for i, sh := range shs {
wg.Add(1)
go func(i int, sh *shell.Shell) {
defer wg.Done()
if err := tryPin(path, sh); err != nil {
errs <- fmt.Errorf("%s -- %s", shsUrls[i], err)
}
}(i, sh)
}
// close the err chan when done.
go func() {
wg.Wait()
close(errs)
}()
// wait on the err chan and print every err we get as we get it.
var failed int
for err := range errs {
botMsg(actor, err.Error())
failed++
}
successes := len(shs) - failed
botMsg(actor, fmt.Sprintf("pinned on %d of %d nodes (%d failures) -- %s%s",
successes, len(shs), failed, gateway, path))
if err := writePin(path, label); err != nil {
botMsg(actor, fmt.Sprintf("failed to write log entry for last pin: %s", err))
}
clusterPinUnpin(b, actor, path, label, true)
}
func Unpin(b *hb.Bot, actor, path string) {
if !strings.HasPrefix(path, "/ipfs") && !strings.HasPrefix(path, "/ipns") {
path = "/ipfs/" + path
}
errs := make(chan error, len(shs))
var wg sync.WaitGroup
botMsg(actor, fmt.Sprintf("now unpinning on %d nodes", len(shs)))
// pin to every node concurrently.
for i, sh := range shs {
wg.Add(1)
go func(i int, sh *shell.Shell) {
defer wg.Done()
if err := tryUnpin(path, sh); err != nil {
errs <- fmt.Errorf("%s -- %s", shsUrls[i], err)
}
}(i, sh)
}
// close the err chan when done.
go func() {
wg.Wait()
close(errs)
}()
// wait on the err chan and print every err we get as we get it.
var failed int
for err := range errs {
botMsg(actor, err.Error())
failed++
}
successes := len(shs) - failed
botMsg(actor, fmt.Sprintf("unpinned on %d of %d nodes (%d failures) -- %s%s",
successes, len(shs), failed, gateway, path))
clusterPinUnpin(b, actor, path, "", false)
}
// StatusCluster gets cluster status of cid with given path.
func StatusCluster(b *hb.Bot, actor, path string) {
ctx := context.Background()
// pick up a random shell
shell := shs[r.Intn(len(shs))]
c, err := resolveCid(path, shell)
if err != nil {
botMsg(actor, fmt.Sprintf("could not resolve cid: %s", err))
return
}
st, err := lbClient.Status(ctx, c, false)
if err != nil {
botMsg(actor, fmt.Sprintf("error obtaining pin status: %s", err))
return
}
prettyClusterStatus(actor, st)
}
// StatusAllCluster gets status of all items in cluster matching the given
// filter.
func StatusAllCluster(b *hb.Bot, actor string, filter api.TrackerStatus) {
ctx := context.Background()
sts, err := lbClient.StatusAll(ctx, filter, false)
if err != nil {
botMsg(actor, fmt.Sprintf("error obtaining pin statuses: %s", err))
return
}
for _, st := range sts {
prettyClusterStatus(actor, st)
time.Sleep(5 * time.Second) // flood prevention
}
}
func prettyClusterStatus(actor string, st *api.GlobalPinInfo) {
botMsg(actor, fmt.Sprintf("Status for %s:", st.Cid))
for _, info := range st.PeerMap {
botMsg(actor, fmt.Sprintf(" - %s : %s | %s\n", info.PeerName, info.Status, info.Error))
}
}
// PinCluster pins the item with given path to cluster.
func PinCluster(b *hb.Bot, actor, path, label string) {
clusterPinUnpin(b, actor, path, label, true)
if err := writePin(path, label); err != nil {
botMsg(actor, fmt.Sprintf("failed to write log entry for last pin: %s", err))
}
}
// UnpinCluster unpins the item with given path to cluster.
func UnpinCluster(b *hb.Bot, actor, path string) {
clusterPinUnpin(b, actor, path, "", false)
}
// RecoverCluster tries to recover item with give path, if it's previous pin or
// unpin operation was failed.
func RecoverCluster(b *hb.Bot, actor, path string) {
ctx := context.Background()
botMsg(actor, fmt.Sprintf("Recovering pin with path %s", path))
// pick up a random shell
shell := shs[r.Intn(len(shs))]
c, err := resolveCid(path, shell)
if err != nil {
botMsg(actor, fmt.Sprintf("could not determine cid to recover: %s", err))
return
}
if c.String() != path {
botMsg(actor, fmt.Sprintf("%s resolved as %s", path, c))
}
gpi, err := lbClient.Recover(ctx, c, false)
if err != nil {
botMsg(actor, fmt.Sprintf("failed to recover: %s", err))
return
}
botMsg(actor, fmt.Sprintf("Recover operation triggered for %s. You can later manually track the status with !status <cid>", c))
prettyClusterStatus(actor, gpi)
}
func resolveCid(path string, sh *shell.Shell) (cid.Cid, error) {
// fix path
if !strings.HasPrefix(path, "/ipfs") && !strings.HasPrefix(path, "/ipns") {
path = "/ipfs/" + path
}
parts := strings.Split(path, "/")
// Only resolve path if /ipns or has subdirectories
if strings.HasPrefix(path, "/ipns") || len(parts) > 3 {
// resolve it
cidstr, err := sh.ResolvePath(path)
if err != nil {
return cid.Undef, err
}
return cid.Decode(cidstr)
}
// parts is ["", "ipfs", "cid"]
return cid.Decode(parts[2])
}
func waitForClusterOp(actor string, c cid.Cid, target api.TrackerStatus) {
botMsg(actor, fmt.Sprintf("%s: operation submitted. Waiting for status to reach %s", c, target))
ctx, cancel := context.WithTimeout(context.Background(), time.Hour)
defer cancel()
fp := cluster.StatusFilterParams{
Cid: c,
Local: false,
Target: target,
CheckFreq: 5 * time.Second,
}
gpi, err := cluster.WaitFor(ctx, lbClient, fp)
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
botMsg(actor, fmt.Sprintf("%s: still not '%s'. I won't keep watching, but you can run !status <cid> to check manually.", c, target))
return
}
botMsg(actor, fmt.Sprintf("%s: an error happened: %s. You can attempt recovery with !recover <cid>.", c, err))
return
}
done := 0
for _, info := range gpi.PeerMap {
if info.Status == target {
done++
}
}
botMsg(actor, fmt.Sprintf("Reached %s in %d cluster peers: %s/ipfs/%s .", target, done, gateway, c))
}
func clusterPinUnpin(b *hb.Bot, actor, path, label string, pin bool) {
ctx := context.Background()
verb := "pin"
if !pin {
verb = "unpin"
}
botMsg(actor, fmt.Sprintf("Cluster-%sning", verb))
var pinObj *api.Pin
var err error
switch pin {
case true:
pinObj, err = lbClient.PinPath(ctx, path, api.PinOptions{Name: label})
if err == nil {
go waitForClusterOp(actor, pinObj.Cid, api.TrackerStatusPinned)
}
case false:
pinObj, err = lbClient.UnpinPath(ctx, path)
if err == nil {
go waitForClusterOp(actor, pinObj.Cid, api.TrackerStatusUnpinned)
}
}
if err != nil {
botMsg(actor, fmt.Sprintf("failed to %s in cluster: %s", verb, err))
return
}
}
var shs []*shell.Shell
var shsUrls []string
var lbClient cluster.Client
func loadHosts(file string) []string {
fi, err := os.Open(file)
if err != nil {
fmt.Printf("failed to open %s hosts file, defaulting to localhost:5001\n", file)
return []string{"/ip4/127.0.0.1/tcp/5001"}
}
var hosts []string
scan := bufio.NewScanner(fi)
for scan.Scan() {
hosts = append(hosts, scan.Text())
}
return hosts
}
func ensurePinLogExists() error {
_, err := os.Stat(pinfile)
if os.IsNotExist(err) {
fi, err := os.Create(pinfile)
if err != nil {
return err
}
fi.Close()
}
return nil
}
func main() {
ctx := context.Background()
name := flag.String("name", "pinbot-test", "set pinbot's nickname")
server := flag.String("server", "irc.freenode.net:6667", "set server to connect to")
channel := flag.String("channel", "#pinbot-test", "set channel to join")
pre := flag.String("prefix", "!", "prefix of command messages")
gw := flag.String("gateway", "https://ipfs.io", "IPFS-to-HTTP gateway to use for success messages")
username := flag.String("user", "", "Cluster API username")
pw := flag.String("pw", "", "Cluster API pw")
flag.Parse()
prefix = *pre
gateway = *gw
msgs = make(chan msgWrap, 500)
go messageQueueProcess()
err := ensurePinLogExists()
if err != nil {
panic(err)
}
clusterpeers := loadHosts("clusterpeers")
var cfgs []*cluster.Config
for _, line := range clusterpeers {
spl := strings.Split(line, ",")
if len(spl) == 0 {
panic("bad host in cluster peers")
}
h := spl[0]
maddr, err := ma.NewMultiaddr(h)
if err != nil {
panic(err)
}
cfg := &cluster.Config{
APIAddr: maddr,
Username: *username,
Password: *pw,
}
cfgs = append(cfgs, cfg)
if len(spl) > 1 && spl[1] == "ssl" {
cfg.SSL = true
}
if len(spl) > 1 && spl[1] == "sslnoverify" {
cfg.SSL = true
cfg.NoVerifyCert = true
}
client, err := cluster.NewDefaultClient(cfg)
if err != nil {
panic(err)
}
shs = append(shs, client.IPFS(ctx))
shsUrls = append(
shsUrls,
fmt.Sprintf("http://127.0.0.1:%d", cluster.DefaultProxyPort),
)
}
lbClient, err = cluster.NewLBClient(&cluster.Failover{}, cfgs, retries)
if err != nil {
panic(err)
}
if len(clusterpeers) == 0 {
for _, h := range loadHosts("hosts") {
shs = append(shs, shell.NewShell(h))
shsUrls = append(shsUrls, "http://"+h)
}
}
if err := friends.Load(); err != nil {
if os.IsNotExist(err) {
friends = DefaultFriendsList
} else {
panic(err)
}
}
fmt.Println("loaded", len(friends.friends), "friends")
bot, err = newBot(*server, *name)
if err != nil {
panic(err)
}
connectToFreenodeIpfs(bot, *channel)
fmt.Println("Connection lost! attempting to reconnect!")
bot.Close()
recontime := time.Second
for {
// Dont try to reconnect this time
bot, err = newBot(*server, *name)
if err != nil {
fmt.Println("ERROR CONNECTING: ", err)
time.Sleep(recontime)
recontime += time.Second
continue
}
recontime = time.Second
connectToFreenodeIpfs(bot, *channel)
fmt.Println("Connection lost! attempting to reconnect!")
bot.Close()
}
}
func newBot(server, name string) (bot *hb.Bot, err error) {
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
err = errors.New("bot creation panicked")
}
}()
bot, err = hb.NewBot(server, name)
if err == nil {
logHandler := log.LvlFilterHandler(log.LvlDebug, log.StdoutHandler)
bot.SetHandler(logHandler)
bot.PingTimeout = time.Hour * 24 * 7
}
return
}
func connectToFreenodeIpfs(con *hb.Bot, channel string) {
// Run() panics badly sometimes
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
con.AddTrigger(pinTrigger)
con.AddTrigger(unpinTrigger)
con.AddTrigger(pinClusterTrigger)
con.AddTrigger(unpinClusterTrigger)
con.AddTrigger(statusClusterTrigger)
con.AddTrigger(statusOngoingTrigger)
con.AddTrigger(recoverClusterTrigger)
con.AddTrigger(listTrigger)
con.AddTrigger(befriendTrigger)
con.AddTrigger(shunTrigger)
con.AddTrigger(OmNomNom)
con.AddTrigger(EatEverything)
con.Channels = []string{channel}
con.Run()
// clears anything remaining in incoming
for range con.Incoming {
}
}