Skip to content

Commit

Permalink
remove unused functions
Browse files Browse the repository at this point in the history
  • Loading branch information
karimra committed Jun 20, 2021
1 parent 30081bf commit 8faf5c6
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 128 deletions.
2 changes: 1 addition & 1 deletion clab/ceos.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ func (c *CLab) createCEOSFiles(node *types.NodeConfig) error {
return err
}
m[5] = m[5] + 1
createFile(path.Join(node.LabDir, "flash", "system_mac_address"), m.String())
utils.CreateFile(path.Join(node.LabDir, "flash", "system_mac_address"), m.String())
return nil
}
6 changes: 3 additions & 3 deletions clab/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ func (c *CLab) RetrieveNodeCertData(n *types.NodeConfig) (*Certificates, error)
}

func (c *CLab) writeCertFiles(certs *Certificates, filesPrefix string) {
createFile(filesPrefix+".pem", string(certs.Cert))
createFile(filesPrefix+"-key.pem", string(certs.Key))
createFile(filesPrefix+".csr", string(certs.Csr))
utils.CreateFile(filesPrefix+".pem", string(certs.Cert))
utils.CreateFile(filesPrefix+"-key.pem", string(certs.Key))
utils.CreateFile(filesPrefix+".csr", string(certs.Csr))
}

//CreateRootCA creates RootCA key/certificate if it is needed by the topology
Expand Down
6 changes: 3 additions & 3 deletions clab/clab.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ func (c *CLab) CreateNodes(ctx context.Context, workers uint) {
defer wg.Done()
for {
select {
case node := <-nodesChan:
if node == nil {
case node, ok := <-nodesChan:
if node == nil || !ok {
log.Debugf("Worker %d terminating...", i)
continue
return
}
log.Debugf("Worker %d received node: %+v", i, node)
if node.Kind == "bridge" || node.Kind == "ovs-bridge" {
Expand Down
65 changes: 26 additions & 39 deletions clab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,24 +261,15 @@ func (c *CLab) initializeNodeCfg(nodeCfg *types.NodeConfig) error {

// NewLink initializes a new link object
func (c *CLab) NewLink(l *types.LinkConfig) *types.Link {
// initialize a new link
link := new(types.Link)
link.Labels = l.Labels

if link.MTU <= 0 {
link.MTU = defaultVethLinkMTU
if len(l.Endpoints) != 2 {
log.Fatalf("endpoint %q has wrong syntax, unexpected number of items", l.Endpoints)
}

for i, d := range l.Endpoints {
// i indicates the number and d presents the string, which need to be
// split in node and endpoint name
if i == 0 {
link.A = c.NewEndpoint(d)
} else {
link.B = c.NewEndpoint(d)
}
return &types.Link{
A: c.NewEndpoint(l.Endpoints[0]),
B: c.NewEndpoint(l.Endpoints[1]),
MTU: defaultVethLinkMTU,
Labels: l.Labels,
}
return link
}

// NewEndpoint initializes a new endpoint object
Expand All @@ -291,9 +282,13 @@ func (c *CLab) NewEndpoint(e string) *types.Endpoint {
if len(split) != 2 {
log.Fatalf("endpoint %s has wrong syntax", e) // skipcq: GO-S0904
}
nName := split[0] // node name
epName := split[1] // endpoint name
nName := split[0] // node name

// initialize the endpoint name based on the split function
endpoint.EndpointName = split[1] // endpoint name
if len(endpoint.EndpointName) > 15 {
log.Fatalf("interface '%s' name exceeds maximum length of 15 characters", endpoint.EndpointName)
}
// generate unqiue MAC
endpoint.MAC = genMac(clabOUI)

Expand All @@ -315,13 +310,12 @@ func (c *CLab) NewEndpoint(e string) *types.Endpoint {
ShortName: "mgmt-net",
}
default:
for name, n := range c.Nodes {
if name == nName {
endpoint.Node = n
n.Endpoints = append(n.Endpoints, endpoint)
break
}
c.m.Lock()
if n, ok := c.Nodes[nName]; ok {
endpoint.Node = n
n.Endpoints = append(n.Endpoints, endpoint)
}
c.m.Unlock()
}

// stop the deployment if the matching node element was not found
Expand All @@ -330,38 +324,31 @@ func (c *CLab) NewEndpoint(e string) *types.Endpoint {
log.Fatalf("not all nodes are specified in the 'topology.nodes' section or the names don't match in the 'links.endpoints' section: %s", nName) // skipcq: GO-S0904
}

// initialize the endpoint name based on the split function
endpoint.EndpointName = epName
if len(endpoint.EndpointName) > 15 {
log.Fatalf("interface '%s' name exceeds maximum length of 15 characters", endpoint.EndpointName)
}
return endpoint
}

// CheckTopologyDefinition runs topology checks and returns any errors found
func (c *CLab) CheckTopologyDefinition(ctx context.Context) error {
if err := c.verifyBridgesExist(); err != nil {
return err
}
if err := c.verifyLinks(); err != nil {
var err error
if err = c.verifyBridgesExist(); err != nil {
return err
}
if err := c.verifyRootNetnsInterfaceUniqueness(); err != nil {
if err = c.verifyLinks(); err != nil {
return err
}
if err := c.VerifyContainersUniqueness(ctx); err != nil {
if err = c.verifyRootNetnsInterfaceUniqueness(); err != nil {
return err
}
if err := c.verifyVirtSupport(); err != nil {
if err = c.VerifyContainersUniqueness(ctx); err != nil {
return err
}
if err := c.verifyHostIfaces(); err != nil {
if err = c.verifyVirtSupport(); err != nil {
return err
}
if err := c.VerifyImages(ctx); err != nil {
if err = c.verifyHostIfaces(); err != nil {
return err
}
if err := c.VerifyImages(ctx); err != nil { // skipcq: RVV-B0005
if err = c.VerifyImages(ctx); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions clab/crpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c *CLab) createCRPDFiles(nodeCfg *types.NodeConfig) error {
// copy crpd sshd conf file to crpd node dir
src := "/etc/containerlab/templates/crpd/sshd_config"
dst := path.Join(nodeCfg.LabDir, "/config/sshd_config")
err = copyFile(src, dst)
err = utils.CopyFile(src, dst)
if err != nil {
return fmt.Errorf("file copy [src %s -> dst %s] failed %v", src, dst, err)
}
Expand All @@ -63,7 +63,7 @@ func (c *CLab) createCRPDFiles(nodeCfg *types.NodeConfig) error {
// copy license file to node specific lab directory
src = nodeCfg.License
dst = path.Join(nodeCfg.LabDir, "/config/license.conf")
if err = copyFile(src, dst); err != nil {
if err = utils.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)
Expand Down
81 changes: 5 additions & 76 deletions clab/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ package clab

import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -39,90 +38,20 @@ func (c *CLab) GetTopology(topo string) error {
return err
}

path, _ := filepath.Abs(topo)
topoAbsPath, err := filepath.Abs(topo)
if err != nil {
return err
}

s := strings.Split(topo, "/")
file := s[len(s)-1]
filename := strings.Split(file, ".")
file := path.Base(topo)
c.TopoFile = &TopoFile{
path: path,
path: topoAbsPath,
fullName: file,
name: filename[0],
name: strings.TrimSuffix(file, path.Ext(file)),
}
return nil
}

// CopyFile copies a file from src to dst. If src and dst files exist, and are
// the same, then return success. Otherwise, copy the file contents from src to dst.
func copyFile(src, dst string) (err error) {
sfi, err := os.Stat(src)
if err != nil {
return err
}
if !sfi.Mode().IsRegular() {
// cannot copy non-regular files (e.g., directories,
// symlinks, devices, etc.)
return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
}
dfi, err := os.Stat(dst)
if err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
if !(dfi.Mode().IsRegular()) {
return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
}
if os.SameFile(sfi, dfi) {
return
}
}
return copyFileContents(src, dst)
}

// copyFileContents copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file.
func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
err = out.Sync()
return
}

func createFile(file, content string) {
var f *os.File
f, err := os.Create(file)
if err != nil {
panic(err)
}
defer f.Close()

if _, err := f.WriteString(content + "\n"); err != nil {
panic(err)
}
}

// CreateNodeDirStructure create the directory structure and files for the lab nodes
func (c *CLab) CreateNodeDirStructure(node *types.NodeConfig) (err error) {
c.m.RLock()
Expand Down
2 changes: 1 addition & 1 deletion clab/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (c *CLab) GenerateGraph(topo string) error {

// create graph filename
dotfile := c.Dir.LabGraph + "/" + c.TopoFile.name + ".dot"
createFile(dotfile, g.String())
utils.CreateFile(dotfile, g.String())
log.Infof("Created %s", dotfile)

pngfile := c.Dir.LabGraph + "/" + c.TopoFile.name + ".png"
Expand Down
4 changes: 2 additions & 2 deletions clab/srl.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (c *CLab) createSRLFiles(node *types.NodeConfig) error {
// 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 {
if err := utils.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)
Expand All @@ -155,7 +155,7 @@ func (c *CLab) createSRLFiles(node *types.NodeConfig) error {
// 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)
err = utils.CopyFile(src, dst)
if err != nil {
return fmt.Errorf("CopyFile src %s -> dst %s failed %v", src, dst, err)
}
Expand Down
2 changes: 1 addition & 1 deletion clab/vr-sros.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (c *CLab) createVrSROSFiles(node *types.NodeConfig) error {
// 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 {
if err := utils.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)
Expand Down

0 comments on commit 8faf5c6

Please sign in to comment.