@@ -2,41 +2,23 @@ package cmd
22
33import (
44 "fmt"
5- "github.com/DSpeichert/netbootd/api"
65 "github.com/DSpeichert/netbootd/config"
7- "github.com/DSpeichert/netbootd/dhcpd"
8- "github.com/DSpeichert/netbootd/httpd"
9- "github.com/DSpeichert/netbootd/store"
10- "github.com/DSpeichert/netbootd/tftpd"
11- systemd "github.com/coreos/go-systemd/daemon"
12- "github.com/rs/zerolog"
13- "github.com/rs/zerolog/log"
146 "github.com/spf13/cobra"
15- "net"
167 "os"
17- "os/signal"
188)
199
2010var (
21- debug bool
22- trace bool
23- addr string
24- ifname string
25- httpPort int
26- apiPort int
27- manifestPath string
11+ debug bool
12+ trace bool
13+ version string
14+ commit string
15+ date string
2816)
2917
3018func init () {
3119 cobra .OnInitialize (config .InitConfig )
3220 rootCmd .Flags ().BoolVarP (& debug , "debug" , "d" , false , "enable debug logging" )
3321 rootCmd .Flags ().BoolVar (& trace , "trace" , false , "enable trace logging" )
34-
35- rootCmd .Flags ().StringVarP (& addr , "address" , "a" , "" , "IP address to listen on (DHCP, TFTP, HTTP)" )
36- rootCmd .Flags ().IntVarP (& httpPort , "http-port" , "p" , 8080 , "HTTP port to listen on" )
37- rootCmd .Flags ().IntVarP (& apiPort , "api-port" , "r" , 8081 , "HTTP API port to listen on" )
38- rootCmd .Flags ().StringVarP (& ifname , "interface" , "i" , "" , "interface to listen on, e.g. eth0 (DHCP)" )
39- rootCmd .Flags ().StringVarP (& manifestPath , "manifests" , "m" , "" , "load manifests from directory" )
4022}
4123
4224var rootCmd = & cobra.Command {
@@ -45,92 +27,7 @@ var rootCmd = &cobra.Command{
4527 Long : `A programmable all-inclusive provisioning server including DHCP, TFTP and HTTP capability.
4628Unlike heavy, complex solutions like Foreman, netbootd is very lightweight and without many features,
4729allows for complete flexibility in provisioning machines.` ,
48- Run : func (cmd * cobra.Command , args []string ) {
49- // configure logging
50- if trace {
51- zerolog .SetGlobalLevel (zerolog .TraceLevel )
52- } else if debug {
53- zerolog .SetGlobalLevel (zerolog .DebugLevel )
54- } else {
55- zerolog .SetGlobalLevel (zerolog .InfoLevel )
56- }
57-
58- // set up store
59- store , _ := store .NewStore (store.Config {
60- // TODO: config
61- PersistenceDirectory : "" ,
62- })
63- if manifestPath != "" {
64- log .Info ().Str ("path" , manifestPath ).Msg ("Loading manifests" )
65- _ = store .LoadFromDirectory (manifestPath )
66- }
67- store .GlobalHints .HttpPort = httpPort
68-
69- // DHCP
70- dhcpServer , err := dhcpd .NewServer (addr , ifname , store )
71- if err != nil {
72- log .Fatal ().Err (err )
73- }
74- go dhcpServer .Serve ()
75-
76- // TFTP
77- tftpServer , err := tftpd .NewServer (store )
78- if err != nil {
79- log .Fatal ().Err (err )
80- }
81- connTftp , err := net .ListenUDP ("udp" , & net.UDPAddr {
82- IP : net .ParseIP (addr ),
83- Port : 69 , // TFTP
84- })
85- if err != nil {
86- log .Fatal ().Err (err )
87- }
88- go tftpServer .Serve (connTftp )
89-
90- // HTTP service
91- httpServer , err := httpd .NewServer (store )
92- if err != nil {
93- log .Fatal ().Err (err )
94- }
95- connHttp , err := net .ListenTCP ("tcp" , & net.TCPAddr {
96- IP : net .ParseIP (addr ),
97- Port : httpPort , // HTTP
98- })
99- if err != nil {
100- log .Fatal ().Err (err )
101- }
102- go httpServer .Serve (connHttp )
103- log .Info ().Interface ("addr" , connHttp .Addr ()).Msg ("HTTP listening" )
104-
105- // HTTP API service
106- apiServer , err := api .NewServer (store )
107- if err != nil {
108- log .Fatal ().Err (err )
109- }
110- connApi , err := net .ListenTCP ("tcp" , & net.TCPAddr {
111- IP : net .ParseIP (addr ),
112- Port : apiPort , // HTTP
113- })
114- if err != nil {
115- log .Fatal ().Err (err )
116- }
117- go apiServer .Serve (connApi )
118- log .Info ().Interface ("api" , connApi .Addr ()).Msg ("HTTP API listening" )
119-
120- // notify systemd
121- sent , err := systemd .SdNotify (true , "READY=1\n " )
122- if err != nil {
123- log .Debug ().Err (err ).Msg ("unable to send systemd daemon successful start message" )
124- } else if sent {
125- log .Debug ().Msg ("systemd was notified." )
126- } else {
127- log .Debug ().Msg ("systemd notifications are not supported." )
128- }
129-
130- sigs := make (chan os.Signal , 1 )
131- signal .Notify (sigs , os .Interrupt )
132- <- sigs
133- },
30+ Version : version + " (" + commit + ") built " + date ,
13431}
13532
13633func Execute () {
0 commit comments