Skip to content

Commit

Permalink
Merge pull request #2708 from owncloud/try-gookikt-config
Browse files Browse the repository at this point in the history
  • Loading branch information
refs committed Nov 23, 2021
2 parents 3bc413b + 43af041 commit 1befac2
Show file tree
Hide file tree
Showing 210 changed files with 6,305 additions and 9,184 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -13,7 +13,6 @@ node_modules/
ocis/ocis
ocis/cmd/ocis/__debug_bin
ocis/cmd/ocis/config/
config/

.idea

Expand Down
10 changes: 6 additions & 4 deletions .make/docs.mk
Expand Up @@ -3,10 +3,12 @@ SKIP_CONFIG_DOCS_GENERATE ?= 0
CONFIG_DOCS_BASE_PATH ?= ../docs/extensions

.PHONY: config-docs-generate
config-docs-generate: $(FLAEX)
@if [ $(SKIP_CONFIG_DOCS_GENERATE) -ne 1 ]; then \
$(FLAEX) >| $(CONFIG_DOCS_BASE_PATH)/$(NAME)/configuration.md \
; fi;
config-docs-generate: #$(FLAEX)
# since https://github.com/owncloud/ocis/pull/2708 flaex can no longer be used
# TODO: how to document configuration
# @if [ $(SKIP_CONFIG_DOCS_GENERATE) -ne 1 ]; then \
# $(FLAEX) >| $(CONFIG_DOCS_BASE_PATH)/$(NAME)/configuration.md \
# ; fi;

.PHONY: grpc-docs-generate
grpc-docs-generate: buf-generate
4 changes: 2 additions & 2 deletions .vscode/launch.json
Expand Up @@ -20,6 +20,6 @@
// set insecure options because we don't have valid certificates in dev environments
"OCIS_INSECURE": "true",
}
},
}
]
}
}
2 changes: 1 addition & 1 deletion accounts/cmd/accounts/main.go
Expand Up @@ -8,7 +8,7 @@ import (
)

func main() {
if err := command.Execute(config.New()); err != nil {
if err := command.Execute(config.DefaultConfig()); err != nil {
os.Exit(1)
}
}
3 changes: 2 additions & 1 deletion accounts/pkg/command/inspect_account.go
Expand Up @@ -5,10 +5,11 @@ import (
"os"
"strconv"

"github.com/owncloud/ocis/accounts/pkg/flagset"

"github.com/asim/go-micro/plugins/client/grpc/v4"
tw "github.com/olekukonko/tablewriter"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/flagset"
accounts "github.com/owncloud/ocis/accounts/pkg/proto/v0"
"github.com/urfave/cli/v2"
)
Expand Down
3 changes: 2 additions & 1 deletion accounts/pkg/command/list_accounts.go
Expand Up @@ -5,10 +5,11 @@ import (
"os"
"strconv"

"github.com/owncloud/ocis/accounts/pkg/flagset"

"github.com/asim/go-micro/plugins/client/grpc/v4"
tw "github.com/olekukonko/tablewriter"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/flagset"
accounts "github.com/owncloud/ocis/accounts/pkg/proto/v0"
"github.com/urfave/cli/v2"
)
Expand Down
3 changes: 2 additions & 1 deletion accounts/pkg/command/remove_account.go
Expand Up @@ -4,9 +4,10 @@ import (
"fmt"
"os"

"github.com/owncloud/ocis/accounts/pkg/flagset"

"github.com/asim/go-micro/plugins/client/grpc/v4"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/flagset"
accounts "github.com/owncloud/ocis/accounts/pkg/proto/v0"
"github.com/urfave/cli/v2"
)
Expand Down
85 changes: 20 additions & 65 deletions accounts/pkg/command/root.go
Expand Up @@ -3,41 +3,29 @@ package command
import (
"context"
"os"
"strings"

"github.com/owncloud/ocis/ocis-pkg/shared"

"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/flagset"
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/ocis-pkg/sync"
"github.com/owncloud/ocis/ocis-pkg/version"
"github.com/spf13/viper"
"github.com/thejerf/suture/v4"
"github.com/urfave/cli/v2"
)

var (
defaultConfigPaths = []string{"/etc/ocis", "$HOME/.ocis", "./config"}
defaultFilename = "accounts"
)

// Execute is the entry point for the ocis-accounts command.
func Execute(cfg *config.Config) error {
app := &cli.App{
Name: "ocis-accounts",
Version: version.String,
Usage: "Provide accounts and groups for oCIS",
Compiled: version.Compiled(),

Authors: []*cli.Author{
{
Name: "ownCloud GmbH",
Email: "support@owncloud.com",
},
},

Flags: flagset.RootWithConfig(cfg),

Before: func(c *cli.Context) error {
cfg.Server.Version = version.String
return ParseConfig(c, cfg)
Expand Down Expand Up @@ -68,60 +56,30 @@ func Execute(cfg *config.Config) error {
return app.Run(os.Args)
}

// NewLogger initializes a service-specific logger instance.
func NewLogger(cfg *config.Config) log.Logger {
return log.NewLogger(
log.Name("accounts"),
log.Level(cfg.Log.Level),
log.Pretty(cfg.Log.Pretty),
log.Color(cfg.Log.Color),
log.File(cfg.Log.File),
)
}

// ParseConfig loads accounts configuration from Viper known paths.
// ParseConfig loads accounts configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
sync.ParsingViperConfig.Lock()
defer sync.ParsingViperConfig.Unlock()
logger := NewLogger(cfg)

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

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

for _, v := range defaultConfigPaths {
viper.AddConfigPath(v)
}
conf, err := ociscfg.BindSourcesToStructs("accounts", cfg)
if err != nil {
return err
}

if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
logger.Debug().
Msg("no config found on preconfigured location")
case viper.UnsupportedConfigError:
logger.Fatal().
Err(err).
Msg("Unsupported config type")
default:
logger.Fatal().
Err(err).
Msg("Failed to read config")
// provide with defaults for shared logging, since we need a valid destination address for BindEnv.
if cfg.Log == nil && cfg.Commons != nil && cfg.Commons.Log != nil {
cfg.Log = &shared.Log{
Level: cfg.Commons.Log.Level,
Pretty: cfg.Commons.Log.Pretty,
Color: cfg.Commons.Log.Color,
File: cfg.Commons.Log.File,
}
} else if cfg.Log == nil && cfg.Commons == nil {
cfg.Log = &shared.Log{}
}

if err := viper.Unmarshal(&cfg); err != nil {
logger.Fatal().
Err(err).
Msg("Failed to parse config")
}
// load all env variables relevant to the config in the current context.
conf.LoadOSEnv(config.GetEnv(cfg), false)

return nil
bindings := config.StructMappings(cfg)
return ociscfg.BindEnv(conf, bindings)
}

// SutureService allows for the accounts command to be embedded and supervised by a suture supervisor tree.
Expand All @@ -131,10 +89,7 @@ type SutureService struct {

// NewSutureService creates a new accounts.SutureService
func NewSutureService(cfg *ociscfg.Config) suture.Service {
if cfg.Mode == 0 {
cfg.Accounts.Supervised = true
}
cfg.Accounts.Log.File = cfg.Log.File
cfg.Accounts.Commons = cfg.Commons
return SutureService{
cfg: cfg.Accounts,
}
Expand Down
24 changes: 6 additions & 18 deletions accounts/pkg/command/server.go
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"strings"

"github.com/owncloud/ocis/ocis-pkg/log"

"github.com/owncloud/ocis/ocis-pkg/sync"

"github.com/oklog/run"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/flagset"
"github.com/owncloud/ocis/accounts/pkg/metrics"
"github.com/owncloud/ocis/accounts/pkg/server/grpc"
"github.com/owncloud/ocis/accounts/pkg/server/http"
Expand All @@ -23,34 +24,21 @@ func Server(cfg *config.Config) *cli.Command {
Name: "server",
Usage: "Start ocis accounts service",
Description: "uses an LDAP server as the storage backend",
Flags: flagset.ServerWithConfig(cfg),
Before: func(ctx *cli.Context) error {
logger := NewLogger(cfg)
if cfg.HTTP.Root != "/" {
cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/")
}

cfg.Repo.Backend = strings.ToLower(cfg.Repo.Backend)

// When running on single binary mode the before hook from the root command won't get called. We manually
// call this before hook from ocis command, so the configuration can be loaded.
if !cfg.Supervised {
return ParseConfig(ctx, cfg)
}
if origins := ctx.StringSlice("cors-allowed-origins"); len(origins) != 0 {
cfg.HTTP.CORS.AllowedOrigins = origins
}
if methods := ctx.StringSlice("cors-allowed-methods"); len(methods) != 0 {
cfg.HTTP.CORS.AllowedMethods = methods
}
if headers := ctx.StringSlice("cors-allowed-headers"); len(headers) != 0 {
cfg.HTTP.CORS.AllowedOrigins = headers
if err := ParseConfig(ctx, cfg); err != nil {
return err
}
logger.Debug().Str("service", "accounts").Msg("ignoring config file parsing when running supervised")

return nil
},
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
logger := log.LoggerFromConfig("accounts", *cfg.Log)
err := tracing.Configure(cfg)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions accounts/pkg/command/update_account.go
Expand Up @@ -4,9 +4,10 @@ import (
"errors"
"fmt"

"github.com/owncloud/ocis/accounts/pkg/flagset"

"github.com/asim/go-micro/plugins/client/grpc/v4"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/flagset"
accounts "github.com/owncloud/ocis/accounts/pkg/proto/v0"
"github.com/urfave/cli/v2"
"google.golang.org/genproto/protobuf/field_mask"
Expand All @@ -24,7 +25,6 @@ func UpdateAccount(cfg *config.Config) *cli.Command {
Flags: flagset.UpdateAccountWithConfig(cfg, a),
Before: func(c *cli.Context) error {
if len(c.StringSlice("password_policies")) > 0 {
// StringSliceFlag doesn't support Destination
a.PasswordProfile.PasswordPolicies = c.StringSlice("password_policies")
}

Expand Down
2 changes: 0 additions & 2 deletions accounts/pkg/command/version.go
Expand Up @@ -8,7 +8,6 @@ import (

tw "github.com/olekukonko/tablewriter"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/flagset"
"github.com/urfave/cli/v2"
)

Expand All @@ -17,7 +16,6 @@ func PrintVersion(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "version",
Usage: "Print the versions of the running instances",
Flags: flagset.ListAccountsWithConfig(cfg),
Action: func(c *cli.Context) error {
reg := registry.GetRegistry()
services, err := reg.GetService(cfg.GRPC.Namespace + "." + cfg.Server.Name)
Expand Down

0 comments on commit 1befac2

Please sign in to comment.