Skip to content

Commit 4cf9a3a

Browse files
committed
cmd: move server to subcommand
1 parent f2ffb5d commit 4cf9a3a

File tree

3 files changed

+131
-110
lines changed

3 files changed

+131
-110
lines changed

cmd/root.go

Lines changed: 6 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,23 @@ package cmd
22

33
import (
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

2010
var (
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

3018
func 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

4224
var 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.
4628
Unlike heavy, complex solutions like Foreman, netbootd is very lightweight and without many features,
4729
allows 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

13633
func Execute() {

cmd/server.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package cmd
2+
3+
import (
4+
"github.com/DSpeichert/netbootd/api"
5+
"github.com/DSpeichert/netbootd/dhcpd"
6+
"github.com/DSpeichert/netbootd/httpd"
7+
"github.com/DSpeichert/netbootd/store"
8+
"github.com/DSpeichert/netbootd/tftpd"
9+
systemd "github.com/coreos/go-systemd/daemon"
10+
"github.com/rs/zerolog"
11+
"github.com/rs/zerolog/log"
12+
"github.com/spf13/cobra"
13+
"net"
14+
"os"
15+
"os/signal"
16+
)
17+
18+
var (
19+
addr string
20+
ifname string
21+
httpPort int
22+
apiPort int
23+
manifestPath string
24+
)
25+
26+
func init() {
27+
serverCmd.Flags().StringVarP(&addr, "address", "a", "", "IP address to listen on (DHCP, TFTP, HTTP)")
28+
serverCmd.Flags().IntVarP(&httpPort, "http-port", "p", 8080, "HTTP port to listen on")
29+
serverCmd.Flags().IntVarP(&apiPort, "api-port", "r", 8081, "HTTP API port to listen on")
30+
serverCmd.Flags().StringVarP(&ifname, "interface", "i", "", "interface to listen on, e.g. eth0 (DHCP)")
31+
serverCmd.Flags().StringVarP(&manifestPath, "manifests", "m", "", "load manifests from directory")
32+
33+
rootCmd.AddCommand(serverCmd)
34+
}
35+
36+
var serverCmd = &cobra.Command{
37+
Use: "server",
38+
Run: func(cmd *cobra.Command, args []string) {
39+
// configure logging
40+
if trace {
41+
zerolog.SetGlobalLevel(zerolog.TraceLevel)
42+
} else if debug {
43+
zerolog.SetGlobalLevel(zerolog.DebugLevel)
44+
} else {
45+
zerolog.SetGlobalLevel(zerolog.InfoLevel)
46+
}
47+
48+
// set up store
49+
store, _ := store.NewStore(store.Config{
50+
// TODO: config
51+
PersistenceDirectory: "",
52+
})
53+
if manifestPath != "" {
54+
log.Info().Str("path", manifestPath).Msg("Loading manifests")
55+
_ = store.LoadFromDirectory(manifestPath)
56+
}
57+
store.GlobalHints.HttpPort = httpPort
58+
59+
// DHCP
60+
dhcpServer, err := dhcpd.NewServer(addr, ifname, store)
61+
if err != nil {
62+
log.Fatal().Err(err)
63+
}
64+
go dhcpServer.Serve()
65+
66+
// TFTP
67+
tftpServer, err := tftpd.NewServer(store)
68+
if err != nil {
69+
log.Fatal().Err(err)
70+
}
71+
connTftp, err := net.ListenUDP("udp", &net.UDPAddr{
72+
IP: net.ParseIP(addr),
73+
Port: 69, // TFTP
74+
})
75+
if err != nil {
76+
log.Fatal().Err(err)
77+
}
78+
go tftpServer.Serve(connTftp)
79+
80+
// HTTP service
81+
httpServer, err := httpd.NewServer(store)
82+
if err != nil {
83+
log.Fatal().Err(err)
84+
}
85+
connHttp, err := net.ListenTCP("tcp", &net.TCPAddr{
86+
IP: net.ParseIP(addr),
87+
Port: httpPort, // HTTP
88+
})
89+
if err != nil {
90+
log.Fatal().Err(err)
91+
}
92+
go httpServer.Serve(connHttp)
93+
log.Info().Interface("addr", connHttp.Addr()).Msg("HTTP listening")
94+
95+
// HTTP API service
96+
apiServer, err := api.NewServer(store)
97+
if err != nil {
98+
log.Fatal().Err(err)
99+
}
100+
connApi, err := net.ListenTCP("tcp", &net.TCPAddr{
101+
IP: net.ParseIP(addr),
102+
Port: apiPort, // HTTP
103+
})
104+
if err != nil {
105+
log.Fatal().Err(err)
106+
}
107+
go apiServer.Serve(connApi)
108+
log.Info().Interface("api", connApi.Addr()).Msg("HTTP API listening")
109+
110+
// notify systemd
111+
sent, err := systemd.SdNotify(true, "READY=1\n")
112+
if err != nil {
113+
log.Debug().Err(err).Msg("unable to send systemd daemon successful start message")
114+
} else if sent {
115+
log.Debug().Msg("systemd was notified.")
116+
} else {
117+
log.Debug().Msg("systemd notifications are not supported.")
118+
}
119+
120+
sigs := make(chan os.Signal, 1)
121+
signal.Notify(sigs, os.Interrupt)
122+
<-sigs
123+
},
124+
}

netbootd.service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ After=network.target
44

55
[Service]
66
Type=notify
7-
ExecStart=/usr/bin/netbootd
7+
ExecStart=/usr/bin/netbootd server --trace
88
#AmbientCapabilities=CAP_NET_BIND_SERVICE CAP_NET_RAW
99
#User=nobody
1010
#Group=nobody

0 commit comments

Comments
 (0)