diff --git a/ocis/pkg/flagset/flagset.go b/ocis/pkg/flagset/flagset.go index 554850ab5..7e92036a2 100644 --- a/ocis/pkg/flagset/flagset.go +++ b/ocis/pkg/flagset/flagset.go @@ -9,7 +9,7 @@ import ( func RootWithConfig(cfg *config.Config) []cli.Flag { return []cli.Flag{ &cli.StringFlag{ - Name: "log-level", + Name: "ocis-log-level", Value: "info", Usage: "Set logging level", EnvVars: []string{"OCIS_LOG_LEVEL"}, @@ -17,14 +17,14 @@ func RootWithConfig(cfg *config.Config) []cli.Flag { }, &cli.BoolFlag{ Value: false, - Name: "log-pretty", + Name: "ocis-log-pretty", Usage: "Enable pretty logging", EnvVars: []string{"OCIS_LOG_PRETTY"}, Destination: &cfg.Log.Pretty, }, &cli.BoolFlag{ Value: true, - Name: "log-color", + Name: "ocis-log-color", Usage: "Enable colored logging", EnvVars: []string{"OCIS_LOG_COLOR"}, Destination: &cfg.Log.Color, diff --git a/ocis/pkg/runtime/runtime.go b/ocis/pkg/runtime/runtime.go index 51f50a1d7..944392729 100644 --- a/ocis/pkg/runtime/runtime.go +++ b/ocis/pkg/runtime/runtime.go @@ -14,6 +14,7 @@ import ( settings "github.com/owncloud/ocis/settings/pkg/command" store "github.com/owncloud/ocis/store/pkg/command" thumbnails "github.com/owncloud/ocis/thumbnails/pkg/command" + web "github.com/owncloud/ocis/web/pkg/command" "github.com/thejerf/suture" @@ -111,6 +112,7 @@ func (r *Runtime) Start() error { // - remove default log flagset values. // - subcommands MUST also set MICRO_LOG_LEVEL to error. // - 2021-03-04T14:06:37+01:00 FTL failed to read config error="open /Users/aunger/.ocis/idp.env: no such file or directory" service=idp still exists + // - normalize flag parsing (and fix hack of renaming ocis top level for the destination side effect) // propagate reva log config to storage services r.c.Storage.Log.Level = r.c.Log.Level @@ -127,6 +129,7 @@ func (r *Runtime) Start() error { addServiceToken("proxy", supervisor.Add(proxy.NewSutureService(globalCtx, r.c.Proxy))) addServiceToken("store", supervisor.Add(store.NewSutureService(globalCtx, r.c.Store))) addServiceToken("thumbnails", supervisor.Add(thumbnails.NewSutureService(globalCtx, r.c.Thumbnails))) + addServiceToken("web", supervisor.Add(web.NewSutureService(globalCtx, r.c.Web))) // TODO(refs) debug line with supervised services. go supervisor.ServeBackground() diff --git a/ocs/pkg/command/root.go b/ocs/pkg/command/root.go index 5f27e9d42..d5b7d77d9 100644 --- a/ocs/pkg/command/root.go +++ b/ocs/pkg/command/root.go @@ -8,7 +8,6 @@ import ( "github.com/micro/cli/v2" "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocs/pkg/config" - "github.com/owncloud/ocis/ocs/pkg/flagset" "github.com/owncloud/ocis/ocs/pkg/version" "github.com/spf13/viper" ) @@ -28,8 +27,6 @@ func Execute(cfg *config.Config) error { }, }, - Flags: flagset.RootWithConfig(cfg), - Before: func(c *cli.Context) error { cfg.Service.Version = version.String return nil diff --git a/ocs/pkg/flagset/flagset.go b/ocs/pkg/flagset/flagset.go index 24ba14a1d..dc260c982 100644 --- a/ocs/pkg/flagset/flagset.go +++ b/ocs/pkg/flagset/flagset.go @@ -5,8 +5,21 @@ import ( "github.com/owncloud/ocis/ocs/pkg/config" ) -// RootWithConfig applies cfg to the root flagset -func RootWithConfig(cfg *config.Config) []cli.Flag { +// HealthWithConfig applies cfg to the root flagset +func HealthWithConfig(cfg *config.Config) []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "debug-addr", + Value: "0.0.0.0:9114", + Usage: "Address to debug endpoint", + EnvVars: []string{"OCS_DEBUG_ADDR"}, + Destination: &cfg.Debug.Addr, + }, + } +} + +// ServerWithConfig applies cfg to the root flagset +func ServerWithConfig(cfg *config.Config) []cli.Flag { return []cli.Flag{ &cli.StringFlag{ Name: "log-level", @@ -26,25 +39,6 @@ func RootWithConfig(cfg *config.Config) []cli.Flag { EnvVars: []string{"OCS_LOG_COLOR", "OCIS_LOG_COLOR"}, Destination: &cfg.Log.Color, }, - } -} - -// HealthWithConfig applies cfg to the root flagset -func HealthWithConfig(cfg *config.Config) []cli.Flag { - return []cli.Flag{ - &cli.StringFlag{ - Name: "debug-addr", - Value: "0.0.0.0:9114", - Usage: "Address to debug endpoint", - EnvVars: []string{"OCS_DEBUG_ADDR"}, - Destination: &cfg.Debug.Addr, - }, - } -} - -// ServerWithConfig applies cfg to the root flagset -func ServerWithConfig(cfg *config.Config) []cli.Flag { - return []cli.Flag{ &cli.StringFlag{ Name: "config-file", Value: "", diff --git a/thumbnails/pkg/command/root.go b/thumbnails/pkg/command/root.go index 2a7e0cfd2..6f1228553 100644 --- a/thumbnails/pkg/command/root.go +++ b/thumbnails/pkg/command/root.go @@ -8,7 +8,6 @@ import ( "github.com/micro/cli/v2" "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/thumbnails/pkg/config" - "github.com/owncloud/ocis/thumbnails/pkg/flagset" "github.com/owncloud/ocis/thumbnails/pkg/version" "github.com/spf13/viper" ) @@ -28,8 +27,6 @@ func Execute(cfg *config.Config) error { }, }, - Flags: flagset.RootWithConfig(cfg), - Before: func(c *cli.Context) error { cfg.Server.Version = version.String return nil diff --git a/thumbnails/pkg/command/server.go b/thumbnails/pkg/command/server.go index 801a7c219..7cb41a9bd 100644 --- a/thumbnails/pkg/command/server.go +++ b/thumbnails/pkg/command/server.go @@ -140,7 +140,7 @@ func Server(cfg *config.Config) *cli.Command { grpc.Namespace(cfg.Server.Namespace), grpc.Address(cfg.Server.Address), grpc.Metrics(metrics), - grpc.Flags(flagset.RootWithConfig(config.New())), + //grpc.Flags(flagset.RootWithConfig(config.New())), ) gr.Add(func() error { diff --git a/thumbnails/pkg/flagset/flagset.go b/thumbnails/pkg/flagset/flagset.go index 76eb7bb4c..717c6c260 100644 --- a/thumbnails/pkg/flagset/flagset.go +++ b/thumbnails/pkg/flagset/flagset.go @@ -8,8 +8,21 @@ import ( "github.com/owncloud/ocis/thumbnails/pkg/config" ) -// RootWithConfig applies cfg to the root flagset -func RootWithConfig(cfg *config.Config) []cli.Flag { +// HealthWithConfig applies cfg to the root flagset +func HealthWithConfig(cfg *config.Config) []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "debug-addr", + Value: "0.0.0.0:9189", + Usage: "Address to debug endpoint", + EnvVars: []string{"THUMBNAILS_DEBUG_ADDR"}, + Destination: &cfg.Debug.Addr, + }, + } +} + +// ServerWithConfig applies cfg to the root flagset +func ServerWithConfig(cfg *config.Config) []cli.Flag { return []cli.Flag{ &cli.StringFlag{ Name: "log-level", @@ -29,25 +42,6 @@ func RootWithConfig(cfg *config.Config) []cli.Flag { EnvVars: []string{"THUMBNAILS_LOG_COLOR", "OCIS_LOG_COLOR"}, Destination: &cfg.Log.Color, }, - } -} - -// HealthWithConfig applies cfg to the root flagset -func HealthWithConfig(cfg *config.Config) []cli.Flag { - return []cli.Flag{ - &cli.StringFlag{ - Name: "debug-addr", - Value: "0.0.0.0:9189", - Usage: "Address to debug endpoint", - EnvVars: []string{"THUMBNAILS_DEBUG_ADDR"}, - Destination: &cfg.Debug.Addr, - }, - } -} - -// ServerWithConfig applies cfg to the root flagset -func ServerWithConfig(cfg *config.Config) []cli.Flag { - return []cli.Flag{ &cli.StringFlag{ Name: "config-file", Value: "", diff --git a/web/cmd/web/main.go b/web/cmd/web/main.go index 6fd97a765..50ca749a7 100644 --- a/web/cmd/web/main.go +++ b/web/cmd/web/main.go @@ -4,10 +4,11 @@ import ( "os" "github.com/owncloud/ocis/web/pkg/command" + "github.com/owncloud/ocis/web/pkg/config" ) func main() { - if err := command.Execute(); err != nil { + if err := command.Execute(config.New()); err != nil { os.Exit(1) } } diff --git a/web/pkg/command/root.go b/web/pkg/command/root.go index 0fca70bb9..69ae423db 100644 --- a/web/pkg/command/root.go +++ b/web/pkg/command/root.go @@ -1,21 +1,19 @@ package command import ( + "context" "os" "strings" "github.com/micro/cli/v2" "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/web/pkg/config" - "github.com/owncloud/ocis/web/pkg/flagset" "github.com/owncloud/ocis/web/pkg/version" "github.com/spf13/viper" ) // Execute is the entry point for the web command. -func Execute() error { - cfg := config.New() - +func Execute(cfg *config.Config) error { app := &cli.App{ Name: "web", Version: version.String, @@ -29,7 +27,7 @@ func Execute() error { }, }, - Flags: flagset.RootWithConfig(cfg), + //Flags: flagset.RootWithConfig(cfg), Commands: []*cli.Command{ Server(cfg), @@ -102,3 +100,31 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error { return nil } + +// SutureService allows for the web command to be embedded and supervised by a suture supervisor tree. +type SutureService struct { + ctx context.Context + cancel context.CancelFunc // used to cancel the context go-micro services used to shutdown a service. + cfg *config.Config +} + +// NewSutureService creates a new web.SutureService +func NewSutureService(ctx context.Context, cfg *config.Config) SutureService { + sctx, cancel := context.WithCancel(ctx) + cfg.Context = sctx // propagate the context down to the go-micro services. + return SutureService{ + ctx: sctx, + cancel: cancel, + cfg: cfg, + } +} + +func (s SutureService) Serve() { + if err := Execute(s.cfg); err != nil { + return + } +} + +func (s SutureService) Stop() { + s.cancel() +} diff --git a/web/pkg/command/server.go b/web/pkg/command/server.go index 237651d56..6194453f9 100644 --- a/web/pkg/command/server.go +++ b/web/pkg/command/server.go @@ -30,7 +30,7 @@ func Server(cfg *config.Config) *cli.Command { return &cli.Command{ Name: "server", Usage: "Start integrated server", - Flags: flagset.ServerWithConfig(cfg), + Flags: append(flagset.ServerWithConfig(cfg), flagset.RootWithConfig(cfg)...), Before: func(c *cli.Context) error { if cfg.HTTP.Root != "/" { cfg.HTTP.Root = strings.TrimRight(cfg.HTTP.Root, "/") @@ -166,7 +166,6 @@ func Server(cfg *config.Config) *cli.Command { http.Namespace(cfg.HTTP.Namespace), http.Config(cfg), http.Metrics(metrics), - http.Flags(flagset.RootWithConfig(config.New())), ) if err != nil { diff --git a/web/pkg/config/config.go b/web/pkg/config/config.go index 70476a849..a060d5941 100644 --- a/web/pkg/config/config.go +++ b/web/pkg/config/config.go @@ -1,5 +1,7 @@ package config +import "context" + // Log defines the available logging configuration. type Log struct { Level string @@ -93,6 +95,8 @@ type Config struct { Asset Asset OIDC OIDC Web Web + + Context context.Context } // New initializes a new configuration with or without defaults. diff --git a/web/pkg/flagset/flagset.go b/web/pkg/flagset/flagset.go index 20368acdb..3a408a2e7 100644 --- a/web/pkg/flagset/flagset.go +++ b/web/pkg/flagset/flagset.go @@ -10,23 +10,20 @@ func RootWithConfig(cfg *config.Config) []cli.Flag { return []cli.Flag{ &cli.StringFlag{ Name: "log-level", - Value: "info", Usage: "Set logging level", - EnvVars: []string{"WEB_LOG_LEVEL"}, + EnvVars: []string{"WEB_LOG_LEVEL", "OCIS_LOG_LEVEL"}, Destination: &cfg.Log.Level, }, &cli.BoolFlag{ Name: "log-pretty", - Value: true, Usage: "Enable pretty logging", - EnvVars: []string{"WEB_LOG_PRETTY"}, + EnvVars: []string{"WEB_LOG_PRETTY", "OCIS_LOG_PRETTY"}, Destination: &cfg.Log.Pretty, }, &cli.BoolFlag{ Name: "log-color", - Value: true, Usage: "Enable colored logging", - EnvVars: []string{"WEB_LOG_COLOR"}, + EnvVars: []string{"WEB_LOG_COLOR", "OCIS_LOG_COLOR"}, Destination: &cfg.Log.Color, }, }