Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0ff17abd59 | |||
| a742bad084 | |||
| 629b6ee74f | |||
| 884b11d1f9 | |||
| 5bbe15ef24 | |||
| fd1c57d377 | |||
| 2fd5947f1b | |||
| 14b3d91191 | |||
| abe3005769 |
@@ -1,9 +1,12 @@
|
|||||||
package cluster
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
fs "github.com/hack-pad/hackpadfs"
|
fs "github.com/hack-pad/hackpadfs"
|
||||||
)
|
)
|
||||||
@@ -40,6 +43,21 @@ func (m *Cluster) openWriter(name string, flags int, args ...any) (io.WriteClose
|
|||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveFile deletes a file from the cluster's config directory
|
||||||
|
func (m *Cluster) RemoveFile(name string, args ...any) error {
|
||||||
|
if len(args) > 0 {
|
||||||
|
name = fmt.Sprintf(name, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := fs.Remove(m.dir, name)
|
||||||
|
switch {
|
||||||
|
case os.IsNotExist(err):
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ReadFile reads a file from the cluster's config directory
|
// ReadFile reads a file from the cluster's config directory
|
||||||
func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
|
func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
@@ -48,3 +66,41 @@ func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
|
|||||||
|
|
||||||
return fs.ReadFile(m.dir, name)
|
return fs.ReadFile(m.dir, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadLines reads a file from the cluster's config directory,
|
||||||
|
// split by lines, trimmed, and accepting `#` to comment lines out.
|
||||||
|
func (m *Cluster) ReadLines(name string, args ...any) ([]string, error) {
|
||||||
|
var out []string
|
||||||
|
|
||||||
|
data, err := m.ReadFile(name, args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sc := bufio.NewScanner(bytes.NewReader(data))
|
||||||
|
for sc.Scan() {
|
||||||
|
s := strings.TrimSpace(sc.Text())
|
||||||
|
switch {
|
||||||
|
case s == "", strings.HasPrefix(s, "#"):
|
||||||
|
// ignore
|
||||||
|
default:
|
||||||
|
// accepted
|
||||||
|
out = append(out, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteStringFile writes the given content to a file on the machine's config directory
|
||||||
|
func (m *Cluster) WriteStringFile(value string, name string, args ...any) error {
|
||||||
|
f, err := m.CreateTruncFile(name, args...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
buf := bytes.NewBufferString(value)
|
||||||
|
_, err = buf.WriteTo(f)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package cluster
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@@ -43,16 +42,9 @@ func (m *Machine) openWriter(name string, flags int, args ...any) (io.WriteClose
|
|||||||
|
|
||||||
// RemoveFile deletes a file from the machine's config directory
|
// RemoveFile deletes a file from the machine's config directory
|
||||||
func (m *Machine) RemoveFile(name string, args ...any) error {
|
func (m *Machine) RemoveFile(name string, args ...any) error {
|
||||||
base := m.zone.zones.dir
|
|
||||||
fullName := m.getFilename(name, args...)
|
fullName := m.getFilename(name, args...)
|
||||||
err := fs.Remove(base, fullName)
|
|
||||||
|
|
||||||
switch {
|
return m.zone.zones.RemoveFile(fullName)
|
||||||
case os.IsNotExist(err):
|
|
||||||
return nil
|
|
||||||
default:
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadFile reads a file from the machine's config directory
|
// ReadFile reads a file from the machine's config directory
|
||||||
@@ -65,15 +57,9 @@ func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
|
|||||||
|
|
||||||
// WriteStringFile writes the given content to a file on the machine's config directory
|
// WriteStringFile writes the given content to a file on the machine's config directory
|
||||||
func (m *Machine) WriteStringFile(value string, name string, args ...any) error {
|
func (m *Machine) WriteStringFile(value string, name string, args ...any) error {
|
||||||
f, err := m.CreateTruncFile(name, args...)
|
fullName := m.getFilename(name, args...)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
buf := bytes.NewBufferString(value)
|
return m.zone.zones.WriteStringFile(value, fullName)
|
||||||
_, err = buf.WriteTo(f)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) getFilename(name string, args ...any) string {
|
func (m *Machine) getFilename(name string, args ...any) string {
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
|
|||||||
|
|
||||||
// first regions defined by zones
|
// first regions defined by zones
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
SortRegions(z.Regions)
|
||||||
for _, region := range z.Regions {
|
for _, region := range z.Regions {
|
||||||
regions[region] = append(regions[region], z)
|
regions[region] = append(regions[region], z)
|
||||||
}
|
}
|
||||||
@@ -74,6 +75,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
|
|||||||
m.finishRegion(r)
|
m.finishRegion(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m.sortRegions()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package cluster
|
||||||
|
|
||||||
|
import "sort"
|
||||||
|
|
||||||
|
// SortRegions sorts regions. first by length those 3-character
|
||||||
|
// or shorter, and then by length. It's mostly aimed at
|
||||||
|
// supporting ISO-3166 order
|
||||||
|
func SortRegions(regions []string) []string {
|
||||||
|
sort.Slice(regions, func(i, j int) bool {
|
||||||
|
r1, r2 := regions[i], regions[j]
|
||||||
|
return regionLess(r1, r2)
|
||||||
|
})
|
||||||
|
|
||||||
|
return regions
|
||||||
|
}
|
||||||
|
|
||||||
|
func regionLess(r1, r2 string) bool {
|
||||||
|
switch {
|
||||||
|
case len(r1) < 4:
|
||||||
|
switch {
|
||||||
|
case len(r1) < len(r2):
|
||||||
|
return true
|
||||||
|
case len(r1) > len(r2):
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return r1 < r2
|
||||||
|
}
|
||||||
|
case len(r2) < 4:
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return r1 < r2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) sortRegions() {
|
||||||
|
sort.Slice(m.Regions, func(i, j int) bool {
|
||||||
|
r1 := m.Regions[i].Name
|
||||||
|
r2 := m.Regions[j].Name
|
||||||
|
return regionLess(r1, r2)
|
||||||
|
})
|
||||||
|
}
|
||||||
+13
-27
@@ -11,6 +11,8 @@ import (
|
|||||||
|
|
||||||
"darvaza.org/core"
|
"darvaza.org/core"
|
||||||
"github.com/libdns/libdns"
|
"github.com/libdns/libdns"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/cluster"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (mgr *Manager) fqdn(name string) string {
|
func (mgr *Manager) fqdn(name string) string {
|
||||||
@@ -86,32 +88,6 @@ func lessRecord(a, b libdns.Record) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SortRegions sorts regions. first by length those 3-character
|
|
||||||
// or shorter, and then by length. It's mostly aimed at
|
|
||||||
// supporting ISO-3166 order
|
|
||||||
func SortRegions(regions []string) []string {
|
|
||||||
sort.Slice(regions, func(i, j int) bool {
|
|
||||||
r1, r2 := regions[i], regions[j]
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case len(r1) < 4:
|
|
||||||
switch {
|
|
||||||
case len(r1) < len(r2):
|
|
||||||
return true
|
|
||||||
case len(r1) > len(r2):
|
|
||||||
return false
|
|
||||||
default:
|
|
||||||
return r1 < r2
|
|
||||||
}
|
|
||||||
case len(r2) < 4:
|
|
||||||
return false
|
|
||||||
default:
|
|
||||||
return r1 < r2
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return regions
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddrRecord represents an A or AAAA record
|
// AddrRecord represents an A or AAAA record
|
||||||
type AddrRecord struct {
|
type AddrRecord struct {
|
||||||
Name string
|
Name string
|
||||||
@@ -172,7 +148,17 @@ func (mgr *Manager) genRegionsSorted() []string {
|
|||||||
regions = append(regions, name)
|
regions = append(regions, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return SortRegions(regions)
|
return cluster.SortRegions(regions)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) genZonesSorted() []string {
|
||||||
|
zones := make([]string, 0, len(mgr.zones))
|
||||||
|
for name := range mgr.zones {
|
||||||
|
zones = append(zones, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(zones)
|
||||||
|
return zones
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mgr *Manager) genAllAddrRecords() []AddrRecord {
|
func (mgr *Manager) genAllAddrRecords() []AddrRecord {
|
||||||
|
|||||||
+3
-2
@@ -14,12 +14,13 @@ func (mgr *Manager) WriteTo(w io.Writer) (int64, error) {
|
|||||||
cache := make(map[string][]netip.Addr)
|
cache := make(map[string][]netip.Addr)
|
||||||
|
|
||||||
// zones
|
// zones
|
||||||
for _, z := range mgr.zones {
|
for _, zoneName := range mgr.genZonesSorted() {
|
||||||
|
z := mgr.zones[zoneName]
|
||||||
|
|
||||||
mgr.writeZoneHosts(&buf, z)
|
mgr.writeZoneHosts(&buf, z)
|
||||||
|
|
||||||
// zone alias
|
// zone alias
|
||||||
addrs := mgr.genZoneAddresses(z)
|
addrs := mgr.genZoneAddresses(z)
|
||||||
zoneName := z.Name
|
|
||||||
|
|
||||||
rr := AddrRecord{
|
rr := AddrRecord{
|
||||||
Name: mgr.fqdn(zoneName + mgr.suffix),
|
Name: mgr.fqdn(zoneName + mgr.suffix),
|
||||||
|
|||||||
Reference in New Issue
Block a user