kea-manage/cmd/root.go

97 lines
2.4 KiB
Go
Raw Normal View History

/*
Copyright © 2024 Matthew Stobbs <matthew@stobbs.ca>
*/
package cmd
import (
"errors"
"log/slog"
"os"
"strconv"
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation"
"git.staur.ca/stobbsm/kea-manage/lib/database"
"git.staur.ca/stobbsm/kea-manage/lib/database/postgres"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "kea-manage",
Short: "Manage the isc-kea dhcp server",
Long: `Manage the isc-kea dhcp server backend in database.`,
Run: runMain,
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
}
// Manage kea dhcp host reservations in postgresql directly
// postgresql schema used from [https://gitlab.isc.org/isc-projects/kea/-/wikis/docs/editing-host-reservations]
// This will only work with hw-address identifiers. If I need support for more, I'll add it
func runMain(cmd *cobra.Command, args []string) {
reslist := []*reservation.ReservationV4{}
vlist := viper.Get("reservationV4").(map[string]any)
for k, v := range vlist {
v := v.([]any)
ki, err := strconv.ParseInt(k, 10, 0)
if err != nil {
slog.Error("reservation", slog.Any("subnetid", err.Error()))
continue
}
for _, vi := range v {
vi := vi.(map[string]any)
r := reservation.ReserveV4MacAddr(
vi["identifier"].(string),
vi["ipv4"].(string),
vi["hostname"].(string),
int(ki),
)
if !r.Validate() {
slog.Error("reservation", slog.Any("validate", r))
continue
}
reslist = append(
reslist,
r,
)
}
}
slog.Debug("reslist", slog.Any("slice", reslist))
dbconf := viper.GetStringMapString("database")
pg := postgres.Open(dbconf["host"], dbconf["port"], dbconf["user"], dbconf["password"], dbconf["name"])
for _, v := range reslist {
v := v
2024-09-22 02:02:02 +00:00
if err := pg.InsertResV4(v); err != nil {
if errors.As(err, &database.Exists) {
if err := pg.UpdateResV4(v); err != nil {
slog.Error("update", slog.Any("reservation", *v))
continue
}
slog.Info("updated", slog.Any("hostname", v.Hostname))
} else {
slog.Error(err.Error())
}
continue
}
slog.Info("inserted", slog.Any("reservation", *v))
}
}