diff --git a/accounts/cmd/accounts/main.go b/accounts/cmd/accounts/main.go index 6fa3f4128c..553fc79ef7 100644 --- a/accounts/cmd/accounts/main.go +++ b/accounts/cmd/accounts/main.go @@ -4,10 +4,11 @@ import ( "os" "github.com/owncloud/ocis/accounts/pkg/command" + "github.com/owncloud/ocis/accounts/pkg/config" ) func main() { - if err := command.Execute(); err != nil { + if err := command.Execute(config.New()); err != nil { os.Exit(1) } } diff --git a/accounts/pkg/command/root.go b/accounts/pkg/command/root.go index 19088bb0d6..8957f25fbc 100644 --- a/accounts/pkg/command/root.go +++ b/accounts/pkg/command/root.go @@ -1,6 +1,7 @@ package command import ( + "context" "os" "strings" @@ -19,8 +20,7 @@ var ( ) // Execute is the entry point for the ocis-accounts command. -func Execute() error { - cfg := config.New() +func Execute(cfg *config.Config) error { app := &cli.App{ Name: "ocis-accounts", Version: version.String, @@ -118,3 +118,31 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error { return nil } + +// SutureService allows for the settings 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 settings.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 { + panic(err) + } +} + +func (s SutureService) Stop() { + s.cancel() +} diff --git a/accounts/pkg/command/server.go b/accounts/pkg/command/server.go index fd4ba79388..a4452ee044 100644 --- a/accounts/pkg/command/server.go +++ b/accounts/pkg/command/server.go @@ -135,8 +135,6 @@ func Server(cfg *config.Config) *cli.Command { http.Context(ctx), http.Config(cfg), http.Metrics(mtrcs), - http.Flags(flagset.RootWithConfig(config.New())), - http.Flags(flagset.ServerWithConfig(config.New())), http.Handler(handler), ) diff --git a/accounts/pkg/config/config.go b/accounts/pkg/config/config.go index 5ce47e94ff..d97e9f1daf 100644 --- a/accounts/pkg/config/config.go +++ b/accounts/pkg/config/config.go @@ -1,6 +1,8 @@ // Package config should be moved to internal package config +import "context" + // LDAP defines the available ldap configuration. type LDAP struct { Hostname string @@ -121,6 +123,8 @@ type Config struct { Index Index ServiceUser ServiceUser Tracing Tracing + + Context context.Context } // New returns a new config. diff --git a/accounts/pkg/flagset/flagset.go b/accounts/pkg/flagset/flagset.go index e4f90896ab..8c984204cf 100644 --- a/accounts/pkg/flagset/flagset.go +++ b/accounts/pkg/flagset/flagset.go @@ -13,21 +13,21 @@ func RootWithConfig(cfg *config.Config) []cli.Flag { Name: "log-level", Value: "info", Usage: "Set logging level", - EnvVars: []string{"ACCOUNTS_LOG_LEVEL"}, + EnvVars: []string{"ACCOUNTS_LOG_LEVEL", "OCIS_LOG_LEVEL"}, Destination: &cfg.Log.Level, }, &cli.BoolFlag{ Value: true, Name: "log-pretty", Usage: "Enable pretty logging", - EnvVars: []string{"ACCOUNTS_LOG_PRETTY"}, + EnvVars: []string{"ACCOUNTS_LOG_PRETTY", "OCIS_LOG_PRETTY"}, Destination: &cfg.Log.Pretty, }, &cli.BoolFlag{ Value: true, Name: "log-color", Usage: "Enable colored logging", - EnvVars: []string{"ACCOUNTS_LOG_COLOR"}, + EnvVars: []string{"ACCOUNTS_LOG_COLOR", "OCIS_LOG_COLOR"}, Destination: &cfg.Log.Color, }, } diff --git a/ocis/pkg/runtime/runtime.go b/ocis/pkg/runtime/runtime.go index b0a703f89d..51102b72d6 100644 --- a/ocis/pkg/runtime/runtime.go +++ b/ocis/pkg/runtime/runtime.go @@ -5,6 +5,7 @@ import ( "os" "os/signal" + accounts "github.com/owncloud/ocis/accounts/pkg/command" settings "github.com/owncloud/ocis/settings/pkg/command" "github.com/thejerf/suture" @@ -78,14 +79,15 @@ func New(cfg *config.Config) Runtime { // within the service Stop() method. Services should cancel their context. type serviceTokens map[string][]suture.ServiceToken +// tokens are used to keep track of the services +var tokens = serviceTokens{} + // Start rpc runtime func (r *Runtime) Start() error { setMicroLogger(r.c.Log) halt := make(chan os.Signal, 1) signal.Notify(halt, os.Interrupt) - // tokens are used to keep track of the services - tokens := serviceTokens{} supervisor := suture.NewSimple("ocis") globalCtx, globalCancel := context.WithCancel(context.Background()) @@ -106,6 +108,7 @@ func (r *Runtime) Start() error { tokens["settings"] = append(tokens["settings"], supervisor.Add(settings.NewSutureService(globalCtx, r.c.Settings))) tokens["storagemetadata"] = append(tokens["storagemetadata"], supervisor.Add(storage.NewStorageMetadata(globalCtx, r.c.Storage))) + tokens["accounts"] = append(tokens["accounts"], supervisor.Add(accounts.NewSutureService(globalCtx, r.c.Accounts))) go supervisor.ServeBackground()