2020-08-04 14:46:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-10-28 14:48:46 +00:00
|
|
|
"bytes"
|
2022-03-03 08:29:14 +00:00
|
|
|
"crypto/ecdsa"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2021-09-01 16:33:48 +00:00
|
|
|
"fmt"
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
2022-05-18 11:03:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
|
2022-05-18 09:22:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
|
2022-05-24 08:22:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
|
2022-05-30 13:43:02 +00:00
|
|
|
sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session"
|
2020-12-17 16:15:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/storagegroup"
|
2022-05-31 17:00:41 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-10 07:08:33 +00:00
|
|
|
storagegroupAPI "github.com/nspcc-dev/neofs-sdk-go/storagegroup"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2020-08-04 14:46:12 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// storagegroupCmd represents the storagegroup command
|
|
|
|
var storagegroupCmd = &cobra.Command{
|
|
|
|
Use: "storagegroup",
|
|
|
|
Short: "Operations with Storage Groups",
|
|
|
|
Long: `Operations with Storage Groups`,
|
2021-09-27 15:36:14 +00:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
// bind exactly that cmd's flags to
|
|
|
|
// the viper before execution
|
2022-05-18 09:22:02 +00:00
|
|
|
commonflags.Bind(cmd)
|
2022-05-30 13:04:40 +00:00
|
|
|
commonflags.BindAPI(cmd)
|
2021-09-27 15:36:14 +00:00
|
|
|
},
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
var sgPutCmd = &cobra.Command{
|
|
|
|
Use: "put",
|
|
|
|
Short: "Put storage group to NeoFS",
|
|
|
|
Long: "Put storage group to NeoFS",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: putSG,
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sgGetCmd = &cobra.Command{
|
|
|
|
Use: "get",
|
|
|
|
Short: "Get storage group from NeoFS",
|
|
|
|
Long: "Get storage group from NeoFS",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: getSG,
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sgListCmd = &cobra.Command{
|
|
|
|
Use: "list",
|
|
|
|
Short: "List storage groups in NeoFS container",
|
|
|
|
Long: "List storage groups in NeoFS container",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: listSG,
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sgDelCmd = &cobra.Command{
|
|
|
|
Use: "delete",
|
|
|
|
Short: "Delete storage group from NeoFS",
|
|
|
|
Long: "Delete storage group from NeoFS",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: delSG,
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
sgMembersFlag = "members"
|
|
|
|
sgIDFlag = "id"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
sgMembers []string
|
|
|
|
sgID string
|
|
|
|
)
|
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
func initSGPutCmd() {
|
2022-05-18 09:22:02 +00:00
|
|
|
commonflags.Init(sgPutCmd)
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
flags := sgPutCmd.Flags()
|
2021-02-09 21:55:38 +00:00
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
flags.String("cid", "", "Container ID")
|
2020-12-17 16:15:27 +00:00
|
|
|
_ = sgPutCmd.MarkFlagRequired("cid")
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
flags.StringSliceVarP(&sgMembers, sgMembersFlag, "m", nil, "ID list of storage group members")
|
2020-12-17 16:15:27 +00:00
|
|
|
_ = sgPutCmd.MarkFlagRequired(sgMembersFlag)
|
2021-09-27 15:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initSGGetCmd() {
|
2022-05-18 09:22:02 +00:00
|
|
|
commonflags.Init(sgGetCmd)
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
flags := sgGetCmd.Flags()
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
flags.String("cid", "", "Container ID")
|
2020-12-17 16:15:27 +00:00
|
|
|
_ = sgGetCmd.MarkFlagRequired("cid")
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
flags.StringVarP(&sgID, sgIDFlag, "", "", "storage group identifier")
|
2020-12-17 16:15:27 +00:00
|
|
|
_ = sgGetCmd.MarkFlagRequired(sgIDFlag)
|
2021-09-27 15:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initSGListCmd() {
|
2022-05-18 09:22:02 +00:00
|
|
|
commonflags.Init(sgListCmd)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
sgListCmd.Flags().String("cid", "", "Container ID")
|
|
|
|
_ = sgListCmd.MarkFlagRequired("cid")
|
2021-09-27 15:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initSGDeleteCmd() {
|
2022-05-18 09:22:02 +00:00
|
|
|
commonflags.Init(sgDelCmd)
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
flags := sgDelCmd.Flags()
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
flags.String("cid", "", "Container ID")
|
2020-12-17 16:15:27 +00:00
|
|
|
_ = sgDelCmd.MarkFlagRequired("cid")
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
flags.StringVarP(&sgID, sgIDFlag, "", "", "storage group identifier")
|
2020-12-17 16:15:27 +00:00
|
|
|
_ = sgDelCmd.MarkFlagRequired(sgIDFlag)
|
|
|
|
}
|
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
func init() {
|
|
|
|
storageGroupChildCommands := []*cobra.Command{
|
|
|
|
sgPutCmd,
|
|
|
|
sgGetCmd,
|
|
|
|
sgListCmd,
|
|
|
|
sgDelCmd,
|
|
|
|
}
|
|
|
|
|
|
|
|
rootCmd.AddCommand(storagegroupCmd)
|
|
|
|
storagegroupCmd.AddCommand(storageGroupChildCommands...)
|
|
|
|
|
|
|
|
for _, sgCommand := range storageGroupChildCommands {
|
|
|
|
flags := sgCommand.Flags()
|
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
flags.String(bearerTokenFlag, "", "File with signed JSON or binary encoded bearer token")
|
2022-05-30 13:04:40 +00:00
|
|
|
commonflags.InitAPI(sgCommand)
|
2021-09-27 15:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initSGPutCmd()
|
|
|
|
initSGGetCmd()
|
|
|
|
initSGListCmd()
|
|
|
|
initSGDeleteCmd()
|
|
|
|
}
|
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
type sgHeadReceiver struct {
|
2022-03-03 08:29:14 +00:00
|
|
|
cmd *cobra.Command
|
|
|
|
key *ecdsa.PrivateKey
|
2022-05-31 17:00:41 +00:00
|
|
|
ownerID user.ID
|
2022-03-03 08:29:14 +00:00
|
|
|
prm internalclient.HeadObjectPrm
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (c sgHeadReceiver) Head(addr oid.Address) (interface{}, error) {
|
|
|
|
obj := addr.Object()
|
|
|
|
|
2022-05-30 13:43:02 +00:00
|
|
|
sessionCli.Prepare(c.cmd, addr.Container(), &obj, c.key, &c.prm)
|
2021-10-28 14:48:46 +00:00
|
|
|
c.prm.SetAddress(addr)
|
|
|
|
|
|
|
|
res, err := internalclient.HeadObject(c.prm)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
var errSplitInfo *object.SplitInfoError
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
case err == nil:
|
2022-03-03 14:19:05 +00:00
|
|
|
return res.Header(), nil
|
2020-12-17 16:15:27 +00:00
|
|
|
case errors.As(err, &errSplitInfo):
|
|
|
|
return errSplitInfo.SplitInfo(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func putSG(cmd *cobra.Command, _ []string) {
|
2022-05-24 08:22:23 +00:00
|
|
|
pk := key.GetOrGenerate(cmd)
|
2021-06-22 16:21:18 +00:00
|
|
|
|
2022-05-24 08:22:23 +00:00
|
|
|
ownerID, err := getOwnerID(pk)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "", err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var cnr cid.ID
|
|
|
|
|
|
|
|
err = readCID(cmd, &cnr)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "", err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
members := make([]oid.ID, len(sgMembers))
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
for i := range sgMembers {
|
2022-05-12 16:37:46 +00:00
|
|
|
err = members[i].DecodeString(sgMembers[i])
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "could not parse object ID: %w", err)
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
var (
|
|
|
|
headPrm internalclient.HeadObjectPrm
|
|
|
|
putPrm internalclient.PutObjectPrm
|
|
|
|
)
|
2021-02-09 21:55:38 +00:00
|
|
|
|
2022-05-30 13:43:02 +00:00
|
|
|
sessionCli.Prepare(cmd, cnr, nil, pk, &putPrm)
|
2021-10-28 14:48:46 +00:00
|
|
|
prepareObjectPrm(cmd, &headPrm, &putPrm)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
headPrm.SetRawFlag(true)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
sg, err := storagegroup.CollectMembers(sgHeadReceiver{
|
2022-03-03 08:29:14 +00:00
|
|
|
cmd: cmd,
|
2022-05-24 08:22:23 +00:00
|
|
|
key: pk,
|
2022-05-31 17:00:41 +00:00
|
|
|
ownerID: *ownerID,
|
2022-03-03 08:29:14 +00:00
|
|
|
prm: headPrm,
|
2022-05-12 16:37:46 +00:00
|
|
|
}, cnr, members)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "could not collect storage group members: %w", err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
sgContent, err := sg.Marshal()
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "could not marshal storage group: %w", err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := object.New()
|
2022-05-31 17:00:41 +00:00
|
|
|
obj.SetContainerID(cnr)
|
2020-12-17 16:15:27 +00:00
|
|
|
obj.SetOwnerID(ownerID)
|
2022-03-03 14:19:05 +00:00
|
|
|
obj.SetType(object.TypeStorageGroup)
|
2021-10-28 14:48:46 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
putPrm.SetHeader(obj)
|
2021-10-28 14:48:46 +00:00
|
|
|
putPrm.SetPayloadReader(bytes.NewReader(sgContent))
|
|
|
|
|
|
|
|
res, err := internalclient.PutObject(putPrm)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "rpc error: %w", err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
cmd.Println("Storage group successfully stored")
|
2022-05-12 16:37:46 +00:00
|
|
|
cmd.Printf(" ID: %s\n CID: %s\n", res.ID(), cnr)
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func getSGID() (*oid.ID, error) {
|
|
|
|
var obj oid.ID
|
|
|
|
err := obj.DecodeString(sgID)
|
2021-09-01 16:33:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse storage group ID: %w", err)
|
|
|
|
}
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
return &obj, nil
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func getSG(cmd *cobra.Command, _ []string) {
|
2022-05-31 17:00:41 +00:00
|
|
|
var cnr cid.ID
|
|
|
|
|
|
|
|
err := readCID(cmd, &cnr)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "", err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
id, err := getSGID()
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "", err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnr)
|
|
|
|
addr.SetObject(*id)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
var prm internalclient.GetObjectPrm
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
prepareSessionPrm(cmd, cnr, id, &prm)
|
2021-10-28 14:48:46 +00:00
|
|
|
prepareObjectPrmRaw(cmd, &prm)
|
|
|
|
prm.SetAddress(addr)
|
|
|
|
prm.SetPayloadWriter(buf)
|
|
|
|
|
|
|
|
_, err = internalclient.GetObject(prm)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "rpc error: %w", err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2022-05-13 09:25:13 +00:00
|
|
|
var sg storagegroupAPI.StorageGroup
|
2021-07-06 12:27:54 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
err = sg.Unmarshal(buf.Bytes())
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "could not unmarshal storage group: %w", err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
cmd.Printf("Expiration epoch: %d\n", sg.ExpirationEpoch())
|
|
|
|
cmd.Printf("Group size: %d\n", sg.ValidationDataSize())
|
2022-05-11 16:35:01 +00:00
|
|
|
printChecksum(cmd, "Group hash", sg.ValidationDataHash)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
if members := sg.Members(); len(members) > 0 {
|
|
|
|
cmd.Println("Members:")
|
|
|
|
|
|
|
|
for i := range members {
|
2022-05-31 17:00:41 +00:00
|
|
|
cmd.Printf("\t%s\n", members[i])
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func listSG(cmd *cobra.Command, _ []string) {
|
2022-05-31 17:00:41 +00:00
|
|
|
var cnr cid.ID
|
|
|
|
|
|
|
|
err := readCID(cmd, &cnr)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "", err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
var prm internalclient.SearchObjectsPrm
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
prepareSessionPrm(cmd, cnr, nil, &prm)
|
2021-10-28 14:48:46 +00:00
|
|
|
prepareObjectPrm(cmd, &prm)
|
2022-05-12 16:37:46 +00:00
|
|
|
prm.SetContainerID(cnr)
|
2021-10-28 14:48:46 +00:00
|
|
|
prm.SetFilters(storagegroup.SearchQuery())
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
res, err := internalclient.SearchObjects(prm)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "rpc error: %w", err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
ids := res.IDList()
|
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
cmd.Printf("Found %d storage groups.\n", len(ids))
|
|
|
|
|
2022-03-28 13:57:15 +00:00
|
|
|
for i := range ids {
|
2022-05-31 17:00:41 +00:00
|
|
|
cmd.Println(ids[i])
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func delSG(cmd *cobra.Command, _ []string) {
|
2022-05-31 17:00:41 +00:00
|
|
|
var cnr cid.ID
|
|
|
|
|
|
|
|
err := readCID(cmd, &cnr)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "", err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
id, err := getSGID()
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "", err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnr)
|
|
|
|
addr.SetObject(*id)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
var prm internalclient.DeleteObjectPrm
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
prepareSessionPrm(cmd, cnr, id, &prm)
|
2021-10-28 14:48:46 +00:00
|
|
|
prepareObjectPrm(cmd, &prm)
|
|
|
|
prm.SetAddress(addr)
|
|
|
|
|
|
|
|
res, err := internalclient.DeleteObject(prm)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "rpc error: %w", err)
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
tomb := res.Tombstone()
|
2022-05-12 16:37:46 +00:00
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
cmd.Println("Storage group removed successfully.")
|
2022-05-31 17:00:41 +00:00
|
|
|
cmd.Printf(" Tombstone: %s\n", tomb)
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|