Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: new command 'start-single-node' #28495

Merged
merged 1 commit into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/verify-archive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ workdir=$(mktemp -d)
tar xzf cockroach.src.tgz -C "$workdir"
(cd "$workdir"/cockroach-* && make clean && make install)

cockroach start --insecure --store type=mem,size=1GiB --background
cockroach start-single-node --insecure --store type=mem,size=1GiB --background
cockroach sql --insecure <<EOF
CREATE DATABASE bank;
CREATE TABLE bank.accounts (id INT PRIMARY KEY, balance DECIMAL);
Expand Down
12 changes: 7 additions & 5 deletions pkg/ccl/cliccl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import (
var storeEncryptionSpecs baseccl.StoreEncryptionSpecList

func init() {
cli.VarFlag(cli.StartCmd.Flags(), &storeEncryptionSpecs, cliflagsccl.EnterpriseEncryption)
for _, cmd := range cli.StartCmds {
cli.VarFlag(cmd.Flags(), &storeEncryptionSpecs, cliflagsccl.EnterpriseEncryption)

// Add a new pre-run command to match encryption specs to store specs.
cli.AddPersistentPreRunE(cli.StartCmd, func(cmd *cobra.Command, _ []string) error {
return populateStoreSpecsEncryption()
})
// Add a new pre-run command to match encryption specs to store specs.
cli.AddPersistentPreRunE(cmd, func(cmd *cobra.Command, _ []string) error {
return populateStoreSpecsEncryption()
})
}
}

// populateStoreSpecsEncryption is a PreRun hook that matches store encryption specs with the
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ func init() {
})

cockroachCmd.AddCommand(
StartCmd,
startCmd,
startSingleNodeCmd,
initCmd,
certCmd,
quitCmd,
Expand Down
37 changes: 19 additions & 18 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1518,24 +1518,25 @@ func TestFlagUsage(t *testing.T) {
cockroach [command]

Available Commands:
start start a node
init initialize a cluster
cert create ca, node, and client certs
quit drain and shutdown node

sql open a sql shell
user get, set, list and remove users
node list, inspect or remove nodes
dump dump sql tables

demo open a demo sql shell
gen generate auxiliary files
version output version information
debug debugging commands
sqlfmt format SQL statements
workload [experimental] generators for data and query loads
systembench Run systembench
help Help about any command
start start a node in a multi-node cluster
start-single-node start a single-node cluster
init initialize a cluster
cert create ca, node, and client certs
quit drain and shutdown node

sql open a sql shell
user get, set, list and remove users
node list, inspect or remove nodes
dump dump sql tables

demo open a demo sql shell
gen generate auxiliary files
version output version information
debug debugging commands
sqlfmt format SQL statements
workload [experimental] generators for data and query loads
systembench Run systembench
help Help about any command

Flags:
-h, --help help for cockroach
Expand Down
6 changes: 4 additions & 2 deletions pkg/cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
Expand Down Expand Up @@ -107,6 +108,8 @@ func initCLIDefaults() {
serverCfg.DelayedBootstrapFn = nil
serverCfg.SocketFile = ""
serverCfg.JoinList = nil
serverCfg.DefaultZoneConfig = config.DefaultZoneConfig()
serverCfg.DefaultSystemZoneConfig = config.DefaultSystemZoneConfig()

startCtx.serverInsecure = baseCfg.Insecure
startCtx.serverSSLCertsDir = base.DefaultCertsDirectory
Expand Down Expand Up @@ -286,8 +289,7 @@ var startCtx struct {
pidFile string

// logging settings specific to file logging.
logDir log.DirName
logDirFlag *pflag.Flag
logDir log.DirName
}

// quitCtx captures the command-line parameters of the `quit` command.
Expand Down
5 changes: 3 additions & 2 deletions pkg/cli/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"net/url"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/cli/cliflags"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/server"
Expand Down Expand Up @@ -75,7 +76,7 @@ func setupTransientServers(

// Set up logging. For demo/transient server we use non-standard
// behavior where we avoid file creation if possible.
df := startCtx.logDirFlag
df := cmd.Flags().Lookup(cliflags.LogDir.Name)
sf := cmd.Flags().Lookup(logflags.LogToStderrName)
if !df.Changed && !sf.Changed {
// User did not request logging flags; shut down logging under
Expand All @@ -87,7 +88,7 @@ func setupTransientServers(
_ = sf.Value.Set(log.Severity_ERROR.String())
sf.Changed = true
}
stopper, err := setupAndInitializeLoggingAndProfiling(ctx)
stopper, err := setupAndInitializeLoggingAndProfiling(ctx, cmd)
if err != nil {
return connURL, adminURL, cleanup, err
}
Expand Down
45 changes: 33 additions & 12 deletions pkg/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/log/logflags"
"github.com/cockroachdb/cockroach/pkg/util/netutil"
"github.com/gogo/protobuf/proto"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -187,15 +188,25 @@ func init() {
initCLIDefaults()

// Every command but start will inherit the following setting.
cockroachCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
AddPersistentPreRunE(cockroachCmd, func(cmd *cobra.Command, _ []string) error {
extraClientFlagInit()
return setDefaultStderrVerbosity(cmd, log.Severity_WARNING)
})

// Add a pre-run command for `start` and `start-single-node`.
for _, cmd := range StartCmds {
AddPersistentPreRunE(cmd, func(cmd *cobra.Command, _ []string) error {
// Finalize the configuration of network and logging settings.
extraServerFlagInit()
return setDefaultStderrVerbosity(cmd, log.Severity_INFO)
})
}

// Add a pre-run command for `start`.
AddPersistentPreRunE(StartCmd, func(cmd *cobra.Command, _ []string) error {
extraServerFlagInit()
return setDefaultStderrVerbosity(cmd, log.Severity_INFO)
// start-single-node starts with default replication of 1.
AddPersistentPreRunE(startSingleNodeCmd, func(cmd *cobra.Command, _ []string) error {
serverCfg.DefaultSystemZoneConfig.NumReplicas = proto.Int32(1)
serverCfg.DefaultZoneConfig.NumReplicas = proto.Int32(1)
return nil
})

// Map any flags registered in the standard "flag" package into the
Expand Down Expand Up @@ -253,8 +264,8 @@ func init() {
// avoid printing some messages to standard output in that case.
_, startCtx.inBackground = envutil.EnvString(backgroundEnvVar, 1)

{
f := StartCmd.Flags()
for _, cmd := range StartCmds {
f := cmd.Flags()

// Server flags.
VarFlag(f, addrSetter{&startCtx.serverListenAddr, &serverListenPort}, cliflags.ListenAddr)
Expand Down Expand Up @@ -312,8 +323,15 @@ func init() {
// variables, but share the same default.
StringFlag(f, &startCtx.serverSSLCertsDir, cliflags.ServerCertsDir, startCtx.serverSSLCertsDir)

// Cluster joining flags.
// Cluster joining flags. We need to enable this both for 'start'
// and 'start-single-node' although the latter does not support
// --join, because it delegates its logic to that of 'start', and
// 'start' will check that the flag is properly defined.
VarFlag(f, &serverCfg.JoinList, cliflags.Join)
// We also hide it from help for 'start-single-node'.
if cmd == startSingleNodeCmd {
_ = f.MarkHidden(cliflags.Join.Name)
}

// Engine flags.
VarFlag(f, cacheSizeValue, cliflags.Cache)
Expand All @@ -329,10 +347,11 @@ func init() {
}

// Log flags.
for _, cmd := range []*cobra.Command{demoCmd, StartCmd} {
logCmds := append(StartCmds, demoCmd)
logCmds = append(logCmds, demoCmd.Commands()...)
for _, cmd := range logCmds {
f := cmd.Flags()
VarFlag(f, &startCtx.logDir, cliflags.LogDir)
startCtx.logDirFlag = f.Lookup(cliflags.LogDir.Name)
VarFlag(f,
pflag.PFlagFromGoFlag(flag.Lookup(logflags.LogFilesCombinedMaxSizeName)).Value,
cliflags.LogDirMaxSize)
Expand Down Expand Up @@ -383,7 +402,7 @@ func init() {
genHAProxyCmd,
quitCmd,
sqlShellCmd,
/* StartCmd is covered above */
/* StartCmds are covered above */
}
clientCmds = append(clientCmds, userCmds...)
clientCmds = append(clientCmds, zoneCmds...)
Expand Down Expand Up @@ -475,6 +494,7 @@ func init() {
sqlCmds := []*cobra.Command{sqlShellCmd, dumpCmd, demoCmd}
sqlCmds = append(sqlCmds, zoneCmds...)
sqlCmds = append(sqlCmds, userCmds...)
sqlCmds = append(sqlCmds, demoCmd.Commands()...)
for _, cmd := range sqlCmds {
f := cmd.Flags()
BoolFlag(f, &sqlCtx.echo, cliflags.EchoSQL, sqlCtx.echo)
Expand All @@ -500,7 +520,8 @@ func init() {
}

// Commands that print tables.
tableOutputCommands := append([]*cobra.Command{sqlShellCmd, genSettingsListCmd, demoCmd},
tableOutputCommands := append(
[]*cobra.Command{sqlShellCmd, genSettingsListCmd, demoCmd},
demoCmd.Commands()...)
tableOutputCommands = append(tableOutputCommands, userCmds...)
tableOutputCommands = append(tableOutputCommands, nodeCmds...)
Expand Down
12 changes: 6 additions & 6 deletions pkg/cli/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestCacheFlagValue(t *testing.T) {
// Avoid leaking configuration changes after the test ends.
defer initCLIDefaults()

f := StartCmd.Flags()
f := startCmd.Flags()
args := []string{"--cache", "100MB"}
if err := f.Parse(args); err != nil {
t.Fatal(err)
Expand All @@ -98,7 +98,7 @@ func TestSQLMemoryPoolFlagValue(t *testing.T) {
// Avoid leaking configuration changes after the test ends.
defer initCLIDefaults()

f := StartCmd.Flags()
f := startCmd.Flags()

// Check absolute values.
testCases := []struct {
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestClockOffsetFlagValue(t *testing.T) {
// Avoid leaking configuration changes after the tests end.
defer initCLIDefaults()

f := StartCmd.Flags()
f := startCmd.Flags()
testData := []struct {
args []string
expected time.Duration
Expand Down Expand Up @@ -366,7 +366,7 @@ func TestServerConnSettings(t *testing.T) {
// Avoid leaking configuration changes after the tests end.
defer initCLIDefaults()

f := StartCmd.Flags()
f := startCmd.Flags()
testData := []struct {
args []string
expectedAddr string
Expand Down Expand Up @@ -458,7 +458,7 @@ func TestServerJoinSettings(t *testing.T) {
// Avoid leaking configuration changes after the tests end.
defer initCLIDefaults()

f := StartCmd.Flags()
f := startCmd.Flags()
testData := []struct {
args []string
expectedJoin []string
Expand Down Expand Up @@ -559,7 +559,7 @@ func TestHttpHostFlagValue(t *testing.T) {
// Avoid leaking configuration changes after the tests end.
defer initCLIDefaults()

f := StartCmd.Flags()
f := startCmd.Flags()
testData := []struct {
args []string
expected string
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/interactive_tests/common.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ proc send_eof {} {
proc start_server {argv} {
report "BEGIN START SERVER"
system "mkfifo url_fifo || true;
$argv start --insecure --pid-file=server_pid --listening-url-file=url_fifo --background -s=path=logs/db >>logs/expect-cmd.log 2>&1 &
$argv start-single-node --insecure --pid-file=server_pid --listening-url-file=url_fifo --background -s=path=logs/db >>logs/expect-cmd.log 2>&1 &
cat url_fifo > server_url"
report "START SERVER DONE"
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/interactive_tests/test_audit_log.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ stop_server $argv

start_test "Check that audit logging works even with a custom directory"
# Start a server with a custom log
system "$argv start --insecure --pid-file=server_pid --listening-url-file=url_fifo --background -s=path=logs/db --sql-audit-dir=logs/db/audit-new >>logs/expect-cmd.log 2>&1 & cat url_fifo > server_url"
system "$argv start-single-node --insecure --pid-file=server_pid --listening-url-file=url_fifo --background -s=path=logs/db --sql-audit-dir=logs/db/audit-new >>logs/expect-cmd.log 2>&1 & cat url_fifo > server_url"

set logfile logs/db/audit-new/cockroach-sql-audit.log

Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/interactive_tests/test_cert_advisory_validation.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ send "$argv cert create-node localhost --certs-dir=$certs_dir --ca-key=$certs_di
eexpect $prompt

start_test "Check that the server reports a warning if attempting to advertise an IP address not in cert."
send "$argv start --certs-dir=$certs_dir --advertise-addr=127.0.0.1\r"
send "$argv start-single-node --certs-dir=$certs_dir --advertise-addr=127.0.0.1\r"
eexpect "advertise address"
eexpect "127.0.0.1"
eexpect "not in node certificate"
Expand All @@ -32,7 +32,7 @@ eexpect $prompt
end_test

start_test "Check that the server reports no warning if the avertise addr is in th cert."
send "$argv start --certs-dir=$certs_dir --advertise-addr=localhost\r"
send "$argv start-single-node --certs-dir=$certs_dir --advertise-addr=localhost\r"
expect {
"not in node certificate" {
report "unexpected warning"
Expand Down
20 changes: 10 additions & 10 deletions pkg/cli/interactive_tests/test_encryption.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ file_has_size "$keydir/aes-256.key" "64"
end_test

start_test "Start normal node."
send "$argv start --insecure --store=$storedir\r"
send "$argv start-single-node --insecure --store=$storedir\r"
eexpect "node starting"
interrupt
eexpect "shutdown completed"
Expand All @@ -44,47 +44,47 @@ eexpect ""
end_test

start_test "Restart with plaintext."
send "$argv start --insecure --store=$storedir --enterprise-encryption=path=$storedir,key=plain,old-key=plain\r"
send "$argv start-single-node --insecure --store=$storedir --enterprise-encryption=path=$storedir,key=plain,old-key=plain\r"
eexpect "node starting"
interrupt
eexpect "shutdown completed"
send "$argv debug encryption-status $storedir --enterprise-encryption=path=$storedir,key=plain,old-key=plain\r"
eexpect " \"Active\": true,\r\n \"Type\": \"Plaintext\","
# Try starting without the encryption flag.
send "$argv start --insecure --store=$storedir\r"
send "$argv start-single-node --insecure --store=$storedir\r"
eexpect "encryption was used on this store before, but no encryption flags specified."
end_test

start_test "Restart with AES-128."
send "$argv start --insecure --store=$storedir --enterprise-encryption=path=$storedir,key=$keydir/aes-128.key,old-key=plain\r"
send "$argv start-single-node --insecure --store=$storedir --enterprise-encryption=path=$storedir,key=$keydir/aes-128.key,old-key=plain\r"
eexpect "node starting"
interrupt
eexpect "shutdown completed"
send "$argv debug encryption-status $storedir --enterprise-encryption=path=$storedir,key=$keydir/aes-128.key,old-key=plain\r"
eexpect " \"Active\": true,\r\n \"Type\": \"AES128_CTR\","
# Try starting without the encryption flag.
send "$argv start --insecure --store=$storedir\r"
send "$argv start-single-node --insecure --store=$storedir\r"
eexpect "encryption was used on this store before, but no encryption flags specified."
# Try with the wrong key.
send "$argv start --insecure --store=$storedir --enterprise-encryption=path=$storedir,key=$keydir/aes-192.key,old-key=plain\r"
send "$argv start-single-node --insecure --store=$storedir --enterprise-encryption=path=$storedir,key=$keydir/aes-192.key,old-key=plain\r"
eexpect "key_manager does not have a key with ID"
end_test

start_test "Restart with AES-256."
send "$argv start --insecure --store=$storedir --enterprise-encryption=path=$storedir,key=$keydir/aes-256.key,old-key=$keydir/aes-128.key\r"
send "$argv start-single-node --insecure --store=$storedir --enterprise-encryption=path=$storedir,key=$keydir/aes-256.key,old-key=$keydir/aes-128.key\r"
eexpect "node starting"
interrupt
eexpect "shutdown completed"
send "$argv debug encryption-status $storedir --enterprise-encryption=path=$storedir,key=$keydir/aes-256.key,old-key=plain\r"
eexpect " \"Active\": true,\r\n \"Type\": \"AES256_CTR\","
# Startup again, but don't specify the old key, it's no longer in use.
send "$argv start --insecure --store=$storedir --enterprise-encryption=path=$storedir,key=$keydir/aes-256.key,old-key=plain\r"
send "$argv start-single-node --insecure --store=$storedir --enterprise-encryption=path=$storedir,key=$keydir/aes-256.key,old-key=plain\r"
eexpect "node starting"
interrupt
# Try starting without the encryption flag.
send "$argv start --insecure --store=$storedir\r"
send "$argv start-single-node --insecure --store=$storedir\r"
eexpect "encryption was used on this store before, but no encryption flags specified."
# Try with the wrong key.
send "$argv start --insecure --store=$storedir --enterprise-encryption=path=$storedir,key=$keydir/aes-192.key,old-key=plain\r"
send "$argv start-single-node --insecure --store=$storedir --enterprise-encryption=path=$storedir,key=$keydir/aes-192.key,old-key=plain\r"
eexpect "key_manager does not have a key with ID"
end_test
Loading