Files
ocis/storage/pkg/command/sharing.go
T

215 lines
5.4 KiB
Go
Raw Normal View History

2019-12-17 17:04:18 +01:00
package command
import (
"context"
2021-03-04 15:28:21 +01:00
"flag"
2019-12-17 17:04:18 +01:00
"os"
"path"
2021-03-04 15:26:03 +00:00
"path/filepath"
2019-12-17 17:04:18 +01:00
"github.com/owncloud/ocis/ocis-pkg/sync"
2019-12-17 17:04:18 +01:00
"github.com/cs3org/reva/cmd/revad/runtime"
"github.com/gofrs/uuid"
2020-01-28 14:41:18 +01:00
"github.com/micro/cli/v2"
2019-12-17 17:04:18 +01:00
"github.com/oklog/run"
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
2020-10-05 20:39:29 +02:00
"github.com/owncloud/ocis/storage/pkg/config"
"github.com/owncloud/ocis/storage/pkg/flagset"
"github.com/owncloud/ocis/storage/pkg/server/debug"
2021-03-11 15:50:01 +01:00
"github.com/thejerf/suture/v4"
2019-12-17 17:04:18 +01:00
)
2020-01-15 17:02:56 +01:00
// Sharing is the entrypoint for the sharing command.
2020-01-28 14:41:18 +01:00
func Sharing(cfg *config.Config) *cli.Command {
return &cli.Command{
2020-01-15 17:02:56 +01:00
Name: "sharing",
2020-10-05 20:39:29 +02:00
Usage: "Start sharing service",
2020-01-15 17:02:56 +01:00
Flags: flagset.SharingWithConfig(cfg),
2020-01-28 14:41:18 +01:00
Before: func(c *cli.Context) error {
cfg.Reva.Sharing.Services = c.StringSlice("service")
return nil
},
2019-12-17 17:04:18 +01:00
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
if cfg.Tracing.Enabled {
switch t := cfg.Tracing.Type; t {
case "agent":
logger.Error().
Str("type", t).
Msg("Reva only supports the jaeger tracing backend")
case "jaeger":
logger.Info().
Str("type", t).
2020-10-05 20:39:29 +02:00
Msg("configuring storage to use the jaeger tracing backend")
2019-12-17 17:04:18 +01:00
case "zipkin":
logger.Error().
Str("type", t).
Msg("Reva only supports the jaeger tracing backend")
default:
logger.Warn().
Str("type", t).
Msg("Unknown tracing backend")
}
} else {
logger.Debug().
Msg("Tracing is not enabled")
}
var (
gr = run.Group{}
ctx, cancel = context.WithCancel(context.Background())
//metrics = metrics.New()
)
defer cancel()
2021-03-04 15:26:03 +00:00
// precreate folders
if cfg.Reva.Sharing.UserDriver == "json" && cfg.Reva.Sharing.UserJSONFile != "" {
2021-03-05 14:08:40 +01:00
if err := os.MkdirAll(filepath.Dir(cfg.Reva.Sharing.UserJSONFile), os.FileMode(0700)); err != nil {
2021-03-04 15:26:03 +00:00
return err
}
}
if cfg.Reva.Sharing.PublicDriver == "json" && cfg.Reva.Sharing.PublicJSONFile != "" {
2021-03-05 14:08:40 +01:00
if err := os.MkdirAll(filepath.Dir(cfg.Reva.Sharing.PublicJSONFile), os.FileMode(0700)); err != nil {
2021-03-04 15:26:03 +00:00
return err
}
}
2019-12-17 17:04:18 +01:00
{
uuid := uuid.Must(uuid.NewV4())
2020-01-15 17:02:56 +01:00
pidFile := path.Join(os.TempDir(), "revad-"+c.Command.Name+"-"+uuid.String()+".pid")
2019-12-17 17:04:18 +01:00
rcfg := map[string]interface{}{
"core": map[string]interface{}{
2020-10-06 16:33:32 +02:00
"max_cpus": cfg.Reva.Sharing.MaxCPUs,
2020-04-15 14:33:37 +02:00
"tracing_enabled": cfg.Tracing.Enabled,
"tracing_endpoint": cfg.Tracing.Endpoint,
"tracing_collector": cfg.Tracing.Collector,
2020-10-06 16:33:32 +02:00
"tracing_service_name": c.Command.Name,
2019-12-17 17:04:18 +01:00
},
"shared": map[string]interface{}{
"jwt_secret": cfg.Reva.JWTSecret,
},
2019-12-17 17:04:18 +01:00
"grpc": map[string]interface{}{
2020-10-06 16:33:32 +02:00
"network": cfg.Reva.Sharing.GRPCNetwork,
"address": cfg.Reva.Sharing.GRPCAddr,
2020-01-15 17:02:56 +01:00
// TODO build services dynamically
2019-12-17 17:04:18 +01:00
"services": map[string]interface{}{
2020-01-15 17:02:56 +01:00
"usershareprovider": map[string]interface{}{
2020-03-22 09:56:13 +01:00
"driver": cfg.Reva.Sharing.UserDriver,
2020-04-14 12:59:16 +02:00
"drivers": map[string]interface{}{
"json": map[string]interface{}{
"file": cfg.Reva.Sharing.UserJSONFile,
},
"sql": map[string]interface{}{
"db_username": cfg.Reva.Sharing.UserSQLUsername,
"db_password": cfg.Reva.Sharing.UserSQLPassword,
"db_host": cfg.Reva.Sharing.UserSQLHost,
"db_port": cfg.Reva.Sharing.UserSQLPort,
"db_name": cfg.Reva.Sharing.UserSQLName,
},
2020-04-14 12:59:16 +02:00
},
2020-01-15 17:02:56 +01:00
},
"publicshareprovider": map[string]interface{}{
2020-03-22 09:56:13 +01:00
"driver": cfg.Reva.Sharing.PublicDriver,
2020-12-12 20:24:51 +00:00
"drivers": map[string]interface{}{
"json": map[string]interface{}{
"file": cfg.Reva.Sharing.PublicJSONFile,
},
},
2019-12-17 17:04:18 +01:00
},
},
},
}
gr.Add(func() error {
runtime.RunWithOptions(
rcfg,
pidFile,
runtime.WithLogger(&logger.Logger),
)
2019-12-17 17:04:18 +01:00
return nil
}, func(_ error) {
logger.Info().
2020-01-15 17:02:56 +01:00
Str("server", c.Command.Name).
2019-12-17 17:04:18 +01:00
Msg("Shutting down server")
cancel()
})
}
{
server, err := debug.Server(
2020-01-15 17:02:56 +01:00
debug.Name(c.Command.Name+"-debug"),
debug.Addr(cfg.Reva.Sharing.DebugAddr),
2019-12-17 17:04:18 +01:00
debug.Logger(logger),
debug.Context(ctx),
debug.Config(cfg),
)
if err != nil {
logger.Info().
Err(err).
2020-01-15 17:02:56 +01:00
Str("server", c.Command.Name+"-debug").
2019-12-17 17:04:18 +01:00
Msg("Failed to initialize server")
return err
}
gr.Add(server.ListenAndServe, func(_ error) {
cancel()
2019-12-17 17:04:18 +01:00
})
}
if !cfg.Reva.StorageMetadata.Supervised {
sync.Trap(&gr, cancel)
2019-12-17 17:04:18 +01:00
}
return gr.Run()
},
}
}
2021-03-04 15:28:21 +01:00
// SharingSutureService allows for the storage-sharing command to be embedded and supervised by a suture supervisor tree.
type SharingSutureService struct {
2021-03-11 15:50:01 +01:00
cfg *config.Config
2021-03-04 15:28:21 +01:00
}
// NewSharingSutureService creates a new store.SharingSutureService
2021-03-11 15:50:01 +01:00
func NewSharing(cfg *ociscfg.Config) suture.Service {
if cfg.Mode == 0 {
cfg.Storage.Reva.Sharing.Supervised = true
}
2021-03-04 15:28:21 +01:00
return SharingSutureService{
2021-03-11 15:50:01 +01:00
cfg: cfg.Storage,
2021-03-04 15:28:21 +01:00
}
}
2021-03-11 15:50:01 +01:00
func (s SharingSutureService) Serve(ctx context.Context) error {
s.cfg.Reva.Sharing.Context = ctx
2021-03-04 15:28:21 +01:00
f := &flag.FlagSet{}
for k := range Sharing(s.cfg).Flags {
if err := Sharing(s.cfg).Flags[k].Apply(f); err != nil {
2021-03-11 15:50:01 +01:00
return err
2021-03-04 15:28:21 +01:00
}
}
2021-03-11 15:50:01 +01:00
cliCtx := cli.NewContext(nil, f, nil)
2021-03-04 15:28:21 +01:00
if Sharing(s.cfg).Before != nil {
2021-03-11 15:50:01 +01:00
if err := Sharing(s.cfg).Before(cliCtx); err != nil {
return err
2021-03-04 15:28:21 +01:00
}
}
2021-03-11 15:50:01 +01:00
if err := Sharing(s.cfg).Action(cliCtx); err != nil {
return err
2021-03-04 15:28:21 +01:00
}
2021-03-11 15:50:01 +01:00
return nil
2021-03-04 15:28:21 +01:00
}