2022-06-02 12:24:31 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
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"
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd "github.com/TrueCloudLab/frostfs-node/cmd/internal/common"
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/container/acl"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
subnetid "github.com/TrueCloudLab/frostfs-sdk-go/subnet/id"
|
|
|
|
"github.com/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
|
|
|
|
containerSubnet string
|
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-01-27 10:44:39 +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.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []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)
|
|
|
|
|
|
|
|
resmap, err := internalclient.NetMapSnapshot(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 {
|
|
|
|
if placementPolicy.ReplicaNumberByIndex(i) > 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),
|
|
|
|
placementPolicy.ReplicaNumberByIndex(i),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2022-09-27 06:49:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
if containerSubnet != "" {
|
|
|
|
var subnetID subnetid.ID
|
2022-06-02 12:24:31 +00:00
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
err = subnetID.DecodeString(containerSubnet)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "could not parse subnetID: %w", err)
|
2022-06-02 12:24:31 +00:00
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
placementPolicy.RestrictSubnet(subnetID)
|
|
|
|
}
|
2022-06-02 12:24:31 +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)
|
|
|
|
|
|
|
|
_, err = internalclient.SyncContainerSettings(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
|
|
|
|
2022-06-02 12:24:31 +00:00
|
|
|
var putPrm internalclient.PutContainerPrm
|
|
|
|
putPrm.SetClient(cli)
|
2022-06-28 07:01:05 +00:00
|
|
|
putPrm.SetContainer(cnr)
|
2022-06-02 12:24:31 +00:00
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
if tok != nil {
|
|
|
|
putPrm.WithinSession(*tok)
|
|
|
|
}
|
|
|
|
|
2022-06-02 12:24:31 +00:00
|
|
|
res, err := internalclient.PutContainer(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()
|
|
|
|
|
|
|
|
cmd.Println("container ID:", id)
|
|
|
|
|
|
|
|
if containerAwait {
|
|
|
|
cmd.Println("awaiting...")
|
|
|
|
|
|
|
|
var getPrm internalclient.GetContainerPrm
|
|
|
|
getPrm.SetClient(cli)
|
|
|
|
getPrm.SetContainer(id)
|
|
|
|
|
|
|
|
for i := 0; i < awaitTimeout; i++ {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
_, err := internalclient.GetContainer(getPrm)
|
|
|
|
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)
|
|
|
|
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")
|
|
|
|
flags.StringVar(&containerSubnet, "subnet", "", "String representation of container subnetwork")
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("can't parse placement policy")
|
|
|
|
}
|
|
|
|
|
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-02-22 13:09:18 +00:00
|
|
|
var domain container.Domain
|
|
|
|
domain.SetName(containerNnsName)
|
|
|
|
domain.SetZone(containerNnsZone)
|
|
|
|
container.WriteDomain(dst, domain)
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
return nil
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|