2022-06-01 12:42:28 +00:00
|
|
|
package object
|
2022-02-16 15:56:16 +00:00
|
|
|
|
|
|
|
import (
|
2022-06-17 07:36:23 +00:00
|
|
|
"context"
|
2022-07-19 12:45:58 +00:00
|
|
|
"errors"
|
2022-02-16 15:56:16 +00:00
|
|
|
"fmt"
|
2022-05-31 16:59:07 +00:00
|
|
|
"strconv"
|
2022-06-17 07:36:23 +00:00
|
|
|
"time"
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
objectV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2022-02-16 15:56:16 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// object lock command.
|
2022-06-01 12:42:28 +00:00
|
|
|
var objectLockCmd = &cobra.Command{
|
2022-10-28 19:24:58 +00:00
|
|
|
Use: "lock",
|
2022-02-16 15:56:16 +00:00
|
|
|
Short: "Lock object in container",
|
|
|
|
Long: "Lock object in container",
|
2022-10-28 19:24:58 +00:00
|
|
|
Run: func(cmd *cobra.Command, _ []string) {
|
2022-10-18 11:43:04 +00:00
|
|
|
cidRaw, _ := cmd.Flags().GetString(commonflags.CIDFlag)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2022-10-28 19:24:58 +00:00
|
|
|
var cnr cid.ID
|
|
|
|
err := cnr.DecodeString(cidRaw)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "Incorrect container arg: %v", err)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2023-03-15 17:21:13 +00:00
|
|
|
key := key.GetOrGenerate(cmd)
|
|
|
|
cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC)
|
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
oidsRaw, _ := cmd.Flags().GetStringSlice(commonflags.OIDFlag)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2023-03-15 17:21:13 +00:00
|
|
|
lockList := make([]oid.ID, 0, len(oidsRaw))
|
|
|
|
oidM := make(map[oid.ID]struct{})
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2023-03-15 17:21:13 +00:00
|
|
|
for i, oidRaw := range oidsRaw {
|
|
|
|
var oID oid.ID
|
|
|
|
err = oID.DecodeString(oidRaw)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, fmt.Sprintf("Incorrect object arg #%d: %%v", i+1), err)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2023-03-15 17:21:13 +00:00
|
|
|
if _, ok := oidM[oID]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
lockList = append(lockList, oID)
|
|
|
|
oidM[oID] = struct{}{}
|
|
|
|
|
|
|
|
for _, relative := range collectObjectRelatives(cmd, cli, cnr, oID) {
|
|
|
|
if _, ok := oidM[relative]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
lockList = append(lockList, relative)
|
|
|
|
oidM[relative] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2022-06-01 12:42:28 +00:00
|
|
|
var idOwner user.ID
|
|
|
|
user.IDFromKey(&idOwner, key.PublicKey)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2022-05-31 16:59:07 +00:00
|
|
|
var lock objectSDK.Lock
|
2022-02-16 15:56:16 +00:00
|
|
|
lock.WriteMembers(lockList)
|
|
|
|
|
2022-07-19 12:45:58 +00:00
|
|
|
exp, _ := cmd.Flags().GetUint64(commonflags.ExpireAt)
|
|
|
|
lifetime, _ := cmd.Flags().GetUint64(commonflags.Lifetime)
|
|
|
|
if exp == 0 && lifetime == 0 { // mutual exclusion is ensured by cobra
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", errors.New("either expiration epoch of a lifetime is required"))
|
2022-07-19 12:45:58 +00:00
|
|
|
}
|
2022-05-31 16:59:07 +00:00
|
|
|
|
2022-07-19 12:45:58 +00:00
|
|
|
if lifetime != 0 {
|
2022-06-17 07:36:23 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
|
|
|
defer cancel()
|
|
|
|
|
2022-05-31 16:59:07 +00:00
|
|
|
endpoint, _ := cmd.Flags().GetString(commonflags.RPC)
|
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
currEpoch, err := internalclient.GetCurrentEpoch(ctx, cmd, endpoint)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "Request current epoch: %w", err)
|
2022-05-31 16:59:07 +00:00
|
|
|
|
2022-11-01 17:20:11 +00:00
|
|
|
exp = currEpoch + lifetime
|
2022-05-31 16:59:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Lock object will expire after %d epoch", exp)
|
2022-11-01 17:20:11 +00:00
|
|
|
|
2022-05-31 16:59:07 +00:00
|
|
|
var expirationAttr objectSDK.Attribute
|
|
|
|
expirationAttr.SetKey(objectV2.SysAttributeExpEpoch)
|
|
|
|
expirationAttr.SetValue(strconv.FormatUint(exp, 10))
|
|
|
|
|
|
|
|
obj := objectSDK.New()
|
2022-05-12 16:37:46 +00:00
|
|
|
obj.SetContainerID(cnr)
|
2023-11-21 08:42:30 +00:00
|
|
|
obj.SetOwnerID(idOwner)
|
2022-05-31 16:59:07 +00:00
|
|
|
obj.SetType(objectSDK.TypeLock)
|
|
|
|
obj.SetAttributes(expirationAttr)
|
2022-02-16 15:56:16 +00:00
|
|
|
obj.SetPayload(lock.Marshal())
|
|
|
|
|
|
|
|
var prm internalclient.PutObjectPrm
|
2023-03-15 17:21:13 +00:00
|
|
|
ReadOrOpenSessionViaClient(cmd, &prm, cli, key, cnr, nil)
|
2022-06-01 12:42:28 +00:00
|
|
|
Prepare(cmd, &prm)
|
2022-03-05 08:49:19 +00:00
|
|
|
prm.SetHeader(obj)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2023-05-24 13:51:57 +00:00
|
|
|
res, err := internalclient.PutObject(cmd.Context(), prm)
|
2023-02-03 16:29:25 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "Store lock object in FrostFS: %w", err)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2022-07-19 13:23:31 +00:00
|
|
|
cmd.Printf("Lock object ID: %s\n", res.ID())
|
2022-02-16 15:56:16 +00:00
|
|
|
cmd.Println("Objects successfully locked.")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func initCommandObjectLock() {
|
2022-06-01 12:42:28 +00:00
|
|
|
commonflags.Init(objectLockCmd)
|
2022-10-20 09:40:33 +00:00
|
|
|
initFlagSession(objectLockCmd, "PUT")
|
2022-05-31 16:59:07 +00:00
|
|
|
|
2022-10-28 19:24:58 +00:00
|
|
|
ff := objectLockCmd.Flags()
|
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
ff.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage)
|
|
|
|
_ = objectLockCmd.MarkFlagRequired(commonflags.CIDFlag)
|
2022-10-28 19:24:58 +00:00
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
ff.StringSlice(commonflags.OIDFlag, nil, commonflags.OIDFlagUsage)
|
|
|
|
_ = objectLockCmd.MarkFlagRequired(commonflags.OIDFlag)
|
2022-10-28 19:24:58 +00:00
|
|
|
|
2022-12-21 07:59:35 +00:00
|
|
|
ff.Uint64P(commonflags.ExpireAt, "e", 0, "The last active epoch for the lock")
|
2022-10-28 19:24:58 +00:00
|
|
|
|
|
|
|
ff.Uint64(commonflags.Lifetime, 0, "Lock lifetime")
|
2022-07-19 12:45:58 +00:00
|
|
|
objectLockCmd.MarkFlagsMutuallyExclusive(commonflags.ExpireAt, commonflags.Lifetime)
|
2022-02-16 15:56:16 +00:00
|
|
|
}
|