Skip to content

Commit

Permalink
Collect partial configs (partial config and ssh keys) before appying
Browse files Browse the repository at this point in the history
  • Loading branch information
steiler committed Feb 29, 2024
1 parent dc4fb94 commit d147574
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
16 changes: 6 additions & 10 deletions nodes/vr_sros/sshKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
_ "embed"
"io"
"strings"
"text/template"

Expand All @@ -18,31 +19,26 @@ import (
//go:embed ssh_keys.go.tpl
var SROSSSHKeysTemplate string

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

s.prepareSSHPubKeys(&tplData)

t, err := template.New("SSHKeys").Funcs(
gomplate.CreateFuncs(context.Background(), new(data.Data))).Parse(SROSSSHKeysTemplate)
if err != nil {
return err
return nil, err
}

buf := new(bytes.Buffer)
err = t.Execute(buf, tplData)
if err != nil {
return err
return nil, err
}

err = s.applyPartialConfig(ctx, s.Cfg.MgmtIPv4Address, scrapliPlatformName,
defaultCredentials.GetUsername(), defaultCredentials.GetPassword(),
buf,
)

return err
return buf, nil
}

// prepareSSHPubKeys maps the ssh pub keys into the SSH key type based slice
Expand Down
30 changes: 22 additions & 8 deletions nodes/vr_sros/vr-sros.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package vr_sros

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

func (s *vrSROS) PostDeploy(ctx context.Context, _ *nodes.PostDeployParams) error {
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)

ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()

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

err = s.applyPartialConfig(ctx, s.Cfg.MgmtIPv4Address, scrapliPlatformName,
defaultCredentials.GetUsername(), defaultCredentials.GetPassword(),
r,
)
_, err = conf.ReadFrom(r)
if err != nil {
return err
}
Expand All @@ -145,7 +144,22 @@ 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 {
err := s.configureSSHPublicKeys(ctx)
sshConf, err := s.generateSSHPublicKeysConfig(ctx)
if err != nil {
return err
}
_, err = conf.ReadFrom(sshConf)
if err != nil {
return err
}
}

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

0 comments on commit d147574

Please sign in to comment.