|
| 1 | +package cluster |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/rand" |
| 6 | + "encoding/hex" |
| 7 | + "fmt" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + ds "github.com/ipfs/go-datastore" |
| 11 | + ipfscluster "github.com/ipfs/ipfs-cluster" |
| 12 | + "github.com/ipfs/ipfs-cluster/allocator/ascendalloc" |
| 13 | + "github.com/ipfs/ipfs-cluster/allocator/descendalloc" |
| 14 | + "github.com/ipfs/ipfs-cluster/config" |
| 15 | + "github.com/ipfs/ipfs-cluster/consensus/crdt" |
| 16 | + "github.com/ipfs/ipfs-cluster/informer/disk" |
| 17 | + "github.com/ipfs/ipfs-cluster/informer/numpin" |
| 18 | + "github.com/ipfs/ipfs-cluster/monitor/pubsubmon" |
| 19 | + "github.com/ipfs/ipfs-cluster/pintracker/maptracker" |
| 20 | + "github.com/ipfs/ipfs-cluster/pintracker/stateless" |
| 21 | + host "github.com/libp2p/go-libp2p-host" |
| 22 | + dht "github.com/libp2p/go-libp2p-kad-dht" |
| 23 | + pubsub "github.com/libp2p/go-libp2p-pubsub" |
| 24 | + ma "github.com/multiformats/go-multiaddr" |
| 25 | + "github.com/prometheus/common/log" |
| 26 | +) |
| 27 | + |
| 28 | +func NewClusterSecret() (string, error) { |
| 29 | + secret := make([]byte, 32) |
| 30 | + _, err := rand.Read(secret) |
| 31 | + if err != nil { |
| 32 | + return "", err |
| 33 | + } |
| 34 | + return hex.EncodeToString(secret), nil |
| 35 | +} |
| 36 | + |
| 37 | +func InitCluster(repoPath, secret string) error { |
| 38 | + decoded, err := ipfscluster.DecodeClusterSecret(secret) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + cfgMgr, cfgs := makeClusterConfigs() |
| 44 | + err = cfgMgr.Default() |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + cfgs.ClusterCfg.Secret = decoded |
| 49 | + |
| 50 | + return cfgMgr.SaveJSON(ConfigPath(repoPath)) |
| 51 | +} |
| 52 | + |
| 53 | +func ConfigPath(repoPath string) string { |
| 54 | + return filepath.Join(repoPath, "service.json") |
| 55 | +} |
| 56 | + |
| 57 | +func MakeAndLoadConfigs(repoPath string) (*config.Manager, *cfgs, error) { |
| 58 | + cfgMgr, cfgs := makeClusterConfigs() |
| 59 | + err := cfgMgr.LoadJSONFromFile(ConfigPath(repoPath)) |
| 60 | + if err != nil { |
| 61 | + return nil, nil, err |
| 62 | + } |
| 63 | + return cfgMgr, cfgs, nil |
| 64 | +} |
| 65 | + |
| 66 | +func ParseBootstraps(addrs []string) ([]ma.Multiaddr, error) { |
| 67 | + var parsed []ma.Multiaddr |
| 68 | + for _, a := range addrs { |
| 69 | + p, err := ma.NewMultiaddr(a) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + parsed = append(parsed, p) |
| 74 | + } |
| 75 | + return parsed, nil |
| 76 | +} |
| 77 | + |
| 78 | +func Bootstrap(ctx context.Context, cluster *ipfscluster.Cluster, bootstraps []ma.Multiaddr) { |
| 79 | + for _, bstrap := range bootstraps { |
| 80 | + log.Infof("Bootstrapping to %s", bstrap) |
| 81 | + err := cluster.Join(ctx, bstrap) |
| 82 | + if err != nil { |
| 83 | + log.Errorf("bootstrap to %s failed: %s", bstrap, err) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func SetupAllocation( |
| 89 | + name string, |
| 90 | + diskInfCfg *disk.Config, |
| 91 | + numpinInfCfg *numpin.Config, |
| 92 | +) (ipfscluster.Informer, ipfscluster.PinAllocator, error) { |
| 93 | + switch name { |
| 94 | + case "disk", "disk-freespace": |
| 95 | + informer, err := disk.NewInformer(diskInfCfg) |
| 96 | + if err != nil { |
| 97 | + return nil, nil, err |
| 98 | + } |
| 99 | + return informer, descendalloc.NewAllocator(), nil |
| 100 | + case "disk-reposize": |
| 101 | + informer, err := disk.NewInformer(diskInfCfg) |
| 102 | + if err != nil { |
| 103 | + return nil, nil, err |
| 104 | + } |
| 105 | + return informer, ascendalloc.NewAllocator(), nil |
| 106 | + case "numpin", "pincount": |
| 107 | + informer, err := numpin.NewInformer(numpinInfCfg) |
| 108 | + if err != nil { |
| 109 | + return nil, nil, err |
| 110 | + } |
| 111 | + return informer, ascendalloc.NewAllocator(), nil |
| 112 | + default: |
| 113 | + return nil, nil, fmt.Errorf("unknown allocation strategy") |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func SetupPinTracker( |
| 118 | + name string, |
| 119 | + h host.Host, |
| 120 | + mapCfg *maptracker.Config, |
| 121 | + statelessCfg *stateless.Config, |
| 122 | + peerName string, |
| 123 | +) (ipfscluster.PinTracker, error) { |
| 124 | + switch name { |
| 125 | + case "map": |
| 126 | + ptrk := maptracker.NewMapPinTracker(mapCfg, h.ID(), peerName) |
| 127 | + log.Debug("map pintracker loaded") |
| 128 | + return ptrk, nil |
| 129 | + case "stateless": |
| 130 | + ptrk := stateless.New(statelessCfg, h.ID(), peerName) |
| 131 | + log.Debug("stateless pintracker loaded") |
| 132 | + return ptrk, nil |
| 133 | + default: |
| 134 | + return nil, fmt.Errorf("unknown pintracker type") |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func SetupConsensus( |
| 139 | + h host.Host, |
| 140 | + dht *dht.IpfsDHT, |
| 141 | + pubsub *pubsub.PubSub, |
| 142 | + crdtCfg *crdt.Config, |
| 143 | + store ds.Datastore, |
| 144 | +) (ipfscluster.Consensus, error) { |
| 145 | + convrdt, err := crdt.New(h, dht, pubsub, crdtCfg, store) |
| 146 | + if err != nil { |
| 147 | + return nil, fmt.Errorf("error creating CRDT component: %s", err) |
| 148 | + } |
| 149 | + return convrdt, nil |
| 150 | +} |
| 151 | + |
| 152 | +type cfgs struct { |
| 153 | + ClusterCfg *ipfscluster.Config |
| 154 | + CrdtCfg *crdt.Config |
| 155 | + MaptrackerCfg *maptracker.Config |
| 156 | + StatelessTrackerCfg *stateless.Config |
| 157 | + PubsubmonCfg *pubsubmon.Config |
| 158 | + DiskInfCfg *disk.Config |
| 159 | + NumpinInfCfg *numpin.Config |
| 160 | +} |
| 161 | + |
| 162 | +func makeClusterConfigs() (*config.Manager, *cfgs) { |
| 163 | + cfg := config.NewManager() |
| 164 | + clusterCfg := &ipfscluster.Config{} |
| 165 | + crdtCfg := &crdt.Config{} |
| 166 | + maptrackerCfg := &maptracker.Config{} |
| 167 | + statelessCfg := &stateless.Config{} |
| 168 | + pubsubmonCfg := &pubsubmon.Config{} |
| 169 | + diskInfCfg := &disk.Config{} |
| 170 | + numpinInfCfg := &numpin.Config{} |
| 171 | + cfg.RegisterComponent(config.Cluster, clusterCfg) |
| 172 | + cfg.RegisterComponent(config.Consensus, crdtCfg) |
| 173 | + cfg.RegisterComponent(config.PinTracker, maptrackerCfg) |
| 174 | + cfg.RegisterComponent(config.PinTracker, statelessCfg) |
| 175 | + cfg.RegisterComponent(config.Monitor, pubsubmonCfg) |
| 176 | + cfg.RegisterComponent(config.Informer, diskInfCfg) |
| 177 | + cfg.RegisterComponent(config.Informer, numpinInfCfg) |
| 178 | + return cfg, &cfgs{ |
| 179 | + clusterCfg, |
| 180 | + crdtCfg, |
| 181 | + maptrackerCfg, |
| 182 | + statelessCfg, |
| 183 | + pubsubmonCfg, |
| 184 | + diskInfCfg, |
| 185 | + numpinInfCfg, |
| 186 | + } |
| 187 | +} |
0 commit comments