|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/url" |
| 6 | + "path/filepath" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + |
| 13 | + "github.com/tarantool/tt/cli/cluster" |
| 14 | + "github.com/tarantool/tt/cli/cmd/internal" |
| 15 | + "github.com/tarantool/tt/cli/cmdcontext" |
| 16 | + "github.com/tarantool/tt/cli/modules" |
| 17 | + "github.com/tarantool/tt/cli/running" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + defaultTimeout = 3 * time.Second |
| 22 | + configFileName = "config.yaml" |
| 23 | +) |
| 24 | + |
| 25 | +func NewClusterCmd() *cobra.Command { |
| 26 | + clusterCmd := &cobra.Command{ |
| 27 | + Use: "cluster", |
| 28 | + Short: "Manage cluster configuration", |
| 29 | + } |
| 30 | + |
| 31 | + clusterCmd.AddCommand(&cobra.Command{ |
| 32 | + Use: "show (<APP_NAME> | <APP_NAME:INSTANCE_NAME> | <URI>)", |
| 33 | + Short: "Show a cluster configuration", |
| 34 | + Long: "Show a cluster configuration for an application, instance or" + |
| 35 | + " from etcd URI.", |
| 36 | + Run: func(cmd *cobra.Command, args []string) { |
| 37 | + cmdCtx.CommandName = cmd.Name() |
| 38 | + err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, |
| 39 | + internalClusterShowModule, args) |
| 40 | + handleCmdErr(cmd, err) |
| 41 | + }, |
| 42 | + Args: cobra.ExactArgs(1), |
| 43 | + ValidArgsFunction: func( |
| 44 | + cmd *cobra.Command, |
| 45 | + args []string, |
| 46 | + toComplete string) ([]string, cobra.ShellCompDirective) { |
| 47 | + if len(args) != 0 { |
| 48 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 49 | + } |
| 50 | + return internal.ValidArgsFunction( |
| 51 | + cliOpts, &cmdCtx, cmd, toComplete, |
| 52 | + running.ExtractActiveAppNames, |
| 53 | + running.ExtractActiveInstanceNames) |
| 54 | + }, |
| 55 | + DisableFlagParsing: true, |
| 56 | + }) |
| 57 | + |
| 58 | + return clusterCmd |
| 59 | +} |
| 60 | + |
| 61 | +// internalClusterShowModule is an entrypoint for `cluster show` command. |
| 62 | +func internalClusterShowModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { |
| 63 | + uri, urlErr := url.Parse(args[0]) |
| 64 | + if urlErr == nil && uri.Scheme != "" { |
| 65 | + // It looks like a URL, let's get data from the etcd. |
| 66 | + return showEtcd(uri) |
| 67 | + } |
| 68 | + |
| 69 | + if !isConfigExist(cmdCtx) { |
| 70 | + return fmt.Errorf("unable to resolve the application name %q: %w", |
| 71 | + args[0], errNoConfig) |
| 72 | + } |
| 73 | + |
| 74 | + colonIds := strings.Index(args[0], string(running.InstanceDelimiter)) |
| 75 | + instance := colonIds != -1 |
| 76 | + |
| 77 | + var runningCtx running.RunningCtx |
| 78 | + if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args); err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + appDir := filepath.Dir(runningCtx.Instances[0].AppPath) |
| 83 | + path := filepath.Join(appDir, configFileName) |
| 84 | + config, err := cluster.GetClusterConfig(path) |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("failed to get a cluster configuration: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + if !instance { |
| 90 | + fmt.Print(config.RawConfig.String()) |
| 91 | + return nil |
| 92 | + } |
| 93 | + |
| 94 | + return printInstanceConfig(config, runningCtx.Instances[0].InstName) |
| 95 | +} |
| 96 | + |
| 97 | +// showEtcd show a configuration from etcd. |
| 98 | +func showEtcd(uri *url.URL) error { |
| 99 | + etcdOpts, err := parseEtcdOpts(uri) |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("invalid URL %q: %w", uri, err) |
| 102 | + } |
| 103 | + |
| 104 | + etcdcli, err := cluster.ConnectEtcd(etcdOpts) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("failed to connect to etcd: %w", err) |
| 107 | + } |
| 108 | + defer etcdcli.Close() |
| 109 | + |
| 110 | + collector := cluster.NewEtcdCollector( |
| 111 | + etcdcli, etcdOpts.Prefix, etcdOpts.Timeout) |
| 112 | + config, err := collector.Collect() |
| 113 | + if err != nil { |
| 114 | + return fmt.Errorf("failed to collect a configuration from etcd: %w", err) |
| 115 | + } |
| 116 | + |
| 117 | + return printConfig(config, uri.Query().Get("name")) |
| 118 | +} |
| 119 | + |
| 120 | +// parseEtcdOpts parses etcd options from a URL. |
| 121 | +func parseEtcdOpts(uri *url.URL) (cluster.EtcdOpts, error) { |
| 122 | + endpoint := url.URL{ |
| 123 | + Scheme: uri.Scheme, |
| 124 | + Host: uri.Host, |
| 125 | + } |
| 126 | + values := uri.Query() |
| 127 | + opts := cluster.EtcdOpts{ |
| 128 | + Endpoints: []string{endpoint.String()}, |
| 129 | + Prefix: uri.Path, |
| 130 | + Username: uri.User.Username(), |
| 131 | + KeyFile: values.Get("ssl_key_file"), |
| 132 | + CertFile: values.Get("ssl_cert_file"), |
| 133 | + CaPath: values.Get("ssl_ca_path"), |
| 134 | + CaFile: values.Get("ssl_ca_file"), |
| 135 | + Timeout: defaultTimeout, |
| 136 | + } |
| 137 | + if password, ok := uri.User.Password(); ok { |
| 138 | + opts.Password = password |
| 139 | + } |
| 140 | + |
| 141 | + verifyPeerStr := values.Get("verify_peer") |
| 142 | + verifyHostStr := values.Get("verify_host") |
| 143 | + timeoutStr := values.Get("timeout") |
| 144 | + |
| 145 | + if verifyPeerStr != "" { |
| 146 | + verifyPeerStr = strings.ToLower(verifyPeerStr) |
| 147 | + if verify, err := strconv.ParseBool(verifyPeerStr); err == nil { |
| 148 | + if verify == false { |
| 149 | + opts.SkipHostVerify = true |
| 150 | + } |
| 151 | + } else { |
| 152 | + err = fmt.Errorf("invalid verify_peer, boolean expected: %w", err) |
| 153 | + return opts, err |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + if verifyHostStr != "" { |
| 158 | + verifyHostStr = strings.ToLower(verifyHostStr) |
| 159 | + if verify, err := strconv.ParseBool(verifyHostStr); err == nil { |
| 160 | + if verify == false { |
| 161 | + opts.SkipHostVerify = true |
| 162 | + } |
| 163 | + } else { |
| 164 | + err = fmt.Errorf("invalid verify_host, boolean expected: %w", err) |
| 165 | + return opts, err |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if timeoutStr != "" { |
| 170 | + if timeout, err := strconv.ParseFloat(timeoutStr, 64); err == nil { |
| 171 | + opts.Timeout = time.Duration(timeout * float64(time.Second)) |
| 172 | + } else { |
| 173 | + err = fmt.Errorf("invalid timeout, float expected: %w", err) |
| 174 | + return opts, err |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return opts, nil |
| 179 | +} |
| 180 | + |
| 181 | +// printConfig prints a cluster configuration or an instance configuration if |
| 182 | +// the instance name is specified. |
| 183 | +func printConfig(config *cluster.Config, instance string) error { |
| 184 | + if instance == "" { |
| 185 | + fmt.Println(config.String()) |
| 186 | + return nil |
| 187 | + } |
| 188 | + |
| 189 | + cconfig, err := cluster.MakeClusterConfig(config) |
| 190 | + if err != nil { |
| 191 | + return fmt.Errorf( |
| 192 | + "failed to parse a configuration data as a cluster config: %w", |
| 193 | + err) |
| 194 | + } |
| 195 | + |
| 196 | + return printInstanceConfig(cconfig, instance) |
| 197 | +} |
| 198 | + |
| 199 | +// printInstanceConfig prints an instance configuration in the cluster. |
| 200 | +func printInstanceConfig(config cluster.ClusterConfig, instance string) error { |
| 201 | + if !cluster.HasInstance(config, instance) { |
| 202 | + return fmt.Errorf("instance %q not found", instance) |
| 203 | + } |
| 204 | + |
| 205 | + iconfig := cluster.Instantiate(config, instance) |
| 206 | + fmt.Print(iconfig.String()) |
| 207 | + return nil |
| 208 | +} |
0 commit comments