[#36] Parse attributes from node config

Well-known attributes set up with explicit configuration
values, other attributes set up from chain of attributes.

Chain of attributes is a string, that contains keys-value pairs,
divided by semicolon. Pairs itself divided by slash.

E.g. "StorageType:HDD/RPM:7200"

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-09-22 14:02:32 +03:00
parent 28534a509d
commit 1759ff57f1
2 changed files with 40 additions and 6 deletions

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"net" "net"
"strconv"
"strings" "strings"
"sync" "sync"
@ -20,6 +21,9 @@ const (
// config keys for cfgNodeInfo // config keys for cfgNodeInfo
cfgNodeKey = "node.key" cfgNodeKey = "node.key"
cfgBootstrapAddress = "node.address" cfgBootstrapAddress = "node.address"
cfgNodeAttributePrefix = "node.attribute"
cfgNodeCapacity = "node.capacity"
cfgNodePrice = "node.price"
// config keys for cfgGRPC // config keys for cfgGRPC
cfgListenAddress = "grpc.endpoint" cfgListenAddress = "grpc.endpoint"
@ -96,6 +100,9 @@ type BootstrapType uint32
type cfgNodeInfo struct { type cfgNodeInfo struct {
bootType BootstrapType bootType BootstrapType
attributes []string
capacity uint64
price uint64
} }
const ( const (
@ -141,6 +148,9 @@ func initCfg(path string) *cfg {
}, },
cfgNodeInfo: cfgNodeInfo{ cfgNodeInfo: cfgNodeInfo{
bootType: StorageNode, bootType: StorageNode,
attributes: readAttributes(viperCfg),
capacity: viperCfg.GetUint64(cfgNodeCapacity),
price: viperCfg.GetUint64(cfgNodePrice),
}, },
} }
} }
@ -169,6 +179,8 @@ func initViper(path string) *viper.Viper {
func defaultConfiguration(v *viper.Viper) { func defaultConfiguration(v *viper.Viper) {
// fixme: all hardcoded private keys must be removed // fixme: all hardcoded private keys must be removed
v.SetDefault(cfgNodeKey, "Kwk6k2eC3L3QuPvD8aiaNyoSXgQ2YL1bwS5CP1oKoA9waeAze97s") v.SetDefault(cfgNodeKey, "Kwk6k2eC3L3QuPvD8aiaNyoSXgQ2YL1bwS5CP1oKoA9waeAze97s")
v.SetDefault(cfgNodeCapacity, "10")
v.SetDefault(cfgNodePrice, "10")
v.SetDefault(cfgBootstrapAddress, "") // address to bootstrap with v.SetDefault(cfgBootstrapAddress, "") // address to bootstrap with
v.SetDefault(cfgMorphRPCAddress, "http://morph_chain.localtest.nspcc.ru:30333/") v.SetDefault(cfgMorphRPCAddress, "http://morph_chain.localtest.nspcc.ru:30333/")
@ -183,3 +195,18 @@ func defaultConfiguration(v *viper.Viper) {
v.SetDefault(cfgNetmapContract, "75194459637323ea8837d2afe8225ec74a5658c3") v.SetDefault(cfgNetmapContract, "75194459637323ea8837d2afe8225ec74a5658c3")
v.SetDefault(cfgNetmapFee, "1") v.SetDefault(cfgNetmapFee, "1")
} }
func readAttributes(v *viper.Viper) (attrs []string) {
const maxAttributes = 100
for i := 0; i < maxAttributes; i++ {
attr := v.GetString(cfgNodeAttributePrefix + "_" + strconv.Itoa(i))
if attr == "" {
return
} else {
attrs = append(attrs, attr)
}
}
return attrs
}

View file

@ -1,10 +1,12 @@
package main package main
import ( import (
v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap"
crypto "github.com/nspcc-dev/neofs-crypto" crypto "github.com/nspcc-dev/neofs-crypto"
"github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap" "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper" "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper"
"github.com/nspcc-dev/neofs-node/pkg/util/attributes"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -30,10 +32,15 @@ func bootstrapNode(c *cfg) {
cliWrapper, err := wrapper.New(cli) cliWrapper, err := wrapper.New(cli)
fatalOnErr(errors.Wrap(err, "bootstrap error")) fatalOnErr(errors.Wrap(err, "bootstrap error"))
peerInfo := new(netmap.NodeInfo) attrs, err := attributes.ParseV2Attributes(c.cfgNodeInfo.attributes, nil)
if err != nil {
fatalOnErr(errors.Wrap(err, "bootstrap attribute error"))
}
peerInfo := new(v2netmap.NodeInfo)
peerInfo.SetAddress(c.viper.GetString(cfgBootstrapAddress)) peerInfo.SetAddress(c.viper.GetString(cfgBootstrapAddress))
peerInfo.SetPublicKey(crypto.MarshalPublicKey(&c.key.PublicKey)) peerInfo.SetPublicKey(crypto.MarshalPublicKey(&c.key.PublicKey))
// todo: add attributes as opts peerInfo.SetAttributes(attrs)
err = cliWrapper.AddPeer(peerInfo) err = cliWrapper.AddPeer(peerInfo)
fatalOnErr(errors.Wrap(err, "bootstrap error")) fatalOnErr(errors.Wrap(err, "bootstrap error"))