Skip to content

Commit

Permalink
templateok
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerza committed Jun 9, 2021
1 parent 50e974c commit c5f771d
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 52 deletions.
18 changes: 10 additions & 8 deletions clab/config/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ func RenderAll(nodes map[string]*types.Node, links map[int]*types.Link) (map[str
}

for _, baseN := range TemplateNames {
tmplN := fmt.Sprintf("%s-%s.tmpl", baseN, vars["roles"])
tmplN := fmt.Sprintf("%s-%s.tmpl", baseN, vars["role"])
data1, err := tmpl.ExecuteTemplate(tmplN, vars)
if err != nil {
log.Errorf("could not render template %s: %s", tmplN, err)
//log.Errorf("could not render template %s: %s vars=%s\n", c.String(), err, varsP)
return nil, fmt.Errorf("could not render template %s: %s", tmplN, err)
return nil, err
}
data1 = strings.ReplaceAll(strings.Trim(data1, "\n \t"), "\n\n\n", "\n\n")
res[nodeName].Data = append(res[nodeName].Data, data1)
res[nodeName].Info = append(res[nodeName].Info, tmplN)
}
Expand All @@ -73,22 +72,25 @@ func (c *NodeConfig) Print(printLines int) {
s.WriteString(c.TargetNode.ShortName)

if log.IsLevelEnabled(log.DebugLevel) {
vars, _ := json.MarshalIndent(c.Vars, "", " ")
s.Write(vars)
s.WriteString(" vars = ")
vars, _ := json.MarshalIndent(c.Vars, "", " ")
s.Write(vars[0 : len(vars)-1])
s.WriteString(" }")
}

if printLines > 0 {
for idx, conf := range c.Data {
fmt.Fprintf(&s, "%s", c.Info[idx])
fmt.Fprintf(&s, "\n Template %s for %s = [[", c.Info[idx], c.TargetNode.ShortName)

cl := strings.SplitN(conf, "\n", printLines+1)
if len(cl) > printLines {
cl[printLines] = "..."
}
for _, l := range cl {
s.WriteString("\n ")
s.WriteString("\n ")
s.WriteString(l)
}
s.WriteString("\n ]]")
}
}

Expand Down
8 changes: 4 additions & 4 deletions clab/config/transport/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type SSHSession struct {
Session *ssh.Session
}

type SSHOption func(*SSHTransport) error
type SSHTransportOption func(*SSHTransport) error

// The reply the execute command and the prompt.
type SSHReply struct{ result, prompt, command string }
Expand Down Expand Up @@ -51,7 +51,7 @@ type SSHTransport struct {
K SSHKind
}

func NewSSHTransport(node *types.Node, options ...SSHOption) (*SSHTransport, error) {
func NewSSHTransport(node *types.Node, options ...SSHTransportOption) (*SSHTransport, error) {
switch node.Kind {
case "vr-sros", "srl":
c := &SSHTransport{}
Expand Down Expand Up @@ -236,7 +236,7 @@ func (t *SSHTransport) Write(data, info *string) error {

// Connect to a host
// Part of the Transport interface
func (t *SSHTransport) Connect(host string, options ...func(*Transport)) error {
func (t *SSHTransport) Connect(host string, options ...TransportOption) error {
// Assign Default Values
if t.PromptChar == "" {
t.PromptChar = "#"
Expand Down Expand Up @@ -279,7 +279,7 @@ func (t *SSHTransport) Close() {

// Add a basic username & password to a config.
// Will initilize the config if required
func WithUserNamePassword(username, password string) SSHOption {
func WithUserNamePassword(username, password string) SSHTransportOption {
return func(tx *SSHTransport) error {
if tx.SSHConfig == nil {
tx.SSHConfig = &ssh.ClientConfig{}
Expand Down
6 changes: 4 additions & 2 deletions clab/config/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import (
// Debug count
var DebugCount int

type TransportOption func(*Transport)

type Transport interface {
// Connect to the target host
Connect(host string, options ...func(*Transport)) error
Connect(host string, options ...TransportOption) error
// Execute some config
Write(data *string, info *string) error
Close()
}

func Write(tx Transport, host string, data, info []string, options ...func(*Transport)) error {
func Write(tx Transport, host string, data, info []string, options ...TransportOption) error {
// the Kind should configure the transport parameters before

err := tx.Connect(host, options...)
Expand Down
27 changes: 13 additions & 14 deletions clab/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ func PrepareVars(nodes map[string]*types.Node, links map[int]*types.Link) map[st
// Init array for this node
res[name] = make(map[string]interface{})
nc := GetNodeConfigFromLabels(node.Labels)
for _, key := range nc.Vars {
for key := range nc.Vars {
res[name][key] = nc.Vars[key]
}
// Create link array
res[name]["links"] = make([]interface{}, 2)
res[name]["links"] = []interface{}{}
// Ensure role or Kind
if _, ok := res[name]["role"]; !ok {
res[name]["role"] = node.Kind
Expand Down Expand Up @@ -61,8 +61,8 @@ func prepareLinkVars(lIdx int, link *types.Link, varsA, varsB map[string]interfa
if len(v2) == 0 {
varsB[key] = v1
} else {
varsA[key+"_far"] = v2[1]
varsB[key] = v2[1]
varsA[key+"_far"] = v2[0]
varsB[key] = v2[0]
varsB[key+"_far"] = v1
}
}
Expand All @@ -88,18 +88,15 @@ func prepareLinkVars(lIdx int, link *types.Link, varsA, varsB map[string]interfa
}
}

//Repeat the following for varsA and varsB
for _, vars := range []map[string]interface{}{varsA, varsB} {
// Set default Link/Interface Names
if _, ok := vars["name"]; !ok {
var linkNr string
if v, ok := vars["linkNr"]; ok {
linkNr = fmt.Sprintf("_%v", v)
}
vars["name"] = []string{fmt.Sprintf("to_%s%s", link.B.Node.ShortName, linkNr),
fmt.Sprintf("to_%s%s", link.A.Node.ShortName, linkNr)}
if _, ok := varsA["name"]; !ok {
var linkNr string
if v, ok := varsA["linkNr"]; ok {
linkNr = fmt.Sprintf("_%v", v)
}
addV("name", fmt.Sprintf("to_%s%s", link.B.Node.ShortName, linkNr),
fmt.Sprintf("to_%s%s", link.A.Node.ShortName, linkNr))
}

return nil
}

Expand Down Expand Up @@ -172,3 +169,5 @@ func ipFarEnd(in netaddr.IPPrefix) netaddr.IPPrefix {
}
return netaddr.IPPrefixFrom(n, in.Bits())
}

// DictString
15 changes: 1 addition & 14 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"sync"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -35,32 +34,20 @@ var configCmd = &cobra.Command{
clab.WithTopoFile(topo),
)

//ctx, cancel := context.WithCancel(context.Background())
//defer cancel()

setFlags(c.Config)
log.Debugf("Topology definition: %+v", c.Config)
// Parse topology information
if err = c.ParseTopology(); err != nil {
return err
}

// config map per node. each node gets a couple of config snippets []string
// config map per node. each node gets a config.NodeConfig
allConfig, err := config.RenderAll(c.Nodes, c.Links)
if err != nil {
return err
}

// render them all

renderErr := 0

if renderErr > 0 {
return fmt.Errorf("%d render warnings", renderErr)
}

if printLines > 0 {
// Debug log all config to be deployed
for _, c := range allConfig {
c.Print(printLines)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/google/uuid v1.2.0
github.com/hashicorp/go-version v1.2.1
github.com/jsimonetti/rtnetlink v0.0.0-20210226120601-1b79e63a70a0
github.com/kellerza/template v0.0.3
github.com/kellerza/template v0.0.4
github.com/mitchellh/go-homedir v1.1.0
github.com/morikuni/aec v1.0.0 // indirect
github.com/olekukonko/tablewriter v0.0.5-0.20201029120751-42e21c7531a3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kellerza/template v0.0.3 h1:JWRnuMZnTHO1onmSj3HYQA/CJqn4x+CiKFD+bbD0SNE=
github.com/kellerza/template v0.0.3/go.mod h1:Og3Jdssypk1J/bNpWIrmymW5FOWI88vsY5Qx/e57V7M=
github.com/kellerza/template v0.0.4 h1:z/qhD9D50bEhbTu/1JN6ANiTECws8rKQBKmLDdwK6hs=
github.com/kellerza/template v0.0.4/go.mod h1:Og3Jdssypk1J/bNpWIrmymW5FOWI88vsY5Qx/e57V7M=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down
7 changes: 0 additions & 7 deletions templates/config/base-vr-sros.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@

{{ end }}



/configure apply-groups ["baseport"]
/configure router bgp apply-groups ["basebgp"]

Expand Down Expand Up @@ -124,11 +122,6 @@
}
}
}
# port "<.*c[0-9]+>" {
# connector {
# breakout c1-10g
# }
# }
}
}
/configure groups {
Expand Down

0 comments on commit c5f771d

Please sign in to comment.