local-server/cmd/dkl-dir2config/main.go

113 lines
2.4 KiB
Go
Raw Normal View History

2018-12-10 10:59:24 +00:00
package main
2018-12-10 13:44:05 +00:00
import (
"flag"
"log"
"os"
yaml "gopkg.in/yaml.v2"
2018-12-10 21:23:20 +00:00
2022-04-28 01:33:19 +00:00
"novit.tech/direktil/pkg/localconfig"
"novit.tech/direktil/local-server/pkg/clustersconfig"
2018-12-10 13:44:05 +00:00
)
var (
2019-02-28 08:27:09 +00:00
dir = flag.String("in", ".", "Source directory")
outPath = flag.String("out", "config.yaml", "Output file")
defaultsPath = flag.String("defaults", "defaults", "Path to the defaults")
2019-01-21 03:53:45 +00:00
2018-12-10 13:44:05 +00:00
src *clustersconfig.Config
dst *localconfig.Config
)
2018-12-10 10:59:24 +00:00
2019-01-21 03:53:45 +00:00
func loadSrc() {
2018-12-10 13:44:05 +00:00
var err error
2019-02-28 08:27:09 +00:00
src, err = clustersconfig.FromDir(*dir, *defaultsPath)
2018-12-10 13:44:05 +00:00
if err != nil {
log.Fatal("failed to load config from dir: ", err)
}
2019-01-21 03:53:45 +00:00
}
func main() {
flag.Parse()
2019-03-01 01:37:51 +00:00
log.SetFlags(log.Ltime | log.Lmicroseconds | log.Lshortfile)
2019-01-21 03:53:45 +00:00
loadSrc()
2018-12-10 13:44:05 +00:00
dst = &localconfig.Config{
SSLConfig: src.SSLConfig,
}
// ----------------------------------------------------------------------
for _, cluster := range src.Clusters {
dst.Clusters = append(dst.Clusters, &localconfig.Cluster{
Name: cluster.Name,
Addons: renderAddons(cluster),
2018-12-10 13:44:05 +00:00
})
}
// ----------------------------------------------------------------------
for _, host := range src.Hosts {
2019-01-21 03:53:45 +00:00
loadSrc() // FIXME ugly fix of some template caching or something
log.Print("rendering host ", host.Name)
2018-12-10 13:44:05 +00:00
ctx, err := newRenderContext(host, src)
if err != nil {
log.Fatal("failed to create render context for host ", host.Name, ": ", err)
}
macs := make([]string, 0)
if host.MAC != "" {
macs = append(macs, host.MAC)
}
ips := make([]string, 0)
if len(host.IP) != 0 {
ips = append(ips, host.IP)
}
ips = append(ips, host.IPs...)
2019-02-06 03:27:20 +00:00
if ctx.Group.Versions["modules"] == "" {
// default modules' version to kernel's version
ctx.Group.Versions["modules"] = ctx.Group.Kernel
}
2018-12-10 13:44:05 +00:00
dst.Hosts = append(dst.Hosts, &localconfig.Host{
Name: host.Name,
2019-12-16 07:00:57 +00:00
ClusterName: ctx.Cluster.Name,
Labels: ctx.Labels,
Annotations: ctx.Annotations,
2018-12-10 13:44:05 +00:00
MACs: macs,
IPs: ips,
IPXE: ctx.Group.IPXE, // TODO render
Kernel: ctx.Group.Kernel,
Initrd: ctx.Group.Initrd,
Versions: ctx.Group.Versions,
2022-04-28 01:33:19 +00:00
BootstrapConfig: ctx.BootstrapConfig(),
Config: ctx.Config(),
2018-12-10 13:44:05 +00:00
})
}
// ----------------------------------------------------------------------
out, err := os.Create(*outPath)
if err != nil {
log.Fatal("failed to create output: ", err)
}
defer out.Close()
if err = yaml.NewEncoder(out).Encode(dst); err != nil {
log.Fatal("failed to render output: ", err)
}
}