Skip to content

Commit

Permalink
New reva config (cs3org#4015)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 authored and Adriana Baldacchino committed Aug 3, 2023
1 parent d2c4174 commit 70b43c3
Show file tree
Hide file tree
Showing 55 changed files with 3,853 additions and 843 deletions.
11 changes: 11 additions & 0 deletions changelog/unreleased/new-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Enhancement: New configuration

Allow multiple driverts of the same service to be in the
same toml config. Add a `vars` section to contain common
parameters addressable using templates in the configuration
of the different drivers. Support templating to reference
values of other parameters in the configuration.
Assign random ports to services where the address is not
specified.

https://github.com/cs3org/reva/pull/4015
120 changes: 96 additions & 24 deletions cmd/revad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ package main
import (
"flag"
"fmt"
"io"
"io/fs"
"os"
"path"
"regexp"
"sync"
"syscall"

"github.com/cs3org/reva/cmd/revad/internal/config"
"github.com/cs3org/reva/cmd/revad/internal/grace"
"github.com/cs3org/reva/cmd/revad/pkg/config"
"github.com/cs3org/reva/cmd/revad/pkg/grace"
"github.com/cs3org/reva/cmd/revad/runtime"
"github.com/cs3org/reva/pkg/logger"
"github.com/cs3org/reva/pkg/sysinfo"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)

var (
Expand All @@ -41,13 +45,16 @@ var (
signalFlag = flag.String("s", "", "send signal to a master process: stop, quit, reload")
configFlag = flag.String("c", "/etc/revad/revad.toml", "set configuration file")
pidFlag = flag.String("p", "", "pid file. If empty defaults to a random file in the OS temporary directory")
logFlag = flag.String("log", "", "log messages with the given severity or above. One of: [trace, debug, info, warn, error, fatal, panic]")
dirFlag = flag.String("dev-dir", "", "runs any toml file in the specified directory. Intended for development use only")

// Compile time variables initialized with gcc flags.
gitCommit, buildDate, version, goVersion string
)

var (
revaProcs []*runtime.Reva
)

func main() {
flag.Parse()

Expand Down Expand Up @@ -126,15 +133,15 @@ func handleSignalFlag() {

// kill process with signal
if err := process.Signal(signal); err != nil {
fmt.Fprintf(os.Stderr, "error signaling process %d with signal %s\n", process.Pid, signal)
fmt.Fprintf(os.Stderr, "error signaling process %d with signal %s: %v\n", process.Pid, signal, err)
os.Exit(1)
}

os.Exit(0)
}
}

func getConfigs() ([]map[string]interface{}, error) {
func getConfigs() ([]*config.Config, error) {
var confs []string
// give priority to read from dev-dir
if *dirFlag != "" {
Expand Down Expand Up @@ -186,58 +193,123 @@ func getConfigsFromDir(dir string) (confs []string, err error) {
return
}

func readConfigs(files []string) ([]map[string]interface{}, error) {
confs := make([]map[string]interface{}, 0, len(files))
func readConfigs(files []string) ([]*config.Config, error) {
confs := make([]*config.Config, 0, len(files))
for _, conf := range files {
fd, err := os.Open(conf)
if err != nil {
return nil, err
}
defer fd.Close()

v, err := config.Read(fd)
c, err := config.Load(fd)
if err != nil {
return nil, err
}
confs = append(confs, v)
confs = append(confs, c)
}
return confs, nil
}

func runConfigs(confs []map[string]interface{}) {
func runConfigs(confs []*config.Config) {
pidfile := getPidfile()
if len(confs) == 1 {
runSingle(confs[0])
runSingle(confs[0], pidfile)
return
}

runMultiple(confs)
}

func runSingle(conf map[string]interface{}) {
if *pidFlag == "" {
*pidFlag = getPidfile()
}

runtime.Run(conf, *pidFlag, *logFlag)
func registerReva(r *runtime.Reva) {
revaProcs = append(revaProcs, r)
}

func getPidfile() string {
uuid := uuid.New().String()
name := fmt.Sprintf("revad-%s.pid", uuid)
func runSingle(conf *config.Config, pidfile string) {
log := initLogger(conf.Log)
reva, err := runtime.New(conf,
runtime.WithPidFile(pidfile),
runtime.WithLogger(log),
)
if err != nil {
abort(log, "error creating reva runtime: %v", err)
}
registerReva(reva)
if err := reva.Start(); err != nil {
abort(log, "error starting reva: %v", err)
}
}

return path.Join(os.TempDir(), name)
func abort(log *zerolog.Logger, format string, a ...any) {
log.Fatal().Msgf(format, a...)
}

func runMultiple(confs []map[string]interface{}) {
func runMultiple(confs []*config.Config) {
var wg sync.WaitGroup

for _, conf := range confs {
wg.Add(1)
pidfile := getPidfile()
go func(wg *sync.WaitGroup, conf map[string]interface{}) {
go func(wg *sync.WaitGroup, conf *config.Config) {
defer wg.Done()
runtime.Run(conf, pidfile, *logFlag)
runSingle(conf, pidfile)
}(&wg, conf)
}
wg.Wait()
os.Exit(0)
}

func getPidfile() string {
uuid := uuid.New().String()
name := fmt.Sprintf("revad-%s.pid", uuid)

return path.Join(os.TempDir(), name)
}

func initLogger(conf *config.Log) *zerolog.Logger {
log, err := newLogger(conf)
if err != nil {
fmt.Fprintf(os.Stderr, "error creating logger: %v", err)
os.Exit(1)
}
return log
}

func newLogger(conf *config.Log) (*zerolog.Logger, error) {
// TODO(labkode): use debug level rather than info as default until reaching a stable version.
// Helps having smaller development files.
if conf.Level == "" {
conf.Level = zerolog.DebugLevel.String()
}

var opts []logger.Option
opts = append(opts, logger.WithLevel(conf.Level))

w, err := getWriter(conf.Output)
if err != nil {
return nil, err
}

opts = append(opts, logger.WithWriter(w, logger.Mode(conf.Mode)))

l := logger.New(opts...)
sub := l.With().Int("pid", os.Getpid()).Logger()
return &sub, nil
}

func getWriter(out string) (io.Writer, error) {
if out == "stderr" || out == "" {
return os.Stderr, nil
}

if out == "stdout" {
return os.Stdout, nil
}

fd, err := os.OpenFile(out, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, errors.Wrap(err, "error creating log file: "+out)
}

return fd, nil
}
Loading

0 comments on commit 70b43c3

Please sign in to comment.