-
Notifications
You must be signed in to change notification settings - Fork 235
/
file.go
116 lines (100 loc) · 2.93 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2020 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
package clab
import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/a8m/envsubst"
"github.com/hairyhenderson/gomplate/v3"
"github.com/hairyhenderson/gomplate/v3/data"
log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
"gopkg.in/yaml.v2"
)
const (
varFileSuffix = "_vars"
)
// loadTopology parses the topology file into c.Conf structure
// as well as populates the TopoFile structure with the topology file related information.
func (c *CLab) loadTopologyFromFile(topo, varsFile string) error {
var err error
c.TopoPaths, err = types.NewTopoPaths(topo)
if err != nil {
return err
}
// load the topology file/template
topologyTemplate, err := template.New(c.TopoPaths.TopologyFilenameBase()).
Funcs(gomplate.CreateFuncs(context.Background(), new(data.Data))).
ParseFiles(c.TopoPaths.TopologyFilenameAbsPath())
if err != nil {
return err
}
// read template variables
templateVars, err := readTemplateVariables(c.TopoPaths.TopologyFilenameAbsPath(), varsFile)
if err != nil {
return err
}
log.Debugf("template variables: %v", templateVars)
// execute template
buf := new(bytes.Buffer)
err = topologyTemplate.Execute(buf, templateVars)
if err != nil {
return fmt.Errorf("failed to execute template: %v", err)
}
// create a hidden file that will contain the rendered topology
if !strings.HasPrefix(c.TopoPaths.TopologyFilenameBase(), ".") {
backupFPath := c.TopoPaths.TopologyBakFileAbsPath()
err = utils.CreateFile(backupFPath, buf.String())
if err != nil {
log.Warnf("Could not write rendered topology: %v", err)
}
}
log.Debugf("topology:\n%s\n", buf.String())
// expand env vars if any
yamlFile, err := envsubst.Bytes(buf.Bytes())
if err != nil {
return err
}
err = yaml.UnmarshalStrict(yamlFile, c.Config)
if err != nil {
return fmt.Errorf("%w\nConsult with release notes to see if any fields were changed/removed", err)
}
c.Config.Topology.ImportEnvs()
return nil
}
func readTemplateVariables(topo, varsFile string) (interface{}, error) {
var templateVars interface{}
// variable file is not explicitly set
if varsFile == "" {
ext := filepath.Ext(topo)
for _, vext := range []string{".yaml", ".yml", ".json"} {
varsFile = fmt.Sprintf("%s%s%s", topo[0:len(topo)-len(ext)], varFileSuffix, vext)
_, err := os.Stat(varsFile)
switch {
case os.IsNotExist(err):
continue
case err != nil:
return nil, err
}
// file with current extention found, go read it.
goto READFILE
}
// no var file found, assume the topology is not a template
// or a template that doesn't require external variables
return nil, nil
}
READFILE:
data, err := os.ReadFile(varsFile)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(data, &templateVars)
return templateVars, err
}