From 5a3a88e0bb62d15cf17667d05a86f34cd35b89d6 Mon Sep 17 00:00:00 2001 From: AlexandrLitkevich Date: Thu, 30 Jan 2025 16:30:52 +0300 Subject: [PATCH] aeon: added the ability to connect from the config Closes #TNTP-1073 --- cli/aeon/cmd/connect.go | 1 + cli/cmd/aeon.go | 52 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/cli/aeon/cmd/connect.go b/cli/aeon/cmd/connect.go index d84272ccd..d153b9c64 100644 --- a/cli/aeon/cmd/connect.go +++ b/cli/aeon/cmd/connect.go @@ -20,4 +20,5 @@ type ConnectCtx struct { Network string // Address is a connection URL, unix socket address and etc. Address string + } diff --git a/cli/cmd/aeon.go b/cli/cmd/aeon.go index 4cf3c54b1..ceb1fba20 100644 --- a/cli/cmd/aeon.go +++ b/cli/cmd/aeon.go @@ -1,9 +1,12 @@ package cmd import ( + "encoding/json" "errors" "fmt" + "os" "path/filepath" + "regexp" "github.com/spf13/cobra" aeon "github.com/tarantool/tt/cli/aeon" @@ -12,6 +15,7 @@ import ( "github.com/tarantool/tt/cli/console" "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/util" + "github.com/tarantool/tt/lib/cluster" libconnect "github.com/tarantool/tt/lib/connect" ) @@ -24,6 +28,10 @@ var connectCtx = aeoncmd.ConnectCtx{ Transport: aeoncmd.TransportPlain, } +// Определяем переменные для флагов +var configPath string +var instance string + func newAeonConnectCmd() *cobra.Command { var aeonCmd = &cobra.Command{ Use: "connect URI", @@ -42,14 +50,17 @@ tt aeon connect unix://`, internalAeonConnect, args) util.HandleCmdErr(cmd, err) }, - Args: cobra.ExactArgs(1), + // Args: cobra.ExactArgs(1), } + aeonCmd.Flags().StringVar(&connectCtx.Ssl.KeyFile, "sslkeyfile", "", "path to a private SSL key file") aeonCmd.Flags().StringVar(&connectCtx.Ssl.CertFile, "sslcertfile", "", "path to a SSL certificate file") aeonCmd.Flags().StringVar(&connectCtx.Ssl.CaFile, "sslcafile", "", "path to a trusted certificate authorities (CA) file") + aeonCmd.Flags().StringVarP(&configPath, "config", "c", "", "path config") + aeonCmd.Flags().StringVarP(&instance, "instanceName", "i", "", "instance name") aeonCmd.Flags().Var(&connectCtx.Transport, "transport", fmt.Sprintf("allowed %s", aeoncmd.ListValidTransports())) @@ -80,7 +91,44 @@ func NewAeonCmd() *cobra.Command { } func aeonConnectValidateArgs(cmd *cobra.Command, args []string) error { - connectCtx.Network, connectCtx.Address = libconnect.ParseBaseURI(args[0]) + if len(args) != 0 { + connectCtx.Network, connectCtx.Address = libconnect.ParseBaseURI(args[0]) + } else if cmd.Flags().Changed("config") && cmd.Flags().Changed("instanceName") { + f, err := os.ReadFile(configPath) + if err != nil { + return err + } + + pb := cluster.NewYamlCollector(f) + config, err := pb.Collect() + if err != nil { + return err + } + + clusterConfig, err := cluster.MakeClusterConfig(config) + if err != nil { + return err + } + + result := cluster.Instantiate(clusterConfig, instance) + + path := []string{"roles_cfg", "aeon.grpc", "advertise", "uri"} + uri, err := result.Get(path) + if err != nil { + return err + } + + us, ok := uri.(string) + if !ok { + return fmt.Errorf("fail to conver string") + } + + re := regexp.MustCompile("^https?://") + cleanedURL := re.ReplaceAllString(us, "") + + connectCtx.Network, connectCtx.Address = libconnect.ParseBaseURI(cleanedURL) + + } if !cmd.Flags().Changed("transport") && (connectCtx.Ssl.KeyFile != "" || connectCtx.Ssl.CertFile != "" || connectCtx.Ssl.CaFile != "") {