Skip to content

Commit

Permalink
Merge pull request #15468 from pydima/master
Browse files Browse the repository at this point in the history
cli: Add --listening-url-file flag
  • Loading branch information
tamird authored Apr 29, 2017
2 parents 630757c + 8c72a26 commit 1d7e0ac
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/base/test_server_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type TestServerArgs struct {
TimeSeriesQueryWorkerMax int
SQLMemoryPoolSize int64
SendNextTimeout time.Duration
ListeningURLFile string

// If set, this will be appended to the Postgres URL by functions that
// automatically open a connection to the server. That's equivalent to running
Expand Down
7 changes: 7 additions & 0 deletions pkg/cli/cliflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ communication; it must resolve from other nodes in the cluster.`,
Description: `The port to bind to for HTTP requests.`,
}

ListeningURLFile = FlagInfo{
Name: "listening-url-file",
Description: `
After the CockroachDB node has started up successfully, it will
write its connection URL to the specified file.`,
}

PIDFile = FlagInfo{
Name: "pid-file",
Description: `
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ func init() {
stringFlag(f, &serverCfg.SocketFile, cliflags.Socket, "")
_ = f.MarkHidden(cliflags.Socket.Name)

stringFlag(f, &serverCfg.ListeningURLFile, cliflags.ListeningURLFile, "")

stringFlag(f, &serverCfg.PIDFile, cliflags.PIDFile, "")

// Use a separate variable to store the value of ServerInsecure.
Expand Down
4 changes: 4 additions & 0 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ type Config struct {
// actions.
EventLogEnabled bool

// ListeningURLFile indicates the file to which the server writes
// its listening URL when it is ready.
ListeningURLFile string

// PIDFile indicates the file to which the server writes its PID when
// it is ready.
PIDFile string
Expand Down
14 changes: 14 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"math"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
Expand All @@ -48,6 +49,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/migrations"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/server/status"
"github.com/cockroachdb/cockroach/pkg/sql"
Expand Down Expand Up @@ -858,6 +860,18 @@ func (s *Server) Start(ctx context.Context) error {
log.Error(ctx, err)
}
}

if s.cfg.ListeningURLFile != "" {
pgURL, err := s.cfg.PGURL(url.User(security.RootUser))
if err == nil {
err = ioutil.WriteFile(s.cfg.ListeningURLFile, []byte(fmt.Sprintf("%s\n", pgURL)), 0644)
}

if err != nil {
log.Error(ctx, err)
}
}

if err := sdnotify.Ready(); err != nil {
log.Errorf(ctx, "failed to signal readiness using systemd protocol: %s", err)
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -478,3 +479,42 @@ func TestClusterStores(t *testing.T) {
return nil
})
}

func TestListenURLFileCreation(t *testing.T) {
defer leaktest.AfterTest(t)()

file, err := ioutil.TempFile(os.TempDir(), t.Name())
if err != nil {
t.Fatal(err)
}
if err := file.Close(); err != nil {
t.Fatal(err)
}

s, err := serverutils.StartServerRaw(base.TestServerArgs{
ListeningURLFile: file.Name(),
})
if err != nil {
t.Fatal(err)
}
defer s.Stopper().Stop(context.TODO())
defer func() {
if err := os.Remove(file.Name()); err != nil {
t.Error(err)
}
}()

data, err := ioutil.ReadFile(file.Name())
if err != nil {
t.Fatal(err)
}

u, err := url.Parse(string(data))
if err != nil {
t.Fatal(err)
}

if s.ServingAddr() != u.Host {
t.Fatalf("expected URL %s to match host %s", u, s.ServingAddr())
}
}
4 changes: 4 additions & 0 deletions pkg/server/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ func makeTestConfigFromParams(params base.TestServerArgs) Config {
cfg.HTTPAddr = params.HTTPAddr
}

if params.ListeningURLFile != "" {
cfg.ListeningURLFile = params.ListeningURLFile
}

// Ensure we have the correct number of engines. Add in-memory ones where
// needed. There must be at least one store/engine.
if len(params.StoreSpecs) == 0 {
Expand Down

0 comments on commit 1d7e0ac

Please sign in to comment.