Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for config generation for ceos #173

Merged
merged 18 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions clab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ const (
dockerNetName = "clab"
dockerNetIPv4Addr = "172.20.20.0/24"
dockerNetIPv6Addr = "2001:172:20:20::/80"

defaultConfigTemplate = "/etc/containerlab/templates/srl/srlconfig.tpl"
)

// supported kinds
var kinds = []string{"srl", "ceos", "linux", "alpine", "bridge"}

var defaultConfigTemplates = map[string]string{
"srl": "/etc/containerlab/templates/srl/srlconfig.tpl",
"ceos": "/etc/containerlab/templates/arista/ceos.cfg.tpl",
}

var srlTypes = map[string]string{
"ixr6": "topology-7250IXR6.yml",
"ixr10": "topology-7250IXR10.yml",
Expand Down Expand Up @@ -229,7 +232,7 @@ func (c *cLab) configInitialization(nodeCfg *NodeConfig, kind string) string {
if c.Config.Topology.Defaults.Config != "" {
return c.Config.Topology.Defaults.Config
}
return defaultConfigTemplate
return defaultConfigTemplates[kind]
}

func (c *cLab) imageInitialization(nodeCfg *NodeConfig, kind string) string {
Expand Down Expand Up @@ -312,9 +315,7 @@ func (c *cLab) NewNode(nodeName string, nodeCfg NodeConfig, idx int) error {
case "ceos":
// initialize the global parameters with defaults, can be overwritten later
node.Config = c.configInitialization(&nodeCfg, node.Kind)
//node.License = t.SRLLicense
node.Image = c.imageInitialization(&nodeCfg, node.Kind)
//node.NodeType = "ixr6"
node.Position = c.positionInitialization(&nodeCfg, node.Kind)

// initialize specifc container information
Expand All @@ -330,7 +331,6 @@ func (c *cLab) NewNode(nodeName string, nodeCfg NodeConfig, idx int) error {
node.User = "root"
node.Group = c.groupInitialization(&nodeCfg, node.Kind)
node.NodeType = nodeCfg.Type
node.Config = nodeCfg.Config

node.Sysctls = make(map[string]string)
node.Sysctls["net.ipv4.ip_forward"] = "0"
Expand All @@ -340,6 +340,10 @@ func (c *cLab) NewNode(nodeName string, nodeCfg NodeConfig, idx int) error {
node.Sysctls["net.ipv6.conf.all.autoconf"] = "0"
node.Sysctls["net.ipv6.conf.default.autoconf"] = "0"

// mount config file
cfgPath := filepath.Join(node.LabDir, "config", "startup-config")
node.Binds = append(node.Binds, fmt.Sprint(cfgPath, ":/mnt/flash/startup-config"))

case "srl":
// initialize the global parameters with defaults, can be overwritten later
node.Config = c.configInitialization(&nodeCfg, node.Kind)
Expand Down
24 changes: 19 additions & 5 deletions clab/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,22 @@ func CreateDirectory(path string, perm os.FileMode) {
}
}

// CreateNodeDirStructure create the directory structure and files for the clab
// CreateNodeDirStructure create the directory structure and files for the lab nodes
func (c *cLab) CreateNodeDirStructure(node *Node) (err error) {
c.m.RLock()
defer c.m.RUnlock()

// create node directory in the lab directory
if node.Kind != "linux" && node.Kind != "bridge" {
CreateDirectory(node.LabDir, 0777)
}

switch node.Kind {
case "srl":
log.Infof("Create directory structure for SRL container: %s", node.ShortName)
var src string
var dst string

// create node directory in lab
CreateDirectory(node.LabDir, 0777)

// copy license file to node specific directory in lab
src = node.License
dst = path.Join(node.LabDir, "license.key")
Expand Down Expand Up @@ -180,9 +183,19 @@ func (c *cLab) CreateNodeDirStructure(node *Node) (err error) {
}
log.Debugf("CopyFile src %s -> dst %s succeeded\n", src, dst)

case "alpine":
case "linux":
case "ceos":
// generate config directory
CreateDirectory(path.Join(node.LabDir, "config"), 0777)
cfg := path.Join(node.LabDir, "config", "startup-config")
if !fileExists(cfg) {
err = node.generateConfig(cfg)
if err != nil {
log.Errorf("node=%s, failed to generate config: %v", node.ShortName, err)
}
} else {
log.Debugf("Config file exists for node %s", node.ShortName)
}
case "bridge":
default:
}
Expand All @@ -192,6 +205,7 @@ func (c *cLab) CreateNodeDirStructure(node *Node) (err error) {

// GenerateConfig generates configuration for the nodes
func (node *Node) generateConfig(dst string) error {
log.Debugf("generating config for node %s from file %s", node.ShortName, node.Config)
tpl, err := template.New(filepath.Base(node.Config)).ParseFiles(node.Config)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions templates/arista/ceos.cfg.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hostname {{ .ShortName }}
karimra marked this conversation as resolved.
Show resolved Hide resolved