coredhcp.go.template 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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"
  11. "github.com/coredhcp/coredhcp/config"
  12. "github.com/coredhcp/coredhcp/logger"
  13. "github.com/coredhcp/coredhcp/plugins"
  14. {{range $plugin := .}}
  15. {{if eq $plugin "github.com/coredhcp/coredhcp/plugins/range"}}rangepl "{{$plugin}}"{{else}}"{{$plugin}}"{{end}}
  16. {{end}}
  17. "github.com/sirupsen/logrus"
  18. )
  19. var (
  20. flagLogFile = flag.String("logfile", "", "Name of the log file to append to. Default: stdout/stderr only")
  21. flagLogNoStdout = flag.Bool("nostdout", false, "Disable logging to stdout/stderr")
  22. flagLogLevel = flag.String("loglevel", "info", fmt.Sprintf("Log level. One of %v", getLogLevels()))
  23. )
  24. var logLevels = map[string]func(*logrus.Logger){
  25. "none": func(l *logrus.Logger) { l.SetOutput(ioutil.Discard) },
  26. "debug": func(l *logrus.Logger) { l.SetLevel(logrus.DebugLevel) },
  27. "info": func(l *logrus.Logger) { l.SetLevel(logrus.InfoLevel) },
  28. "warning": func(l *logrus.Logger) { l.SetLevel(logrus.WarnLevel) },
  29. "error": func(l *logrus.Logger) { l.SetLevel(logrus.ErrorLevel) },
  30. "fatal": func(l *logrus.Logger) { l.SetLevel(logrus.FatalLevel) },
  31. }
  32. func getLogLevels() []string {
  33. var levels []string
  34. for k := range logLevels {
  35. levels = append(levels, k)
  36. }
  37. return levels
  38. }
  39. var desiredPlugins = []*plugins.Plugin{
  40. {{range $plugin := .}}
  41. {{$import := importname $plugin}}&{{if eq $import "range"}}rangepl{{else}}{{$import}}{{end}}.Plugin,
  42. {{end}}
  43. }
  44. func main() {
  45. flag.Parse()
  46. log := logger.GetLogger("main")
  47. fn, ok := logLevels[*flagLogLevel]
  48. if !ok {
  49. log.Fatalf("Invalid log level '%s'. Valid log levels are %v", *flagLogLevel, getLogLevels())
  50. }
  51. fn(log.Logger)
  52. log.Infof("Setting log level to '%s'", *flagLogLevel)
  53. if *flagLogFile != "" {
  54. log.Infof("Logging to file %s", *flagLogFile)
  55. logger.WithFile(log, *flagLogFile)
  56. }
  57. if *flagLogNoStdout {
  58. log.Infof("Disabling logging to stdout/stderr")
  59. logger.WithNoStdOutErr(log)
  60. }
  61. config, err := config.Load()
  62. if err != nil {
  63. log.Fatal(err)
  64. }
  65. // register plugins
  66. for _, plugin := range desiredPlugins {
  67. if err := plugins.RegisterPlugin(plugin); err != nil {
  68. log.Fatalf("Failed to register plugin '%s': %v", plugin.Name, err)
  69. }
  70. }
  71. // start server
  72. server := coredhcp.NewServer(config)
  73. if err := server.Start(); err != nil {
  74. log.Fatal(err)
  75. }
  76. if err := server.Wait(); err != nil {
  77. log.Error(err)
  78. }
  79. time.Sleep(time.Second)
  80. }