Added server implementation and serve command

This commit is contained in:
David J. Allen 2024-04-11 10:19:29 -06:00
parent 112db14dd4
commit 3905a8a023
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
4 changed files with 158 additions and 11 deletions

View file

@ -1,3 +1,6 @@
//go:build client || all
// +build client all
package cmd
import (
@ -43,11 +46,13 @@ var generateCmd = &cobra.Command{
for _, target := range targets {
// split the target and type
tmp := strings.Split(target, ":")
configType := tmp[0]
configTemplate := tmp[1]
g := generator.Generator{
Type: tmp[0],
Template: tmp[1],
}
// NOTE: we probably don't want to hardcode the types, but should do for now
if configType == "dhcp" {
if g.Type == "dhcp" {
// fetch eths from SMD
eths, err := client.FetchEthernetInterfaces()
if err != nil {
@ -57,17 +62,17 @@ var generateCmd = &cobra.Command{
break
}
// generate a new config from that data
g := generator.New()
g.GenerateDHCP(config, configTemplate, eths)
} else if configType == "dns" {
g.GenerateDHCP(&config, eths)
} else if g.Type == "dns" {
// TODO: fetch from SMD
// TODO: generate config from pulled info
} else if configType == "syslog" {
} else if g.Type == "syslog" {
} else if configType == "ansible" {
} else if g.Type == "ansible" {
} else if configType == "warewulf" {
} else if g.Type == "warewulf" {
}

View file

@ -11,7 +11,7 @@ import (
var (
configPath string
config *configurator.Config
config configurator.Config
)
var rootCmd = &cobra.Command{
@ -34,7 +34,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "set the config path")
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "set the config path")
}
func initConfig() {

37
cmd/serve.go Normal file
View file

@ -0,0 +1,37 @@
//go:build server || all
// +build server all
package cmd
import (
"errors"
"fmt"
"net/http"
"os"
"github.com/OpenCHAMI/configurator/internal/server"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Start configurator as a server and listen for requests",
Run: func(cmd *cobra.Command, args []string) {
// set up the routes and start the server
server := server.New()
err := server.Start(&config)
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("Server closed.")
} else if err != nil {
logrus.Errorf("failed to start server: %v", err)
os.Exit(1)
}
},
}
func init() {
serveCmd.Flags().StringVar(&config.Server.Host, "host", config.Server.Host, "set the server host")
serveCmd.Flags().IntVar(&config.Server.Port, "port", config.Server.Port, "set the server port")
rootCmd.AddCommand(serveCmd)
}