2022-06-02 12:24:31 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2022-06-28 11:18:54 +00:00
|
|
|
"fmt"
|
2022-06-02 12:24:31 +00:00
|
|
|
"time"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
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"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2022-06-02 12:24:31 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var deleteContainerCmd = &cobra.Command{
|
|
|
|
Use: "delete",
|
|
|
|
Short: "Delete existing container",
|
2023-03-02 10:56:11 +00:00
|
|
|
Long: `Delete existing container.
|
2022-06-02 12:24:31 +00:00
|
|
|
Only owner of the container has a permission to remove container.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
id := parseContainerID(cmd)
|
|
|
|
|
2022-10-20 09:40:33 +00:00
|
|
|
tok := getSession(cmd)
|
2022-06-02 12:24:31 +00:00
|
|
|
|
2022-09-19 13:10:56 +00:00
|
|
|
pk := key.Get(cmd)
|
2022-06-02 12:24:31 +00:00
|
|
|
cli := internalclient.GetSDKClientByFlag(cmd, pk, commonflags.RPC)
|
|
|
|
|
2022-09-27 06:49:45 +00:00
|
|
|
if force, _ := cmd.Flags().GetBool(commonflags.ForceFlag); !force {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Reading the container to check ownership...")
|
2022-12-08 07:28:57 +00:00
|
|
|
|
|
|
|
var getPrm internalclient.GetContainerPrm
|
|
|
|
getPrm.SetClient(cli)
|
|
|
|
getPrm.SetContainer(id)
|
|
|
|
|
|
|
|
resGet, err := internalclient.GetContainer(getPrm)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't get the container: %w", err)
|
2022-12-08 07:28:57 +00:00
|
|
|
|
|
|
|
owner := resGet.Container().Owner()
|
|
|
|
|
|
|
|
if tok != nil {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Checking session issuer...")
|
2022-12-08 07:28:57 +00:00
|
|
|
|
|
|
|
if !tok.Issuer().Equals(owner) {
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", fmt.Errorf("session issuer differs with the container owner: expected %s, has %s", owner, tok.Issuer()))
|
2022-12-08 07:28:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Checking provided account...")
|
2022-12-08 07:28:57 +00:00
|
|
|
|
|
|
|
var acc user.ID
|
|
|
|
user.IDFromKey(&acc, pk.PublicKey)
|
|
|
|
|
|
|
|
if !acc.Equals(owner) {
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", fmt.Errorf("provided account differs with the container owner: expected %s, has %s", owner, acc))
|
2022-12-08 07:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Account matches the container owner.")
|
2022-12-08 07:28:57 +00:00
|
|
|
|
2022-12-16 12:14:26 +00:00
|
|
|
if tok != nil {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Skip searching for LOCK objects - session provided.")
|
2022-12-16 12:14:26 +00:00
|
|
|
} else {
|
|
|
|
fs := objectSDK.NewSearchFilters()
|
|
|
|
fs.AddTypeFilter(objectSDK.MatchStringEqual, objectSDK.TypeLock)
|
2022-06-28 11:18:54 +00:00
|
|
|
|
2022-12-16 12:14:26 +00:00
|
|
|
var searchPrm internalclient.SearchObjectsPrm
|
|
|
|
searchPrm.SetClient(cli)
|
|
|
|
searchPrm.SetContainerID(id)
|
|
|
|
searchPrm.SetFilters(fs)
|
|
|
|
searchPrm.SetTTL(2)
|
2022-06-28 11:18:54 +00:00
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Searching for LOCK objects...")
|
2022-06-28 11:18:54 +00:00
|
|
|
|
2022-12-16 12:14:26 +00:00
|
|
|
res, err := internalclient.SearchObjects(searchPrm)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't search for LOCK objects: %w", err)
|
2022-06-28 11:18:54 +00:00
|
|
|
|
2022-12-16 12:14:26 +00:00
|
|
|
if len(res.IDList()) != 0 {
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "",
|
2023-04-03 13:39:29 +00:00
|
|
|
fmt.Errorf("container wasn't removed because LOCK objects were found, "+
|
|
|
|
"use --%s flag to remove anyway", commonflags.ForceFlag))
|
2022-12-16 12:14:26 +00:00
|
|
|
}
|
2022-06-28 11:18:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 12:24:31 +00:00
|
|
|
var delPrm internalclient.DeleteContainerPrm
|
|
|
|
delPrm.SetClient(cli)
|
|
|
|
delPrm.SetContainer(id)
|
|
|
|
|
|
|
|
if tok != nil {
|
|
|
|
delPrm.WithinSession(*tok)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := internalclient.DeleteContainer(delPrm)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "rpc error: %w", err)
|
2022-06-02 12:24:31 +00:00
|
|
|
|
|
|
|
cmd.Println("container delete method invoked")
|
|
|
|
|
|
|
|
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 removed:", containerID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", errDeleteTimeout)
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func initContainerDeleteCmd() {
|
|
|
|
flags := deleteContainerCmd.Flags()
|
|
|
|
|
2022-09-19 13:10:56 +00:00
|
|
|
flags.StringP(commonflags.WalletPath, commonflags.WalletPathShorthand, commonflags.WalletPathDefault, commonflags.WalletPathUsage)
|
|
|
|
flags.StringP(commonflags.Account, commonflags.AccountShorthand, commonflags.AccountDefault, commonflags.AccountUsage)
|
|
|
|
flags.StringP(commonflags.RPC, commonflags.RPCShorthand, commonflags.RPCDefault, commonflags.RPCUsage)
|
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
flags.StringVar(&containerID, commonflags.CIDFlag, "", commonflags.CIDFlagUsage)
|
2022-10-11 14:02:03 +00:00
|
|
|
flags.BoolVar(&containerAwait, "await", false, "Block execution until container is removed")
|
2022-12-08 07:28:57 +00:00
|
|
|
flags.BoolP(commonflags.ForceFlag, commonflags.ForceFlagShorthand, false, "Skip validation checks (ownership, presence of LOCK objects)")
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|