mirror of
https://gitea.elkins.co/Networking/ccl.git
synced 2025-03-06 10:18:49 -06:00
192 lines
4.2 KiB
Go
192 lines
4.2 KiB
Go
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"sort"
|
|
|
|
"gitea.elkins.co/Networking/ccl/internal/pkg/container"
|
|
"gitea.elkins.co/Networking/ccl/internal/pkg/network"
|
|
"github.com/emirpasic/gods/sets/hashset"
|
|
toml "github.com/pelletier/go-toml"
|
|
log "github.com/sirupsen/logrus"
|
|
"golang.org/x/exp/slices"
|
|
"gopkg.in/guregu/null.v4"
|
|
)
|
|
|
|
const (
|
|
CONFIG_FILE_DEFAULT = "/etc/ccl.toml"
|
|
)
|
|
|
|
var (
|
|
ConfigFile = CONFIG_FILE_DEFAULT
|
|
Networks = []network.Network{}
|
|
Containers = []container.Container{}
|
|
)
|
|
|
|
func Categories() []string {
|
|
categories := []string{"all"}
|
|
gs := hashset.New()
|
|
for _, c := range Containers {
|
|
gs.Add(c.Category)
|
|
}
|
|
for _, c := range gs.Values() {
|
|
categories = append(categories, c.(string))
|
|
}
|
|
slices.Sort(categories)
|
|
return categories
|
|
}
|
|
|
|
func Union(ids []string, catMask []string) (conts []container.Container) {
|
|
if len(ids) == 0 {
|
|
ids = []string{"all"}
|
|
}
|
|
h := hashset.New()
|
|
for _, id := range ids {
|
|
if j := slices.Index(catMask, id); j >= 0 {
|
|
catMask = slices.Delete(catMask, j, j+1)
|
|
}
|
|
for _, c := range Containers {
|
|
if (id == "all" || c.Category == id) && !slices.Contains(catMask, c.Category) {
|
|
h.Add(c.Name)
|
|
}
|
|
if c.Name == id {
|
|
h.Add(c.Name)
|
|
}
|
|
}
|
|
}
|
|
for _, c := range h.Values() {
|
|
name := c.(string)
|
|
match := slices.IndexFunc(Containers, func(c container.Container) bool { return c.Name == name })
|
|
conts = append(conts, Containers[match])
|
|
}
|
|
if len(conts) == 0 {
|
|
log.WithFields(log.Fields{
|
|
"ids": ids,
|
|
"catMask": catMask,
|
|
}).Warnln("No matching containers. If disabled, try adding -a")
|
|
}
|
|
slices.SortFunc(conts, func(a, b container.Container) bool {
|
|
if a.Category < b.Category {
|
|
return true
|
|
}
|
|
if a.Category > b.Category {
|
|
return false
|
|
}
|
|
return a.Name <= b.Name
|
|
})
|
|
return
|
|
}
|
|
|
|
func UnionNetworks(ids []string) []network.Network {
|
|
nets := make([]network.Network, len(Networks))
|
|
rejects := []int{}
|
|
copy(nets, Networks)
|
|
for i := range nets {
|
|
if !slices.Contains(ids, nets[i].Name) {
|
|
rejects = append(rejects, i)
|
|
}
|
|
}
|
|
// reverse the rejects list, so we can delete from the end backward
|
|
// and thereby not fuck up sequential indicies
|
|
sort.SliceStable(rejects, func(i, j int) bool {
|
|
return true
|
|
})
|
|
for _, j := range rejects {
|
|
nets = slices.Delete(nets, j, j+1)
|
|
}
|
|
return nets
|
|
}
|
|
|
|
func Init(conn context.Context) error {
|
|
// A parsing convenience
|
|
type parse struct {
|
|
Networks []network.Network
|
|
Containers []container.Container
|
|
}
|
|
|
|
f, err := os.ReadFile(ConfigFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p := parse{}
|
|
err = toml.Unmarshal(f, &p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
Containers, Networks = p.Containers, p.Networks
|
|
for i := range p.Containers {
|
|
p.Containers[i].Init(conn, Networks)
|
|
}
|
|
slices.SortFunc(Containers, func(a, b container.Container) bool {
|
|
return a.Name < b.Name
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func Show(ids []string, contMask []string) string {
|
|
type parse struct {
|
|
Containers []container.Container `toml:"containers,omitempty"`
|
|
Networks []network.Network `toml:"networks,omitempty"`
|
|
}
|
|
|
|
getNet := func(name string) *network.Network {
|
|
for i := range Networks {
|
|
if Networks[i].Name == name {
|
|
return &Networks[i]
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
ipSliceEq := func(a, b []net.IP) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for i := range a {
|
|
if !bytes.Equal(a[i], b[i]) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
conts := Union(ids, contMask)
|
|
// clear out values equal to the defined networks
|
|
for c := range conts {
|
|
for n := range conts[c].Networks {
|
|
d := getNet(conts[c].Networks[n].Name)
|
|
if d == nil {
|
|
conts[c].LogEntry().WithField("network", conts[c].Networks[n].Name).Warnln("Network defaults not defined")
|
|
continue
|
|
}
|
|
if conts[c].Networks[n].IPv6 == d.IPv6 {
|
|
conts[c].Networks[n].IPv6 = null.Bool{}
|
|
}
|
|
if ipSliceEq(conts[c].Networks[n].DNS, d.DNS) {
|
|
conts[c].Networks[n].DNS = nil
|
|
}
|
|
}
|
|
}
|
|
usednets := hashset.New()
|
|
for _, c := range conts {
|
|
for _, n := range c.Networks {
|
|
usednets.Add(n.Name)
|
|
}
|
|
}
|
|
for _, un := range usednets.Values() {
|
|
ids = append(ids, un.(string))
|
|
}
|
|
nets := UnionNetworks(ids)
|
|
|
|
output, err := toml.Marshal(parse{conts, nets})
|
|
if err != nil {
|
|
fmt.Println("could not marshal containers:", err)
|
|
os.Exit(1)
|
|
}
|
|
return string(output)
|
|
}
|