-
Notifications
You must be signed in to change notification settings - Fork 243
/
vr-openbsd.go
104 lines (84 loc) · 2.93 KB
/
vr-openbsd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2020 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
package vr_openbsd
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{"openbsd"}
defaultCredentials = nodes.NewCredentials("admin", "admin")
saveCmd = "sh -c \"/backup.sh -u $USERNAME -p $PASSWORD backup\""
)
const (
configDirName = "config"
)
// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
return new(vrOpenBSD)
}, defaultCredentials)
}
type vrOpenBSD struct {
nodes.DefaultNode
}
func (n *vrOpenBSD) 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{
"CONNECTION_MODE": nodes.VrDefConnMode,
"USERNAME": defaultCredentials.GetUsername(),
"PASSWORD": defaultCredentials.GetPassword(),
"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 config backup 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",
n.Cfg.Env["USERNAME"], n.Cfg.Env["PASSWORD"], n.Cfg.ShortName, n.Cfg.Env["CONNECTION_MODE"])
return nil
}
func (n *vrOpenBSD) PreDeploy(_ context.Context, params *nodes.PreDeployParams) error {
utils.CreateDirectory(n.Cfg.LabDir, 0777)
_, err := n.LoadOrGenerateCertificate(params.Cert, params.TopologyName)
if err != nil {
return nil
}
return err
}
func (n *vrOpenBSD) SaveConfig(ctx context.Context) error {
cmd, _ := exec.NewExecCmdFromString(saveCmd)
execResult, err := n.RunExec(ctx, cmd)
if err != nil {
return fmt.Errorf("%s: failed to execute cmd: %v", 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 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 *vrOpenBSD) CheckInterfaceName() error {
return nodes.GenericVMInterfaceCheck(n.Cfg.ShortName, n.Endpoints)
}