2022-06-02 12:07:30 +00:00
|
|
|
package storagegroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
2022-07-12 10:40:15 +00:00
|
|
|
"fmt"
|
2022-06-02 12:07:30 +00:00
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
internalclient "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
|
|
|
objectCli "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/object"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/services/object_manager/storagegroup"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
storagegroupSDK "github.com/TrueCloudLab/frostfs-sdk-go/storagegroup"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/user"
|
2022-06-02 12:07:30 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const sgMembersFlag = "members"
|
|
|
|
|
|
|
|
var sgMembers []string
|
|
|
|
|
|
|
|
var sgPutCmd = &cobra.Command{
|
|
|
|
Use: "put",
|
2023-01-27 10:44:39 +00:00
|
|
|
Short: "Put storage group to FrostFS",
|
|
|
|
Long: "Put storage group to FrostFS",
|
2022-06-02 12:07:30 +00:00
|
|
|
Run: putSG,
|
|
|
|
}
|
|
|
|
|
|
|
|
func initSGPutCmd() {
|
|
|
|
commonflags.Init(sgPutCmd)
|
|
|
|
|
|
|
|
flags := sgPutCmd.Flags()
|
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
flags.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage)
|
|
|
|
_ = sgPutCmd.MarkFlagRequired(commonflags.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
|
|
|
|
2022-07-19 12:45:58 +00:00
|
|
|
flags.Uint64(commonflags.Lifetime, 0, "Storage group lifetime in epochs")
|
|
|
|
_ = sgPutCmd.MarkFlagRequired(commonflags.Lifetime)
|
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-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-19 12:45:58 +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 (
|
2022-04-29 17:02:52 +00:00
|
|
|
headPrm internalclient.HeadObjectPrm
|
|
|
|
putPrm internalclient.PutObjectPrm
|
|
|
|
getCnrPrm internalclient.GetContainerPrm
|
2022-06-02 12:07:30 +00:00
|
|
|
)
|
|
|
|
|
2022-04-29 17:02:52 +00:00
|
|
|
cli := internalclient.GetSDKClientByFlag(cmd, pk, commonflags.RPC)
|
|
|
|
getCnrPrm.SetClient(cli)
|
|
|
|
getCnrPrm.SetContainer(cnr)
|
|
|
|
|
|
|
|
resGetCnr, err := internalclient.GetContainer(getCnrPrm)
|
|
|
|
common.ExitOnErr(cmd, "get container RPC call: %w", err)
|
|
|
|
|
2022-10-20 09:40:33 +00:00
|
|
|
objectCli.OpenSessionViaClient(cmd, &putPrm, cli, pk, cnr, nil)
|
2022-06-02 12:07:30 +00:00
|
|
|
objectCli.Prepare(cmd, &headPrm, &putPrm)
|
|
|
|
|
|
|
|
headPrm.SetRawFlag(true)
|
2022-10-20 09:40:33 +00:00
|
|
|
headPrm.SetClient(cli)
|
2022-06-02 12:07:30 +00:00
|
|
|
|
|
|
|
sg, err := storagegroup.CollectMembers(sgHeadReceiver{
|
|
|
|
cmd: cmd,
|
|
|
|
key: pk,
|
|
|
|
ownerID: &ownerID,
|
|
|
|
prm: headPrm,
|
2022-04-29 17:02:52 +00:00
|
|
|
}, cnr, members, !container.IsHomomorphicHashingDisabled(resGetCnr.Container()))
|
2022-06-02 12:07:30 +00:00
|
|
|
common.ExitOnErr(cmd, "could not collect storage group members: %w", err)
|
|
|
|
|
2022-06-20 16:56:41 +00:00
|
|
|
var netInfoPrm internalclient.NetworkInfoPrm
|
|
|
|
netInfoPrm.SetClient(cli)
|
|
|
|
|
|
|
|
ni, err := internalclient.NetworkInfo(netInfoPrm)
|
|
|
|
common.ExitOnErr(cmd, "can't fetch network info: %w", err)
|
|
|
|
|
2022-07-19 12:45:58 +00:00
|
|
|
lifetime, _ := cmd.Flags().GetUint64(commonflags.Lifetime)
|
2022-06-20 16:56:41 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
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) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|