Skip to content

Commit

Permalink
used go:embed with crpd
Browse files Browse the repository at this point in the history
  • Loading branch information
hellt committed Jun 28, 2021
1 parent 98aad8c commit 3b35368
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
1 change: 0 additions & 1 deletion templates/crpd/juniper.conf → nodes/crpd/crpd.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
## Last changed: 2021-01-11 09:07:59 UTC
version 20200609.165031.6_builder.r1115480;
system {
root-authentication {
Expand Down
22 changes: 15 additions & 7 deletions nodes/crpd/crpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package crpd

import (
"context"
_ "embed"
"fmt"
"path"

Expand All @@ -16,6 +17,14 @@ import (
"github.com/srl-labs/containerlab/utils"
)

var (
//go:embed crpd.cfg
cfgTemplate string

//go:embed sshd_config
sshdCfg string
)

func init() {
nodes.Register(nodes.NodeKindCRPD, func() nodes.Node {
return new(crpd)
Expand Down Expand Up @@ -82,23 +91,22 @@ func createCRPDFiles(nodeCfg *types.NodeConfig) error {
// copy crpd config from default template or user-provided conf file
cfg := path.Join(nodeCfg.LabDir, "/config/juniper.conf")

err := nodeCfg.GenerateConfig(cfg, nodes.DefaultConfigTemplates[nodeCfg.Kind])
err := nodeCfg.GenerateConfig(cfg, cfgTemplate)
if err != nil {
log.Errorf("node=%s, failed to generate config: %v", nodeCfg.ShortName, err)
}

// copy crpd sshd conf file to crpd node dir
src := "/etc/containerlab/templates/crpd/sshd_config"
// write crpd sshd conf file to crpd node dir
dst := path.Join(nodeCfg.LabDir, "/config/sshd_config")
err = utils.CopyFile(src, dst)
err = utils.CreateFile(dst, sshdCfg)
if err != nil {
return fmt.Errorf("file copy [src %s -> dst %s] failed %v", src, dst, err)
return fmt.Errorf("failed to write sshd_config file %v", err)
}
log.Debugf("CopyFile src %s -> dst %s succeeded\n", src, dst)
log.Debug("Writing sshd_config succeeded")

if nodeCfg.License != "" {
// copy license file to node specific lab directory
src = nodeCfg.License
src := nodeCfg.License
dst = path.Join(nodeCfg.LabDir, "/config/license.conf")
if err = utils.CopyFile(src, dst); err != nil {
return fmt.Errorf("file copy [src %s -> dst %s] failed %v", src, dst, err)
Expand Down
File renamed without changes.
9 changes: 6 additions & 3 deletions utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,20 @@ func CopyFileContents(src, dst string) (err error) {
return
}

func CreateFile(file, content string) {
// CreateFile writes content to a file by path `file`
func CreateFile(file, content string) error {
var f *os.File
f, err := os.Create(file)
if err != nil {
panic(err)
return err
}
defer f.Close()

if _, err := f.WriteString(content + "\n"); err != nil {
panic(err)
return err
}

return nil
}

// CreateDirectory creates a directory by a path with a mode/permission specified by perm.
Expand Down

0 comments on commit 3b35368

Please sign in to comment.