2022-06-02 12:07:30 +00:00
|
|
|
package storagegroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
2022-07-12 10:40:15 +00:00
|
|
|
"fmt"
|
2022-06-20 16:56:41 +00:00
|
|
|
"strconv"
|
2022-06-02 12:07:30 +00:00
|
|
|
|
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
|
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
|
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
|
|
|
|
objectCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/object"
|
|
|
|
sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/storagegroup"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-07-01 16:48:15 +00:00
|
|
|
storagegroupSDK "github.com/nspcc-dev/neofs-sdk-go/storagegroup"
|
2022-06-02 12:07:30 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const sgMembersFlag = "members"
|
|
|
|
|
|
|
|
var sgMembers []string
|
|
|
|
|
|
|
|
var sgPutCmd = &cobra.Command{
|
|
|
|
Use: "put",
|
|
|
|
Short: "Put storage group to NeoFS",
|
|
|
|
Long: "Put storage group to NeoFS",
|
|
|
|
Run: putSG,
|
|
|
|
}
|
|
|
|
|
|
|
|
func initSGPutCmd() {
|
|
|
|
commonflags.Init(sgPutCmd)
|
|
|
|
|
|
|
|
flags := sgPutCmd.Flags()
|
|
|
|
|
2022-06-20 16:33:34 +00:00
|
|
|
flags.String(cidFlag, "", "Container ID")
|
|
|
|
_ = sgPutCmd.MarkFlagRequired(cidFlag)
|
2022-06-02 12:07:30 +00:00
|
|
|
|
|
|
|
flags.StringSliceVarP(&sgMembers, sgMembersFlag, "m", nil, "ID list of storage group members")
|
|
|
|
_ = sgPutCmd.MarkFlagRequired(sgMembersFlag)
|
2022-06-20 16:56:41 +00:00
|
|
|
|
|
|
|
flags.Uint64(sgLifetimeFlag, 0, "Storage group lifetime in epochs")
|
|
|
|
_ = sgPutCmd.MarkFlagRequired(sgLifetimeFlag)
|
2022-06-02 12:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func putSG(cmd *cobra.Command, _ []string) {
|
|
|
|
pk := key.GetOrGenerate(cmd)
|
|
|
|
|
|
|
|
var ownerID user.ID
|
|
|
|
user.IDFromKey(&ownerID, pk.PublicKey)
|
|
|
|
|
|
|
|
var cnr cid.ID
|
|
|
|
readCID(cmd, &cnr)
|
|
|
|
|
2022-06-20 16:56:41 +00:00
|
|
|
lifetimeStr := cmd.Flag(sgLifetimeFlag).Value.String()
|
|
|
|
lifetime, err := strconv.ParseUint(lifetimeStr, 10, 64)
|
|
|
|
common.ExitOnErr(cmd, "could not parse lifetime: %w", err)
|
|
|
|
|
2022-07-12 10:40:15 +00:00
|
|
|
members := make([]oid.ID, len(sgMembers))
|
|
|
|
uniqueFilter := make(map[oid.ID]struct{}, len(sgMembers))
|
2022-06-02 12:07:30 +00:00
|
|
|
|
|
|
|
for i := range sgMembers {
|
2022-07-12 10:40:15 +00:00
|
|
|
err = members[i].DecodeString(sgMembers[i])
|
2022-06-02 12:07:30 +00:00
|
|
|
common.ExitOnErr(cmd, "could not parse object ID: %w", err)
|
2022-07-12 10:40:15 +00:00
|
|
|
|
|
|
|
if _, alreadyExists := uniqueFilter[members[i]]; alreadyExists {
|
|
|
|
common.ExitOnErr(cmd, "", fmt.Errorf("%s member in not unique", members[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
uniqueFilter[members[i]] = struct{}{}
|
2022-06-02 12:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
headPrm internalclient.HeadObjectPrm
|
|
|
|
putPrm internalclient.PutObjectPrm
|
|
|
|
)
|
|
|
|
|
|
|
|
sessionCli.Prepare(cmd, cnr, nil, pk, &putPrm)
|
|
|
|
objectCli.Prepare(cmd, &headPrm, &putPrm)
|
|
|
|
|
|
|
|
headPrm.SetRawFlag(true)
|
|
|
|
|
|
|
|
sg, err := storagegroup.CollectMembers(sgHeadReceiver{
|
|
|
|
cmd: cmd,
|
|
|
|
key: pk,
|
|
|
|
ownerID: &ownerID,
|
|
|
|
prm: headPrm,
|
|
|
|
}, cnr, members)
|
|
|
|
common.ExitOnErr(cmd, "could not collect storage group members: %w", err)
|
|
|
|
|
2022-06-20 16:56:41 +00:00
|
|
|
cli := internalclient.GetSDKClientByFlag(cmd, pk, commonflags.RPC)
|
|
|
|
|
|
|
|
var netInfoPrm internalclient.NetworkInfoPrm
|
|
|
|
netInfoPrm.SetClient(cli)
|
|
|
|
|
|
|
|
ni, err := internalclient.NetworkInfo(netInfoPrm)
|
|
|
|
common.ExitOnErr(cmd, "can't fetch network info: %w", err)
|
|
|
|
|
|
|
|
sg.SetExpirationEpoch(ni.NetworkInfo().CurrentEpoch() + lifetime)
|
|
|
|
|
2022-06-02 12:07:30 +00:00
|
|
|
obj := object.New()
|
|
|
|
obj.SetContainerID(cnr)
|
|
|
|
obj.SetOwnerID(&ownerID)
|
2022-07-01 16:48:15 +00:00
|
|
|
|
|
|
|
storagegroupSDK.WriteToObject(*sg, obj)
|
2022-06-02 12:07:30 +00:00
|
|
|
|
|
|
|
putPrm.SetHeader(obj)
|
2022-07-01 16:48:15 +00:00
|
|
|
putPrm.SetPayloadReader(bytes.NewReader(obj.Payload()))
|
2022-06-02 12:07:30 +00:00
|
|
|
|
|
|
|
res, err := internalclient.PutObject(putPrm)
|
|
|
|
common.ExitOnErr(cmd, "rpc error: %w", err)
|
|
|
|
|
|
|
|
cmd.Println("Storage group successfully stored")
|
|
|
|
cmd.Printf(" ID: %s\n CID: %s\n", res.ID(), cnr)
|
|
|
|
}
|
|
|
|
|
|
|
|
type sgHeadReceiver struct {
|
|
|
|
cmd *cobra.Command
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
ownerID *user.ID
|
|
|
|
prm internalclient.HeadObjectPrm
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c sgHeadReceiver) Head(addr oid.Address) (interface{}, error) {
|
|
|
|
obj := addr.Object()
|
|
|
|
|
|
|
|
sessionCli.Prepare(c.cmd, addr.Container(), &obj, c.key, &c.prm)
|
|
|
|
c.prm.SetAddress(addr)
|
|
|
|
|
|
|
|
res, err := internalclient.HeadObject(c.prm)
|
|
|
|
|
|
|
|
var errSplitInfo *object.SplitInfoError
|
|
|
|
|
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
case err == nil:
|
|
|
|
return res.Header(), nil
|
|
|
|
case errors.As(err, &errSplitInfo):
|
|
|
|
return errSplitInfo.SplitInfo(), nil
|
|
|
|
}
|
|
|
|
}
|