This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
734 lines (670 loc) · 23.1 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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
package main
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
_ "net/http/pprof"
"net/url"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strings"
"text/tabwriter"
"time"
"github.com/dustin/go-humanize"
"github.com/joho/godotenv"
"github.com/libp2p/go-libp2p-core/crypto"
core "github.com/libp2p/go-libp2p-core/peer"
mbase "github.com/multiformats/go-multibase"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/textileio/bidbot/buildinfo"
"github.com/textileio/bidbot/httpapi"
"github.com/textileio/bidbot/lib/auction"
"github.com/textileio/bidbot/lib/dshelper"
"github.com/textileio/bidbot/lib/filclient"
"github.com/textileio/bidbot/lib/peerflags"
"github.com/textileio/bidbot/service"
"github.com/textileio/bidbot/service/limiter"
"github.com/textileio/bidbot/service/lotusclient"
"github.com/textileio/bidbot/service/pricing"
"github.com/textileio/bidbot/service/store"
"github.com/textileio/cli"
"github.com/textileio/go-libp2p-pubsub-rpc/finalizer"
golog "github.com/textileio/go-log/v2"
)
var (
cliName = "bidbot"
defaultConfigPath = filepath.Join(os.Getenv("HOME"), "."+cliName)
log = golog.Logger(cliName)
v = viper.New()
dealsListFields = []string{"ID", "DealSize", "DealDuration", "Status", "AskPrice", "VerifiedAskPrice",
"StartEpoch", "DataURIFetchAttempts", "CreatedAt", "ClientAddress", "ErrorCause"}
validStorageProviderID = regexp.MustCompile("^[a-z]0[0-9]+$")
cidGravityURL = "https://api.cidgravity.com/api/integrations/bidbot"
)
func init() {
_ = godotenv.Load(".env")
configPath := os.Getenv("BIDBOT_PATH")
if configPath == "" {
configPath = defaultConfigPath
}
_ = godotenv.Load(filepath.Join(configPath, ".env"))
rootCmd.AddCommand(initCmd, daemonCmd, idCmd, versionCmd, dealsCmd, downloadCmd, pauseCmd, resumeCmd,
installServiceCmd)
dealsCmd.AddCommand(dealsListCmd)
dealsCmd.AddCommand(dealsShowCmd)
commonFlags := []cli.Flag{
{
Name: "http-port",
DefValue: "9999",
Description: "HTTP API listen address",
},
}
daemonFlags := []cli.Flag{
{
Name: "miner-addr",
DefValue: "",
Description: "Miner address (f0xxxx); depreciated, use storage-provider-id",
},
{
Name: "storage-provider-id",
DefValue: "",
Description: "Storage Provider ID (f0xxxx); required",
},
{
Name: "wallet-addr-sig",
DefValue: "",
Description: "Miner wallet address signature; required; see 'bidbot help init' for instructions",
},
{
Name: "ask-price",
DefValue: 0,
Description: "Bid ask price for deals in attoFIL per GiB per epoch; default is 100 nanoFIL",
},
{
Name: "verified-ask-price",
DefValue: 0,
Description: "Bid ask price for verified deals in attoFIL per GiB per epoch; default is 100 nanoFIL",
},
{
Name: "fast-retrieval",
DefValue: true,
Description: "Offer deals with fast retrieval",
},
{
Name: "deal-start-window",
DefValue: 0,
Description: "Number of epochs after which won deals must start on-chain; required",
},
{
Name: "deal-duration-min",
DefValue: auction.MinDealDuration,
Description: "Minimum deal duration to bid on in epochs; default is ~6 months",
},
{
Name: "deal-duration-max",
DefValue: auction.MaxDealDuration,
Description: "Maximum deal duration to bid on in epochs; default is ~1 year",
},
{
Name: "deal-size-min",
DefValue: 56 * 1024,
Description: "Minimum deal size to bid on in bytes",
},
{
Name: "deal-size-max",
DefValue: 32 * 1024 * 1024 * 1024,
Description: "Maximum deal size to bid on in bytes",
},
{
Name: "discard-orphan-deals-after",
DefValue: 24 * time.Hour,
Description: "The timeout before discarding deal with no progress",
},
{
Name: "running-bytes-limit",
DefValue: "",
Description: `Maximum running total bytes to process for a period of time.
bidbot rejects new auctions if the limit would be exceeded.
In the form of '10MiB/1m', '500 tb/24h' or '5 PiB / 128h', etc.
See https://en.wikipedia.org/wiki/Byte#Multiple-byte_units for valid byte units.
Default to no limit. Be aware that the bytes counter resets when bidbot restarts.
Also take the file system overhead into consideration when calculating the limit.
No limit by default.`,
},
{
Name: "concurrent-imports-limit",
DefValue: 0,
Description: `If bigger than zero, only run that many imports concurrently. Zero means no limits.`,
},
{
Name: "concurrent-data-download-limit",
DefValue: store.MaxDataURIFetchConcurrency,
Description: `The maximum number of concurrent data fetches`,
},
{
Name: "boost-download",
DefValue: false,
Description: `Experimental: creates multiple TCP connections to boost download speeds.
Use with caution since might not be compatible in all filesystems.`,
},
{
Name: "sealing-sectors-limit",
DefValue: 0,
Description: `If bigger than zero, stop bidding if the number of Lotus sealing sectors exceeds this limit.
Zero means no limits`,
},
{
Name: "finalize-early",
DefValue: false,
Description: "Match lotus FinalizeEarly sector state accounting",
},
{
Name: "deal-data-fetch-attempts",
DefValue: 3,
Description: "Number of times fetching deal data will be attempted before failing",
},
{
Name: "deal-data-fetch-timeout",
DefValue: "3h",
Description: `The timeout to fetch deal data. Be conservative to leave enough room for network instability.`,
},
{
Name: "cid-gravity-key",
DefValue: "",
Description: "The API key to the CID gravity system. No CID gravity integration if left empty.",
},
{
Name: "cid-gravity-strict",
DefValue: false,
Description: "When CID gravity is enabled, stop bidding if there's any problem loading cid-gravity pricing rules.",
},
{Name: "lotus-miner-api-maddr", DefValue: "/ip4/127.0.0.1/tcp/2345/http",
Description: "Lotus miner API multiaddress"},
{Name: "lotus-miner-api-token", DefValue: "",
Description: "Lotus miner API authorization token with write permission"},
{Name: "lotus-market-api-maddr", DefValue: "",
Description: "Lotus market API multiaddress (only when market subsystem is split)"},
{Name: "lotus-market-api-token", DefValue: "",
Description: "Lotus market API authorization token with write permission"},
{Name: "lotus-api-conn-retries", DefValue: "2", Description: "Lotus API connection retries"},
{Name: "lotus-gateway-url", DefValue: "https://api.node.glif.io", Description: "Lotus gateway URL"},
{Name: "log-debug", DefValue: false, Description: "Enable debug level log"},
{Name: "log-json", DefValue: false, Description: "Enable structured logging"},
{Name: "log-plaintext", DefValue: false,
Description: "Log in plain text instead of colorized. Useful when logging to syslog."},
}
daemonFlags = append(daemonFlags, peerflags.Flags...)
dealsFlags := []cli.Flag{{Name: "json", DefValue: false,
Description: "output in json format instead of tabular print"}}
dealsListFlags := []cli.Flag{{Name: "status", DefValue: "",
Description: "filter by auction statuses, separated by comma"}}
installServiceFlags := []cli.Flag{
{Name: "user", DefValue: "", Description: "The OS user the service will run as."},
{Name: "group", DefValue: "", Description: "The OS group the service will run as."}}
cobra.OnInitialize(func() {
v.SetConfigType("json")
v.SetConfigName("config")
v.AddConfigPath(os.Getenv("BIDBOT_PATH"))
v.AddConfigPath(defaultConfigPath)
err := v.ReadInConfig()
if err != nil {
log.Errorf("loading config from %s: %v", v.ConfigFileUsed(), err)
}
})
cli.ConfigureCLI(v, "BIDBOT", commonFlags, rootCmd.PersistentFlags())
cli.ConfigureCLI(v, "BIDBOT", peerflags.Flags, initCmd.PersistentFlags())
cli.ConfigureCLI(v, "BIDBOT", daemonFlags, daemonCmd.PersistentFlags())
cli.ConfigureCLI(v, "BIDBOT", dealsFlags, dealsCmd.PersistentFlags())
cli.ConfigureCLI(v, "BIDBOT", dealsListFlags, dealsListCmd.PersistentFlags())
cli.ConfigureCLI(v, "BIDBOT", installServiceFlags, installServiceCmd.PersistentFlags())
}
var rootCmd = &cobra.Command{
Use: cliName,
Short: "Bidbot listens for Filecoin storage deal auctions",
Long: `Bidbot listens for Filecoin storage deal auctions.
bidbot will automatically bid on storage deals that pass configured filters at
the configured prices.
To get started, run 'bidbot init' and follow the instructions.
`,
Args: cobra.ExactArgs(0),
}
var initCmd = &cobra.Command{
Use: "init",
Short: "Initializes bidbot configuration files",
Long: `Initializes bidbot configuration files and generates a new keypair.
bidbot uses a repository in the local file system. By default, the repo is
located at ~/.bidbot. To change the repo location, set the $BIDBOT_PATH
environment variable:
export BIDBOT_PATH=/path/to/bidbotrepo
bidbot will fetch and write deal data to a local directory. This directory should be
accessible to Lotus. By default, the directory is located at ~/.bidbot/deal_data.
The change the deal data directory, set the $BIDBOT_DEAL_DATA_DIRECTORY environment variable:
export BIDBOT_DEAL_DATA_DIRECTORY=/path/to/lotus/accessible/directory
`,
Args: cobra.ExactArgs(0),
Run: func(c *cobra.Command, args []string) {
path, err := peerflags.WriteConfig(v, "BIDBOT_PATH", defaultConfigPath)
cli.CheckErrf("writing config: %v", err)
fmt.Printf("Initialized configuration file: %s\n\n", path)
_, key, err := mbase.Decode(v.GetString("private-key"))
cli.CheckErrf("decoding private key: %v", err)
priv, err := crypto.UnmarshalPrivateKey(key)
cli.CheckErrf("unmarshaling private key: %v", err)
id, err := core.IDFromPrivateKey(priv)
cli.CheckErrf("getting peer id: %v", err)
signingToken := hex.EncodeToString([]byte(id))
fmt.Printf(`Bidbot needs a signature from a miner wallet address to authenticate bids.
1. Sign this token with an address from your miner owner or worker Lotus wallet address of the storage provider:
lotus wallet sign [owner-or-worker-address] %s
2. Start listening for deal auctions using the wallet address and signature from step 1:
bidbot daemon --storage-provider-id <id>
--wallet-addr-sig <signature>
--lotus-miner-api-maddr <lotus-miner-api-maddr>
--lotus-miner-api-token <lotus-miner-api-token-with-write-access>
[--lotus-market-api-maddr <lotus-market-api-maddr>]
[--lotus-market-api-token <lotus-market-api-token-with-write-access>]
--deal-start-window <correct-deal-start-epoch-window-for-your-miner>
Note: In the event you win an auction, you must use this wallet address to make the deal(s).
Good luck!
`, signingToken)
},
}
var daemonCmd = &cobra.Command{
Use: "daemon",
Short: "Run a network-connected bidding bot",
Long: "Run a network-connected bidding bot that listens for and bids on storage deal auctions.",
Args: cobra.ExactArgs(0),
PersistentPreRun: func(c *cobra.Command, args []string) {
cli.ExpandEnvVars(v, v.AllSettings())
err := cli.ConfigureLogging(v, []string{
cliName,
"bidbot/service",
"bidbot/pricing",
"bidbot/store",
"bidbot/datauri",
"bidbot/api",
"bidbot/lotus",
"psrpc",
"psrpc/peer",
"psrpc/mdns",
})
cli.CheckErrf("setting log levels: %v", err)
},
Run: func(c *cobra.Command, args []string) {
log.Infof("bidbot %s", buildinfo.Summary())
storageProviderID := v.GetString("storage-provider-id")
if storageProviderID == "" {
storageProviderID = v.GetString("miner-addr") // fallback to support existing config
}
if storageProviderID == "" {
cli.CheckErr(errors.New("--storage-provider-id is required. See 'bidbot help init' for instructions"))
}
if !validStorageProviderID.MatchString(storageProviderID) {
cli.CheckErr(errors.New("--storage-provider-id should be in the form of f0xxxx"))
}
if v.GetString("wallet-addr-sig") == "" {
cli.CheckErr(errors.New("--wallet-addr-sig is required. See 'bidbot help init' for instructions"))
}
pconfig, err := peerflags.GetConfig(v, "BIDBOT_PATH", defaultConfigPath, false)
cli.CheckErrf("getting peer config: %v", err)
settings, err := cli.MarshalConfig(v, !v.GetBool("log-json"),
"cid-gravity-key", "private-key", "wallet-addr-sig", "lotus-miner-api-token", "lotus-market-api-token")
cli.CheckErrf("marshaling config: %v", err)
log.Infof("loaded config from %s: %s", v.ConfigFileUsed(), string(settings))
fin := finalizer.NewFinalizer()
repoPath := os.Getenv("BIDBOT_PATH")
if repoPath == "" {
repoPath = defaultConfigPath
}
store, err := dshelper.NewBadgerTxnDatastore(filepath.Join(repoPath, "bidstore"))
cli.CheckErrf("creating datastore: %v", err)
fin.Add(store)
walletAddrSig, err := hex.DecodeString(v.GetString("wallet-addr-sig"))
cli.CheckErrf("decoding wallet address signature: %v", err)
lc, err := lotusclient.New(
v.GetString("lotus-miner-api-maddr"),
v.GetString("lotus-miner-api-token"),
v.GetString("lotus-market-api-maddr"),
v.GetString("lotus-market-api-token"),
v.GetInt("lotus-api-conn-retries"),
v.GetBool("fake-mode"),
v.GetBool("finalize-early"),
)
cli.CheckErrf("creating lotus client: %v", err)
fin.Add(lc)
fc, err := filclient.New(v.GetString("lotus-gateway-url"), v.GetBool("fake-mode"))
cli.CheckErrf("creating chain client: %v", err)
fin.Add(fc)
dealDataDirectory := os.Getenv("BIDBOT_DEAL_DATA_DIRECTORY")
if dealDataDirectory == "" {
dealDataDirectory = filepath.Join(defaultConfigPath, "deal_data")
}
var bytesLimiter limiter.Limiter = limiter.NopeLimiter{}
if limit := v.GetString("running-bytes-limit"); limit != "" {
lim, err := parseRunningBytesLimit(limit)
cli.CheckErrf(fmt.Sprintf("parsing '%s': %%w", limit), err)
bytesLimiter = lim
}
config := service.Config{
Peer: pconfig,
BidParams: service.BidParams{
StorageProviderID: storageProviderID,
WalletAddrSig: walletAddrSig,
AskPrice: v.GetInt64("ask-price"),
VerifiedAskPrice: v.GetInt64("verified-ask-price"),
FastRetrieval: v.GetBool("fast-retrieval"),
DealStartWindow: v.GetUint64("deal-start-window"),
DealDataDirectory: dealDataDirectory,
DealDataFetchAttempts: v.GetUint32("deal-data-fetch-attempts"),
DealDataFetchTimeout: v.GetDuration("deal-data-fetch-timeout"),
DiscardOrphanDealsAfter: v.GetDuration("discard-orphan-deals-after"),
},
AuctionFilters: service.AuctionFilters{
DealDuration: service.MinMaxFilter{
Min: v.GetUint64("deal-duration-min"),
Max: v.GetUint64("deal-duration-max"),
},
DealSize: service.MinMaxFilter{
Min: v.GetUint64("deal-size-min"),
Max: v.GetUint64("deal-size-max"),
},
},
BytesLimiter: bytesLimiter,
ConcurrentImports: v.GetInt("concurrent-imports-limit"),
ChunkedDownload: v.GetBool("boost-download"),
SealingSectorsLimit: v.GetInt("sealing-sectors-limit"),
PricingRules: pricing.EmptyRules{},
PricingRulesStrict: v.GetBool("cid-gravity-strict"),
ConcurrentDownloads: v.GetInt("concurrent-data-download-limit"),
}
if cidGravityKey := v.GetString("cid-gravity-key"); cidGravityKey != "" {
config.PricingRules = pricing.NewCIDGravityRules(cidGravityURL, cidGravityKey)
}
serv, err := service.New(config, store, lc, fc)
cli.CheckErrf("starting service: %v", err)
fin.Add(serv)
err = serv.Subscribe(true)
cli.CheckErrf("subscribing to deal auction feed: %v", err)
api, err := httpapi.NewServer(":"+v.GetString("http-port"), serv)
cli.CheckErrf("creating http API server: %v", err)
fin.Add(api)
cli.HandleInterrupt(func() {
cli.CheckErr(fin.Cleanupf("closing service: %v", nil))
})
},
}
var idCmd = &cobra.Command{
Use: "id",
Short: "shows the id, public key and addresses of the bidbot",
Args: cobra.ExactArgs(0),
Run: func(c *cobra.Command, args []string) {
res, err := http.Get(urlFor("id"))
cli.CheckErr(err)
defer func() {
err := res.Body.Close()
cli.CheckErr(err)
}()
b, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != http.StatusOK {
log.Fatalf("%s: %s", res.Status, string(b))
}
fmt.Println(string(b))
},
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "shows current version of bidbot",
Args: cobra.ExactArgs(0),
Run: func(c *cobra.Command, args []string) {
local := buildinfo.Summary()
fail := func(format string, vars ...interface{}) {
fmt.Printf("local%s\n", local)
log.Fatalf(format, vars...)
}
res, err := http.Get(urlFor("version"))
if err != nil {
fail(err.Error())
}
defer func() {
err := res.Body.Close()
if err != nil {
fail(err.Error())
}
}()
b, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != http.StatusOK {
fail("%s: %s", res.Status, string(b))
}
remote := string(b)
if remote == local {
fmt.Println(local)
return
}
fmt.Println("WARNING! You local and remote version don't match:")
fmt.Printf("local%s\n", local)
fmt.Printf("\ndaemon%s\n", remote)
},
}
var dealsCmd = &cobra.Command{
Use: "deals",
Aliases: []string{
"deal",
},
Short: "Interact with storage deals",
Long: "Interact with storage deals.",
Args: cobra.ExactArgs(0),
}
var dealsListCmd = &cobra.Command{
Use: "list",
Short: "List deals, optionally filtered by status",
Args: cobra.ExactArgs(0),
Run: func(c *cobra.Command, args []string) {
var query string
if status := v.GetString("status"); status != "" {
query = fmt.Sprintf("?status=%s", url.QueryEscape(status))
}
bids := getBids(urlFor("deals") + query)
if v.GetBool("json") {
b, err := json.MarshalIndent(bids, "", "\t")
cli.CheckErr(err)
fmt.Println(string(b))
return
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.DiscardEmptyColumns)
for i, bid := range bids {
if i == 0 {
for _, field := range dealsListFields {
_, err := fmt.Fprintf(w, "%s\t", field)
cli.CheckErr(err)
}
_, err := fmt.Fprintln(w, "")
cli.CheckErr(err)
}
value := reflect.ValueOf(bid)
for _, field := range dealsListFields {
_, err := fmt.Fprintf(w, "%v\t", value.FieldByName(field))
cli.CheckErr(err)
}
_, err := fmt.Fprintln(w, "")
cli.CheckErr(err)
}
_ = w.Flush()
},
}
var dealsShowCmd = &cobra.Command{
Use: "show <bid-id>",
Short: "Show details of one deal",
Long: `Show details of one deal, specified by the bid ID, which can be obtained by 'bidbot deals list'`,
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
bids := getBids(urlFor("deals", args[0]))
if len(bids) == 0 {
return
}
bid := bids[0]
if v.GetBool("json") {
b, err := json.MarshalIndent(bid, "", "\t")
cli.CheckErr(err)
fmt.Println(string(b))
return
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
typ := reflect.TypeOf(bid)
value := reflect.ValueOf(bid)
for i := 0; i < typ.NumField(); i++ {
_, err := fmt.Fprintf(w, "%s:\t%v\n", typ.Field(i).Name, value.Field(i))
cli.CheckErr(err)
}
_ = w.Flush()
},
}
var downloadCmd = &cobra.Command{
Use: "download <payload-cid> <data-uri>",
Short: "Download and write storage deal data to disk",
Long: `Downloads and writes storage deal data to disk by the payload cid and data uri.
Deal data is written to BIDBOT_DEAL_DATA_DIRECTORY in CAR format.
`,
Args: cobra.ExactArgs(2),
Run: func(c *cobra.Command, args []string) {
base := urlFor("datauri") + "?"
params := url.Values{}
params.Add("cid", args[0])
params.Add("uri", args[1])
res, err := http.Get(base + params.Encode())
cli.CheckErr(err)
defer func() {
err := res.Body.Close()
cli.CheckErr(err)
}()
if _, err = io.Copy(os.Stdout, res.Body); err != io.EOF {
cli.CheckErr(err)
}
},
}
var pauseCmd = &cobra.Command{
Use: "pause",
Short: "Pause bidding in auctions. No effect if already paused.",
Args: cobra.ExactArgs(0),
Run: func(c *cobra.Command, args []string) {
req, err := http.NewRequest(http.MethodPut, urlFor("pause"), nil)
cli.CheckErr(err)
res, err := http.DefaultClient.Do(req)
cli.CheckErr(err)
cli.CheckErr(res.Body.Close())
}}
var resumeCmd = &cobra.Command{
Use: "resume",
Short: "Resume bidding in auctions. No effect if no paused.",
Args: cobra.ExactArgs(0),
Run: func(c *cobra.Command, args []string) {
req, err := http.NewRequest(http.MethodPut, urlFor("resume"), nil)
cli.CheckErr(err)
res, err := http.DefaultClient.Do(req)
cli.CheckErr(err)
cli.CheckErr(res.Body.Close())
}}
var installServiceCmd = &cobra.Command{
Use: "install-service",
Short: "Install bidbot as systemd service. Requires sudo.",
Args: cobra.ExactArgs(0),
Run: func(c *cobra.Command, args []string) {
if runtime.GOOS != "linux" {
log.Fatal("only available on Linux with systemd.")
}
exePath, err := os.Executable()
cli.CheckErr(err)
if os.Getuid() != 0 {
log.Fatalf("try 'sudo %s install-service'.", exePath)
}
u, g := v.GetString("user"), v.GetString("group")
if u == "" || g == "" {
log.Fatal("both --user and --group are required to install service.")
}
_, err = user.Lookup(u)
cli.CheckErrf("looking up user: %v", err)
_, err = user.LookupGroup(g)
cli.CheckErrf("looking up group: %v", err)
bidbotPath := path.Dir(v.ConfigFileUsed())
content := fmt.Sprintf(systemdServiceTemplate, u, g, exePath, bidbotPath)
loc := "/etc/systemd/system/bidbot.service"
err = os.WriteFile(loc, []byte(content), 0644)
cli.CheckErr(err)
cmd := exec.Command("systemctl", "enable", "bidbot")
cli.CheckErr(cmd.Run())
fmt.Printf(`Service installed to %s and was configured to start on system boot.
To start the service right now, make sure the bidbot daemon is not running, and run "sudo systemctl start bidbot".
`, loc)
}}
func main() {
cli.CheckErr(rootCmd.Execute())
}
func urlFor(parts ...string) string {
u := "http://127.0.0.1:" + v.GetString("http-port")
if len(parts) > 0 {
u += "/" + path.Join(parts...)
}
return u
}
func getBids(u string) (bids []store.Bid) {
res, err := http.Get(u)
cli.CheckErr(err)
defer func() {
err := res.Body.Close()
cli.CheckErr(err)
}()
if res.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(res.Body)
log.Fatalf("%s: %s", res.Status, string(b))
}
decoder := json.NewDecoder(res.Body)
err = decoder.Decode(&bids)
cli.CheckErr(err)
return
}
func parseRunningBytesLimit(s string) (limiter.Limiter, error) {
parts := strings.Split(s, "/")
if len(parts) != 2 {
return nil, errors.New("should be separated by forward slash (/)")
}
sBytes := strings.TrimSpace(parts[0])
nBytes, err := humanize.ParseBytes(sBytes)
if err != nil {
return nil, err
}
ds := strings.TrimSpace(parts[1])
d, err := time.ParseDuration(ds)
if err != nil {
return nil, err
}
log.Infof("limit total running bytes to %d bytes over %v", nBytes, d)
return limiter.NewRunningTotalLimiter(nBytes, d), nil
}
const systemdServiceTemplate = `[Unit]
Description=Textile Bidbot
# Wait for network AND daemon
After=network-online.target
[Service]
Type=simple
User=%s
Group=%s
LimitNOFILE=1024
Restart=on-failure
RestartSec=10
ExecStart=%s daemon --log-plaintext
StandardOutput=journal
StandardError=journal
Environment=BIDBOT_PATH=%s
[Install]
WantedBy=multi-user.target
`