migrated to cobra structure
adding abstractions for database to avoid passing around the connection
This commit is contained in:
parent
312399093f
commit
1d0a5811ce
109
cmd/root.go
Normal file
109
cmd/root.go
Normal file
@ -0,0 +1,109 @@
|
||||
/*
|
||||
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)
|
||||
}
|
||||
}
|
@ -68,10 +68,6 @@ func (r *ReservationV4) check(conn *pgx.Conn) error {
|
||||
}
|
||||
|
||||
func (r *ReservationV4) Insert(conn *pgx.Conn) error {
|
||||
// if err := r.check(conn); err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
tx, err := conn.Begin(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -1,3 +1,10 @@
|
||||
database:
|
||||
type: pgx
|
||||
host: 10.1.0.6
|
||||
port: 5432
|
||||
user: kea
|
||||
password: xGq42ZMeMUIWRK
|
||||
database: kea
|
||||
reservationV4:
|
||||
- type: macaddr
|
||||
identifer: "90:e2:ba:19:25:70"
|
||||
|
1
lib/database/database.go
Normal file
1
lib/database/database.go
Normal file
@ -0,0 +1 @@
|
||||
package database
|
23
lib/database/postgres/postgres.go
Normal file
23
lib/database/postgres/postgres.go
Normal file
@ -0,0 +1,23 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type Postgres struct {
|
||||
connString string
|
||||
}
|
||||
|
||||
func Open(host string, port int, user string, password string, name string) *Postgres {
|
||||
var p = &Postgres{
|
||||
connString: fmt.Sprintf("postgres://%s:%s@%s:%d/%s", user, password, host, port, name),
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Postgres) Connection(ctx context.Context) (*pgx.Conn, error) {
|
||||
return pgx.Connect(ctx, p.connString)
|
||||
}
|
65
main.go
65
main.go
@ -1,78 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"git.staur.ca/stobbsm/kea-manage/cmd"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// 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 main() {
|
||||
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,
|
||||
),
|
||||
}
|
||||
|
||||
viper.SetConfigName("kea-manage")
|
||||
viper.AddConfigPath("/etc")
|
||||
viper.AddConfigPath(os.Getenv("HOME") + "/.config/kea-manage")
|
||||
|
||||
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
|
||||
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)
|
||||
}
|
||||
cmd.Execute()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user