Browse Source
introduce `jpictl list`, `tools.LazyBuffer` and updated build system Reviewed-on: #55pull/56/head v0.8.7
Alejandro Mery
5 months ago
13 changed files with 430 additions and 137 deletions
@ -0,0 +1,200 @@
|
||||
package main |
||||
|
||||
import ( |
||||
"bytes" |
||||
"io" |
||||
"net/netip" |
||||
"os" |
||||
|
||||
"darvaza.org/core" |
||||
"github.com/spf13/cobra" |
||||
|
||||
"git.jpi.io/amery/jpictl/pkg/cluster" |
||||
"git.jpi.io/amery/jpictl/pkg/rings" |
||||
"git.jpi.io/amery/jpictl/pkg/tools" |
||||
) |
||||
|
||||
type inventory struct { |
||||
r []*cluster.Region |
||||
z [][]*cluster.Zone |
||||
} |
||||
|
||||
func (g *inventory) renderRingZero(out *tools.LazyBuffer) error { |
||||
ring0 := netip.PrefixFrom(rings.UnsafeRingZeroAddress(0, 0, 0), rings.RingZeroBits) |
||||
from, to, _ := rings.PrefixToRange(ring0) |
||||
|
||||
_ = out.Printf("; wg%v\n", 0) |
||||
_ = out.Printf("%s\t%s-%s\n", ring0, from, to) |
||||
|
||||
if err := g.renderRingZeroRegions(out); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return g.renderRingZeroZones(out) |
||||
} |
||||
|
||||
func (g *inventory) renderRingZeroRegions(out *tools.LazyBuffer) error { |
||||
for _, r := range g.r { |
||||
if err := g.renderRingZeroRegion(out, r); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func (*inventory) renderRingZeroRegion(out *tools.LazyBuffer, r *cluster.Region) error { |
||||
addr := rings.UnsafeRingZeroAddress(r.ID, 0, 0) |
||||
ring0r := netip.PrefixFrom(addr, rings.RingZeroBits+4) |
||||
from, to, _ := rings.PrefixToRange(ring0r) |
||||
|
||||
_ = out.Printf("%s\t%s-%s\t# %s\n", ring0r, from, to, r.Name) |
||||
return nil |
||||
} |
||||
|
||||
func (g *inventory) renderRingZeroZones(out *tools.LazyBuffer) error { |
||||
for i, r := range g.r { |
||||
for _, z := range g.z[i] { |
||||
if err := g.renderRingZeroZone(out, r, z); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
func (*inventory) renderRingZeroZone(out *tools.LazyBuffer, r *cluster.Region, z *cluster.Zone) error { |
||||
addr := rings.UnsafeRingZeroAddress(r.ID, z.ID, 0) |
||||
ring0rz := netip.PrefixFrom(addr, rings.RingZeroBits+4+4) |
||||
from, to, _ := rings.PrefixToRange(ring0rz) |
||||
|
||||
_ = out.Printf("; wg%v: %s (%s)\n", 0, z.Name, r.Name) |
||||
_ = out.Printf("%s\t%s-%s\t%s\n", ring0rz, from, to, z.Name) |
||||
|
||||
z.ForEachMachine(func(m *cluster.Machine) bool { |
||||
if m.IsGateway() { |
||||
addr, _ := m.RingZeroAddress() |
||||
cidr := netip.PrefixFrom(addr, 32) |
||||
_ = out.Printf("%s\t\t%s-%v\n", cidr, m.Name, 0) |
||||
} |
||||
|
||||
return false |
||||
}) |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func (g *inventory) renderRingOne(out *tools.LazyBuffer) error { |
||||
for i, r := range g.r { |
||||
for _, z := range g.z[i] { |
||||
if err := g.renderRingOneZone(out, r, z); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func (*inventory) renderRingOneZone(out *tools.LazyBuffer, r *cluster.Region, z *cluster.Zone) error { |
||||
ring1, err := rings.RingOnePrefix(r.ID, z.ID) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
from, to, _ := rings.PrefixToRange(ring1) |
||||
|
||||
_ = out.Printf("; wg%v: %s (%s)\n", 1, z.Name, r.Name) |
||||
_ = out.Printf("%s\t%s-%s\t%s\n", ring1, from, to, z.Name) |
||||
|
||||
z.ForEachMachine(func(m *cluster.Machine) bool { |
||||
addr := m.RingOneAddress() |
||||
cidr := netip.PrefixFrom(addr, 32) |
||||
_ = out.Printf("%s\t\t%s-%v\n", cidr, m.Name, 1) |
||||
return false |
||||
}) |
||||
return nil |
||||
} |
||||
|
||||
func (g *inventory) Marshal() ([]byte, error) { |
||||
var buf tools.LazyBuffer |
||||
|
||||
if err := g.renderRingZero(&buf); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
if err := g.renderRingOne(&buf); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return buf.Bytes(), nil |
||||
} |
||||
|
||||
func (g *inventory) WriteTo(out io.Writer) (int64, error) { |
||||
b, err := g.Marshal() |
||||
if err != nil { |
||||
return 0, err |
||||
} |
||||
|
||||
buf := bytes.NewBuffer(b) |
||||
return buf.WriteTo(out) |
||||
} |
||||
|
||||
func genInventory(m *cluster.Cluster) (*inventory, error) { |
||||
g := new(inventory) |
||||
|
||||
g.populateRegions(m) |
||||
g.populateZones() |
||||
|
||||
return g, nil |
||||
} |
||||
|
||||
func (g *inventory) populateRegions(m *cluster.Cluster) { |
||||
m.ForEachRegion(func(r *cluster.Region) bool { |
||||
if r.IsPrimary() { |
||||
g.r = append(g.r, r) |
||||
} |
||||
return false |
||||
}) |
||||
|
||||
core.SliceSortFn(g.r, func(a, b *cluster.Region) bool { |
||||
return a.ID < b.ID |
||||
}) |
||||
} |
||||
|
||||
func (g *inventory) populateZones() { |
||||
g.z = make([][]*cluster.Zone, len(g.r)) |
||||
for i, r := range g.r { |
||||
r.ForEachZone(func(z *cluster.Zone) bool { |
||||
g.z[i] = append(g.z[i], z) |
||||
return false |
||||
}) |
||||
|
||||
core.SliceSortFn(g.z[i], func(a, b *cluster.Zone) bool { |
||||
return a.ID < b.ID |
||||
}) |
||||
} |
||||
} |
||||
|
||||
// Command
|
||||
var listCmd = &cobra.Command{ |
||||
Use: "list", |
||||
Short: "list shows the IP/CIDR inventory", |
||||
PreRun: setVerbosity, |
||||
RunE: func(_ *cobra.Command, _ []string) error { |
||||
m, err := cfg.LoadZones(false) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
out, err := genInventory(m) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
_, err = out.WriteTo(os.Stdout) |
||||
return err |
||||
}, |
||||
} |
||||
|
||||
func init() { |
||||
rootCmd.AddCommand(listCmd) |
||||
} |
@ -0,0 +1,72 @@
|
||||
package tools |
||||
|
||||
import ( |
||||
"bytes" |
||||
"fmt" |
||||
"io" |
||||
) |
||||
|
||||
// LazyBuffer is a [bytes.Buffer] that minimizes counting and error checks.
|
||||
type LazyBuffer bytes.Buffer |
||||
|
||||
// Sys returns the underlying [bytes.Buffer].
|
||||
func (buf *LazyBuffer) Sys() *bytes.Buffer { |
||||
if buf == nil { |
||||
return nil |
||||
} |
||||
return (*bytes.Buffer)(buf) |
||||
} |
||||
|
||||
// Len tells the size in bytes of the currently stored data.
|
||||
func (buf *LazyBuffer) Len() int { return buf.Sys().Len() } |
||||
|
||||
// String returns the stored data as string.
|
||||
func (buf *LazyBuffer) String() string { return buf.Sys().String() } |
||||
|
||||
// Bytes returns the stored data as a bytes slice.
|
||||
func (buf *LazyBuffer) Bytes() []byte { return buf.Sys().Bytes() } |
||||
|
||||
// Write implements the standard io.Writer interface.
|
||||
func (buf *LazyBuffer) Write(b []byte) (int, error) { return buf.Sys().Write(b) } |
||||
|
||||
// WriteTo implements the standard WriteTo() interface.
|
||||
func (buf *LazyBuffer) WriteTo(out io.Writer) (int64, error) { return buf.Sys().WriteTo(out) } |
||||
|
||||
// Print appends the [fmt.Print] equivalent to the buffer.
|
||||
func (buf *LazyBuffer) Print(a ...any) error { |
||||
_, err := fmt.Fprint(buf.Sys(), a...) |
||||
return err |
||||
} |
||||
|
||||
// Println appends the [fmt.Println] equivalent to the buffer.
|
||||
func (buf *LazyBuffer) Println(a ...any) error { |
||||
_, err := fmt.Fprintln(buf.Sys(), a...) |
||||
return err |
||||
} |
||||
|
||||
// Printf appends the [fmt.Printf] equivalent to the buffer.
|
||||
func (buf *LazyBuffer) Printf(format string, a ...any) error { |
||||
_, err := fmt.Fprintf(buf.Sys(), format, a...) |
||||
return err |
||||
} |
||||
|
||||
// WriteRunes appends the given runes as UTF-8 characters to the buffer.
|
||||
func (buf *LazyBuffer) WriteRunes(runes ...rune) { |
||||
for _, r := range runes { |
||||
_, _ = buf.Sys().WriteRune(r) |
||||
} |
||||
} |
||||
|
||||
// WriteBytes writes the given byte arrays to the buffer.
|
||||
func (buf *LazyBuffer) WriteBytes(s ...[]byte) { |
||||
for _, b := range s { |
||||
_, _ = buf.Sys().Write(b) |
||||
} |
||||
} |
||||
|
||||
// WriteStrings writes the given strings as UTF-8 to the buffer.
|
||||
func (buf *LazyBuffer) WriteStrings(strings ...string) { |
||||
for _, s := range strings { |
||||
_, _ = buf.Sys().WriteString(s) |
||||
} |
||||
} |
@ -1,7 +1,11 @@
|
||||
//go:build tools
|
||||
|
||||
// Package tools contains helpers
|
||||
package tools |
||||
|
||||
import ( |
||||
_ "github.com/mgechev/revive" |
||||
) |
||||
import "io" |
||||
|
||||
// LazyClose closes an [io.Closer] and discards the error
|
||||
func LazyClose(p io.Closer) { |
||||
if p != nil { |
||||
_ = p.Close() |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue