Skip to content

Commit

Permalink
Merge pull request #5 from srl-labs/scrapliv1
Browse files Browse the repository at this point in the history
adapted for Scrapli v1
  • Loading branch information
hellt committed Jul 15, 2022
2 parents 786adbe + fb4e3ee commit b9cb14f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 145 deletions.
9 changes: 6 additions & 3 deletions cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"strings"

"github.com/scrapli/scrapligo/driver/base"
"github.com/scrapli/scrapligo/driver/network"
"github.com/scrapli/scrapligo/driver/opoptions"
)

const (
Expand Down Expand Up @@ -68,11 +68,14 @@ func configureTLSProfile(d *network.Driver, profileName, key, cert string) error
return err
}
// key and cert are send outside of sendconfigs, because it was not working properly with `eager` option
_, err = d.SendConfig(fmt.Sprintf("set / system tls server-profile %s key \"%s\"", profileName, key), base.WithSendEager(true))
_, err = d.SendConfig(fmt.Sprintf("set / system tls server-profile %s key \"%s\"", profileName, key),
opoptions.WithEager(),
)
if err != nil {
return err
}
_, err = d.SendConfig(fmt.Sprintf("set / system tls server-profile %s certificate \"%s\"", profileName, cert), base.WithSendEager(true))
_, err = d.SendConfig(fmt.Sprintf("set / system tls server-profile %s certificate \"%s\"", profileName, cert),
opoptions.WithEager())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/srl-labs/srlinux-scrapli

go 1.16

require github.com/scrapli/scrapligo v0.1.1
require github.com/scrapli/scrapligo v1.1.0
35 changes: 18 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
github.com/carlmontanari/difflibgo v0.0.0-20210718170140-424f52054f94/go.mod h1:+3MuSIeC3qmdSesR12cTLeb47R/Vvo+bHdB6hC5HShk=
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/scrapli/scrapligo v0.1.0 h1:6bAtdQY9Phnacy811lf8kgoWqIMYAM/E3b5N07wVbyU=
github.com/scrapli/scrapligo v0.1.0/go.mod h1:0tHMgiCiTuWOvSceFU7klaYThXvRZNvc7k+fmQrtH54=
github.com/scrapli/scrapligo v0.1.1 h1:kSRgH81a4Ec0qM3VK1AHp3/C0mvPclVteQMaW7vTXS0=
github.com/scrapli/scrapligo v0.1.1/go.mod h1:0tHMgiCiTuWOvSceFU7klaYThXvRZNvc7k+fmQrtH54=
github.com/sirikothe/gotextfsm v1.0.0 h1:4kKwbUziG9G+31PfLY+vI3FzYK/kcByh4ndT3NyPMkc=
github.com/sirikothe/gotextfsm v1.0.0/go.mod h1:CJYqpTg9u5VPCoD0VEl9E68prCIiWQD8m457k098DdQ=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/scrapli/scrapligo v1.1.0 h1:KjCam57kIV2rlxAQg/J1G7v/xgRHvpJF+Gjz+LXhQaI=
github.com/scrapli/scrapligo v1.1.0/go.mod h1:jvRMdb90MNnswMiku8UNXj8JZaOIPhwhcqqFwr9qeoY=
github.com/sirikothe/gotextfsm v1.0.1-0.20200816110946-6aa2cfd355e4 h1:FHUL2HofYJuslFOQdy/JjjP36zxqIpd/dcoiwLMIs7k=
github.com/sirikothe/gotextfsm v1.0.1-0.20200816110946-6aa2cfd355e4/go.mod h1:CJYqpTg9u5VPCoD0VEl9E68prCIiWQD8m457k098DdQ=
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 h1:SLP7Q4Di66FONjDJbCYrCRrh97focO6sLogHO7/g8F0=
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8 changes: 4 additions & 4 deletions ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const (

var ()

// WaitSRLMgmtSrv returns when the node boot sequence reached the stage when it is ready to accept config commands
// returns an error if not ready by the expiry of the timer readyTimeout.
func WaitSRLMgmtSrv(ctx context.Context, d *network.Driver) error {
// WaitSRLMgmtSrvReady returns when the node boot sequence reaches the stage when it is ready to accept config commands
// returns an error if not ready by readyTimeout.
func WaitSRLMgmtSrvReady(ctx context.Context, d *network.Driver) error {
ctx, cancel := context.WithTimeout(ctx, readyTimeout)
defer cancel()

Expand All @@ -30,7 +30,7 @@ func WaitSRLMgmtSrv(ctx context.Context, d *network.Driver) error {
for {
select {
case <-ctx.Done():
return fmt.Errorf("timed out waiting for SR Linux node %s to boot: %v", d.Host, err)
return fmt.Errorf("timed out waiting for SR Linux node %s to boot: %v", d.Transport.GetHost(), err)
default:
// two commands are checked, first if the mgmt_server is running
resp, err := d.SendCommand(mgmtServerRdyCmd)
Expand Down
120 changes: 0 additions & 120 deletions scrapli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,129 +4,9 @@

package srlinux

import (
"github.com/scrapli/scrapligo/driver/base"
"github.com/scrapli/scrapligo/driver/network"
)

const (
// PTY dimensions to override scrapli defaults,
// since width=256 results in too long `---` delimiters in show commands
defaultPtyWidth = 140
defaultPtyHeight = 60
)

// NewSRLinuxDriver global var allows to rewrite the driver initialization function for patched variant in testing
var NewSRLinuxDriver = newSRLinuxDriver

// NewSRLinuxDriver returns a driver setup for operation with Nokia SR Linux devices.
func newSRLinuxDriver(
host string,
options ...base.Option,
) (*network.Driver, error) {
defaultPrivilegeLevels := map[string]*base.PrivilegeLevel{
"exec": {
Pattern: `(?im)^--{(\s\[FACTORY\])?[\+\*\s]{1,}running\s}--\[.+?\]--\s*\n[abcd]:\S+#\s*$`,
Name: "exec",
PreviousPriv: "",
Deescalate: "",
Escalate: "",
EscalateAuth: false,
EscalatePrompt: ``,
},
// configuration privilege level maps to the exclusive config mode on SR Linux
"configuration": {
Pattern: `(?im)^--{(\s\[FACTORY\])?[\+\*\!\s]{1,}candidate\sprivate\s[\-\w\s]+}--\[.+?\]--\s*\n[abcd]:\S+#\s*$`,
Name: "configuration",
PreviousPriv: "exec",
Deescalate: "discard now",
Escalate: "enter candidate private",
EscalateAuth: false,
EscalatePrompt: ``,
},
}

defaultFailedWhenContains := []string{
"Error:",
}

const defaultDefaultDesiredPriv = "exec"

// prepend default pty window size
options = append([]base.Option{base.WithTransportPtySize(defaultPtyWidth, defaultPtyHeight)}, options...)

d, err := network.NewNetworkDriver(
host,
defaultPrivilegeLevels,
defaultDefaultDesiredPriv,
defaultFailedWhenContains,
SRLinuxOnOpen,
SRLinuxOnClose,
options...)

if err != nil {
return nil, err
}

d.Augments["abortConfig"] = SRLinuxAbortConfig

return d, nil
}

// SRLinuxOnOpen is a default on open callable for SR Linux.
func SRLinuxOnOpen(d *network.Driver) error {
err := d.AcquirePriv(d.DefaultDesiredPriv)
if err != nil {
return err
}

if _, err = d.SendCommand("environment cli-engine type basic", nil); err != nil {
return err
}

if _, err = d.SendCommand("environment complete-on-space false", nil); err != nil {
return err
}

return err
}

// SRLinuxOnClose is a default on close callable for SR Linux.
func SRLinuxOnClose(d *network.Driver) error {
err := d.AcquirePriv(d.DefaultDesiredPriv)
if err != nil {
return err
}

err = d.Channel.Write([]byte("quit"), false)
if err != nil {
return err
}

err = d.Channel.SendReturn()
if err != nil {
return err
}

return nil
}

// SRLinuxAbortConfig aborts SR Linux configuration session.
func SRLinuxAbortConfig(d *network.Driver) (*base.Response, error) {
_, err := d.Channel.SendInput("discard /", false, false, -1)

d.CurrentPriv = "exec"

return nil, err
}

// NewPatchedSRLinuxDriver returns a new driver and allows to rewrite the default function NewSRLinuxDriver
func NewPatchedSRLinuxDriver(
host string,
options ...base.Option,
) (*network.Driver, error) {
return newSRLinuxDriver(
host,
options...,
)
}

0 comments on commit b9cb14f

Please sign in to comment.