All checks were successful
Tests / test (push) Successful in 3m46s
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
90 lines
2.5 KiB
Go
90 lines
2.5 KiB
Go
package topology
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"text/tabwriter"
|
|
)
|
|
|
|
func (p *Plan) Write(w io.Writer) error {
|
|
tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
|
|
|
|
fmt.Fprintf(tw, "lab %s: nodes %d, segments %d, ports %d\n", p.Name, len(p.Nodes), len(p.Segments), len(p.Ports))
|
|
|
|
fmt.Fprintf(tw, "\nnodes\n")
|
|
fmt.Fprintf(tw, " name\trole\timage\tcpus\tmemory\tssh\n")
|
|
for _, n := range p.Nodes {
|
|
fmt.Fprintf(tw, " %s\t%s\t%s\t%d\t%d MiB\t127.0.0.1:%d\n", n.Name, n.Role, n.Image, n.CPUs, n.Memory, n.SSHPort)
|
|
}
|
|
|
|
var extras []NodePlan
|
|
for _, n := range p.Nodes {
|
|
if len(n.Secondary) > 0 || n.Loopback.IsValid() || n.FRR != "" || n.Provision != "" || n.PostProvision != "" || len(n.Files) > 0 || len(n.Services) > 0 {
|
|
extras = append(extras, n)
|
|
}
|
|
}
|
|
if len(extras) > 0 {
|
|
fmt.Fprintf(tw, "\nroles\n")
|
|
fmt.Fprintf(tw, " name\tloopback\tsecondary\tfrr\tprovision\tpost_provision\tfiles\tservices\n")
|
|
for _, n := range extras {
|
|
fmt.Fprintf(tw, " %s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", n.Name, orDash(loopback(n)), orDash(secondary(n)),
|
|
orDash(filepath.Base(n.FRR)), orDash(filepath.Base(n.Provision)), orDash(filepath.Base(n.PostProvision)),
|
|
orDash(strings.Join(sortedKeys(n.Files), ",")), orDash(strings.Join(n.Services, ",")))
|
|
}
|
|
}
|
|
|
|
for _, s := range p.Segments {
|
|
fmt.Fprintf(tw, "\nsegment %s: %s, mtu %d, bridge %s, %s\n", s.Name, s.Network, s.MTU, s.Bridge, gateway(s))
|
|
fmt.Fprintf(tw, " node\tinterface\taddress\tmac\ttap\n")
|
|
for _, port := range p.Ports {
|
|
if port.Segment != s.Name {
|
|
continue
|
|
}
|
|
fmt.Fprintf(tw, " %s\t%s\t%s\t%s\t%s\n", port.Node, port.Interface, port.Address, port.MAC, port.Tap)
|
|
}
|
|
}
|
|
return tw.Flush()
|
|
}
|
|
|
|
func gateway(s SegmentPlan) string {
|
|
switch {
|
|
case len(s.Routers) == 0:
|
|
return "no gateway"
|
|
case s.VRID == 0:
|
|
return fmt.Sprintf("gateway %s on %s", s.Gateway, s.Routers[0])
|
|
default:
|
|
return fmt.Sprintf("gateway %s by vrrp %d on %s", s.Gateway, s.VRID, strings.Join(s.Routers, ", "))
|
|
}
|
|
}
|
|
|
|
func loopback(n NodePlan) string {
|
|
if !n.Loopback.IsValid() {
|
|
return ""
|
|
}
|
|
return LoopbackInterface + " " + n.Loopback.String()
|
|
}
|
|
|
|
func secondary(n NodePlan) string {
|
|
segments := make([]string, 0, len(n.Secondary))
|
|
for s := range n.Secondary {
|
|
segments = append(segments, s)
|
|
}
|
|
sort.Strings(segments)
|
|
var parts []string
|
|
for _, s := range segments {
|
|
for _, prefix := range n.Secondary[s] {
|
|
parts = append(parts, s+" "+prefix.String())
|
|
}
|
|
}
|
|
return strings.Join(parts, ", ")
|
|
}
|
|
|
|
func orDash(s string) string {
|
|
if s == "" || s == "." {
|
|
return "-"
|
|
}
|
|
return s
|
|
}
|