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
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)")
ekmsNum := flag.Int("ekms", 0, "number of EKMS to generate")
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 *configPath == "" {
err = flag.Set("config", configPath_default)
if err != nil {
}
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
// if no --ekms argument given or negative value
if *ekmsNum < 1 {
logrus.Info("-ekms flag not provided or invalid value, using value in config file: ", tcConf.EKMS.Num)
} else {
tcConf.EKMS.Num = *ekmsNum
logrus.Info("EKMS number set to ", tcConf.EKMS.Num)
}
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