This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
126 lines (112 loc) · 3.76 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// This is a generated file, edits should be made in the corresponding source file
// And this file regenerated using `coredhcp-generator --from core-plugins.txt`
// nolint
package main
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/coredhcp/coredhcp/config"
"github.com/coredhcp/coredhcp/logger"
"github.com/coredhcp/coredhcp/server"
"github.com/coredhcp/coredhcp/plugins"
pl_dns "github.com/coredhcp/coredhcp/plugins/dns"
pl_file "github.com/coredhcp/coredhcp/plugins/file"
pl_leasetime "github.com/coredhcp/coredhcp/plugins/leasetime"
pl_nbp "github.com/coredhcp/coredhcp/plugins/nbp"
pl_netmask "github.com/coredhcp/coredhcp/plugins/netmask"
pl_prefix "github.com/coredhcp/coredhcp/plugins/prefix"
pl_range "github.com/coredhcp/coredhcp/plugins/range"
pl_router "github.com/coredhcp/coredhcp/plugins/router"
pl_searchdomains "github.com/coredhcp/coredhcp/plugins/searchdomains"
pl_serverid "github.com/coredhcp/coredhcp/plugins/serverid"
pl_sleep "github.com/coredhcp/coredhcp/plugins/sleep"
pl_staticroute "github.com/coredhcp/coredhcp/plugins/staticroute"
pl_hollowdhcp "go.hollow.sh/dhcpserver/pkg/plugin"
"go.hollow.sh/toolbox/version"
"github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
)
var (
flagLogFile = flag.StringP("logfile", "l", "", "Name of the log file to append to. Default: stdout/stderr only")
flagLogNoStdout = flag.BoolP("nostdout", "N", false, "Disable logging to stdout/stderr")
flagLogLevel = flag.StringP("loglevel", "L", "info", fmt.Sprintf("Log level. One of %v", getLogLevels()))
flagConfig = flag.StringP("conf", "c", "", "Use this configuration file instead of the default location")
flagPlugins = flag.BoolP("plugins", "P", false, "list plugins")
)
var logLevels = map[string]func(*logrus.Logger){
"none": func(l *logrus.Logger) { l.SetOutput(ioutil.Discard) },
"debug": func(l *logrus.Logger) { l.SetLevel(logrus.DebugLevel) },
"info": func(l *logrus.Logger) { l.SetLevel(logrus.InfoLevel) },
"warning": func(l *logrus.Logger) { l.SetLevel(logrus.WarnLevel) },
"error": func(l *logrus.Logger) { l.SetLevel(logrus.ErrorLevel) },
"fatal": func(l *logrus.Logger) { l.SetLevel(logrus.FatalLevel) },
}
func getLogLevels() []string {
var levels []string
for k := range logLevels {
levels = append(levels, k)
}
return levels
}
var desiredPlugins = []*plugins.Plugin{
&pl_dns.Plugin,
&pl_file.Plugin,
&pl_leasetime.Plugin,
&pl_nbp.Plugin,
&pl_netmask.Plugin,
&pl_prefix.Plugin,
&pl_range.Plugin,
&pl_router.Plugin,
&pl_searchdomains.Plugin,
&pl_serverid.Plugin,
&pl_sleep.Plugin,
&pl_staticroute.Plugin,
&pl_hollowdhcp.Plugin,
}
func main() {
flag.Parse()
if *flagPlugins {
for _, p := range desiredPlugins {
fmt.Println(p.Name)
}
os.Exit(0)
}
log := logger.GetLogger("main")
// Added outside of coredhcp generator:
log.Infof("Hollow DHCP Server Version Info: %s", version.String())
fn, ok := logLevels[*flagLogLevel]
if !ok {
log.Fatalf("Invalid log level '%s'. Valid log levels are %v", *flagLogLevel, getLogLevels())
}
fn(log.Logger)
log.Infof("Setting log level to '%s'", *flagLogLevel)
if *flagLogFile != "" {
log.Infof("Logging to file %s", *flagLogFile)
logger.WithFile(log, *flagLogFile)
}
if *flagLogNoStdout {
log.Infof("Disabling logging to stdout/stderr")
logger.WithNoStdOutErr(log)
}
config, err := config.Load(*flagConfig)
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}
// register plugins
for _, plugin := range desiredPlugins {
if err := plugins.RegisterPlugin(plugin); err != nil {
log.Fatalf("Failed to register plugin '%s': %v", plugin.Name, err)
}
}
// start server
srv, err := server.Start(config)
if err != nil {
log.Fatal(err)
}
if err := srv.Wait(); err != nil {
log.Print(err)
}
time.Sleep(time.Second)
}