diff options
author | Valery Piashchynski <[email protected]> | 2022-03-06 12:13:02 +0100 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2022-03-06 12:13:02 +0100 |
commit | 70e4f020afd0352dc52114651f0f65c1965ed399 (patch) | |
tree | cc6595214e4ff39600099fb8c6f39e7a3dd560e9 | |
parent | 587702be62b65c151d27dc79e62fcbbd11290e6f (diff) |
remove config plugin usage from the root
Signed-off-by: Valery Piashchynski <[email protected]>
23 files changed, 170 insertions, 97 deletions
diff --git a/internal/cli/reset/command.go b/internal/cli/reset/command.go index 1d7dd78c..a0634c31 100644 --- a/internal/cli/reset/command.go +++ b/internal/cli/reset/command.go @@ -8,7 +8,6 @@ import ( "github.com/fatih/color" "github.com/mattn/go-runewidth" - "github.com/roadrunner-server/config/v2" "github.com/roadrunner-server/errors" "github.com/spf13/cobra" "github.com/vbauerster/mpb/v5" @@ -18,7 +17,7 @@ import ( var spinnerStyle = []string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"} //nolint:gochecknoglobals // NewCommand creates `reset` command. -func NewCommand(cfgPlugin *config.Plugin) *cobra.Command { //nolint:funlen +func NewCommand(cfgFile *string) *cobra.Command { //nolint:funlen return &cobra.Command{ Use: "reset", Short: "Reset workers of all or specific RoadRunner service", @@ -29,7 +28,11 @@ func NewCommand(cfgPlugin *config.Plugin) *cobra.Command { //nolint:funlen resetterReset = "resetter.Reset" ) - client, err := internalRpc.NewClient(cfgPlugin) + if cfgFile == nil { + return errors.E(op, errors.Str("no configuration file provided")) + } + + client, err := internalRpc.NewClient(*cfgFile) if err != nil { return err } diff --git a/internal/cli/reset/command_test.go b/internal/cli/reset/command_test.go index 1983cc5c..ca68cb54 100644 --- a/internal/cli/reset/command_test.go +++ b/internal/cli/reset/command_test.go @@ -5,12 +5,12 @@ import ( "github.com/roadrunner-server/roadrunner/v2/internal/cli/reset" - "github.com/roadrunner-server/config/v2" "github.com/stretchr/testify/assert" ) func TestCommandProperties(t *testing.T) { - cmd := reset.NewCommand(&config.Plugin{}) + path := "" + cmd := reset.NewCommand(&path) assert.Equal(t, "reset", cmd.Use) assert.NotNil(t, cmd.RunE) diff --git a/internal/cli/root.go b/internal/cli/root.go index 723e8a01..0f35d416 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -6,6 +6,7 @@ import ( "path/filepath" "runtime" + "github.com/roadrunner-server/errors" "github.com/roadrunner-server/roadrunner/v2/internal/cli/reset" "github.com/roadrunner-server/roadrunner/v2/internal/cli/serve" "github.com/roadrunner-server/roadrunner/v2/internal/cli/workers" @@ -13,24 +14,25 @@ import ( "github.com/roadrunner-server/roadrunner/v2/internal/meta" "github.com/joho/godotenv" - "github.com/roadrunner-server/config/v2" "github.com/spf13/cobra" ) // NewCommand creates root command. func NewCommand(cmdName string) *cobra.Command { //nolint:funlen - const envDotenv = "DOTENV_PATH" // env var name: path to the .env file + const ( + envDotenv string = "DOTENV_PATH" // env var name: path to the .env file + ) var ( // flag values - cfgFile string // path to the .rr.yaml - workDir string // working directory - dotenv string // path to the .env file - debug bool // debug mode - override []string // override config values + cfgFile = strPtr("") // path to the .rr.yaml + workDir string // working directory + dotenv string // path to the .env file + debug bool // debug mode + override = &[]string{} // override config values + // do not print startup message + silent = boolPtr(false) ) - var configPlugin = &config.Plugin{} // will be overwritten on pre-run action - cmd := &cobra.Command{ Use: cmdName, Short: "High-performance PHP application server, load-balancer and process manager", @@ -38,14 +40,18 @@ func NewCommand(cmdName string) *cobra.Command { //nolint:funlen SilenceUsage: true, Version: fmt.Sprintf("%s (build time: %s, %s)", meta.Version(), meta.BuildTime(), runtime.Version()), PersistentPreRunE: func(*cobra.Command, []string) error { - if cfgFile != "" { - if absPath, err := filepath.Abs(cfgFile); err == nil { - cfgFile = absPath // switch config path to the absolute - - // force working absPath related to config file - if err = os.Chdir(filepath.Dir(absPath)); err != nil { - return err - } + // cfgFile could be defined by user or default `.rr.yaml` + // this check added just to be safe + if cfgFile == nil || *cfgFile == "" { + return errors.Str("no configuration file provided") + } + + if absPath, err := filepath.Abs(*cfgFile); err == nil { + cfgFile = &absPath // switch config path to the absolute + + // force working absPath related to config file + if err = os.Chdir(filepath.Dir(absPath)); err != nil { + return err } } @@ -66,36 +72,37 @@ func NewCommand(cmdName string) *cobra.Command { //nolint:funlen } } - cfg := &config.Plugin{Path: cfgFile, Prefix: "rr", Flags: override} - if err := cfg.Init(); err != nil { - return err - } - if debug { srv := dbg.NewServer() go func() { _ = srv.Start(":6061") }() // TODO implement graceful server stopping } - // overwrite - *configPlugin = *cfg - return nil }, } f := cmd.PersistentFlags() - f.StringVarP(&cfgFile, "config", "c", ".rr.yaml", "config file") - f.StringVarP(&workDir, "WorkDir", "w", "", "working directory") // TODO change to `workDir`? + f.StringVarP(cfgFile, "config", "c", ".rr.yaml", "config file") + f.StringVarP(&workDir, "WorkDir", "w", "", "working directory") f.StringVarP(&dotenv, "dotenv", "", "", fmt.Sprintf("dotenv file [$%s]", envDotenv)) f.BoolVarP(&debug, "debug", "d", false, "debug mode") - f.StringArrayVarP(&override, "override", "o", nil, "override config value (dot.notation=value)") + f.BoolVarP(silent, "silent", "s", false, "print startup message") + f.StringArrayVarP(override, "override", "o", nil, "override config value (dot.notation=value)") cmd.AddCommand( - workers.NewCommand(configPlugin), - reset.NewCommand(configPlugin), - serve.NewCommand(configPlugin), + workers.NewCommand(cfgFile), + reset.NewCommand(cfgFile), + serve.NewCommand(override, cfgFile, silent), ) return cmd } + +func strPtr(s string) *string { + return &s +} + +func boolPtr(b bool) *bool { + return &b +} diff --git a/internal/cli/serve/command.go b/internal/cli/serve/command.go index a0ce36db..559868d5 100644 --- a/internal/cli/serve/command.go +++ b/internal/cli/serve/command.go @@ -14,23 +14,35 @@ import ( "github.com/spf13/cobra" ) +const ( + rrPrefix string = "rr" +) + // NewCommand creates `serve` command. -func NewCommand(cfgPlugin *configImpl.Plugin) *cobra.Command { //nolint:funlen +func NewCommand(override *[]string, cfgFile *string, silent *bool) *cobra.Command { //nolint:funlen,gocognit return &cobra.Command{ Use: "serve", Short: "Start RoadRunner server", RunE: func(*cobra.Command, []string) error { const op = errors.Op("handle_serve_command") + // just to be safe + if cfgFile == nil { + return errors.E(op, errors.Str("no configuration file provided")) + } // create endure container config - containerCfg, err := container.NewConfig(cfgPlugin) + containerCfg, err := container.NewConfig(*cfgFile) if err != nil { return errors.E(op, err) } - // set the grace period which would be same for all the plugins - cfgPlugin.Timeout = containerCfg.GracePeriod - cfgPlugin.Version = meta.Version() + cfg := &configImpl.Plugin{ + Path: *cfgFile, + Prefix: rrPrefix, + Timeout: containerCfg.GracePeriod, + Flags: *override, + Version: meta.Version(), + } // create endure container endureContainer, err := container.NewContainer(*containerCfg) @@ -39,7 +51,7 @@ func NewCommand(cfgPlugin *configImpl.Plugin) *cobra.Command { //nolint:funlen } // register config plugin - if err = endureContainer.Register(cfgPlugin); err != nil { + if err = endureContainer.Register(cfg); err != nil { return errors.E(op, err) } @@ -77,7 +89,9 @@ func NewCommand(cfgPlugin *configImpl.Plugin) *cobra.Command { //nolint:funlen os.Exit(1) }() - fmt.Printf("[INFO] RoadRunner server started; version: %s, buildtime: %s\n", meta.Version(), meta.BuildTime()) + if !*silent { + fmt.Printf("[INFO] RoadRunner server started; version: %s, buildtime: %s\n", meta.Version(), meta.BuildTime()) + } for { select { diff --git a/internal/cli/serve/command_test.go b/internal/cli/serve/command_test.go index 8aef0ff7..4c8c6128 100644 --- a/internal/cli/serve/command_test.go +++ b/internal/cli/serve/command_test.go @@ -5,12 +5,19 @@ import ( "github.com/roadrunner-server/roadrunner/v2/internal/cli/serve" - "github.com/roadrunner-server/config/v2" "github.com/stretchr/testify/assert" ) func TestCommandProperties(t *testing.T) { - cmd := serve.NewCommand(&config.Plugin{}) + path := "" + cmd := serve.NewCommand(nil, &path, nil) + + assert.Equal(t, "serve", cmd.Use) + assert.NotNil(t, cmd.RunE) +} + +func TestCommandNil(t *testing.T) { + cmd := serve.NewCommand(nil, nil, nil) assert.Equal(t, "serve", cmd.Use) assert.NotNil(t, cmd.RunE) diff --git a/internal/cli/workers/command.go b/internal/cli/workers/command.go index e3f8f7e3..f498b7af 100644 --- a/internal/cli/workers/command.go +++ b/internal/cli/workers/command.go @@ -13,14 +13,13 @@ import ( tm "github.com/buger/goterm" "github.com/fatih/color" - "github.com/roadrunner-server/config/v2" "github.com/roadrunner-server/errors" "github.com/roadrunner-server/informer/v2" "github.com/spf13/cobra" ) // NewCommand creates `workers` command. -func NewCommand(cfgPlugin *config.Plugin) *cobra.Command { //nolint:funlen +func NewCommand(cfgFile *string) *cobra.Command { //nolint:funlen var ( // interactive workers updates interactive bool @@ -35,7 +34,11 @@ func NewCommand(cfgPlugin *config.Plugin) *cobra.Command { //nolint:funlen informerList = "informer.List" ) - client, err := internalRpc.NewClient(cfgPlugin) + if cfgFile == nil { + return errors.E(op, errors.Str("no configuration file provided")) + } + + client, err := internalRpc.NewClient(*cfgFile) if err != nil { return err } diff --git a/internal/cli/workers/command_test.go b/internal/cli/workers/command_test.go index d0c92e63..a3d144df 100644 --- a/internal/cli/workers/command_test.go +++ b/internal/cli/workers/command_test.go @@ -5,19 +5,18 @@ import ( "github.com/roadrunner-server/roadrunner/v2/internal/cli/workers" - "github.com/roadrunner-server/config/v2" "github.com/stretchr/testify/assert" ) func TestCommandProperties(t *testing.T) { - cmd := workers.NewCommand(&config.Plugin{}) + cmd := workers.NewCommand(nil) assert.Equal(t, "workers", cmd.Use) assert.NotNil(t, cmd.RunE) } func TestCommandFlags(t *testing.T) { - cmd := workers.NewCommand(&config.Plugin{}) + cmd := workers.NewCommand(nil) cases := []struct { giveName string diff --git a/internal/container/config.go b/internal/container/config.go index 149d6b9b..71deafc2 100644 --- a/internal/container/config.go +++ b/internal/container/config.go @@ -4,8 +4,8 @@ import ( "fmt" "time" - "github.com/roadrunner-server/config/v2" endure "github.com/roadrunner-server/endure/pkg/container" + "github.com/spf13/viper" ) type Config struct { @@ -21,8 +21,16 @@ const ( ) // NewConfig creates endure container configuration. -func NewConfig(cfgPlugin *config.Plugin) (*Config, error) { - if !cfgPlugin.Has(endureKey) { +func NewConfig(cfgFile string) (*Config, error) { + v := viper.New() + v.SetConfigFile(cfgFile) + + err := v.ReadInConfig() + if err != nil { + return nil, err + } + + if !v.IsSet(endureKey) { return &Config{ // return config with defaults GracePeriod: defaultGracePeriod, PrintGraph: false, @@ -38,7 +46,8 @@ func NewConfig(cfgPlugin *config.Plugin) (*Config, error) { LogLevel string `mapstructure:"log_level"` }{} - if err := cfgPlugin.UnmarshalKey(endureKey, &rrCfgEndure); err != nil { + err = v.UnmarshalKey(endureKey, &rrCfgEndure) + if err != nil { return nil, err } diff --git a/internal/container/config_test.go b/internal/container/config_test.go index e20b2d9e..413d1eac 100644 --- a/internal/container/config_test.go +++ b/internal/container/config_test.go @@ -4,24 +4,14 @@ import ( "testing" "time" - "github.com/roadrunner-server/roadrunner/v2/internal/container" - "github.com/roadrunner-server/config/v2" endure "github.com/roadrunner-server/endure/pkg/container" + "github.com/roadrunner-server/roadrunner/v2/internal/container" "github.com/stretchr/testify/assert" ) func TestNewConfig_SuccessfulReading(t *testing.T) { - cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte(` -endure: - grace_period: 10s - print_graph: true - retry_on_fail: true - log_level: warn -`)} - assert.NoError(t, cfgPlugin.Init()) - - c, err := container.NewConfig(cfgPlugin) + c, err := container.NewConfig("test/endure_ok.yaml") assert.NoError(t, err) assert.NotNil(t, c) @@ -35,7 +25,7 @@ func TestNewConfig_WithoutEndureKey(t *testing.T) { cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte{}} assert.NoError(t, cfgPlugin.Init()) - c, err := container.NewConfig(cfgPlugin) + c, err := container.NewConfig("test/without_endure_ok.yaml") assert.NoError(t, err) assert.NotNil(t, c) @@ -47,26 +37,25 @@ func TestNewConfig_WithoutEndureKey(t *testing.T) { func TestNewConfig_LoggingLevels(t *testing.T) { for _, tt := range []struct { + path string giveLevel string wantLevel endure.Level wantError bool }{ - {giveLevel: "debug", wantLevel: endure.DebugLevel}, - {giveLevel: "info", wantLevel: endure.InfoLevel}, - {giveLevel: "warn", wantLevel: endure.WarnLevel}, - {giveLevel: "warning", wantLevel: endure.WarnLevel}, - {giveLevel: "error", wantLevel: endure.ErrorLevel}, - {giveLevel: "panic", wantLevel: endure.PanicLevel}, - {giveLevel: "fatal", wantLevel: endure.FatalLevel}, + {path: "test/endure_ok_debug.yaml", giveLevel: "debug", wantLevel: endure.DebugLevel}, + {path: "test/endure_ok_info.yaml", giveLevel: "info", wantLevel: endure.InfoLevel}, + {path: "test/endure_ok_warn.yaml", giveLevel: "warn", wantLevel: endure.WarnLevel}, + {path: "test/endure_ok_panic.yaml", giveLevel: "panic", wantLevel: endure.PanicLevel}, + {path: "test/endure_ok_fatal.yaml", giveLevel: "fatal", wantLevel: endure.FatalLevel}, - {giveLevel: "foobar", wantError: true}, + {path: "test/endure_ok_foobar.yaml", giveLevel: "foobar", wantError: true}, } { tt := tt t.Run(tt.giveLevel, func(t *testing.T) { cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte("endure:\n log_level: " + tt.giveLevel)} assert.NoError(t, cfgPlugin.Init()) - c, err := container.NewConfig(cfgPlugin) + c, err := container.NewConfig(tt.path) if tt.wantError { assert.Nil(t, c) diff --git a/internal/container/test/endure_ok.yaml b/internal/container/test/endure_ok.yaml new file mode 100644 index 00000000..03db7c23 --- /dev/null +++ b/internal/container/test/endure_ok.yaml @@ -0,0 +1,5 @@ +endure: + grace_period: 10s + print_graph: true + retry_on_fail: true + log_level: warn diff --git a/internal/container/test/endure_ok_debug.yaml b/internal/container/test/endure_ok_debug.yaml new file mode 100644 index 00000000..5953c4de --- /dev/null +++ b/internal/container/test/endure_ok_debug.yaml @@ -0,0 +1,5 @@ +endure: + grace_period: 10s + print_graph: true + retry_on_fail: true + log_level: debug diff --git a/internal/container/test/endure_ok_fatal.yaml b/internal/container/test/endure_ok_fatal.yaml new file mode 100644 index 00000000..69e16467 --- /dev/null +++ b/internal/container/test/endure_ok_fatal.yaml @@ -0,0 +1,5 @@ +endure: + grace_period: 10s + print_graph: true + retry_on_fail: true + log_level: fatal diff --git a/internal/container/test/endure_ok_foobar.yaml b/internal/container/test/endure_ok_foobar.yaml new file mode 100644 index 00000000..90728d2c --- /dev/null +++ b/internal/container/test/endure_ok_foobar.yaml @@ -0,0 +1,5 @@ +endure: + grace_period: 10s + print_graph: true + retry_on_fail: true + log_level: foobar diff --git a/internal/container/test/endure_ok_info.yaml b/internal/container/test/endure_ok_info.yaml new file mode 100644 index 00000000..7ad62861 --- /dev/null +++ b/internal/container/test/endure_ok_info.yaml @@ -0,0 +1,5 @@ +endure: + grace_period: 10s + print_graph: true + retry_on_fail: true + log_level: info diff --git a/internal/container/test/endure_ok_panic.yaml b/internal/container/test/endure_ok_panic.yaml new file mode 100644 index 00000000..5ac4dc9e --- /dev/null +++ b/internal/container/test/endure_ok_panic.yaml @@ -0,0 +1,5 @@ +endure: + grace_period: 10s + print_graph: true + retry_on_fail: true + log_level: panic diff --git a/internal/container/test/endure_ok_warn.yaml b/internal/container/test/endure_ok_warn.yaml new file mode 100644 index 00000000..03db7c23 --- /dev/null +++ b/internal/container/test/endure_ok_warn.yaml @@ -0,0 +1,5 @@ +endure: + grace_period: 10s + print_graph: true + retry_on_fail: true + log_level: warn diff --git a/internal/container/test/without_endure_ok.yaml b/internal/container/test/without_endure_ok.yaml new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/internal/container/test/without_endure_ok.yaml diff --git a/internal/rpc/client.go b/internal/rpc/client.go index 4e58972b..7d945add 100644 --- a/internal/rpc/client.go +++ b/internal/rpc/client.go @@ -1,24 +1,34 @@ -// Package prc contains wrapper around RPC client ONLY for internal usage. +// Package rpc contains wrapper around RPC client ONLY for internal usage. package rpc import ( "net/rpc" - "github.com/roadrunner-server/config/v2" "github.com/roadrunner-server/errors" goridgeRpc "github.com/roadrunner-server/goridge/v3/pkg/rpc" rpcPlugin "github.com/roadrunner-server/rpc/v2" + "github.com/spf13/viper" ) // NewClient creates client ONLY for internal usage (communication between our application with RR side). // Client will be connected to the RPC. -func NewClient(cfgPlugin *config.Plugin) (*rpc.Client, error) { - if !cfgPlugin.Has(rpcPlugin.PluginName) { +func NewClient(cfg string) (*rpc.Client, error) { + v := viper.New() + v.SetConfigFile(cfg) + + err := v.ReadInConfig() + if err != nil { + return nil, err + } + + if !v.IsSet(rpcPlugin.PluginName) { return nil, errors.E("rpc service disabled") } rpcConfig := &rpcPlugin.Config{} - if err := cfgPlugin.UnmarshalKey(rpcPlugin.PluginName, rpcConfig); err != nil { + + err = v.UnmarshalKey(rpcPlugin.PluginName, rpcConfig) + if err != nil { return nil, err } diff --git a/internal/rpc/client_test.go b/internal/rpc/client_test.go index 34240826..0744e167 100644 --- a/internal/rpc/client_test.go +++ b/internal/rpc/client_test.go @@ -14,28 +14,22 @@ func TestNewClient_RpcServiceDisabled(t *testing.T) { cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte{}} assert.NoError(t, cfgPlugin.Init()) - c, err := rpc.NewClient(cfgPlugin) + c, err := rpc.NewClient("test/config_rpc_empty.yaml") assert.Nil(t, c) assert.EqualError(t, err, "rpc service disabled") } func TestNewClient_WrongRcpConfiguration(t *testing.T) { - cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte("rpc:\n $foo bar")} - assert.NoError(t, cfgPlugin.Init()) - - c, err := rpc.NewClient(cfgPlugin) + c, err := rpc.NewClient("test/config_rpc_wrong.yaml") assert.Nil(t, c) assert.Error(t, err) - assert.Contains(t, err.Error(), "config_plugin_unmarshal_key") + assert.Contains(t, err.Error(), "'' expected a map, got 'string'") } func TestNewClient_ConnectionError(t *testing.T) { - cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte("rpc:\n listen: tcp://127.0.0.1:0")} - assert.NoError(t, cfgPlugin.Init()) - - c, err := rpc.NewClient(cfgPlugin) + c, err := rpc.NewClient("test/config_rpc_conn_err.yaml") assert.Nil(t, c) assert.Error(t, err) @@ -43,15 +37,12 @@ func TestNewClient_ConnectionError(t *testing.T) { } func TestNewClient_SuccessfullyConnected(t *testing.T) { - l, err := net.Listen("tcp", "127.0.0.1:0") + l, err := net.Listen("tcp", "127.0.0.1:55555") assert.NoError(t, err) defer func() { assert.NoError(t, l.Close()) }() - cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte("rpc:\n listen: tcp://" + l.Addr().String())} - assert.NoError(t, cfgPlugin.Init()) - - c, err := rpc.NewClient(cfgPlugin) + c, err := rpc.NewClient("test/config_rpc_ok.yaml") assert.NotNil(t, c) assert.NoError(t, err) diff --git a/internal/rpc/test/config_rpc_conn_err.yaml b/internal/rpc/test/config_rpc_conn_err.yaml new file mode 100644 index 00000000..706c68a3 --- /dev/null +++ b/internal/rpc/test/config_rpc_conn_err.yaml @@ -0,0 +1,2 @@ +rpc: + listen: tcp://127.0.0.1:0 diff --git a/internal/rpc/test/config_rpc_empty.yaml b/internal/rpc/test/config_rpc_empty.yaml new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/internal/rpc/test/config_rpc_empty.yaml diff --git a/internal/rpc/test/config_rpc_ok.yaml b/internal/rpc/test/config_rpc_ok.yaml new file mode 100644 index 00000000..8b3140ce --- /dev/null +++ b/internal/rpc/test/config_rpc_ok.yaml @@ -0,0 +1,2 @@ +rpc: + listen: tcp://127.0.0.1:55555 diff --git a/internal/rpc/test/config_rpc_wrong.yaml b/internal/rpc/test/config_rpc_wrong.yaml new file mode 100644 index 00000000..8282ee1f --- /dev/null +++ b/internal/rpc/test/config_rpc_wrong.yaml @@ -0,0 +1,2 @@ +rpc: + $foo bar |