-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Generate SSH config that: - Set the Usernaem for the nodes - Set StrictHostKeyChecking=no - Set UserKnownHostsFile=/dev/null * improve comments * align field names * align template naming * deploy->add renaming * remove hostname template entry and use os.Create * ssh config template changes make Username optional silence ssh config removal not exists error * source ssh config path from TopoPaths * comment * added doc entry --------- Co-authored-by: Roman Dodin <dodin.roman@gmail.com>
- Loading branch information
Showing
5 changed files
with
149 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package clab | ||
|
||
import ( | ||
"os" | ||
"text/template" | ||
|
||
"github.com/srl-labs/containerlab/types" | ||
) | ||
|
||
// SSHConfigTmpl is the top-level data structure for the | ||
// sshconfig template. | ||
type SSHConfigTmpl struct { | ||
Nodes []SSHConfigNodeTmpl | ||
TopologyName string | ||
} | ||
|
||
// SSHConfigNodeTmpl represents values for a single node | ||
// in the sshconfig template. | ||
type SSHConfigNodeTmpl struct { | ||
Name string | ||
Username string | ||
} | ||
|
||
// tmplSshConfig is the SSH config template. | ||
const tmplSshConfig = `# Containerlab SSH Config for the {{ .TopologyName }} lab | ||
{{- range .Nodes }} | ||
Host {{ .Name }} | ||
{{- if ne .Username ""}} | ||
User {{ .Username }} | ||
{{- end }} | ||
StrictHostKeyChecking=no | ||
UserKnownHostsFile=/dev/null | ||
{{ end }}` | ||
|
||
// RemoveSSHConfig removes the lab specific ssh config file | ||
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 && !os.IsNotExist(err) { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// AddSSHConfig adds the lab specific ssh config file. | ||
func (c *CLab) AddSSHConfig(topoPaths *types.TopoPaths) error { | ||
tmpl := &SSHConfigTmpl{ | ||
TopologyName: c.Config.Name, | ||
Nodes: make([]SSHConfigNodeTmpl, 0, len(c.Nodes)), | ||
} | ||
|
||
// add the data for all nodes to the template input | ||
for _, n := range c.Nodes { | ||
// get the Kind from the KindRegistry and and extract | ||
// the kind registered Username | ||
NodeRegistryEntry := c.Reg.Kind(n.Config().Kind) | ||
nodeData := SSHConfigNodeTmpl{ | ||
Name: n.Config().LongName, | ||
Username: NodeRegistryEntry.Credentials().GetUsername(), | ||
} | ||
tmpl.Nodes = append(tmpl.Nodes, nodeData) | ||
} | ||
|
||
t, err := template.New("sshconfig").Parse(tmplSshConfig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f, err := os.Create(topoPaths.SSHConfigPath()) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
err = t.Execute(f, tmpl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters