Skip to content

Commit

Permalink
Merge pull request #380 from srl-labs/move-node-dir-creation
Browse files Browse the repository at this point in the history
scope out node' files to respective node file
  • Loading branch information
hellt authored Apr 12, 2021
2 parents face507 + fb5f777 commit 0beed7e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 98 deletions.
2 changes: 1 addition & 1 deletion clab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (c *CLab) typeInit(nodeCfg *NodeConfig, kind string) string {

// configInit processes the path to a config file that can be provided on
// multiple configuration levels
// returns an errof if the reference path doesn't exist
// returns an error if the reference path doesn't exist
func (c *CLab) configInit(nodeCfg *NodeConfig, kind string) (string, error) {
var cfg string
var err error
Expand Down
36 changes: 36 additions & 0 deletions clab/crpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package clab
import (
"fmt"
"path"

log "github.com/sirupsen/logrus"
)

func initCrpdNode(c *CLab, nodeCfg NodeConfig, node *Node, user string, envs map[string]string) error {
Expand Down Expand Up @@ -32,3 +34,37 @@ func initCrpdNode(c *CLab, nodeCfg NodeConfig, node *Node, user string, envs map

return err
}

func (c *CLab) createCRPDFiles(node *Node) error {
// create config and logs directory that will be bind mounted to crpd
CreateDirectory(path.Join(node.LabDir, "config"), 0777)
CreateDirectory(path.Join(node.LabDir, "log"), 0777)

// copy crpd config from default template or user-provided conf file
cfg := path.Join(node.LabDir, "/config/juniper.conf")

err := node.generateConfig(cfg)
if err != nil {
log.Errorf("node=%s, failed to generate config: %v", node.ShortName, err)
}

// copy crpd sshd conf file to crpd node dir
src := "/etc/containerlab/templates/crpd/sshd_config"
dst := node.LabDir + "/config/sshd_config"
err = copyFile(src, dst)
if err != nil {
return fmt.Errorf("file copy [src %s -> dst %s] failed %v", src, dst, err)
}
log.Debugf("CopyFile src %s -> dst %s succeeded\n", src, dst)

if node.License != "" {
// copy license file to node specific lab directory
src = node.License
dst = path.Join(node.LabDir, "/config/license.conf")
if err = copyFile(src, dst); err != nil {
return fmt.Errorf("file copy [src %s -> dst %s] failed %v", src, dst, err)
}
log.Debugf("CopyFile src %s -> dst %s succeeded", src, dst)
}
return err
}
106 changes: 9 additions & 97 deletions clab/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"text/template"
Expand Down Expand Up @@ -150,118 +149,31 @@ func (c *CLab) CreateNodeDirStructure(node *Node) (err error) {

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

// copy license file to node specific directory in lab
src = node.License
dst = path.Join(node.LabDir, "license.key")
if err = copyFile(src, dst); err != nil {
return fmt.Errorf("CopyFile src %s -> dst %s failed %v", src, dst, err)
}
log.Debugf("CopyFile src %s -> dst %s succeeded", src, dst)

// generate SRL topology file
err = generateSRLTopologyFile(node.Topology, node.LabDir, node.Index)
if err != nil {
if err := c.createSRLFiles(node); err != nil {
return err
}

// generate a config file if the destination does not exist
// if the node has a `config:` statement, the file specified in that section
// will be used as a template in nodeGenerateConfig()
CreateDirectory(path.Join(node.LabDir, "config"), 0777)
dst = path.Join(node.LabDir, "config", "config.json")
if !fileExists(dst) {
err = node.generateConfig(dst)
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)
}

// copy env config to node specific directory in lab
src = "/etc/containerlab/templates/srl/srl_env.conf"
dst = node.LabDir + "/" + "srlinux.conf"
err = copyFile(src, dst)
if err != nil {
return fmt.Errorf("CopyFile src %s -> dst %s failed %v", src, dst, err)
}
log.Debugf("CopyFile src %s -> dst %s succeeded\n", src, dst)

case "linux":
case "ceos":
if err := c.createCEOSFiles(node); err != nil {
return err
}

case "crpd":
// create config and logs directory that will be bind mounted to crpd
CreateDirectory(path.Join(node.LabDir, "config"), 0777)
CreateDirectory(path.Join(node.LabDir, "log"), 0777)

// copy crpd config from default template or user-provided conf file
cfg := path.Join(node.LabDir, "/config/juniper.conf")
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)
}
// copy crpd sshd conf file to crpd node dir
src := "/etc/containerlab/templates/crpd/sshd_config"
dst := node.LabDir + "/config/sshd_config"
err = copyFile(src, dst)
if err != nil {
return fmt.Errorf("file copy [src %s -> dst %s] failed %v", src, dst, err)
}
log.Debugf("CopyFile src %s -> dst %s succeeded\n", src, dst)

if node.License != "" {
// copy license file to node specific lab directory
src = node.License
dst = path.Join(node.LabDir, "/config/license.conf")
if err = copyFile(src, dst); err != nil {
return fmt.Errorf("file copy [src %s -> dst %s] failed %v", src, dst, err)
}
log.Debugf("CopyFile src %s -> dst %s succeeded", src, dst)
if err := c.createCRPDFiles(node); err != nil {
return err
}
case "vr-sros":
// create config directory that will be bind mounted to vrnetlab container at / path
CreateDirectory(path.Join(node.LabDir, "tftpboot"), 0777)

if node.License != "" {
// copy license file to node specific lab directory
src := node.License
dst := path.Join(node.LabDir, "/tftpboot/license.txt")
if err = copyFile(src, dst); err != nil {
return fmt.Errorf("file copy [src %s -> dst %s] failed %v", src, dst, err)
}
log.Debugf("CopyFile src %s -> dst %s succeeded", src, dst)

cfg := path.Join(node.LabDir, "tftpboot", "config.txt")
if node.Config != "" {
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)
}
if err := c.createVrSROSFiles(node); err != nil {
return err
}
case "bridge":
default:
}

return nil
}

// GenerateConfig generates configuration for the nodes
func (node *Node) generateConfig(dst string) error {
if fileExists(dst) && (node.Config == defaultConfigTemplates[node.Kind]) {
log.Debugf("config file '%s' for node '%s' already exists and will not be generated", dst, node.ShortName)
return nil
}
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 {
Expand Down
41 changes: 41 additions & 0 deletions clab/srl.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,44 @@ func initSRLNode(c *CLab, nodeCfg NodeConfig, node *Node, user string, envs map[

return err
}

func (c *CLab) createSRLFiles(node *Node) error {
log.Debugf("Creating directory structure for SRL container: %s", node.ShortName)
var src string
var dst string

// copy license file to node specific directory in lab
src = node.License
dst = path.Join(node.LabDir, "license.key")
if err := copyFile(src, dst); err != nil {
return fmt.Errorf("CopyFile src %s -> dst %s failed %v", src, dst, err)
}
log.Debugf("CopyFile src %s -> dst %s succeeded", src, dst)

// generate SRL topology file
err := generateSRLTopologyFile(node.Topology, node.LabDir, node.Index)
if err != nil {
return err
}

// generate a config file if the destination does not exist
// if the node has a `config:` statement, the file specified in that section
// will be used as a template in nodeGenerateConfig()
CreateDirectory(path.Join(node.LabDir, "config"), 0777)
dst = path.Join(node.LabDir, "config", "config.json")
err = node.generateConfig(dst)
if err != nil {
log.Errorf("node=%s, failed to generate config: %v", node.ShortName, err)
}

// copy env config to node specific directory in lab
src = "/etc/containerlab/templates/srl/srl_env.conf"
dst = node.LabDir + "/" + "srlinux.conf"
err = copyFile(src, dst)
if err != nil {
return fmt.Errorf("CopyFile src %s -> dst %s failed %v", src, dst, err)
}
log.Debugf("CopyFile src %s -> dst %s succeeded\n", src, dst)

return err
}
28 changes: 28 additions & 0 deletions clab/vr-sros.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package clab
import (
"fmt"
"path"

log "github.com/sirupsen/logrus"
)

func initSROSNode(c *CLab, nodeCfg NodeConfig, node *Node, user string, envs map[string]string) error {
Expand Down Expand Up @@ -47,3 +49,29 @@ func initSROSNode(c *CLab, nodeCfg NodeConfig, node *Node, user string, envs map
)
return err
}

func (c *CLab) createVrSROSFiles(node *Node) error {
// create config directory that will be bind mounted to vrnetlab container at / path
CreateDirectory(path.Join(node.LabDir, "tftpboot"), 0777)

if node.License != "" {
// copy license file to node specific lab directory
src := node.License
dst := path.Join(node.LabDir, "/tftpboot/license.txt")
if err := copyFile(src, dst); err != nil {
return fmt.Errorf("file copy [src %s -> dst %s] failed %v", src, dst, err)
}
log.Debugf("CopyFile src %s -> dst %s succeeded", src, dst)

cfg := path.Join(node.LabDir, "tftpboot", "config.txt")
if node.Config != "" {
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)
}
}
return nil
}

0 comments on commit 0beed7e

Please sign in to comment.