/
main.go
159 lines (136 loc) · 4.91 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
package main
import (
"context"
"fmt"
"net/url"
"os"
"os/signal"
"syscall"
"github.com/filecoin-project/lassie/pkg/aggregateeventrecorder"
"github.com/filecoin-project/lassie/pkg/indexerlookup"
"github.com/filecoin-project/lassie/pkg/lassie"
"github.com/filecoin-project/lassie/pkg/net/host"
"github.com/filecoin-project/lassie/pkg/retriever"
"github.com/google/uuid"
"github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/config"
"github.com/urfave/cli/v2"
)
var logger = log.Logger("lassie/main")
func main() {
// set up a context that is canceled when a command is interrupted
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// set up a signal handler to cancel the context
go func() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, syscall.SIGTERM, syscall.SIGINT)
select {
case <-interrupt:
fmt.Println()
logger.Info("received interrupt signal")
cancel()
case <-ctx.Done():
}
// Allow any further SIGTERM or SIGINT to kill process
signal.Stop(interrupt)
}()
app := &cli.App{
Name: "lassie",
Usage: "Utility for retrieving content from the Filecoin network",
Suggest: true,
Flags: []cli.Flag{
FlagVerbose,
FlagVeryVerbose,
},
Commands: []*cli.Command{
daemonCmd,
fetchCmd,
versionCmd,
},
}
if err := app.RunContext(ctx, os.Args); err != nil {
logger.Fatal(err)
}
}
func after(cctx *cli.Context) error {
ResetGlobalFlags()
return nil
}
func buildLassieConfigFromCLIContext(cctx *cli.Context, lassieOpts []lassie.LassieOption, libp2pOpts []config.Option) (*lassie.LassieConfig, error) {
providerTimeout := cctx.Duration("provider-timeout")
globalTimeout := cctx.Duration("global-timeout")
bitswapConcurrency := cctx.Int("bitswap-concurrency")
bitswapConcurrencyPerRetrieval := cctx.Int("bitswap-concurrency-per-retrieval")
lassieOpts = append(lassieOpts, lassie.WithProviderTimeout(providerTimeout))
if globalTimeout > 0 {
lassieOpts = append(lassieOpts, lassie.WithGlobalTimeout(globalTimeout))
}
if len(protocols) > 0 {
lassieOpts = append(lassieOpts, lassie.WithProtocols(protocols))
}
host, err := host.InitHost(cctx.Context, libp2pOpts)
if err != nil {
return nil, err
}
lassieOpts = append(lassieOpts, lassie.WithHost(host))
if len(fetchProviders) > 0 {
finderOpt := lassie.WithCandidateSource(retriever.NewDirectCandidateSource(fetchProviders, retriever.WithLibp2pCandidateDiscovery(host)))
if cctx.IsSet("ipni-endpoint") {
logger.Warn("Ignoring ipni-endpoint flag since direct provider is specified")
}
lassieOpts = append(lassieOpts, finderOpt)
} else if cctx.IsSet("ipni-endpoint") {
endpoint := cctx.String("ipni-endpoint")
endpointUrl, err := url.ParseRequestURI(endpoint)
if err != nil {
logger.Errorw("Failed to parse IPNI endpoint as URL", "err", err)
return nil, fmt.Errorf("cannot parse given IPNI endpoint %s as valid URL: %w", endpoint, err)
}
finder, err := indexerlookup.NewCandidateSource(indexerlookup.WithHttpEndpoint(endpointUrl))
if err != nil {
logger.Errorw("Failed to instantiate IPNI candidate finder", "err", err)
return nil, err
}
lassieOpts = append(lassieOpts, lassie.WithCandidateSource(finder))
logger.Debug("Using explicit IPNI endpoint to find candidates", "endpoint", endpoint)
}
if len(providerBlockList) > 0 {
lassieOpts = append(lassieOpts, lassie.WithProviderBlockList(providerBlockList))
}
if bitswapConcurrency > 0 {
lassieOpts = append(lassieOpts, lassie.WithBitswapConcurrency(bitswapConcurrency))
}
if bitswapConcurrencyPerRetrieval > 0 {
lassieOpts = append(lassieOpts, lassie.WithBitswapConcurrencyPerRetrieval(bitswapConcurrencyPerRetrieval))
} else if bitswapConcurrency > 0 {
lassieOpts = append(lassieOpts, lassie.WithBitswapConcurrencyPerRetrieval(bitswapConcurrency))
}
return lassie.NewLassieConfig(lassieOpts...), nil
}
func getEventRecorderConfig(endpointURL string, authToken string, instanceID string) *aggregateeventrecorder.EventRecorderConfig {
return &aggregateeventrecorder.EventRecorderConfig{
InstanceID: instanceID,
EndpointURL: endpointURL,
EndpointAuthorization: authToken,
}
}
// setupLassieEventRecorder creates and subscribes an EventRecorder if an event recorder URL is given
func setupLassieEventRecorder(
ctx context.Context,
cfg *aggregateeventrecorder.EventRecorderConfig,
lassie *lassie.Lassie,
) {
if cfg.EndpointURL != "" {
if cfg.InstanceID == "" {
uuid, err := uuid.NewRandom()
if err != nil {
logger.Warnw("failed to generate default event recorder instance ID UUID, no instance ID will be provided", "err", err)
}
cfg.InstanceID = uuid.String() // returns "" if uuid is invalid
}
eventRecorder := aggregateeventrecorder.NewAggregateEventRecorder(ctx, *cfg)
lassie.RegisterSubscriber(eventRecorder.RetrievalEventSubscriber())
logger.Infow("Reporting retrieval events to event recorder API", "url", cfg.EndpointURL, "instance_id", cfg.InstanceID)
}
}