coredhcp.go.template 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2018-present the CoreDHCP Authors. All rights reserved
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. package main
  5. import (
  6. "flag"
  7. "fmt"
  8. "io/ioutil"
  9. "time"
  10. "github.com/coredhcp/coredhcp/config"
  11. "github.com/coredhcp/coredhcp/logger"
  12. "github.com/coredhcp/coredhcp/server"
  13. "github.com/coredhcp/coredhcp/plugins"
  14. {{- range $plugin := .}}
  15. {{- /* We import all plugins as pl_<pluginname> to avoid conflicts with reserved keywords */}}
  16. {{importname $plugin}} "{{$plugin}}"
  17. {{- end}}
  18. "github.com/sirupsen/logrus"
  19. )
  20. var (
  21. flagLogFile = flag.String("logfile", "", "Name of the log file to append to. Default: stdout/stderr only")
  22. flagLogNoStdout = flag.Bool("nostdout", false, "Disable logging to stdout/stderr")
  23. flagLogLevel = flag.String("loglevel", "info", fmt.Sprintf("Log level. One of %v", getLogLevels()))
  24. )
  25. var logLevels = map[string]func(*logrus.Logger){
  26. "none": func(l *logrus.Logger) { l.SetOutput(ioutil.Discard) },
  27. "debug": func(l *logrus.Logger) { l.SetLevel(logrus.DebugLevel) },
  28. "info": func(l *logrus.Logger) { l.SetLevel(logrus.InfoLevel) },
  29. "warning": func(l *logrus.Logger) { l.SetLevel(logrus.WarnLevel) },
  30. "error": func(l *logrus.Logger) { l.SetLevel(logrus.ErrorLevel) },
  31. "fatal": func(l *logrus.Logger) { l.SetLevel(logrus.FatalLevel) },
  32. }
  33. func getLogLevels() []string {
  34. var levels []string
  35. for k := range logLevels {
  36. levels = append(levels, k)
  37. }
  38. return levels
  39. }
  40. var desiredPlugins = []*plugins.Plugin{
  41. {{- range $plugin := .}}
  42. &{{importname $plugin}}.Plugin,
  43. {{- end}}
  44. }
  45. func main() {
  46. flag.Parse()
  47. log := logger.GetLogger("main")
  48. fn, ok := logLevels[*flagLogLevel]
  49. if !ok {
  50. log.Fatalf("Invalid log level '%s'. Valid log levels are %v", *flagLogLevel, getLogLevels())
  51. }
  52. fn(log.Logger)
  53. log.Infof("Setting log level to '%s'", *flagLogLevel)
  54. if *flagLogFile != "" {
  55. log.Infof("Logging to file %s", *flagLogFile)
  56. logger.WithFile(log, *flagLogFile)
  57. }
  58. if *flagLogNoStdout {
  59. log.Infof("Disabling logging to stdout/stderr")
  60. logger.WithNoStdOutErr(log)
  61. }
  62. config, err := config.Load()
  63. if err != nil {
  64. log.Fatalf("Failed to load configuration: %v", err)
  65. }
  66. // register plugins
  67. for _, plugin := range desiredPlugins {
  68. if err := plugins.RegisterPlugin(plugin); err != nil {
  69. log.Fatalf("Failed to register plugin '%s': %v", plugin.Name, err)
  70. }
  71. }
  72. // start server
  73. srv, err := server.Start(config)
  74. if err != nil {
  75. log.Fatal(err)
  76. }
  77. if err := srv.Wait(); err != nil {
  78. log.Print(err)
  79. }
  80. time.Sleep(time.Second)
  81. }