diff --git a/cmd/eris-db.go b/cmd/eris-db.go index f67501ecc8acb7b116bcdb60e24e0190b0f3df84..369623252fae2011232472db1add45e6dde5ae6a 100644 --- a/cmd/eris-db.go +++ b/cmd/eris-db.go @@ -92,7 +92,7 @@ func defaultDebug() bool { return setDefaultBool("ERIS_DB_DEBUG", false) } -// setDefaultBool returns the provided default value if the environment variab;e +// setDefaultBool returns the provided default value if the environment variable // is not set or not parsable as a bool. func setDefaultBool(environmentVariable string, defaultValue bool) bool { value := os.Getenv(environmentVariable) diff --git a/cmd/serve.go b/cmd/serve.go index d862072641bb577dc1b6b0dd373ced7640cd459b..567cc4078bfb8ae86d6519b88be33ab7633e5ab4 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -18,7 +18,9 @@ package commands import ( "os" + "os/signal" "path" + "syscall" cobra "github.com/spf13/cobra" @@ -36,7 +38,7 @@ The Eris-DB node is modularly configured for the consensus engine and applicatio manager. The client API can be disabled.`, Example: `$ eris-db serve -- will start the Eris-DB node based on the configuration file "server_config.toml" in the current working directory $ eris-db serve --work-dir <path-to-working-directory> -- will start the Eris-DB node based on the configuration file "server_config.toml" in the provided working directory -$ eris-db serve --chainid <CHAIN_ID> -- will overrule the configuration entry assert_chain_id`, +$ eris-db serve --chain-id <CHAIN_ID> -- will overrule the configuration entry assert_chain_id`, PreRun: func(cmd *cobra.Command, args []string) { // if WorkDir was not set by a flag or by $ERIS_DB_WORKDIR // NOTE [ben]: we can consider an `Explicit` flag that eliminates @@ -62,12 +64,14 @@ func buildServeCommand() { } func addServeFlags() { - ServeCmd.PersistentFlags().StringVarP(&do.ChainId, "chainid", "c", - defaultChainId(), "specify the chain id to use for assertion against genesis files or existing state. If omitted, and no id is set in $CHAIN_ID, then assert_chain_id is used from the configuration file.") + ServeCmd.PersistentFlags().StringVarP(&do.ChainId, "chain-id", "c", + defaultChainId(), "specify the chain id to use for assertion against the genesis file or the existing state. If omitted, and no id is set in $CHAIN_ID, then assert_chain_id is used from the configuration file.") ServeCmd.PersistentFlags().StringVarP(&do.WorkDir, "work-dir", "w", defaultWorkDir(), "specify the working directory for the chain to run. If omitted, and no path set in $ERIS_DB_WORKDIR, the current working directory is taken.") - ServeCmd.PersistentFlags().StringVarP(&do.DataDir, "data-dir", "a", + ServeCmd.PersistentFlags().StringVarP(&do.DataDir, "data-dir", "", defaultDataDir(), "specify the data directory. If omitted and not set in $ERIS_DB_DATADIR, <working_directory>/data is taken.") + ServeCmd.PersistentFlags().BoolVarP(&do.DisableRpc, "disable-rpc", "", + defaultDisableRpc(), "indicate for the RPC to be disabled. If omitted the RPC is enabled by default, unless (deprecated) $ERISDB_API is set to false.") } //------------------------------------------------------------------------------ @@ -133,26 +137,39 @@ func Serve(cmd *cobra.Command, args []string) { log.Fatalf("Failed to load core: %s", err) } - serverConfig, err := core.LoadServerConfig(do) - if err != nil { - log.Fatalf("Failed to load server configuration: %s.", err) - os.Exit(1) - } - serverProcess, err := newCore.NewGatewayV0(serverConfig) - if err != nil { - log.Fatalf("Failed to load servers: %s.", err) - os.Exit(1) - } - err = serverProcess.Start() - if err != nil { - log.Fatalf("Failed to start servers: %s.", err) - os.Exit(1) - } - _, err = newCore.NewGatewayTendermint(serverConfig) - if err != nil { - log.Fatalf("Failed to start Tendermint gateway") + if !do.DisableRpc { + serverConfig, err := core.LoadServerConfig(do) + if err != nil { + log.Fatalf("Failed to load server configuration: %s.", err) + os.Exit(1) + } + serverProcess, err := newCore.NewGatewayV0(serverConfig) + if err != nil { + log.Fatalf("Failed to load servers: %s.", err) + os.Exit(1) + } + err = serverProcess.Start() + if err != nil { + log.Fatalf("Failed to start servers: %s.", err) + os.Exit(1) + } + _, err = newCore.NewGatewayTendermint(serverConfig) + if err != nil { + log.Fatalf("Failed to start Tendermint gateway") + } + <-serverProcess.StopEventChannel() + } else { + signals := make(chan os.Signal, 1) + done := make(chan bool, 1) + signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) + go func() { + signal := <-signals + // TODO: [ben] clean up core; in a manner consistent with enabled rpc + log.Fatalf("Received %s signal. Marmots out.", signal) + done <- true + }() + <-done } - <-serverProcess.StopEventChannel() } //------------------------------------------------------------------------------ @@ -175,3 +192,11 @@ func defaultDataDir() string { // wait setting a default value, and initialise the data directory from serve() return setDefaultString("ERIS_DB_DATADIR", "") } + +func defaultDisableRpc() bool { + // we currently observe environment variable ERISDB_API (true = enable) + // and default to enabling the RPC if it is not set. + // TODO: [ben] deprecate ERISDB_API across the stack for 0.12.1, and only disable + // the rpc through a command line flag --disable-rpc + return !setDefaultBool("ERISDB_API", true) +} diff --git a/definitions/do.go b/definitions/do.go index ed5c5a96b7b8c34134fc953cb458f52fb1f03342..0ad4e10880c0f21c72338f1a5ca466a966231e30 100644 --- a/definitions/do.go +++ b/definitions/do.go @@ -47,6 +47,7 @@ type Do struct { // AccountTypes []string // Zip bool // Tarball bool + DisableRpc bool Config *viper.Viper // Accounts []*Account // Result string @@ -60,6 +61,7 @@ func NowDo() *Do { do.DataDir = "" do.ChainId = "" do.GenesisFile = "" + do.DisableRpc = false do.Config = viper.New() return do }