Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
68706: cli/demo: make `demo` recognize `--listening-url-file` r=otan a=knz

Informs cockroachdb#68437.

When building automation, it is useful to start a `demo` cluster in
such a way that another command can subsequently connect without
manual steps. Prior to this patch, this was not possible, as
the connection credentials would only be displayed in the
informational messages. This patch fixes it.

Release note (cli change): `cockroach demo` now recognizes the
command-line flag `--listening-url-file` like `start` and
`start-single-node`. When specified, the `demo` utility will write a
valid connection URL to that file after the test cluster has been
initialized.

This facility also makes it possible to automatically wait until the
demo cluster has been initialized in automation, for example by
passing the name of a unix named FIFO via the new flag.

Co-authored-by: Raphael 'kena' Poss <[email protected]>
  • Loading branch information
craig[bot] and knz committed Aug 12, 2021
2 parents 2fe9c6f + 76199f6 commit 65abfcc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/cli/democluster/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ type Context struct {
// HTTPPort is the first HTTP port number to use when instantiating
// servers. Use zero for auto-allocated random ports.
HTTPPort int

// ListeningURLFile can be set to a file which is written to after
// the demo cluster has started, to contain a valid connection URL.
ListeningURLFile string
}

// IsInteractive returns true if the demo cluster configuration
Expand Down
8 changes: 8 additions & 0 deletions pkg/cli/democluster/demo_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ func (c *transientCluster) Start(
}
c.connURL = purl.ToPQ().String()

// Write the URL to a file if this was requested by configuration.
if c.demoCtx.ListeningURLFile != "" {
c.infoLog(ctx, "listening URL file: %s", c.demoCtx.ListeningURLFile)
if err = ioutil.WriteFile(c.demoCtx.ListeningURLFile, []byte(fmt.Sprintf("%s\n", c.connURL)), 0644); err != nil {
c.warnLog(ctx, "failed writing the URL: %v", err)
}
}

// Start up the update check loop.
// We don't do this in (*server.Server).Start() because we don't want this
// overhead and possible interference in tests.
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,7 @@ func init() {

intFlag(f, &demoCtx.SQLPort, cliflags.DemoSQLPort)
intFlag(f, &demoCtx.HTTPPort, cliflags.DemoHTTPPort)
stringFlag(f, &demoCtx.ListeningURLFile, cliflags.ListeningURLFile)
}

// statement-diag command.
Expand Down
26 changes: 26 additions & 0 deletions pkg/cli/interactive_tests/test_demo.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,30 @@ interrupt
eexpect eof


end_test

start_test "Check that demo populates the connection URL in a configured file"

spawn $argv demo --no-example-database --listening-url-file=test.url
eexpect "Welcome"
eexpect "defaultdb>"

# Check the URL is valid. If the connection fails, the system command will fail too.
system "$argv sql --url `cat test.url` -e 'select 1'"

interrupt
eexpect eof

# Ditto, insecure
spawn $argv demo --no-example-database --listening-url-file=test.url --insecure
eexpect "Welcome"
eexpect "defaultdb>"

# Check the URL is valid. If the connection fails, the system command will fail too.
system "$argv sql --url `cat test.url` -e 'select 1'"

interrupt
eexpect eof


end_test

0 comments on commit 65abfcc

Please sign in to comment.