ccl/internal/pkg/config/config.go
Joel D. Elkins b758f04a50 Migrate to podman API rather than cli
Large change greatly expanding the linked footprint of this little
utility, but it is much faster and adds a couple of very nice features:

- set container sysctl settings to disable router advertisements as part
  of container definition. this means we no longer need to do this via
  `ip netns exec <container> sysctl -w ...` (followed by
  `ip netns exec ip -6 a flush...`). Major win.
- able to very quickly ascertain creation and run state of defined
  containers.
2022-07-25 10:13:36 -05:00

84 lines
1.7 KiB
Go

package config
import (
"context"
"os"
"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"
"golang.org/x/exp/slices"
)
const (
CONFIG_FILE_DEFAULT = "/etc/ccl.toml"
)
type command string
var (
ConfigFile string = CONFIG_FILE_DEFAULT
networks = &[]network.Network{}
containers = &[]container.Container{}
categories = &[]string{}
)
func Categories() []string {
if categories != nil {
return *categories
}
categories = &[]string{"all"}
gs := hashset.New()
for _, c := range *containers {
gs.Add(c.Category)
}
for _, c := range gs.Values() {
*categories = append(*categories, c.(string))
}
return *categories
}
func Union(ids []string) (conts []container.Container) {
if len(ids) == 0 {
return *containers
}
h := hashset.New()
for _, id := range ids {
for _, c := range *containers {
if id == "all" || c.Name == id || c.Category == 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])
}
return
}
// A parsing convenience
type parse struct {
Networks []network.Network
Containers []container.Container
}
func Init(conn context.Context) error {
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)
}
return nil
}