mirror of
https://github.com/davidallendj/configurator.git
synced 2025-12-20 11:37:00 -07:00
Added server implementation and serve command
This commit is contained in:
parent
112db14dd4
commit
3905a8a023
4 changed files with 158 additions and 11 deletions
|
|
@ -1,3 +1,6 @@
|
||||||
|
//go:build client || all
|
||||||
|
// +build client all
|
||||||
|
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
@ -43,11 +46,13 @@ var generateCmd = &cobra.Command{
|
||||||
for _, target := range targets {
|
for _, target := range targets {
|
||||||
// split the target and type
|
// split the target and type
|
||||||
tmp := strings.Split(target, ":")
|
tmp := strings.Split(target, ":")
|
||||||
configType := tmp[0]
|
g := generator.Generator{
|
||||||
configTemplate := tmp[1]
|
Type: tmp[0],
|
||||||
|
Template: tmp[1],
|
||||||
|
}
|
||||||
|
|
||||||
// NOTE: we probably don't want to hardcode the types, but should do for now
|
// 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
|
// fetch eths from SMD
|
||||||
eths, err := client.FetchEthernetInterfaces()
|
eths, err := client.FetchEthernetInterfaces()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -57,17 +62,17 @@ var generateCmd = &cobra.Command{
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// generate a new config from that data
|
// generate a new config from that data
|
||||||
g := generator.New()
|
|
||||||
g.GenerateDHCP(config, configTemplate, eths)
|
g.GenerateDHCP(&config, eths)
|
||||||
} else if configType == "dns" {
|
} else if g.Type == "dns" {
|
||||||
// TODO: fetch from SMD
|
// TODO: fetch from SMD
|
||||||
// TODO: generate config from pulled info
|
// 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" {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
configPath string
|
configPath string
|
||||||
config *configurator.Config
|
config configurator.Config
|
||||||
)
|
)
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
|
|
@ -34,7 +34,7 @@ func Execute() {
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "set the config path")
|
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "set the config path")
|
||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
|
|
|
||||||
37
cmd/serve.go
Normal file
37
cmd/serve.go
Normal 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)
|
||||||
|
}
|
||||||
105
internal/server/server.go
Normal file
105
internal/server/server.go
Normal file
|
|
@ -0,0 +1,105 @@
|
||||||
|
//go:build server || all
|
||||||
|
// +build server all
|
||||||
|
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
configurator "github.com/OpenCHAMI/configurator/internal"
|
||||||
|
"github.com/OpenCHAMI/configurator/internal/generator"
|
||||||
|
"github.com/OpenCHAMI/jwtauth/v5"
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
tokenAuth *jwtauth.JWTAuth = nil
|
||||||
|
)
|
||||||
|
|
||||||
|
type Server struct {
|
||||||
|
*http.Server
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *Server {
|
||||||
|
return &Server{
|
||||||
|
Server: &http.Server{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) Start(config *configurator.Config) error {
|
||||||
|
// create client just for the server to use to fetch data from SMD
|
||||||
|
client := &configurator.SmdClient{
|
||||||
|
Host: config.SmdHost,
|
||||||
|
Port: config.SmdPort,
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the server address with config values
|
||||||
|
s.Server.Addr = fmt.Sprintf("%s:%d", config.Server.Host, config.Server.Port)
|
||||||
|
|
||||||
|
// fetch JWKS public key from authorization server
|
||||||
|
if config.Options.JwksUri != "" && tokenAuth == nil {
|
||||||
|
for i := 0; i < config.Options.JwksRetries; i++ {
|
||||||
|
var err error
|
||||||
|
tokenAuth, err = configurator.FetchPublicKeyFromURL(config.Options.JwksUri)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("failed to fetch JWKS: %w", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// create new go-chi router with its routes
|
||||||
|
router := chi.NewRouter()
|
||||||
|
router.Use(middleware.RedirectSlashes)
|
||||||
|
router.Use(middleware.Timeout(60 * time.Second))
|
||||||
|
router.Group(func(r chi.Router) {
|
||||||
|
if config.Options.JwksUri != "" {
|
||||||
|
r.Use(
|
||||||
|
jwtauth.Verifier(tokenAuth),
|
||||||
|
jwtauth.Authenticator(tokenAuth),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
r.HandleFunc("/target", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
g := generator.Generator{
|
||||||
|
Type: r.URL.Query().Get("type"),
|
||||||
|
Template: r.URL.Query().Get("template"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: we probably don't want to hardcode the types, but should do for now
|
||||||
|
if g.Type == "dhcp" {
|
||||||
|
// fetch eths from SMD
|
||||||
|
eths, err := client.FetchEthernetInterfaces()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("failed to fetch DHCP metadata: %v\n", err)
|
||||||
|
w.Write([]byte("An error has occurred"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(eths) <= 0 {
|
||||||
|
logrus.Warnf("no ethernet interfaces found")
|
||||||
|
w.Write([]byte("no ethernet interfaces found"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// generate a new config from that data
|
||||||
|
|
||||||
|
err = g.GenerateDHCP(config, eths)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("failed to generate DHCP: %v", err)
|
||||||
|
w.Write([]byte("An error has occurred."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
r.HandleFunc("/templates", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// TODO: handle GET request
|
||||||
|
// TODO: handle POST request
|
||||||
|
|
||||||
|
})
|
||||||
|
})
|
||||||
|
s.Handler = router
|
||||||
|
return s.ListenAndServe()
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue