Skip to content

Commit

Permalink
source ssh config path from TopoPaths
Browse files Browse the repository at this point in the history
  • Loading branch information
steiler committed Oct 20, 2023
1 parent f58799a commit 984fe30
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
20 changes: 7 additions & 13 deletions clab/sshconfig.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package clab

import (
"fmt"
"os"
"text/template"

"github.com/srl-labs/containerlab/types"
)

// SSHConfigTmpl is the top-level data structure for the
Expand Down Expand Up @@ -32,22 +33,18 @@ Host {{ .Name }}
UserKnownHostsFile=/dev/null
{{ end }}`

// sshConfigFileTmpl is the template for the ssh config file.
const sshConfigFileTmpl = "/etc/ssh/ssh_config.d/clab-%s.conf"

// RemoveSSHConfig removes the lab specific ssh config file
func (c *CLab) RemoveSSHConfig() error {
filename := fmt.Sprintf(sshConfigFileTmpl, c.Config.Name)
err := os.Remove(filename)
func (c *CLab) RemoveSSHConfig(topoPaths *types.TopoPaths) error {
err := os.Remove(topoPaths.SSHConfigPath())
// if there is an error, thats not "Not Exists", then return it
if err != nil && err != os.ErrNotExist {
if err != nil && !os.IsNotExist(err) {
return err
}
return nil
}

// AddSSHConfig adds the lab specific ssh config file.
func (c *CLab) AddSSHConfig() error {
func (c *CLab) AddSSHConfig(topoPaths *types.TopoPaths) error {
tmpl := &SSHConfigTmpl{
TopologyName: c.Config.Name,
Nodes: make([]SSHConfigNodeTmpl, 0, len(c.Nodes)),
Expand All @@ -70,10 +67,7 @@ func (c *CLab) AddSSHConfig() error {
return err
}

// resolve the output filename with the topology name
filename := fmt.Sprintf(sshConfigFileTmpl, c.Config.Name)

f, err := os.Create(filename)
f, err := os.Create(topoPaths.SSHConfigPath())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func deployFn(_ *cobra.Command, _ []string) error {
}

log.Info("Adding ssh config for containerlab nodes")
err = c.AddSSHConfig()
err = c.AddSSHConfig(c.TopoPaths)
if err != nil {
log.Errorf("failed to create ssh config file: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func destroyLab(ctx context.Context, c *clab.CLab) (err error) {
}

log.Info("Removing ssh config for containerlab nodes")
err = c.RemoveSSHConfig()
err = c.RemoveSSHConfig(c.TopoPaths)
if err != nil {
log.Errorf("failed to remove ssh config file: %v", err)
}
Expand Down
5 changes: 5 additions & 0 deletions types/topo_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
CertFileSuffix = ".pem"
KeyFileSuffix = ".key"
CSRFileSuffix = ".csr"
sshConfigFilePathTmpl = "/etc/ssh/ssh_config.d/clab-%s.conf"
)

// clabTmpDir is the directory where clab stores temporary and/or downloaded files.
Expand Down Expand Up @@ -110,6 +111,10 @@ func (t *TopoPaths) SetExternalCaFiles(certFile, keyFile string) error {
return nil
}

func (t *TopoPaths) SSHConfigPath() string {
return fmt.Sprintf(sshConfigFilePathTmpl, t.topoName)
}

// TLSBaseDir returns the path of the TLS directory structure.
func (t *TopoPaths) TLSBaseDir() string {
return path.Join(t.labDir, tlsDir)
Expand Down

0 comments on commit 984fe30

Please sign in to comment.