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

Add SONiC VM support #2114

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions clab/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
vr_vsrx "github.com/srl-labs/containerlab/nodes/vr_vsrx"
vr_xrv "github.com/srl-labs/containerlab/nodes/vr_xrv"
vr_xrv9k "github.com/srl-labs/containerlab/nodes/vr_xrv9k"
vr_sonic "github.com/srl-labs/containerlab/nodes/vr_sonic"
xrd "github.com/srl-labs/containerlab/nodes/xrd"
)

Expand Down Expand Up @@ -82,6 +83,7 @@ func (c *CLab) RegisterNodes() {
vr_vjunosevolved.Register(c.Reg)
vr_xrv.Register(c.Reg)
vr_xrv9k.Register(c.Reg)
vr_sonic.Register(c.Reg)
xrd.Register(c.Reg)
rare.Register(c.Reg)
c8000.Register(c.Reg)
Expand Down
112 changes: 112 additions & 0 deletions nodes/vr_sonic/vr-sonic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2020 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
// The author of this code is Adam Kulagowski (adam.kulagowski@codilime.com), CodiLime (codilime.com).

package vr_sonic

import (
"context"
"fmt"
"path"

log "github.com/sirupsen/logrus"

"github.com/srl-labs/containerlab/clab/exec"
"github.com/srl-labs/containerlab/nodes"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)

var (
kindnames = []string{"vr-sonic"}
defaultCredentials = nodes.NewCredentials("admin", "YourPaSsWoRd")
hellt marked this conversation as resolved.
Show resolved Hide resolved
)

const (
configDirName = "config"
startupCfgFName = "config_db.json"
saveCmd = `sh -c "/backup.sh -u $USERNAME -p $PASSWORD backup"`
)

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
return new(vrSONiC)
}, defaultCredentials)
}

type vrSONiC struct {
nodes.DefaultNode
}

func (n *vrSONiC) Init(cfg *types.NodeConfig, opts ...nodes.NodeOption) error {
// Init DefaultNode
n.DefaultNode = *nodes.NewDefaultNode(n)
// set virtualization requirement
n.HostRequirements.VirtRequired = true

n.Cfg = cfg
for _, o := range opts {
o(n)
}
// env vars are used to set launch.py arguments in vrnetlab container
defEnv := map[string]string{
"USERNAME": defaultCredentials.GetUsername(),
"PASSWORD": defaultCredentials.GetPassword(),
"CONNECTION_MODE": nodes.VrDefConnMode,
"DOCKER_NET_V4_ADDR": n.Mgmt.IPv4Subnet,
"DOCKER_NET_V6_ADDR": n.Mgmt.IPv6Subnet,
}
n.Cfg.Env = utils.MergeStringMaps(defEnv, n.Cfg.Env)

// mount config dir to support startup-config functionality
n.Cfg.Binds = append(n.Cfg.Binds, fmt.Sprint(path.Join(n.Cfg.LabDir, configDirName), ":/config"))

if n.Cfg.Env["CONNECTION_MODE"] == "macvtap" {
// mount dev dir to enable macvtap
n.Cfg.Binds = append(n.Cfg.Binds, "/dev:/dev")
}

n.Cfg.Cmd = fmt.Sprintf("--username %s --password %s --hostname %s --connection-mode %s --trace",
defaultCredentials.GetUsername(), defaultCredentials.GetPassword(), n.Cfg.ShortName, n.Cfg.Env["CONNECTION_MODE"])

return nil
}

func (n *vrSONiC) PreDeploy(_ context.Context, params *nodes.PreDeployParams) error {
utils.CreateDirectory(n.Cfg.LabDir, 0777)
_, err := n.LoadOrGenerateCertificate(params.Cert, params.TopologyName)
if err != nil {
log.Errorf("Error handling certifcate for %s: %w", n.Cfg.ShortName, err)
return nil
}

return nodes.LoadStartupConfigFileVr(n, configDirName, startupCfgFName)
}

func (n *vrSONiC) SaveConfig(ctx context.Context) error {
cmd, err := exec.NewExecCmdFromString(saveCmd)
if err != nil {
return fmt.Errorf("%s: failed to create execute cmd: %w", n.Cfg.ShortName, err)
}

execResult, err := n.RunExec(ctx, cmd)
if err != nil {
return fmt.Errorf("%s: failed to execute cmd: %w", n.Cfg.ShortName, err)
}

if len(execResult.GetStdErrString()) > 0 {
return fmt.Errorf("%s errors: %s", n.Cfg.ShortName, execResult.GetStdErrString())
}

confPath := n.Cfg.LabDir + "/" + configDirName
log.Infof("saved /etc/sonic/config_db.json backup from %s node to %s\n", n.Cfg.ShortName, confPath)

return nil
}

// CheckInterfaceName checks if a name of the interface referenced in the topology file correct.
func (n *vrSONiC) CheckInterfaceName() error {
return nodes.GenericVMInterfaceCheck(n.Cfg.ShortName, n.Endpoints)
}