Skip to content

Commit

Permalink
cli: Add --listening-url-file flag
Browse files Browse the repository at this point in the history
  • Loading branch information
pydima committed Apr 28, 2017
1 parent 1ae28f6 commit e1723c9
Show file tree
Hide file tree
Showing 7 changed files with 71 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
39 changes: 39 additions & 0 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -478,3 +480,40 @@ func TestClusterStores(t *testing.T) {
return nil
})
}

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

cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
filePath := path.Join(cwd, "listening_url_file")

s, _, _ := serverutils.StartServer(t, base.TestServerArgs{
ListeningURLFile: filePath,
})
defer s.Stopper().Stop(context.TODO())
defer func() {
os.Remove(filePath)
}()

testutils.SucceedsSoon(t, func() error {
data, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}

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

if s.ServingAddr() != u.Host {
return errors.Errorf(
"expected address in file %s, got %s, full URL in file %s",
s.ServingAddr(), u.Host, string(data))
}
return nil
})
}
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 e1723c9

Please sign in to comment.