110 lines
2.5 KiB
Go
110 lines
2.5 KiB
Go
|
/*
|
||
|
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
|
||
|
*/
|
||
|
package cmd
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"log/slog"
|
||
|
"os"
|
||
|
|
||
|
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation"
|
||
|
"git.staur.ca/stobbsm/kea-manage/lib/database/postgres"
|
||
|
"github.com/jackc/pgx/v5"
|
||
|
"github.com/spf13/cobra"
|
||
|
)
|
||
|
|
||
|
// 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() {
|
||
|
// Here you will define your flags and configuration settings.
|
||
|
// Cobra supports persistent flags, which, if defined here,
|
||
|
// will be global for your application.
|
||
|
|
||
|
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kea-manage.yaml)")
|
||
|
|
||
|
// Cobra also supports local flags, which will only run
|
||
|
// when this action is called directly.
|
||
|
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||
|
}
|
||
|
|
||
|
// 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{
|
||
|
reservation.ReserveV4MacAddr(
|
||
|
"90:e2:ba:19:25:70",
|
||
|
"10.0.255.2",
|
||
|
"mars",
|
||
|
100,
|
||
|
),
|
||
|
reservation.ReserveV4MacAddr(
|
||
|
"90:e2:ba:19:25:70",
|
||
|
"10.1.255.2",
|
||
|
"mars",
|
||
|
101,
|
||
|
),
|
||
|
reservation.ReserveV4MacAddr(
|
||
|
"90:e2:ba:19:25:70",
|
||
|
"10.2.255.2",
|
||
|
"mars",
|
||
|
102,
|
||
|
),
|
||
|
reservation.ReserveV4MacAddr(
|
||
|
"90:e2:ba:19:25:70",
|
||
|
"10.3.255.2",
|
||
|
"mars",
|
||
|
103,
|
||
|
),
|
||
|
reservation.ReserveV4MacAddr(
|
||
|
"BC:24:11:E5:C5:AF",
|
||
|
"10.0.0.4",
|
||
|
"ns01",
|
||
|
100,
|
||
|
),
|
||
|
reservation.ReserveV4MacAddr(
|
||
|
"BC:24:11:B1:1C:AD",
|
||
|
"10.1.0.6",
|
||
|
"db01",
|
||
|
101,
|
||
|
),
|
||
|
}
|
||
|
|
||
|
pg := postgres.Open("10.1.0.6", 5432, "kea", "xGq42ZMeMUIWRK", "kea")
|
||
|
|
||
|
conn, err := pg.Connection(context.Background())
|
||
|
if err != nil {
|
||
|
slog.Error(err.Error())
|
||
|
} else {
|
||
|
slog.Info("connected to database")
|
||
|
}
|
||
|
defer conn.Close(context.Background())
|
||
|
|
||
|
for _, v := range reslist {
|
||
|
v := v
|
||
|
if err = v.Insert(conn); err != nil {
|
||
|
slog.Error(err.Error())
|
||
|
continue
|
||
|
}
|
||
|
slog.Info("inserted reservation", *v)
|
||
|
}
|
||
|
}
|