Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make extensions handle their config #148

Merged
merged 11 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,21 @@ To the list of available services.

## Configuration

We provide overall three different variants of configuration. The variant based on environment variables and commandline flags are split up into global values and command-specific values.
oCIS Single Binary is not responsible for configuring extensions. Instead, each extension could either be configured by environment variables, cli flags or config files.

### Configuration using config files

Out of the box extensions will attempt to read configuration details from:

```console
/etc/ocis
$HOME/.ocis
./config
```

For this configuration to be picked up, have a look at your extension `root` command and look for which default config name it has assigned. *i.e: ocis-proxy reads `ocis.json | yaml | toml ...`*.

> Important note: As per the time of this writing, Viper does not play nice with urfave/cli flags, this results in values defined on config files taking precedence over cli flags. This behavior is different with environment variables, these will ALWAYS override any value, as they are the most explicit.

### Envrionment variables

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ require (
github.com/micro/go-micro/v2 v2.0.1-0.20200212105717-d76baf59de2e
github.com/micro/micro/v2 v2.0.1-0.20200210100719-f38a1d8d5348
github.com/openzipkin/zipkin-go v0.2.2
github.com/owncloud/ocis-devldap v0.0.0-20200311185721-105f9cbe4ce4 // indirect
github.com/owncloud/ocis-glauth v0.2.0
github.com/owncloud/ocis-graph v0.0.0-20200217115956-172417259283
github.com/owncloud/ocis-graph-explorer v0.0.0-20200210111049-017eeb40dc0c
github.com/owncloud/ocis-hello v0.1.0-alpha1.0.20200207094758-c866cafca7e5
github.com/owncloud/ocis-konnectd v0.0.0-20200303180152-937016f63393
github.com/owncloud/ocis-ocs v0.0.0-20200207130609-800a64d45fac
github.com/owncloud/ocis-phoenix v0.1.1-0.20200213204418-06f50c42c225
github.com/owncloud/ocis-pkg/v2 v2.0.2
github.com/owncloud/ocis-pkg/v2 v2.0.3-0.20200309150924-5c659fd4b0ad
github.com/owncloud/ocis-proxy v0.0.0-20200310100127-5a38d286e52c
github.com/owncloud/ocis-reva v0.0.0-20200213202552-584d47daa8bc
github.com/owncloud/ocis-webdav v0.0.0-20200210113150-6c4d498c38b0
github.com/spf13/viper v1.6.2
go.opencensus.io v0.22.3
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d // indirect
go.opencensus.io v0.22.2
golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 // indirect
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect
stash.kopano.io/kc/konnect v0.29.0 // indirect
Expand Down
374 changes: 266 additions & 108 deletions go.sum

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions pkg/command/graph-explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ func GraphExplorerCommand(cfg *config.Config) *cli.Command {
Usage: "Start graph explorer",
Category: "Extensions",
Flags: flagset.ServerWithConfig(cfg.GraphExplorer),
Action: func(c *cli.Context) error {
scfg := configureGraphExplorer(cfg)
Action: func(ctx *cli.Context) error {
graphExplorerCommand := command.Server(configureGraphExplorer(cfg))

return cli.HandleAction(
command.Server(scfg).Action,
c,
)
if err := graphExplorerCommand.Before(ctx); err != nil {
return err
}

return cli.HandleAction(graphExplorerCommand.Action, ctx)
},
}
}
Expand All @@ -33,9 +34,6 @@ func configureGraphExplorer(cfg *config.Config) *svcconfig.Config {
cfg.GraphExplorer.Log.Level = cfg.Log.Level
cfg.GraphExplorer.Log.Pretty = cfg.Log.Pretty
cfg.GraphExplorer.Log.Color = cfg.Log.Color
cfg.GraphExplorer.Tracing.Enabled = false
cfg.GraphExplorer.HTTP.Addr = "localhost:9135"
cfg.GraphExplorer.HTTP.Root = "/"

return cfg.GraphExplorer
}
Expand Down
16 changes: 7 additions & 9 deletions pkg/command/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ func GraphCommand(cfg *config.Config) *cli.Command {
Usage: "Start graph server",
Category: "Extensions",
Flags: flagset.ServerWithConfig(cfg.Graph),
Action: func(c *cli.Context) error {
scfg := configureGraph(cfg)
Action: func(ctx *cli.Context) error {
graphCommand := command.Server(configureGraph(cfg))

return cli.HandleAction(
command.Server(scfg).Action,
c,
)
if err := graphCommand.Before(ctx); err != nil {
return err
}

return cli.HandleAction(graphCommand.Action, ctx)
},
}
}
Expand All @@ -33,9 +34,6 @@ func configureGraph(cfg *config.Config) *svcconfig.Config {
cfg.Graph.Log.Level = cfg.Log.Level
cfg.Graph.Log.Pretty = cfg.Log.Pretty
cfg.Graph.Log.Color = cfg.Log.Color
cfg.Graph.Tracing.Enabled = false
cfg.Graph.HTTP.Addr = "localhost:9120"
cfg.Graph.HTTP.Root = "/"

return cfg.Graph
}
Expand Down
11 changes: 3 additions & 8 deletions pkg/command/konnectd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package command

import (
Expand All @@ -18,14 +17,13 @@ func KonnectdCommand(cfg *config.Config) *cli.Command {
Category: "Extensions",
Flags: flagset.ServerWithConfig(cfg.Konnectd),
Action: func(c *cli.Context) error {
serverConfig := configureKonnectd(cfg)
serverCommand := command.Server(serverConfig)
konnectdCommand := command.Server(configureKonnectd(cfg))

if err := serverCommand.Before(c); err != nil {
if err := konnectdCommand.Before(c); err != nil {
return err
}

return cli.HandleAction(serverCommand.Action, c)
return cli.HandleAction(konnectdCommand.Action, c)
},
}
}
Expand All @@ -34,9 +32,6 @@ func configureKonnectd(cfg *config.Config) *svcconfig.Config {
cfg.Konnectd.Log.Level = cfg.Log.Level
cfg.Konnectd.Log.Pretty = cfg.Log.Pretty
cfg.Konnectd.Log.Color = cfg.Log.Color
cfg.Konnectd.Tracing.Enabled = false
cfg.Konnectd.HTTP.Addr = "localhost:9130"
cfg.Konnectd.HTTP.Root = "/"

return cfg.Konnectd
}
Expand Down
16 changes: 7 additions & 9 deletions pkg/command/ocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ func OCSCommand(cfg *config.Config) *cli.Command {
Usage: "Start ocs server",
Category: "Extensions",
Flags: flagset.ServerWithConfig(cfg.OCS),
Action: func(c *cli.Context) error {
scfg := configureOCS(cfg)
Action: func(ctx *cli.Context) error {
ocsCommand := command.Server(configureOCS(cfg))

return cli.HandleAction(
command.Server(scfg).Action,
c,
)
if err := ocsCommand.Before(ctx); err != nil {
return err
}

return cli.HandleAction(ocsCommand.Action, ctx)
},
}
}
Expand All @@ -33,9 +34,6 @@ func configureOCS(cfg *config.Config) *svcconfig.Config {
cfg.OCS.Log.Level = cfg.Log.Level
cfg.OCS.Log.Pretty = cfg.Log.Pretty
cfg.OCS.Log.Color = cfg.Log.Color
cfg.OCS.Tracing.Enabled = false
cfg.OCS.HTTP.Addr = "localhost:9110"
cfg.OCS.HTTP.Root = "/"

return cfg.OCS
}
Expand Down
11 changes: 5 additions & 6 deletions pkg/command/phoenix.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ func PhoenixCommand(cfg *config.Config) *cli.Command {
}

cfg.Phoenix.Phoenix.Config.Apps = c.StringSlice("web-config-app")

return nil
},
Action: func(c *cli.Context) error {
phoenixCommand := command.Server(configurePhoenix(cfg))

scfg := configurePhoenix(cfg)
if err := phoenixCommand.Before(c); err != nil {
return err
}

return cli.HandleAction(
command.Server(scfg).Action,
c,
)
return cli.HandleAction(phoenixCommand.Action, c)
},
}
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/command/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ func ProxyCommand(cfg *config.Config) *cli.Command {
Usage: "Start proxy server",
Category: "Extensions",
Flags: flagset.ServerWithConfig(cfg.Proxy),
Action: func(c *cli.Context) error {
serverConfig := configureProxy(cfg)
serverCommand := command.Server(serverConfig)
Action: func(ctx *cli.Context) error {
proxyCommand := command.Server(configureProxy(cfg))

if err := serverCommand.Before(c); err != nil {
if err := proxyCommand.Before(ctx); err != nil {
return err
}

return cli.HandleAction(serverCommand.Action, c)
return cli.HandleAction(proxyCommand.Action, ctx)
},
}
}
Expand Down
46 changes: 0 additions & 46 deletions pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package command

import (
"os"
"strings"

"github.com/micro/cli/v2"
"github.com/owncloud/ocis-pkg/v2/log"
Expand All @@ -11,7 +10,6 @@ import (
"github.com/owncloud/ocis/pkg/micro/runtime"
"github.com/owncloud/ocis/pkg/register"
"github.com/owncloud/ocis/pkg/version"
"github.com/spf13/viper"
)

// Execute is the entry point for the ocis-ocis command.
Expand All @@ -30,53 +28,9 @@ func Execute() error {
Email: "support@owncloud.com",
},
},

Flags: flagset.RootWithConfig(cfg),

Before: func(c *cli.Context) error {
logger := NewLogger(cfg)

viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("OCIS")
viper.AutomaticEnv()

if c.IsSet("config-file") {
viper.SetConfigFile(c.String("config-file"))
} else {
viper.SetConfigName("ocis")

viper.AddConfigPath("/etc/ocis")
viper.AddConfigPath("$HOME/.ocis")
viper.AddConfigPath("./config")
}

if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
logger.Info().
Msg("Continue without config")
case viper.UnsupportedConfigError:
logger.Fatal().
Err(err).
Msg("Unsupported config type")
default:
logger.Fatal().
Err(err).
Msg("Failed to read config")
}
}

if err := viper.Unmarshal(&cfg); err != nil {
logger.Fatal().
Err(err).
Msg("Failed to parse config")
}

return nil
},
}

// Load commands from the registry
for _, fn := range register.Commands {
app.Commands = append(
app.Commands,
Expand Down
1 change: 1 addition & 0 deletions pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func Server(cfg *config.Config) *cli.Command {
runtime.Services(append(runtime.MicroServices, runtime.Extensions...)),
runtime.Logger(logger),
runtime.MicroRuntime(cmd.DefaultCmd.Options().Runtime),
runtime.Context(c),
)

runtime.Start()
Expand Down
1 change: 1 addition & 0 deletions pkg/command/server_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func Simple(cfg *config.Config) *cli.Command {
runtime.Logger(logger),
runtime.Services(append(runtime.RuntimeServices, SimpleRuntimeServices...)),
runtime.MicroRuntime(cmd.DefaultCmd.Options().Runtime),
runtime.Context(c),
)

{
Expand Down
14 changes: 6 additions & 8 deletions pkg/command/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ func WebDAVCommand(cfg *config.Config) *cli.Command {
Category: "Extensions",
Flags: flagset.ServerWithConfig(cfg.WebDAV),
Action: func(c *cli.Context) error {
scfg := configureWebDAV(cfg)
webdavCommand := command.Server(configureWebDAV(cfg))

return cli.HandleAction(
command.Server(scfg).Action,
c,
)
if err := webdavCommand.Before(c); err != nil {
return err
}

return cli.HandleAction(webdavCommand.Action, c)
},
}
}
Expand All @@ -33,9 +34,6 @@ func configureWebDAV(cfg *config.Config) *svcconfig.Config {
cfg.WebDAV.Log.Level = cfg.Log.Level
cfg.WebDAV.Log.Pretty = cfg.Log.Pretty
cfg.WebDAV.Log.Color = cfg.Log.Color
cfg.WebDAV.Tracing.Enabled = false
cfg.WebDAV.HTTP.Addr = "localhost:9115"
cfg.WebDAV.HTTP.Root = "/"

return cfg.WebDAV
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/micro/runtime/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime

import (
"github.com/micro/cli/v2"
gorun "github.com/micro/go-micro/v2/runtime"
"github.com/owncloud/ocis-pkg/v2/log"
)
Expand All @@ -10,6 +11,7 @@ type Options struct {
Services []string
Logger log.Logger
MicroRuntime *gorun.Runtime
Context *cli.Context
}

// Option undocummented
Expand Down Expand Up @@ -46,3 +48,10 @@ func MicroRuntime(rt *gorun.Runtime) Option {
o.MicroRuntime = rt
}
}

// Context option
func Context(c *cli.Context) Option {
return func(o *Options) {
o.Context = c
}
}
Loading