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

unify configuration and commands #2818

Merged
merged 38 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6206fe2
add missing commands and unify service / namespace options
wkloucek Nov 24, 2021
af6cbda
add accounts log level
wkloucek Dec 16, 2021
a99f20f
fix traefik basic auth defaul
wkloucek Dec 16, 2021
2f4f3be
fix ocs test
wkloucek Dec 16, 2021
f7b949a
remove service names
wkloucek Dec 16, 2021
6bf4671
switch accounts to struct tag based env config
wkloucek Dec 16, 2021
a35c472
fix accounts test
wkloucek Dec 16, 2021
d0030ab
switch ocs to struct tag based env config
wkloucek Dec 16, 2021
4067ae9
switch glauth to struct tag based env config
wkloucek Dec 16, 2021
30656c5
switch graph to struct tag based env config
wkloucek Dec 16, 2021
ee57288
switch all other services to struct tag based env config
wkloucek Dec 17, 2021
9aae539
split **/pkg/config/config.go up to multiple files
wkloucek Dec 17, 2021
a77c8ac
add version to accounts
wkloucek Dec 17, 2021
7abcf96
revert storage, remove tracing.service and bring back common
wkloucek Dec 17, 2021
2bcd4f5
directly pass env to config
wkloucek Dec 17, 2021
b70cbd2
move accounts config sanitazing into config parsing
wkloucek Dec 17, 2021
d5e8ac0
fix unit tests
wkloucek Dec 17, 2021
0f49197
bring back missing commands
wkloucek Dec 17, 2021
dcc01ca
gix graph url
wkloucek Dec 17, 2021
d550409
fix some TODOs
wkloucek Dec 17, 2021
9c1cf9b
don't expose not used config via env
wkloucek Dec 17, 2021
e5e8c39
resolve thumbnails TODOs
wkloucek Dec 17, 2021
fe1672a
migrate ocis-pkg to envdecode
wkloucek Dec 17, 2021
e3bfb66
maintain envdecode inside ocis-pkg
wkloucek Dec 21, 2021
c9ee779
expose proxy debug port only on localhost
wkloucek Jan 3, 2022
7ffe93b
remove supervised flag from configs
wkloucek Jan 3, 2022
ea5dd75
satisfy linters
wkloucek Jan 3, 2022
6d0b754
improve envdecode error handling
wkloucek Jan 3, 2022
eee0d0c
remove build flags, add debug server to accounts
wkloucek Jan 3, 2022
e0656da
simplify commands and version handling
wkloucek Jan 3, 2022
55bf175
move config parsing in separate package for each service
wkloucek Jan 3, 2022
3d4df57
remove version from service config
wkloucek Jan 3, 2022
e37eff7
add missing commons in supervised mode
wkloucek Jan 3, 2022
11466c5
ensure, that each config is only parsed once
wkloucek Jan 3, 2022
b3dde1f
remove reference to flags
wkloucek Jan 4, 2022
f917691
add changelog
wkloucek Jan 4, 2022
d9744eb
improve command description
wkloucek Jan 7, 2022
62dbd89
switch to http.StatusOK instead of 200
wkloucek Jan 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
11 changes: 6 additions & 5 deletions accounts/pkg/command/add_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ func AddAccount(cfg *config.Config) *cli.Command {
PasswordProfile: &accounts.PasswordProfile{},
}
return &cli.Command{
Name: "add",
Usage: "Create a new account",
Aliases: []string{"create", "a"},
Flags: flagset.AddAccountWithConfig(cfg, a),
Name: "add",
Usage: "create a new account",
Category: "account management",
Aliases: []string{"create", "a"},
Flags: flagset.AddAccountWithConfig(cfg, a),
Before: func(c *cli.Context) error {
// Write value of username to the flags beneath, as preferred name
// and on-premises-sam-account-name is probably confusing for users.
Expand All @@ -41,7 +42,7 @@ func AddAccount(cfg *config.Config) *cli.Command {

},
Action: func(c *cli.Context) error {
accSvcID := cfg.GRPC.Namespace + "." + cfg.Server.Name
accSvcID := cfg.GRPC.Namespace + "." + cfg.Service.Name
accSvc := accounts.NewAccountsService(accSvcID, grpc.NewClient())
_, err := accSvc.CreateAccount(c.Context, &accounts.CreateAccountRequest{
Account: a,
Expand Down
53 changes: 53 additions & 0 deletions accounts/pkg/command/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package command

import (
"fmt"
"net/http"

"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/config/parser"
"github.com/owncloud/ocis/accounts/pkg/logging"
"github.com/urfave/cli/v2"
)

// Health is the entrypoint for the health command.
func Health(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "health",
Usage: "check health status",
Category: "info",
Before: func(c *cli.Context) error {
return parser.ParseConfig(cfg)
},
Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)

resp, err := http.Get(
fmt.Sprintf(
"http://%s/healthz",
cfg.Debug.Addr,
),
)

if err != nil {
logger.Fatal().
Err(err).
Msg("Failed to request health check")
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
logger.Fatal().
Int("code", resp.StatusCode).
Msg("Health seems to be in bad state")
}

logger.Debug().
Int("code", resp.StatusCode).
Msg("Health got a good state")

return nil
},
}
}
5 changes: 3 additions & 2 deletions accounts/pkg/command/inspect_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
func InspectAccount(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "inspect",
Usage: "Show detailed data on an existing account",
Usage: "show detailed data on an existing account",
Category: "account management",
ArgsUsage: "id",
Flags: flagset.InspectAccountWithConfig(cfg),
Action: func(c *cli.Context) error {
accServiceID := cfg.GRPC.Namespace + "." + cfg.Server.Name
accServiceID := cfg.GRPC.Namespace + "." + cfg.Service.Name
if c.NArg() != 1 {
fmt.Println("Please provide a user-id")
os.Exit(1)
Expand Down
11 changes: 6 additions & 5 deletions accounts/pkg/command/list_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import (
// ListAccounts command lists all accounts
func ListAccounts(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "list",
Usage: "List existing accounts",
Aliases: []string{"ls"},
Flags: flagset.ListAccountsWithConfig(cfg),
Name: "list",
Usage: "list existing accounts",
Category: "account management",
Aliases: []string{"ls"},
Flags: flagset.ListAccountsWithConfig(cfg),
Action: func(c *cli.Context) error {
accSvcID := cfg.GRPC.Namespace + "." + cfg.Server.Name
accSvcID := cfg.GRPC.Namespace + "." + cfg.Service.Name
accSvc := accounts.NewAccountsService(accSvcID, grpc.NewClient())
resp, err := accSvc.ListAccounts(c.Context, &accounts.ListAccountsRequest{})

Expand Down
7 changes: 4 additions & 3 deletions accounts/pkg/command/rebuild_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
// RebuildIndex rebuilds the entire configured index.
func RebuildIndex(cdf *config.Config) *cli.Command {
return &cli.Command{
Name: "rebuildIndex",
Usage: "Rebuilds the service's index, i.e. deleting and then re-adding all existing documents",
Aliases: []string{"rebuild", "ri"},
Name: "rebuildIndex",
Usage: "rebuilds the service's index, i.e. deleting and then re-adding all existing documents",
Category: "account management",
Aliases: []string{"rebuild", "ri"},
Action: func(ctx *cli.Context) error {
idxSvcID := "com.owncloud.api.accounts"
idxSvc := index.NewIndexService(idxSvcID, grpc.NewClient())
Expand Down
5 changes: 3 additions & 2 deletions accounts/pkg/command/remove_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import (
func RemoveAccount(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "remove",
Usage: "Removes an existing account",
Usage: "removes an existing account",
Category: "account management",
ArgsUsage: "id",
Aliases: []string{"rm"},
Flags: flagset.RemoveAccountWithConfig(cfg),
Action: func(c *cli.Context) error {
accServiceID := cfg.GRPC.Namespace + "." + cfg.Server.Name
accServiceID := cfg.GRPC.Namespace + "." + cfg.Service.Name
if c.NArg() != 1 {
fmt.Println("Please provide a user-id")
os.Exit(1)
Expand Down
83 changes: 24 additions & 59 deletions accounts/pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,49 @@ import (
"context"
"os"

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

"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/ocis-pkg/clihelper"
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
"github.com/owncloud/ocis/ocis-pkg/version"
"github.com/thejerf/suture/v4"
"github.com/urfave/cli/v2"
)

// GetCommands provides all commands for this service
func GetCommands(cfg *config.Config) cli.Commands {
return []*cli.Command{
// start this service
Server(cfg),

// interaction with this service
AddAccount(cfg),
UpdateAccount(cfg),
ListAccounts(cfg),
InspectAccount(cfg),
RemoveAccount(cfg),
RebuildIndex(cfg),

// infos about this service
Health(cfg),
Version(cfg),
}
}

// Execute is the entry point for the ocis-accounts command.
func Execute(cfg *config.Config) error {
app := &cli.App{
app := clihelper.DefaultApp(&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: "[email protected]",
},
},
Before: func(c *cli.Context) error {
cfg.Server.Version = version.String
return ParseConfig(c, cfg)
},

Commands: []*cli.Command{
Server(cfg),
AddAccount(cfg),
UpdateAccount(cfg),
ListAccounts(cfg),
InspectAccount(cfg),
RemoveAccount(cfg),
PrintVersion(cfg),
RebuildIndex(cfg),
},
}
Commands: GetCommands(cfg),
})

cli.HelpFlag = &cli.BoolFlag{
Name: "help,h",
Usage: "Show the help",
}

cli.VersionFlag = &cli.BoolFlag{
Name: "version,v",
Usage: "Print the version",
}

return app.Run(os.Args)
}

// ParseConfig loads accounts configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
conf, err := ociscfg.BindSourcesToStructs("accounts", cfg)
if err != nil {
return err
}

// 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{}
}

// load all env variables relevant to the config in the current context.
conf.LoadOSEnv(config.GetEnv(cfg), false)

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.
type SutureService struct {
cfg *config.Config
Expand Down
48 changes: 26 additions & 22 deletions accounts/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,33 @@ package command

import (
"context"
"strings"

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

"github.com/oklog/run"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/config/parser"
"github.com/owncloud/ocis/accounts/pkg/logging"
"github.com/owncloud/ocis/accounts/pkg/metrics"
"github.com/owncloud/ocis/accounts/pkg/server/debug"
"github.com/owncloud/ocis/accounts/pkg/server/grpc"
"github.com/owncloud/ocis/accounts/pkg/server/http"
svc "github.com/owncloud/ocis/accounts/pkg/service/v0"
"github.com/owncloud/ocis/accounts/pkg/tracing"
"github.com/owncloud/ocis/ocis-pkg/version"
"github.com/urfave/cli/v2"
)

// Server is the entry point for the server command.
func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "server",
Usage: "Start ocis accounts service",
Description: "uses an LDAP server as the storage backend",
Before: func(ctx *cli.Context) error {
if cfg.HTTP.Root != "/" {
cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/")
}

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

if err := ParseConfig(ctx, cfg); err != nil {
return err
}

return nil
Name: "server",
Usage: fmt.Sprintf("start %s extension without runtime (unsupervised mode)", cfg.Service.Name),
Category: "server",
Before: func(c *cli.Context) error {
return parser.ParseConfig(cfg)
},
Action: func(c *cli.Context) error {
logger := log.LoggerFromConfig("accounts", *cfg.Log)
logger := logging.Configure(cfg.Service.Name, cfg.Log)
err := tracing.Configure(cfg)
if err != nil {
return err
Expand All @@ -47,7 +39,7 @@ func Server(cfg *config.Config) *cli.Command {

defer cancel()

mtrcs.BuildInfo.WithLabelValues(cfg.Server.Version).Set(1)
mtrcs.BuildInfo.WithLabelValues(version.String).Set(1)

handler, err := svc.New(svc.Logger(logger), svc.Config(cfg))
if err != nil {
Expand All @@ -58,7 +50,7 @@ func Server(cfg *config.Config) *cli.Command {
httpServer := http.Server(
http.Config(cfg),
http.Logger(logger),
http.Name(cfg.Server.Name),
http.Name(cfg.Service.Name),
http.Context(ctx),
http.Metrics(mtrcs),
http.Handler(handler),
Expand All @@ -72,7 +64,7 @@ func Server(cfg *config.Config) *cli.Command {
grpcServer := grpc.Server(
grpc.Config(cfg),
grpc.Logger(logger),
grpc.Name(cfg.Server.Name),
grpc.Name(cfg.Service.Name),
grpc.Context(ctx),
grpc.Metrics(mtrcs),
grpc.Handler(handler),
Expand All @@ -83,6 +75,18 @@ func Server(cfg *config.Config) *cli.Command {
cancel()
})

// prepare a debug server and add it to the group run.
debugServer, err := debug.Server(debug.Logger(logger), debug.Context(ctx), debug.Config(cfg))
if err != nil {
logger.Error().Err(err).Str("server", "debug").Msg("Failed to initialize server")
return err
}

gr.Add(debugServer.ListenAndServe, func(_ error) {
_ = debugServer.Shutdown(ctx)
cancel()
})

return gr.Run()
},
}
Expand Down
3 changes: 2 additions & 1 deletion accounts/pkg/command/update_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func UpdateAccount(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "update",
Usage: "Make changes to an existing account",
Category: "account management",
ArgsUsage: "id",
Flags: flagset.UpdateAccountWithConfig(cfg, a),
Before: func(c *cli.Context) error {
Expand All @@ -40,7 +41,7 @@ func UpdateAccount(cfg *config.Config) *cli.Command {
},
Action: func(c *cli.Context) error {
a.Id = c.Args().First()
accSvcID := cfg.GRPC.Namespace + "." + cfg.Server.Name
accSvcID := cfg.GRPC.Namespace + "." + cfg.Service.Name
accSvc := accounts.NewAccountsService(accSvcID, grpc.NewClient())
_, err := accSvc.UpdateAccount(c.Context, &accounts.UpdateAccountRequest{
Account: a,
Expand Down
Loading