2024-09-20 21:21:41 +00:00
|
|
|
/*
|
2024-09-24 23:10:39 +00:00
|
|
|
Copyright © 2024 Matthew Stobbs <matthew@stobbs.ca>
|
2024-09-20 21:21:41 +00:00
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-09-24 20:43:54 +00:00
|
|
|
"errors"
|
2024-09-20 21:21:41 +00:00
|
|
|
"log/slog"
|
|
|
|
"os"
|
2024-12-02 19:20:39 +00:00
|
|
|
"strconv"
|
2024-09-20 21:21:41 +00:00
|
|
|
|
|
|
|
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation"
|
2024-09-24 20:43:54 +00:00
|
|
|
"git.staur.ca/stobbsm/kea-manage/lib/database"
|
2024-09-20 21:21:41 +00:00
|
|
|
"git.staur.ca/stobbsm/kea-manage/lib/database/postgres"
|
|
|
|
"github.com/spf13/cobra"
|
2024-09-24 20:43:54 +00:00
|
|
|
"github.com/spf13/viper"
|
2024-09-20 21:21:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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) {
|
2024-09-24 20:43:54 +00:00
|
|
|
reslist := []*reservation.ReservationV4{}
|
2024-12-02 19:20:39 +00:00
|
|
|
vlist := viper.Get("reservationV4").(map[string]any)
|
2024-09-24 20:43:54 +00:00
|
|
|
|
2024-12-02 19:20:39 +00:00
|
|
|
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()))
|
2024-09-24 22:58:42 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-12-02 19:20:39 +00:00
|
|
|
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
|
|
|
|
}
|
2024-09-24 22:58:42 +00:00
|
|
|
|
2024-12-02 19:20:39 +00:00
|
|
|
reslist = append(
|
|
|
|
reslist,
|
|
|
|
r,
|
|
|
|
)
|
|
|
|
}
|
2024-09-20 21:21:41 +00:00
|
|
|
}
|
|
|
|
|
2024-09-24 20:43:54 +00:00
|
|
|
slog.Debug("reslist", slog.Any("slice", reslist))
|
|
|
|
|
|
|
|
dbconf := viper.GetStringMapString("database")
|
|
|
|
pg := postgres.Open(dbconf["host"], dbconf["port"], dbconf["user"], dbconf["password"], dbconf["name"])
|
2024-09-20 21:21:41 +00:00
|
|
|
|
|
|
|
for _, v := range reslist {
|
|
|
|
v := v
|
2024-09-22 02:02:02 +00:00
|
|
|
if err := pg.InsertResV4(v); err != nil {
|
2024-09-24 20:43:54 +00:00
|
|
|
if errors.As(err, &database.Exists) {
|
2024-12-02 19:20:39 +00:00
|
|
|
if err := pg.UpdateResV4(v); err != nil {
|
|
|
|
slog.Error("update", slog.Any("reservation", *v))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
slog.Info("updated", slog.Any("hostname", v.Hostname))
|
2024-09-24 20:43:54 +00:00
|
|
|
} else {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
}
|
2024-09-20 21:21:41 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-09-24 20:43:54 +00:00
|
|
|
slog.Info("inserted", slog.Any("reservation", *v))
|
2024-09-20 21:21:41 +00:00
|
|
|
}
|
|
|
|
}
|