From 18d02314c1a8eadbb0d1c9d593714b17d828abad Mon Sep 17 00:00:00 2001 From: Rohan Yadav Date: Wed, 1 Apr 2020 23:57:42 -0400 Subject: [PATCH] demo: display connection urls for nodes in the demo cluster Fixes #46901. Release note (cli change): cockroach demo now displays a connection URL to the demo cluster, and in a multi-node cluster it displays connection strings for all of the nodes in the cluster. --- pkg/cli/demo.go | 49 +++++++++++++++++++++++-- pkg/cli/interactive_tests/test_demo.tcl | 25 +++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/pkg/cli/demo.go b/pkg/cli/demo.go index 1a0ab3f3b810..6e4cbe46b49f 100644 --- a/pkg/cli/demo.go +++ b/pkg/cli/demo.go @@ -595,7 +595,12 @@ func setupTransientCluster( // Prepare the URL for use by the SQL shell. // TODO (rohany): there should be a way that the user can request a specific node // to connect to to see the effects of the artificial latency. - c.connURL, err = makeURLForServer(c.s, gen, c.certsDir) + c.connURL, err = makeURLForServer( + c.s, + gen, + c.certsDir, + true, /* includeAppName */ + ) if err != nil { return c, err } @@ -659,7 +664,12 @@ func (c *transientCluster) setupWorkload( if demoCtx.runWorkload { var sqlURLs []string for i := range c.servers { - sqlURL, err := makeURLForServer(c.servers[i], gen, c.certsDir) + sqlURL, err := makeURLForServer( + c.servers[i], + gen, + c.certsDir, + true, /* includeAppName */ + ) if err != nil { return err } @@ -723,7 +733,7 @@ func (c *transientCluster) runWorkload( } func makeURLForServer( - s *server.TestServer, gen workload.Generator, certPath string, + s *server.TestServer, gen workload.Generator, certPath string, includeAppName bool, ) (string, error) { options := url.Values{} cfg := base.Config{ @@ -734,7 +744,9 @@ func makeURLForServer( if err := cfg.LoadSecurityOptions(options, security.RootUser); err != nil { return "", err } - options.Add("application_name", sqlbase.ReportableAppNamePrefix+"cockroach demo") + if includeAppName { + options.Add("application_name", sqlbase.ReportableAppNamePrefix+"cockroach demo") + } sqlURL := url.URL{ Scheme: "postgres", User: url.User(security.RootUser), @@ -969,6 +981,35 @@ func runDemo(cmd *cobra.Command, gen workload.Generator) (err error) { # `, c.s.AdminURL()) + // Don't include the application name in the URLs we display to users. + if demoCtx.nodes == 1 { + connURL, err := makeURLForServer( + c.servers[0], + gen, + c.certsDir, + false, /* includeAppName */ + ) + if err != nil { + return checkAndMaybeShout(err) + } + fmt.Printf("# Connect to the cluster on a SQL shell at: '%s'.\n#\n", connURL) + } else { + fmt.Printf("# Connect to different nodes in the cluster on a SQL shell at:\n") + for _, s := range c.servers { + connURL, err := makeURLForServer( + s, + gen, + c.certsDir, + false, /* includeAppName */ + ) + if err != nil { + return checkAndMaybeShout(err) + } + fmt.Printf("# * Node %d: '%s'\n", s.NodeID(), connURL) + } + fmt.Printf("#\n") + } + if !demoCtx.insecure { fmt.Printf( "# The user %q with password %q has been created. Use it to access the Web UI!\n#\n", diff --git a/pkg/cli/interactive_tests/test_demo.tcl b/pkg/cli/interactive_tests/test_demo.tcl index ec70e9cf5fa5..20a1e4603cbb 100644 --- a/pkg/cli/interactive_tests/test_demo.tcl +++ b/pkg/cli/interactive_tests/test_demo.tcl @@ -39,3 +39,28 @@ eexpect root@ # Ensure db is movr. eexpect "movr>" end_test + +# Test that demo displays connection URLs for nodes in the cluster. +start_test "Check that node URLs are displayed" +spawn $argv demo --insecure +# Check that we see our message. +eexpect "Connect to the cluster on a SQL shell at" + +# Start the test again with a multi node cluster. +spawn $argv demo --insecure --nodes 3 + +# Check that we get a message for each node. +eexpect "Connect to different nodes in the cluster on a SQL shell at" +eexpect "Node 1" +eexpect "Node 2" +eexpect "Node 3" + +spawn $argv demo --insecure=false +eexpect "Connect to the cluster on a SQL shell at" +# Expect that security related tags are part of the connection URL. +eexpect "sslcert=" +eexpect "sslkey=" +eexpect "sslrootcert=" + +end_test +