From 32f9884a58fef88baeca102c13a373f1f74d434f Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Tue, 17 Dec 2019 11:43:58 +0100 Subject: [PATCH] added command categories (extensions, fullstack) + reduce complexity metrics hopefully --- pkg/command/graph.go | 7 +- pkg/command/hello.go | 7 +- pkg/command/konnectd.go | 7 +- pkg/command/ocs.go | 7 +- pkg/command/phoenix.go | 7 +- pkg/command/root.go | 47 +++++++------ pkg/command/server.go | 148 +++++++++++++++++++++------------------- pkg/command/webdav.go | 7 +- 8 files changed, 127 insertions(+), 110 deletions(-) diff --git a/pkg/command/graph.go b/pkg/command/graph.go index 2e930ae2fd..fc97afbb64 100644 --- a/pkg/command/graph.go +++ b/pkg/command/graph.go @@ -17,9 +17,10 @@ import ( // GraphCommand is the entrypoint for the graph command. func GraphCommand(cfg *config.Config) cli.Command { return cli.Command{ - Name: "graph", - Usage: "Start graph server", - Flags: flagset.ServerWithConfig(cfg.Graph), + Name: "graph", + Usage: "Start graph server", + Flags: flagset.ServerWithConfig(cfg.Graph), + Category: "Extensions", Action: func(c *cli.Context) error { scfg := configureGraph(cfg) diff --git a/pkg/command/hello.go b/pkg/command/hello.go index c8d33a455f..a9e0ec93aa 100644 --- a/pkg/command/hello.go +++ b/pkg/command/hello.go @@ -18,9 +18,10 @@ import ( // HelloCommand is the entrypoint for the hello command. func HelloCommand(cfg *config.Config) cli.Command { return cli.Command{ - Name: "hello", - Usage: "Start hello server", - Flags: flagset.ServerWithConfig(cfg.Hello), + Name: "hello", + Usage: "Start hello server", + Flags: flagset.ServerWithConfig(cfg.Hello), + Category: "Extensions", Action: func(c *cli.Context) error { scfg := configureHello(cfg) diff --git a/pkg/command/konnectd.go b/pkg/command/konnectd.go index e3fd4d5021..10aec47d95 100644 --- a/pkg/command/konnectd.go +++ b/pkg/command/konnectd.go @@ -17,9 +17,10 @@ import ( // KonnectdCommand is the entrypoint for the konnectd command. func KonnectdCommand(cfg *config.Config) cli.Command { return cli.Command{ - Name: "konnectd", - Usage: "Start konnectd server", - Flags: flagset.ServerWithConfig(cfg.Konnectd), + Name: "konnectd", + Usage: "Start konnectd server", + Category: "Extensions", + Flags: flagset.ServerWithConfig(cfg.Konnectd), Action: func(c *cli.Context) error { scfg := configureKonnectd(cfg) diff --git a/pkg/command/ocs.go b/pkg/command/ocs.go index 770610c488..55005a5f4c 100644 --- a/pkg/command/ocs.go +++ b/pkg/command/ocs.go @@ -17,9 +17,10 @@ import ( // OCSCommand is the entrypoint for the ocs command. func OCSCommand(cfg *config.Config) cli.Command { return cli.Command{ - Name: "ocs", - Usage: "Start ocs server", - Flags: flagset.ServerWithConfig(cfg.OCS), + Name: "ocs", + Usage: "Start ocs server", + Category: "Extensions", + Flags: flagset.ServerWithConfig(cfg.OCS), Action: func(c *cli.Context) error { scfg := configureOCS(cfg) diff --git a/pkg/command/phoenix.go b/pkg/command/phoenix.go index 6affd112fb..240253bdc8 100644 --- a/pkg/command/phoenix.go +++ b/pkg/command/phoenix.go @@ -17,9 +17,10 @@ import ( // PhoenixCommand is the entrypoint for the phoenix command. func PhoenixCommand(cfg *config.Config) cli.Command { return cli.Command{ - Name: "phoenix", - Usage: "Start phoenix server", - Flags: flagset.ServerWithConfig(cfg.Phoenix), + Name: "phoenix", + Usage: "Start phoenix server", + Flags: flagset.ServerWithConfig(cfg.Phoenix), + Category: "Extensions", Action: func(c *cli.Context) error { scfg := configurePhoenix(cfg) diff --git a/pkg/command/root.go b/pkg/command/root.go index 305cc10049..65a6792072 100644 --- a/pkg/command/root.go +++ b/pkg/command/root.go @@ -99,28 +99,6 @@ func Execute() error { }, } - // register micro runtime services. - // TODO(refs) use the registry? Some interfaces would need to be tweaked - app.Commands = append(app.Commands, api.Commands()...) - app.Commands = append(app.Commands, bot.Commands()...) - app.Commands = append(app.Commands, broker.Commands()...) - app.Commands = append(app.Commands, health.Commands()...) - app.Commands = append(app.Commands, proxy.Commands()...) - app.Commands = append(app.Commands, monitor.Commands()...) - app.Commands = append(app.Commands, router.Commands()...) - app.Commands = append(app.Commands, tunnel.Commands()...) - app.Commands = append(app.Commands, network.Commands()...) - app.Commands = append(app.Commands, registry.Commands()...) - app.Commands = append(app.Commands, runtime.Commands()...) - app.Commands = append(app.Commands, debug.Commands()...) - app.Commands = append(app.Commands, server.Commands()...) - app.Commands = append(app.Commands, service.Commands()...) - app.Commands = append(app.Commands, store.Commands()...) - app.Commands = append(app.Commands, token.Commands()...) - app.Commands = append(app.Commands, new.Commands()...) - app.Commands = append(app.Commands, build.Commands()...) - app.Commands = append(app.Commands, web.Commands()...) - for _, fn := range register.Commands { app.Commands = append( app.Commands, @@ -128,6 +106,9 @@ func Execute() error { ) } + // adds micro runtime specific set of commands + addRuntime(app) + cli.HelpFlag = &cli.BoolFlag{ Name: "help,h", Usage: "Show the help", @@ -150,3 +131,25 @@ func NewLogger(cfg *config.Config) log.Logger { log.Color(cfg.Log.Color), ) } + +func addRuntime(app *cli.App) { + app.Commands = append(app.Commands, api.Commands()...) + app.Commands = append(app.Commands, bot.Commands()...) + app.Commands = append(app.Commands, broker.Commands()...) + app.Commands = append(app.Commands, health.Commands()...) + app.Commands = append(app.Commands, proxy.Commands()...) + app.Commands = append(app.Commands, monitor.Commands()...) + app.Commands = append(app.Commands, router.Commands()...) + app.Commands = append(app.Commands, tunnel.Commands()...) + app.Commands = append(app.Commands, network.Commands()...) + app.Commands = append(app.Commands, registry.Commands()...) + app.Commands = append(app.Commands, runtime.Commands()...) + app.Commands = append(app.Commands, debug.Commands()...) + app.Commands = append(app.Commands, server.Commands()...) + app.Commands = append(app.Commands, service.Commands()...) + app.Commands = append(app.Commands, store.Commands()...) + app.Commands = append(app.Commands, token.Commands()...) + app.Commands = append(app.Commands, new.Commands()...) + app.Commands = append(app.Commands, build.Commands()...) + app.Commands = append(app.Commands, web.Commands()...) +} diff --git a/pkg/command/server.go b/pkg/command/server.go index 5b73b49c37..50e30376a1 100644 --- a/pkg/command/server.go +++ b/pkg/command/server.go @@ -15,18 +15,44 @@ import ( gorun "github.com/micro/go-micro/runtime" openzipkin "github.com/openzipkin/zipkin-go" zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http" + "github.com/owncloud/ocis-pkg/log" "github.com/owncloud/ocis/pkg/config" "github.com/owncloud/ocis/pkg/flagset" "go.opencensus.io/stats/view" "go.opencensus.io/trace" ) +// Services to start as part of the fullstack option +var services = []string{ + "network", // :8085 + "runtime", // :8088 + "registry", // :8000 + "broker", // :8001 + "store", // :8002 + "tunnel", // :8083 + "router", // :8084 + "monitor", // :???? + "debug", // :???? + "proxy", // :8081 + "api", // :8080 + "web", // :8082 + "bot", // :???? + + // extensions + "hello", + "phoenix", + "graph", + "ocs", + "webdav", +} + // Server is the entrypoint for the server command. func Server(cfg *config.Config) cli.Command { app := cli.Command{ - Name: "server", - Usage: "Start fullstack server", - Flags: flagset.ServerWithConfig(cfg), + Name: "server", + Usage: "Start fullstack server", + Category: "Fullstack", + Flags: flagset.ServerWithConfig(cfg), Before: func(c *cli.Context) error { if cfg.HTTP.Root != "/" { cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") @@ -121,77 +147,59 @@ func Server(cfg *config.Config) cli.Command { Msg("Tracing is not enabled") } - // by the time we reach this point, all micro runtime services subcommands - // should be available for being initialized - muRuntime := cmd.DefaultCmd.Options().Runtime - env := os.Environ() + mruntime := cmd.DefaultCmd.Options().Runtime - services := []string{ - "network", // :8085 - "runtime", // :8088 - "registry", // :8000 - "broker", // :8001 - "store", // :8002 - "tunnel", // :8083 - "router", // :8084 - "monitor", // :???? - "debug", // :???? - "proxy", // :8081 - "api", // :8080 - "web", // :8082 - "bot", // :???? - - // ocis extensions - "hello", - "phoenix", - "graph", - "ocs", - "webdav", - } - - for _, service := range services { - args := []gorun.CreateOption{ - gorun.WithCommand(os.Args[0], service), - gorun.WithEnv(env), - gorun.WithOutput(os.Stdout), - } - - muService := &gorun.Service{Name: service} - if err := (*muRuntime).Create(muService, args...); err != nil { - logger.Error().Msgf("Failed to create runtime enviroment: %v", err) - } - } - - shutdown := make(chan os.Signal, 1) - signal.Notify(shutdown, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT) - - logger.Info().Msg("Starting service runtime") - - // start the runtime - if err := (*muRuntime).Start(); err != nil { - os.Exit(1) - } - - logger.Info().Msgf("Service runtime started") - - select { - case <-shutdown: - logger.Info().Msg("shutdown signal received") - logger.Info().Msg("stopping service runtime") - } - - // stop all the things - if err := (*muRuntime).Stop(); err != nil { - logger.Err(err) - } - - logger.Info().Msgf("Service runtime shutdown") - - // exit success - os.Exit(0) + // fork uses the micro runtime to fork go-micro services + forkServices(logger, mruntime) + // trap blocks until a kill signal is sent + trap(logger, mruntime) return nil }, } return app } + +func trap(logger log.Logger, runtime *gorun.Runtime) { + shutdown := make(chan os.Signal, 1) + signal.Notify(shutdown, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT) + + logger.Info().Msg("Starting service runtime") + if err := (*runtime).Start(); err != nil { + os.Exit(1) + } + + logger.Info().Msgf("Service runtime started") + + select { + case <-shutdown: + logger.Info().Msg("shutdown signal received") + logger.Info().Msg("stopping service runtime") + } + + if err := (*runtime).Stop(); err != nil { + logger.Err(err) + } + + logger.Info().Msgf("Service runtime shutdown") + os.Exit(0) +} + +func forkServices(logger log.Logger, runtime *gorun.Runtime) { + env := os.Environ() + + for _, service := range services { + args := []gorun.CreateOption{ + // the binary calls itself with the micro service as a subcommand as first argument + gorun.WithCommand(os.Args[0], service), + gorun.WithEnv(env), + // and logs to STDOUT. Perhaps this can be overridden to use a log.Logger + gorun.WithOutput(os.Stdout), + } + + muService := &gorun.Service{Name: service} + if err := (*runtime).Create(muService, args...); err != nil { + logger.Error().Msgf("Failed to create runtime enviroment: %v", err) + } + } +} diff --git a/pkg/command/webdav.go b/pkg/command/webdav.go index 918c7e508e..5a812d68c1 100644 --- a/pkg/command/webdav.go +++ b/pkg/command/webdav.go @@ -17,9 +17,10 @@ import ( // WebDAVCommand is the entrypoint for the webdav command. func WebDAVCommand(cfg *config.Config) cli.Command { return cli.Command{ - Name: "webdav", - Usage: "Start webdav server", - Flags: flagset.ServerWithConfig(cfg.WebDAV), + Name: "webdav", + Usage: "Start webdav server", + Category: "Extensions", + Flags: flagset.ServerWithConfig(cfg.WebDAV), Action: func(c *cli.Context) error { scfg := configureWebDAV(cfg)