From 3c24d9b2674044e12a6709aa15c321cad8d15dc6 Mon Sep 17 00:00:00 2001 From: Roman Dodin Date: Sun, 12 Nov 2023 13:28:53 +0100 Subject: [PATCH] Do not attempt creating ssh config files if `ssh_config.d` doesn't exist (#1708) * ensure path exists when creating ssh lab config * do not create ssh config if ssh_config.d dir doesn't exist --- clab/sshconfig.go | 13 +++++++++++-- cmd/deploy.go | 2 +- go.sum | 4 ---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/clab/sshconfig.go b/clab/sshconfig.go index b3522aa34..7b5379874 100644 --- a/clab/sshconfig.go +++ b/clab/sshconfig.go @@ -2,9 +2,12 @@ package clab import ( "os" + "path" "text/template" + log "github.com/sirupsen/logrus" "github.com/srl-labs/containerlab/types" + "github.com/srl-labs/containerlab/utils" ) // SSHConfigTmpl is the top-level data structure for the @@ -44,7 +47,13 @@ func (c *CLab) RemoveSSHConfig(topoPaths *types.TopoPaths) error { } // AddSSHConfig adds the lab specific ssh config file. -func (c *CLab) AddSSHConfig(topoPaths *types.TopoPaths) error { +func (c *CLab) AddSSHConfig() error { + sshConfigDir := path.Dir(c.TopoPaths.SSHConfigPath()) + if !utils.FileOrDirExists(sshConfigDir) { + log.Debugf("ssh config directory %s does not exist, skipping ssh config generation", sshConfigDir) + return nil + } + tmpl := &SSHConfigTmpl{ TopologyName: c.Config.Name, Nodes: make([]SSHConfigNodeTmpl, 0, len(c.Nodes)), @@ -67,7 +76,7 @@ func (c *CLab) AddSSHConfig(topoPaths *types.TopoPaths) error { return err } - f, err := os.Create(topoPaths.SSHConfigPath()) + f, err := os.Create(c.TopoPaths.SSHConfigPath()) if err != nil { return err } diff --git a/cmd/deploy.go b/cmd/deploy.go index 4936b6d87..9735a07be 100644 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -282,7 +282,7 @@ func deployFn(_ *cobra.Command, _ []string) error { } log.Info("Adding ssh config for containerlab nodes") - err = c.AddSSHConfig(c.TopoPaths) + err = c.AddSSHConfig() if err != nil { log.Errorf("failed to create ssh config file: %v", err) } diff --git a/go.sum b/go.sum index ab1797247..fa560cfab 100644 --- a/go.sum +++ b/go.sum @@ -2678,10 +2678,6 @@ github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q github.com/spf13/viper v1.13.0/go.mod h1:Icm2xNL3/8uyh/wFuB1jI7TiTNKp8632Nwegu+zgdYw= github.com/stefanberger/go-pkcs11uri v0.0.0-20201008174630-78d3cae3a980 h1:lIOOHPEbXzO3vnmx2gok1Tfs31Q8GQqKLc8vVqyQq/I= github.com/stefanberger/go-pkcs11uri v0.0.0-20201008174630-78d3cae3a980/go.mod h1:AO3tvPzVZ/ayst6UlUKUv6rcPQInYe3IknH3jYhAKu8= -github.com/steiler/acls v0.0.0-20231106135104-9c34ae82c793 h1:lY8SggLL0JyttzcNEIRzbywKiKcIlVlF7ZPwTthG9eA= -github.com/steiler/acls v0.0.0-20231106135104-9c34ae82c793/go.mod h1:kS9/GuHDS4t2YmY2ijbaK3m1iU4+BgRZS7GoDTC9BfY= -github.com/steiler/acls v0.0.0-20231106152733-bb3d5d7b05c8 h1:RqP82h2DREJxj7AJbT0k7z2Jye6lweij5s14PUHic1o= -github.com/steiler/acls v0.0.0-20231106152733-bb3d5d7b05c8/go.mod h1:kS9/GuHDS4t2YmY2ijbaK3m1iU4+BgRZS7GoDTC9BfY= github.com/steiler/acls v0.1.0 h1:fKVnEJ7ebghq2Ed5N1cU9fZrCCRj4xVRPrP7OswaRX8= github.com/steiler/acls v0.1.0/go.mod h1:kS9/GuHDS4t2YmY2ijbaK3m1iU4+BgRZS7GoDTC9BfY= github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=