2022-06-02 12:24:31 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
containerApi "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/container"
|
|
|
|
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"
|
2023-07-24 12:23:25 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/acl"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2022-06-02 12:24:31 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
containerACL string
|
|
|
|
containerPolicy string
|
|
|
|
containerAttributes []string
|
|
|
|
containerAwait bool
|
|
|
|
containerName string
|
2023-02-22 13:09:18 +00:00
|
|
|
containerNnsName string
|
|
|
|
containerNnsZone string
|
2022-06-02 12:24:31 +00:00
|
|
|
containerNoTimestamp bool
|
2022-09-27 06:49:45 +00:00
|
|
|
force bool
|
2022-06-02 12:24:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var createContainerCmd = &cobra.Command{
|
|
|
|
Use: "create",
|
|
|
|
Short: "Create new container",
|
2023-03-02 10:56:11 +00:00
|
|
|
Long: `Create new container and register it in the FrostFS.
|
2022-06-02 12:24:31 +00:00
|
|
|
It will be stored in sidechain when inner ring will accepts it.`,
|
2024-03-11 14:11:49 +00:00
|
|
|
Run: func(cmd *cobra.Command, _ []string) {
|
2022-12-27 09:36:30 +00:00
|
|
|
placementPolicy, err := parseContainerPolicy(cmd, containerPolicy)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", err)
|
2022-06-02 12:24:31 +00:00
|
|
|
|
2022-11-29 10:22:17 +00:00
|
|
|
key := key.Get(cmd)
|
2022-09-27 06:49:45 +00:00
|
|
|
cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC)
|
|
|
|
|
|
|
|
if !force {
|
|
|
|
var prm internalclient.NetMapSnapshotPrm
|
|
|
|
prm.SetClient(cli)
|
|
|
|
|
2023-05-24 13:51:57 +00:00
|
|
|
resmap, err := internalclient.NetMapSnapshot(cmd.Context(), prm)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "unable to get netmap snapshot to validate container placement, "+
|
2022-09-27 06:49:45 +00:00
|
|
|
"use --force option to skip this check: %w", err)
|
|
|
|
|
2022-11-30 10:52:26 +00:00
|
|
|
nodesByRep, err := resmap.NetMap().ContainerNodes(*placementPolicy, nil)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "could not build container nodes based on given placement policy, "+
|
2022-09-27 06:49:45 +00:00
|
|
|
"use --force option to skip this check: %w", err)
|
2022-11-30 10:52:26 +00:00
|
|
|
|
|
|
|
for i, nodes := range nodesByRep {
|
2024-03-29 12:31:37 +00:00
|
|
|
if placementPolicy.ReplicaDescriptor(i).NumberOfObjects() > uint32(len(nodes)) {
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", fmt.Errorf(
|
2022-11-30 10:52:26 +00:00
|
|
|
"the number of nodes '%d' in selector is not enough for the number of replicas '%d', "+
|
|
|
|
"use --force option to skip this check",
|
|
|
|
len(nodes),
|
2024-03-29 12:31:37 +00:00
|
|
|
placementPolicy.ReplicaDescriptor(i).NumberOfObjects(),
|
2022-11-30 10:52:26 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2022-09-27 06:49:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
var cnr container.Container
|
|
|
|
cnr.Init()
|
|
|
|
|
|
|
|
err = parseAttributes(&cnr, containerAttributes)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", err)
|
2022-06-02 12:24:31 +00:00
|
|
|
|
2022-06-17 13:40:51 +00:00
|
|
|
var basicACL acl.Basic
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "decode basic ACL string: %w", basicACL.DecodeString(containerACL))
|
2022-06-02 12:24:31 +00:00
|
|
|
|
2022-10-20 09:40:33 +00:00
|
|
|
tok := getSession(cmd)
|
2022-06-02 12:24:31 +00:00
|
|
|
|
2022-10-20 09:40:33 +00:00
|
|
|
if tok != nil {
|
2022-06-02 12:24:31 +00:00
|
|
|
issuer := tok.Issuer()
|
2022-06-28 07:01:05 +00:00
|
|
|
cnr.SetOwner(issuer)
|
2022-06-02 12:24:31 +00:00
|
|
|
} else {
|
|
|
|
var idOwner user.ID
|
|
|
|
user.IDFromKey(&idOwner, key.PublicKey)
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
cnr.SetOwner(idOwner)
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
cnr.SetPlacementPolicy(*placementPolicy)
|
2022-06-02 12:24:31 +00:00
|
|
|
cnr.SetBasicACL(basicACL)
|
|
|
|
|
2022-04-29 16:59:05 +00:00
|
|
|
var syncContainerPrm internalclient.SyncContainerPrm
|
|
|
|
syncContainerPrm.SetClient(cli)
|
|
|
|
syncContainerPrm.SetContainer(&cnr)
|
|
|
|
|
2023-05-24 13:51:57 +00:00
|
|
|
_, err = internalclient.SyncContainerSettings(cmd.Context(), syncContainerPrm)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "syncing container's settings rpc error: %w", err)
|
2022-04-29 16:59:05 +00:00
|
|
|
|
2023-07-24 12:23:25 +00:00
|
|
|
putPrm := internalclient.PutContainerPrm{
|
2023-08-10 08:43:17 +00:00
|
|
|
Client: cli,
|
|
|
|
ClientParams: client.PrmContainerPut{
|
2023-07-24 12:23:25 +00:00
|
|
|
Container: &cnr,
|
|
|
|
Session: tok,
|
|
|
|
},
|
2022-06-22 10:55:31 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 13:51:57 +00:00
|
|
|
res, err := internalclient.PutContainer(cmd.Context(), putPrm)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "put container rpc error: %w", err)
|
2022-06-02 12:24:31 +00:00
|
|
|
|
|
|
|
id := res.ID()
|
|
|
|
|
2023-12-18 11:53:00 +00:00
|
|
|
cmd.Println("CID:", id)
|
2022-06-02 12:24:31 +00:00
|
|
|
|
|
|
|
if containerAwait {
|
|
|
|
cmd.Println("awaiting...")
|
|
|
|
|
2023-08-10 08:09:36 +00:00
|
|
|
getPrm := internalclient.GetContainerPrm{
|
|
|
|
Client: cli,
|
|
|
|
ClientParams: client.PrmContainerGet{
|
|
|
|
ContainerID: &id,
|
|
|
|
},
|
|
|
|
}
|
2022-06-02 12:24:31 +00:00
|
|
|
|
|
|
|
for i := 0; i < awaitTimeout; i++ {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2023-05-24 13:51:57 +00:00
|
|
|
_, err := internalclient.GetContainer(cmd.Context(), getPrm)
|
2022-06-02 12:24:31 +00:00
|
|
|
if err == nil {
|
|
|
|
cmd.Println("container has been persisted on sidechain")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", errCreateTimeout)
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func initContainerCreateCmd() {
|
|
|
|
flags := createContainerCmd.Flags()
|
|
|
|
|
2022-11-29 10:22:17 +00:00
|
|
|
// Init common flags
|
|
|
|
flags.StringP(commonflags.RPC, commonflags.RPCShorthand, commonflags.RPCDefault, commonflags.RPCUsage)
|
2023-05-24 11:50:19 +00:00
|
|
|
flags.Bool(commonflags.TracingFlag, false, commonflags.TracingFlagUsage)
|
2022-11-29 10:22:17 +00:00
|
|
|
flags.DurationP(commonflags.Timeout, commonflags.TimeoutShorthand, commonflags.TimeoutDefault, commonflags.TimeoutUsage)
|
|
|
|
flags.StringP(commonflags.WalletPath, commonflags.WalletPathShorthand, commonflags.WalletPathDefault, commonflags.WalletPathUsage)
|
|
|
|
flags.StringP(commonflags.Account, commonflags.AccountShorthand, commonflags.AccountDefault, commonflags.AccountUsage)
|
|
|
|
|
2022-10-11 14:02:03 +00:00
|
|
|
flags.StringVar(&containerACL, "basic-acl", acl.NamePrivate, fmt.Sprintf("HEX encoded basic ACL value or keywords like '%s', '%s', '%s'",
|
2022-06-17 13:40:51 +00:00
|
|
|
acl.NamePublicRW, acl.NamePrivate, acl.NamePublicROExtended,
|
|
|
|
))
|
2022-06-02 12:24:31 +00:00
|
|
|
flags.StringVarP(&containerPolicy, "policy", "p", "", "QL-encoded or JSON-encoded placement policy or path to file with it")
|
2022-10-11 14:02:03 +00:00
|
|
|
flags.StringSliceVarP(&containerAttributes, "attributes", "a", nil, "Comma separated pairs of container attributes in form of Key1=Value1,Key2=Value2")
|
|
|
|
flags.BoolVar(&containerAwait, "await", false, "Block execution until container is persisted")
|
|
|
|
flags.StringVar(&containerName, "name", "", "Container name attribute")
|
2023-02-22 13:09:18 +00:00
|
|
|
flags.StringVar(&containerNnsName, "nns-name", "", "Container nns name attribute")
|
|
|
|
flags.StringVar(&containerNnsZone, "nns-zone", "", "Container nns zone attribute")
|
2022-10-11 14:02:03 +00:00
|
|
|
flags.BoolVar(&containerNoTimestamp, "disable-timestamp", false, "Disable timestamp container attribute")
|
2022-09-27 06:49:45 +00:00
|
|
|
flags.BoolVarP(&force, commonflags.ForceFlag, commonflags.ForceFlagShorthand, false,
|
2022-10-11 14:02:03 +00:00
|
|
|
"Skip placement validity check")
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
func parseContainerPolicy(cmd *cobra.Command, policyString string) (*netmap.PlacementPolicy, error) {
|
2022-06-02 12:24:31 +00:00
|
|
|
_, err := os.Stat(policyString) // check if `policyString` is a path to file with placement policy
|
|
|
|
if err == nil {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Reading placement policy from file: %s", policyString)
|
2022-06-02 12:24:31 +00:00
|
|
|
|
|
|
|
data, err := os.ReadFile(policyString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read file with placement policy: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
policyString = string(data)
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
var result netmap.PlacementPolicy
|
|
|
|
|
|
|
|
err = result.DecodeString(policyString)
|
2022-06-02 12:24:31 +00:00
|
|
|
if err == nil {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Parsed QL encoded policy")
|
2022-06-08 23:18:26 +00:00
|
|
|
return &result, nil
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 07:44:43 +00:00
|
|
|
if err := result.UnmarshalJSON([]byte(policyString)); err == nil {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Parsed JSON encoded policy")
|
2022-06-08 23:18:26 +00:00
|
|
|
return &result, nil
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 07:44:43 +00:00
|
|
|
return nil, fmt.Errorf("can't parse placement policy: %w", err)
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
func parseAttributes(dst *container.Container, attributes []string) error {
|
2022-06-02 12:24:31 +00:00
|
|
|
for i := range attributes {
|
2023-02-27 14:19:35 +00:00
|
|
|
k, v, found := strings.Cut(attributes[i], attributeDelimiter)
|
|
|
|
if !found {
|
2022-06-28 07:01:05 +00:00
|
|
|
return errors.New("invalid container attribute")
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:19:35 +00:00
|
|
|
dst.SetAttribute(k, v)
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !containerNoTimestamp {
|
2022-06-28 07:01:05 +00:00
|
|
|
container.SetCreationTime(dst, time.Now())
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if containerName != "" {
|
2022-06-28 07:01:05 +00:00
|
|
|
container.SetName(dst, containerName)
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 07:52:41 +00:00
|
|
|
if containerNnsName != "" {
|
|
|
|
dst.SetAttribute(containerApi.SysAttributeName, containerNnsName)
|
|
|
|
}
|
|
|
|
if containerNnsZone != "" {
|
|
|
|
dst.SetAttribute(containerApi.SysAttributeZone, containerNnsZone)
|
|
|
|
}
|
2023-02-22 13:09:18 +00:00
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
return nil
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|