-
Notifications
You must be signed in to change notification settings - Fork 242
/
vr-sros.go
269 lines (226 loc) · 7.65 KB
/
vr-sros.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright 2020 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
package vr_sros
import (
"context"
"fmt"
"path"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/scrapli/scrapligo/driver/network"
"github.com/scrapli/scrapligo/driver/options"
scraplilogging "github.com/scrapli/scrapligo/logging"
"github.com/scrapli/scrapligo/platform"
"github.com/scrapli/scrapligo/transport"
"github.com/scrapli/scrapligo/util"
log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/clab/exec"
"github.com/srl-labs/containerlab/netconf"
"github.com/srl-labs/containerlab/nodes"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)
var (
kindnames = []string{"vr-sros", "vr-nokia_sros"}
defaultCredentials = nodes.NewCredentials("admin", "admin")
)
const (
vrsrosDefaultType = "sr-1"
scrapliPlatformName = "nokia_sros"
configDirName = "tftpboot"
startupCfgFName = "config.txt"
licenseFName = "license.txt"
)
// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
return new(vrSROS)
}, defaultCredentials)
}
type vrSROS struct {
nodes.DefaultNode
}
func (s *vrSROS) Init(cfg *types.NodeConfig, opts ...nodes.NodeOption) error {
// Init DefaultNode
s.DefaultNode = *nodes.NewDefaultNode(s)
// set virtualization requirement
s.HostRequirements.VirtRequired = true
s.LicensePolicy = types.LicensePolicyWarn
s.Cfg = cfg
for _, o := range opts {
o(s)
}
// vr-sros type sets the vrnetlab/sros variant (https://github.com/hellt/vrnetlab/sros)
if s.Cfg.NodeType == "" {
s.Cfg.NodeType = vrsrosDefaultType
}
// env vars are used to set launch.py arguments in vrnetlab container
defEnv := map[string]string{
"CONNECTION_MODE": nodes.VrDefConnMode,
"DOCKER_NET_V4_ADDR": s.Mgmt.IPv4Subnet,
"DOCKER_NET_V6_ADDR": s.Mgmt.IPv6Subnet,
}
s.Cfg.Env = utils.MergeStringMaps(defEnv, s.Cfg.Env)
// mount tftpboot dir
s.Cfg.Binds = append(s.Cfg.Binds, fmt.Sprint(path.Join(s.Cfg.LabDir, "tftpboot"), ":/tftpboot"))
if s.Cfg.Env["CONNECTION_MODE"] == "macvtap" {
// mount dev dir to enable macvtap
s.Cfg.Binds = append(s.Cfg.Binds, "/dev:/dev")
}
s.Cfg.Cmd = fmt.Sprintf("--trace --connection-mode %s --hostname %s --variant \"%s\"", s.Cfg.Env["CONNECTION_MODE"],
s.Cfg.ShortName,
s.Cfg.NodeType,
)
return nil
}
func (s *vrSROS) PreDeploy(_ context.Context, params *nodes.PreDeployParams) error {
utils.CreateDirectory(s.Cfg.LabDir, 0777)
_, err := s.LoadOrGenerateCertificate(params.Cert, params.TopologyName)
if err != nil {
return nil
}
return createVrSROSFiles(s)
}
func (s *vrSROS) PostDeploy(ctx context.Context, _ *nodes.PostDeployParams) error {
if isPartialConfigFile(s.Cfg.StartupConfig) {
log.Infof("Waiting for %s to boot and apply config from %s", s.Cfg.LongName, s.Cfg.StartupConfig)
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
err := s.applyPartialConfig(ctx, s.Cfg.MgmtIPv4Address, scrapliPlatformName,
defaultCredentials.GetUsername(), defaultCredentials.GetPassword(),
s.Cfg.StartupConfig,
)
if err != nil {
return err
}
log.Infof("%s: configuration applied", s.Cfg.LongName)
}
return nil
}
func (s *vrSROS) SaveConfig(_ context.Context) error {
err := netconf.SaveConfig(s.Cfg.LongName,
defaultCredentials.GetUsername(),
defaultCredentials.GetPassword(),
scrapliPlatformName,
)
if err != nil {
return err
}
log.Infof("saved %s running configuration to startup configuration file\n", s.Cfg.ShortName)
return nil
}
// CheckInterfaceName checks if a name of the interface referenced in the topology file correct.
func (s *vrSROS) CheckInterfaceName() error {
// vsim doesn't seem to support >20 interfaces, yet we allow to set max if number 32 just in case.
// https://regex101.com/r/bx6kzM/1
ifRe := regexp.MustCompile(`eth([1-9]|[12][0-9]|3[0-2])$`)
for _, e := range s.Endpoints {
if !ifRe.MatchString(e.GetIfaceName()) {
return fmt.Errorf("nokia SR OS interface name %q doesn't match the required pattern. SR OS interfaces should be named as ethX, where X is from 1 to 32", e.GetIfaceName())
}
}
return nil
}
func createVrSROSFiles(node nodes.Node) error {
nodeCfg := node.Config()
// use default startup config load function if config in full form is provided
if !isPartialConfigFile(nodeCfg.StartupConfig) {
nodes.LoadStartupConfigFileVr(node, configDirName, startupCfgFName)
}
if nodeCfg.License != "" {
// copy license file to node specific lab directory
src := nodeCfg.License
dst := filepath.Join(nodeCfg.LabDir, configDirName, licenseFName)
if err := utils.CopyFile(src, dst, 0644); 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 nil
}
// isPartialConfigFile returns true if the config file name contains .partial substring.
func isPartialConfigFile(c string) bool {
return strings.Contains(strings.ToUpper(c), ".PARTIAL")
}
// isHealthy checks if the "/health" file created by vrnetlab exists and contains "0 running".
func (s *vrSROS) isHealthy(ctx context.Context) bool {
ex := exec.NewExecCmdFromSlice([]string{"grep", "0 running", "/health"})
res, err := s.RunExec(ctx, ex)
if err != nil {
return false
}
log.Debugf("Node %q health status: %v", s.Cfg.ShortName, res.ReturnCode == 0)
return res.ReturnCode == 0
}
// applyPartialConfig applies partial configuration to the SR OS.
func (s *vrSROS) applyPartialConfig(ctx context.Context, addr, platformName, username, password string, configFile string) error {
var err error
var d *network.Driver
configContent, err := utils.ReadFileContent(configFile)
if err != nil {
return err
}
// check file contains content, otherwise exit early
if len(strings.TrimSpace(string(configContent))) == 0 {
return nil
}
for loop := true; loop; {
if !s.isHealthy(ctx) {
time.Sleep(5 * time.Second) // cool-off period
log.Debugf("Waiting for %s to become healthy", s.Cfg.ShortName)
continue
}
select {
case <-ctx.Done():
return fmt.Errorf("%s: timed out waiting to accept configs", addr)
default:
li, err := scraplilogging.NewInstance(
scraplilogging.WithLevel("debug"),
scraplilogging.WithLogger(log.Debugln))
if err != nil {
return err
}
opts := []util.Option{
options.WithAuthNoStrictKey(),
options.WithAuthUsername(username),
options.WithAuthPassword(password),
options.WithTransportType(transport.StandardTransport),
options.WithTimeoutOps(5 * time.Second),
options.WithLogger(li),
}
p, err := platform.NewPlatform(platformName, addr, opts...)
if err != nil {
return fmt.Errorf("%s: failed to create platform: %+v", addr, err)
}
d, err = p.GetNetworkDriver()
if err != nil {
return fmt.Errorf("%s: could not create the driver: %+v", addr, err)
}
err = d.Open()
if err == nil {
// driver successfully opened, exit the loop
loop = false
} else {
log.Debugf("%s: not yet ready - %v", addr, err)
time.Sleep(5 * time.Second) // cool-off period
}
}
}
mr, err := d.SendConfigsFromFile(configFile)
if err != nil || mr.Failed != nil {
return fmt.Errorf("failed to apply config; error: %+v %+v", err, mr.Failed)
}
// condfig snippets should not have commit command, so we need to commit manually
r, err := d.SendConfig("commit")
if err != nil || r.Failed != nil {
return fmt.Errorf("failed to commit config; error: %+v %+v", err, mr.Failed)
}
r, err = d.SendCommand("/admin save")
if err != nil || r.Failed != nil {
return fmt.Errorf("failed to persist config; error: %+v %+v", err, mr.Failed)
}
return nil
}