-
Notifications
You must be signed in to change notification settings - Fork 1
/
metadata.go
117 lines (101 loc) · 3.64 KB
/
metadata.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
package component
import (
"fmt"
"io"
"strings"
ipfsOptions "github.com/ipfs/interface-go-ipfs-core/options"
ipfsConfig "github.com/ipfs/kubo/config"
ipfsFsrepo "github.com/ipfs/kubo/repo/fsrepo"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/dapr/components-contrib/metadata"
)
type ipfsMetadata struct {
// Path where to store the IPFS repository
// It will be initialized automatically if needed
// Defaults to the "best known path" set by IPFS
RepoPath string `mapstructure:"repoPath"`
// If set, uses an external IPFS daemon, connecting to its APIs
// Can be a HTTP(S) address or a multi-address
// If set, a local node will not be initialized
ExternalAPI string `mapstructure:"externalAPI"`
// The options below can only be set when a new local repo is being initialized
// List of bootstrap nodes, as a comma-separated string
// If empty, defaults to the official bootstrap nodes provided by the IPFS project
// Users should not modify this unless they're using a private cluster
BootstrapNodes string `mapstructure:"bootstrapNodes"`
// Swarm key to use for connecting to private IPFS networks
// If empty, the node will connect to the default, public IPFS network
// Generate with https://github.com/Kubuxu/go-ipfs-swarm-key-gen
// When using a swarm key, users should also configure the bootstrap nodes
SwarmKey string `mapstructure:"swarmKey"`
// Routing mode: "dht" (default) or "dhtclient"
Routing string `mapstructure:"routing"`
// Max local storage used
// Default: the default value used by go-ipfs (currently, "10GB")
StorageMax string `mapstructure:"storageMax"`
// Watermark for running garbage collection, 0-100 (as a percentage)
// Default: the default value used by go-ipfs (currently, 90)
StorageGCWatermark int64 `mapstructure:"storageGCWatermark"`
// Interval for running garbage collection
// Default: the default value used by go-ipfs (currently, "1h")
StorageGCPeriod string `mapstructure:"storageGCPeriod"`
}
// FromMap initializes the metadata object from a map.
func (m *ipfsMetadata) FromMap(mp map[string]string) (err error) {
if len(mp) > 0 {
err = metadata.DecodeMetadata(mp, m)
if err != nil {
return err
}
}
if m.RepoPath == "" {
m.RepoPath, err = ipfsFsrepo.BestKnownPath()
if err != nil {
return fmt.Errorf("error determining the best known repo path: %v", err)
}
}
return nil
}
// IPFSConfig returns the configuration object for using with the go-ipfs library.
// This is executed only when initializing a new repository.
func (m *ipfsMetadata) IPFSConfig() (*ipfsConfig.Config, error) {
identity, err := ipfsConfig.CreateIdentity(io.Discard, []ipfsOptions.KeyGenerateOption{
ipfsOptions.Key.Type(ipfsOptions.Ed25519Key),
})
if err != nil {
return nil, err
}
cfg, err := ipfsConfig.InitWithIdentity(identity)
if err != nil {
return nil, err
}
if m.BootstrapNodes != "" {
var peers []peer.AddrInfo
peers, err = ipfsConfig.ParseBootstrapPeers(
strings.Split(m.BootstrapNodes, ","),
)
if err != nil {
return nil, fmt.Errorf("invalid value for metadata property 'bootstrapNodes': %v", err)
}
cfg.SetBootstrapPeers(peers)
}
r := strings.ToLower(m.Routing)
switch r {
case "dht", "dhtclient", "dhtserver", "none":
cfg.Routing.Type = ipfsConfig.NewOptionalString(r)
case "":
cfg.Routing.Type = ipfsConfig.NewOptionalString("dht")
default:
return nil, fmt.Errorf("invalid value for metadata property 'routing'")
}
if m.StorageMax != "" {
cfg.Datastore.StorageMax = m.StorageMax
}
if m.StorageGCWatermark != 0 {
cfg.Datastore.StorageGCWatermark = m.StorageGCWatermark
}
if m.StorageGCPeriod != "" {
cfg.Datastore.GCPeriod = m.StorageGCPeriod
}
return cfg, nil
}