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

DAOS-14635 control: Remove agent default log path #13490

Merged
merged 7 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 6 additions & 8 deletions src/control/cmd/daos_agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"time"

"github.com/pkg/errors"
Expand All @@ -23,7 +23,6 @@ import (
const (
defaultConfigFile = "daos_agent.yml"
defaultRuntimeDir = "/var/run/daos_agent"
defaultLogFile = "/tmp/daos_agent.log"
)

type refreshMinutes time.Duration
Expand Down Expand Up @@ -73,20 +72,20 @@ type FabricInterfaceConfig struct {
// LoadConfig reads a config file and uses it to populate a Config.
func LoadConfig(cfgPath string) (*Config, error) {
if cfgPath == "" {
return nil, errors.New("no path supplied")
return nil, errors.New("no config path supplied")
}
data, err := ioutil.ReadFile(cfgPath)
data, err := os.ReadFile(cfgPath)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "reading config file")
}

cfg := DefaultConfig()
if err := yaml.UnmarshalStrict(data, cfg); err != nil {
return nil, err
return nil, errors.Wrapf(err, "parsing config: %s", cfgPath)
}

if !daos.SystemNameIsValid(cfg.SystemName) {
return nil, fmt.Errorf("invalid system name: %q", cfg.SystemName)
return nil, fmt.Errorf("invalid system name: %s", cfg.SystemName)
}

return cfg, nil
Expand All @@ -100,7 +99,6 @@ func DefaultConfig() *Config {
ControlPort: build.DefaultControlPort,
AccessPoints: []string{localServer},
RuntimeDir: defaultRuntimeDir,
LogFile: defaultLogFile,
LogLevel: common.DefaultControlLogLevel,
TransportConfig: security.DefaultAgentTransportConfig(),
}
Expand Down
141 changes: 76 additions & 65 deletions src/control/cmd/daos_agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,14 @@ func parseOpts(args []string, opts *cliOptions, invoker control.Invoker, log *lo
logCmd.SetLog(log)
}

if jsonCmd, ok := cmd.(cmdutil.JSONOutputter); ok && opts.JSON {
jsonCmd.EnableJSONOutput(os.Stdout, &wroteJSON)
// disable output on stdout other than JSON
log.ClearLevel(logging.LogLevelInfo)
}

if opts.Debug {
log.SetLevel(logging.LogLevelTrace)
}

if opts.JSONLogs {
log.WithJSONOutput()
if jsonCmd, ok := cmd.(cmdutil.JSONOutputter); ok && opts.JSON {
jsonCmd.EnableJSONOutput(os.Stdout, &wroteJSON)
// disable output on stdout other than JSON
log.ClearLevel(logging.LogLevelInfo)
}

switch cmd.(type) {
Expand All @@ -164,69 +160,15 @@ func parseOpts(args []string, opts *cliOptions, invoker control.Invoker, log *lo
}
}

cfg := DefaultConfig()
if cfgPath != "" {
var err error
if cfg, err = LoadConfig(cfgPath); err != nil {
return errors.WithMessage(err, "failed to load agent configuration")
}

// Command line debug option overrides log level in config file
if !opts.Debug {
log.WithLogLevel(logging.LogLevel(cfg.LogLevel))
}
log.Debugf("agent config loaded from %s", cfgPath)
cfg, err := processConfig(log, cmd, opts, cfgPath)
if err != nil {
return err
}

if suppCmd, ok := cmd.(supportAgentConfig); ok {
suppCmd.setSupportConf(cfgPath)
}

if opts.RuntimeDir != "" {
log.Debugf("Overriding socket path from config file with %s", opts.RuntimeDir)
cfg.RuntimeDir = opts.RuntimeDir
}

if opts.LogFile != "" {
log.Debugf("Overriding LogFile path from config file with %s", opts.LogFile)
cfg.LogFile = opts.LogFile
}

if opts.Insecure {
log.Debugf("Overriding AllowInsecure from config file with %t", opts.Insecure)
cfg.TransportConfig.AllowInsecure = true
}

if cfg.LogFile != "" {
f, err := common.AppendFile(cfg.LogFile)
if err != nil {
log.Errorf("Failure creating log file: %s", err)
return err
}
defer f.Close()

// Create an additional set of loggers which append everything
// to the specified file.
log.WithErrorLogger(logging.NewErrorLogger("agent", f)).
WithNoticeLogger(logging.NewNoticeLogger("agent", f)).
WithInfoLogger(logging.NewInfoLogger("agent", f)).
WithDebugLogger(logging.NewDebugLogger(f)).
WithTraceLogger(logging.NewTraceLogger(f))
}

if err := cfg.TransportConfig.PreLoadCertData(); err != nil {
return errors.Wrap(err, "Unable to load Certificate Data")
}

var err error
if cfg.AccessPoints, err = common.ParseHostList(cfg.AccessPoints, cfg.ControlPort); err != nil {
return errors.Wrap(err, "Failed to parse config access_points")
}

if cfgCmd, ok := cmd.(configSetter); ok {
cfgCmd.setConfig(cfg)
}

if ctlCmd, ok := cmd.(ctlInvoker); ok {
// Generate a control config based on the loaded agent config.
ctlCfg := control.DefaultConfig()
Expand All @@ -251,6 +193,75 @@ func parseOpts(args []string, opts *cliOptions, invoker control.Invoker, log *lo
return err
}

func processConfig(log *logging.LeveledLogger, cmd flags.Commander, opts *cliOptions, cfgPath string) (*Config, error) {
kjacque marked this conversation as resolved.
Show resolved Hide resolved
cfg := DefaultConfig()
if cfgPath != "" {
var err error
if cfg, err = LoadConfig(cfgPath); err != nil {
return nil, errors.Wrap(err, "failed to load agent configuration")
}
}

if opts.LogFile != "" {
log.Debugf("Overriding LogFile path from config file with %s", opts.LogFile)
cfg.LogFile = opts.LogFile
}

if opts.Debug {
cfg.LogLevel = common.ControlLogLevelTrace
}

if err := configureLogging(log, cmd, cfg, opts); err != nil {
return nil, err
}

if opts.RuntimeDir != "" {
log.Debugf("Overriding socket path from config file with %s", opts.RuntimeDir)
cfg.RuntimeDir = opts.RuntimeDir
}

if opts.Insecure {
log.Debugf("Overriding AllowInsecure from config file with %t", opts.Insecure)
cfg.TransportConfig.AllowInsecure = true
}

if err := cfg.TransportConfig.PreLoadCertData(); err != nil {
return nil, errors.Wrap(err, "Unable to load Certificate Data")
}

var err error
if cfg.AccessPoints, err = common.ParseHostList(cfg.AccessPoints, cfg.ControlPort); err != nil {
return nil, errors.Wrap(err, "Failed to parse config access_points")
}

if cfgCmd, ok := cmd.(configSetter); ok {
cfgCmd.setConfig(cfg)
}

if cfgPath != "" {
log.Infof("loaded agent config from path: %s", cfgPath)
}

return cfg, nil
}

func configureLogging(log *logging.LeveledLogger, cmd flags.Commander, cfg *Config, opts *cliOptions) error {
if logCmd, ok := cmd.(cmdutil.LogSetter); ok {
logCmd.SetLog(log)

logCfg := cmdutil.LogConfig{
LogFile: cfg.LogFile,
LogLevel: cfg.LogLevel,
JSON: opts.JSONLogs,
}
if err := cmdutil.ConfigureLogger(log, logCfg); err != nil {
return err
}
}

return nil
}

func main() {
var opts cliOptions
log := logging.NewCommandLineLogger()
Expand Down
68 changes: 5 additions & 63 deletions src/control/cmd/daos_server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
package main

import (
"os"

"github.com/pkg/errors"

"github.com/daos-stack/daos/src/control/common"
"github.com/daos-stack/daos/src/control/common/cmdutil"
"github.com/daos-stack/daos/src/control/logging"
"github.com/daos-stack/daos/src/control/server"
Expand Down Expand Up @@ -85,72 +82,17 @@ func (cmd *startCmd) setCLIOverrides() error {
}

func (cmd *startCmd) configureLogging() error {
log, ok := cmd.Logger.(*logging.LeveledLogger)
if !ok {
return errors.New("logger is not a LeveledLogger")
}

// Set log level mask for default logger from config,
// unless it was explicitly set to debug via CLI flag.
applyLogConfig := func() error {
switch logging.LogLevel(cmd.config.ControlLogMask) {
case logging.LogLevelTrace:
log.SetLevel(logging.LogLevelTrace)
cmd.Debugf("Switching control log level to TRACE")
case logging.LogLevelDebug:
log.SetLevel(logging.LogLevelDebug)
cmd.Debugf("Switching control log level to DEBUG")
case logging.LogLevelNotice:
log.SetLevel(logging.LogLevelNotice)
cmd.Debugf("Switching control log level to NOTICE")
case logging.LogLevelError:
cmd.Debugf("Switching control log level to ERROR")
log.SetLevel(logging.LogLevelError)
}

if cmd.config.ControlLogJSON {
cmd.Logger = log.WithJSONOutput()
}

return nil
}

hostname, err := os.Hostname()
if err != nil {
return err
}

for i, srv := range cmd.config.Engines {
if srv.LogFile == "" {
cmd.Errorf("no daos log file specified for server %d", i)
}
}

// Set log file for default logger if specified in config.
if cmd.config.ControlLogFile != "" {
f, err := common.AppendFile(cmd.config.ControlLogFile)
if err != nil {
return errors.WithMessage(err, "create log file")
}

cmd.Infof("%s logging to file %s",
os.Args[0], cmd.config.ControlLogFile)

// Create an additional set of loggers which append everything
// to the specified file.
cmd.Logger = log.
WithErrorLogger(logging.NewErrorLogger(hostname, f)).
WithNoticeLogger(logging.NewNoticeLogger(hostname, f)).
WithInfoLogger(logging.NewInfoLogger(hostname, f)).
WithDebugLogger(logging.NewDebugLogger(f)).
WithTraceLogger(logging.NewTraceLogger(f))

return applyLogConfig()
}

cmd.Info("no control log file specified; logging to stdout")

return applyLogConfig()
return cmdutil.ConfigureLogger(cmd.Logger, cmdutil.LogConfig{
LogFile: cmd.config.ControlLogFile,
LogLevel: cmd.config.ControlLogMask,
JSON: cmd.config.ControlLogJSON,
})
}

func (cmd *startCmd) Execute(args []string) error {
Expand Down
Loading