-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #383 from kellerza/config
Config template support 🚀
- Loading branch information
Showing
24 changed files
with
1,376 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
repos: | ||
- repo: https://github.com/codespell-project/codespell | ||
rev: v2.0.0 | ||
hooks: | ||
- id: codespell | ||
- repo: https://github.com/syntaqx/git-hooks | ||
rev: v0.0.17 | ||
hooks: | ||
- id: forbid-binary | ||
- id: go-fmt | ||
- id: go-test | ||
- id: go-mod-tidy | ||
- id: shfmt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package config | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// Split a string on commas and trim each line | ||
func SplitTrim(s string) []string { | ||
res := strings.Split(s, ",") | ||
for i, v := range res { | ||
res[i] = strings.Trim(v, " \n\t") | ||
} | ||
return res | ||
} | ||
|
||
// The new agreed node config | ||
type NodeSettings struct { | ||
Vars map[string]string | ||
Transport string | ||
Templates []string | ||
} | ||
|
||
// Temporary function to extract NodeSettings from the Labels | ||
// In the next phase node settings will be added to the clab file | ||
func GetNodeConfig(labels map[string]string) NodeSettings { | ||
nc := NodeSettings{ | ||
Vars: labels, | ||
Transport: "ssh", | ||
} | ||
if len(TemplateNames) > 0 { | ||
nc.Templates = TemplateNames | ||
} else if t, ok := labels["templates"]; ok { | ||
nc.Templates = SplitTrim(t) | ||
} else { | ||
nc.Templates = []string{"base"} | ||
} | ||
if t, ok := labels["transport"]; ok { | ||
nc.Transport = t | ||
} | ||
return nc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
jT "github.com/kellerza/template" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/srl-labs/containerlab/nodes" | ||
"github.com/srl-labs/containerlab/types" | ||
) | ||
|
||
// templates to execute | ||
var TemplateNames []string | ||
|
||
// path to additional templates | ||
var TemplatePaths []string | ||
|
||
type NodeConfig struct { | ||
TargetNode *types.NodeConfig | ||
// All the variables used to render the template | ||
Vars map[string]interface{} | ||
// the Rendered templates | ||
Data []string | ||
Info []string | ||
} | ||
|
||
func RenderAll(nodes map[string]nodes.Node, links map[int]*types.Link) (map[string]*NodeConfig, error) { | ||
res := make(map[string]*NodeConfig) | ||
|
||
if len(TemplatePaths) == 0 { | ||
return nil, fmt.Errorf("please specify one of more paths with --template-path") | ||
} | ||
|
||
if len(TemplateNames) == 0 { | ||
var err error | ||
TemplateNames, err = GetTemplateNamesInDirs(TemplatePaths) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(TemplateNames) == 0 { | ||
return nil, fmt.Errorf("no templates files were found by %s path", TemplatePaths) | ||
} | ||
} | ||
|
||
tmpl, err := jT.New("", jT.SearchPath(TemplatePaths...)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for nodeName, vars := range PrepareVars(nodes, links) { | ||
res[nodeName] = &NodeConfig{ | ||
TargetNode: nodes[nodeName].Config(), | ||
Vars: vars, | ||
} | ||
|
||
for _, baseN := range TemplateNames { | ||
tmplN := fmt.Sprintf("%s__%s.tmpl", baseN, vars["role"]) | ||
data1, err := tmpl.ExecuteTemplate(tmplN, vars) | ||
if err != nil { | ||
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) | ||
} | ||
} | ||
return res, nil | ||
} | ||
|
||
// Implement stringer for conf snippet | ||
func (c *NodeConfig) String() string { | ||
|
||
s := fmt.Sprintf("%s: %v", c.TargetNode.ShortName, c.Info) | ||
return s | ||
} | ||
|
||
// Print the config | ||
func (c *NodeConfig) Print(printLines int) { | ||
var s strings.Builder | ||
|
||
s.WriteString(c.TargetNode.ShortName) | ||
|
||
if log.IsLevelEnabled(log.DebugLevel) { | ||
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, "\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(l) | ||
} | ||
s.WriteString("\n ]]") | ||
} | ||
} | ||
|
||
log.Infoln(s.String()) | ||
} |
Oops, something went wrong.