Newer
Older
package templateCreate
import (
"flag"
"os"
Clemens Barth
committed
"strings"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
var configPath_default = "templateCreate/templateCreate.clab_vars.yaml" // config path if no other path is given via -config flag
var tcConf *TemplateCreateConfig // pointer to parsed config
// ipPrefix: ipBase without number after last ".".
// e.g. ipBase = "172.100.20.0" -> ipPrefix = "172.100.20.".
var ipPrefix string
Clemens Barth
committed
var peerGenMode string
var curSubnetNr int
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
func ParseTemplateCreateConfig() {
// TODO: flag validation
configPath := flag.String("config", "", "path to the config file")
logLevel := flag.String("log", "", "logrus log level (debug, info, warn, error, fatal, panic)")
flag.Parse()
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
// parse string, this is built-in feature of logrus
ll, err := logrus.ParseLevel(*logLevel)
if err != nil {
ll = logrus.InfoLevel
logrus.Info("-log flag not provided, using default: ", ll)
}
// set global log level
logrus.SetLevel(ll)
logrus.Info("Setting log level to ", ll)
// if no argument
if *configPath == "" {
err = flag.Set("config", configPath_default)
if err != nil {
ll = logrus.InfoLevel
logrus.Info("-log flag not provided, using default: ", ll)
}
logrus.Info("-config flag not provided, using default: ", *configPath)
}
// unmarshal config
config := &TemplateCreateConfig{}
file, err := os.ReadFile(*configPath)
if err != nil {
logrus.Fatal(err)
}
if err := yaml.Unmarshal(file, config); err != nil {
logrus.Fatal(err)
}
// set TemplateCreateConfig variable
tcConf = config
//logrus.Info(*tcConf) // print entire parsed config
Clemens Barth
committed
setConfVals()
}
func setConfVals() {
peerGenMode = tcConf.PeerGen.Mode
logrus.Infof("Generation mode is %v", peerGenMode)
// IP without part after last dot, but including last dot
// to add the subnet numbers to for individual IPs
// e.g. ipBase: 172.100.20.4 -> ipPrefix: 172.100.20.
ipPrefix = tcConf.General.IpBase[:strings.LastIndex(tcConf.General.IpBase, ".")+1]
// the next free subnet number in the network
// control nodes start from .1
// e.g. 5 control nodes, xxx.xxx.xx.1 - xxx.xxx.xx.6 -> curSubnetNr = 6
curSubnetNr = tcConf.General.ControlNodeCount + 1