Skip to content

Commit

Permalink
use bytes buffer directly with io.Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
hellt committed Mar 1, 2024
1 parent d147574 commit cab95b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
6 changes: 3 additions & 3 deletions nodes/vr_sros/sshKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
//go:embed ssh_keys.go.tpl
var SROSSSHKeysTemplate string

// generateSSHPublicKeysConfig configures public keys extracted from clab host
// on SR OS node using SSH.
func (s *vrSROS) generateSSHPublicKeysConfig(ctx context.Context) (io.Reader, error) {
// generateSSHPublicKeysConfig generates public keys configuration blob
// to add keys extracted from the clab host.
func (s *vrSROS) generateSSHPublicKeysConfig() (io.Reader, error) {
tplData := SROSTemplateData{}

s.prepareSSHPubKeys(&tplData)
Expand Down
22 changes: 12 additions & 10 deletions nodes/vr_sros/vr-sros.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package vr_sros

import (
"bufio"
"bytes"
"context"
"fmt"
Expand Down Expand Up @@ -119,23 +118,23 @@ func (s *vrSROS) PreDeploy(_ context.Context, params *nodes.PreDeployParams) err
}

func (s *vrSROS) PostDeploy(ctx context.Context, _ *nodes.PostDeployParams) error {
// b holds the configuration to be applied to the node
b := &bytes.Buffer{}
conf := bufio.NewReadWriter(bufio.NewReader(b), bufio.NewWriter(b))

if isPartialConfigFile(s.Cfg.StartupConfig) {
log.Infof("%s: applying config from %s", s.Cfg.LongName, s.Cfg.StartupConfig)
log.Infof("%s: adding config from %s", s.Cfg.LongName, s.Cfg.StartupConfig)

r, err := os.Open(s.Cfg.StartupConfig)
if err != nil {
return err
}

_, err = conf.ReadFrom(r)
defer r.Close()

_, err = io.Copy(b, r)
if err != nil {
return err
}

log.Infof("%s: configuration applied", s.Cfg.LongName)
}

// skip ssh key configuration if CLAB_SKIP_SROS_SSH_KEY_CONFIG env var is set
Expand All @@ -144,21 +143,24 @@ func (s *vrSROS) PostDeploy(ctx context.Context, _ *nodes.PostDeployParams) erro
_, skipSSHKeyCfg := os.LookupEnv("CLAB_SKIP_SROS_SSH_KEY_CONFIG")

if len(s.sshPubKeys) > 0 && !skipSSHKeyCfg {
sshConf, err := s.generateSSHPublicKeysConfig(ctx)
log.Infof("%s: adding public keys configuration", s.Cfg.LongName)

sshConf, err := s.generateSSHPublicKeysConfig()
if err != nil {
return err
}
_, err = conf.ReadFrom(sshConf)

_, err = io.Copy(b, sshConf)
if err != nil {
return err
}
}

// apply the aggregated config snippets
if conf.Available() > 0 {
if b.Len() > 0 {
err := s.applyPartialConfig(ctx, s.Cfg.MgmtIPv4Address, scrapliPlatformName,
defaultCredentials.GetUsername(), defaultCredentials.GetPassword(),
conf,
b,
)
if err != nil {
return err
Expand Down

0 comments on commit cab95b9

Please sign in to comment.