Skip to content

Commit

Permalink
aeon: added the ability to connect from the config
Browse files Browse the repository at this point in the history
Closes #TNTP-1073
  • Loading branch information
AlexandrLitkevich committed Feb 1, 2025
1 parent f13b13e commit b16f839
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
52 changes: 49 additions & 3 deletions cli/cmd/aeon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package cmd
import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"

"github.com/spf13/cobra"
aeon "github.com/tarantool/tt/cli/aeon"
Expand All @@ -12,6 +14,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"
)

Expand All @@ -24,13 +27,17 @@ var connectCtx = aeoncmd.ConnectCtx{
Transport: aeoncmd.TransportPlain,
}

var configPath string
var instance string

func newAeonConnectCmd() *cobra.Command {
var aeonCmd = &cobra.Command{
Use: "connect URI",
Short: "Connect to the aeon instance",
Long: `Connect to the aeon instance.
tt aeon connect localhost:50051
tt aeon connect unix://<socket-path>`,
tt aeon connect unix://<socket-path>
tt aeon connect -c path instanceName>`,
PreRunE: func(cmd *cobra.Command, args []string) error {
err := aeonConnectValidateArgs(cmd, args)
util.HandleCmdErr(cmd, err)
Expand All @@ -42,14 +49,16 @@ tt aeon connect unix://<socket-path>`,
internalAeonConnect, args)
util.HandleCmdErr(cmd, err)
},
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()))
Expand Down Expand Up @@ -80,7 +89,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("it is impossible to result in a 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 != "") {
Expand Down
5 changes: 0 additions & 5 deletions test/integration/aeon/test_aeon.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ def test_cli_ssl_arguments_success(tt_cmd, aeon_ssl, certificates):
@pytest.mark.parametrize(
"args, error",
[
((), "Error: accepts 1 arg(s), received 0"),
(
("localhost:50051", "@aeon_unix_socket"),
"Error: accepts 1 arg(s), received 2",
),
(
(
"--transport",
Expand Down

0 comments on commit b16f839

Please sign in to comment.