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

132 lines
2.7 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 (
"bytes"
"flag"
"fmt"
"log"
"os"
yaml "gopkg.in/yaml.v2"
"novit.nc/direktil/pkg/localconfig"
2018-12-10 21:23:20 +00:00
"novit.nc/direktil/local-server/pkg/clustersconfig"
2018-12-10 13:44:05 +00:00
)
var (
2019-01-21 03:53:45 +00:00
dir = flag.String("in", ".", "Source directory")
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
src, err = clustersconfig.FromDir(*dir)
if err != nil {
log.Fatal("failed to load config from dir: ", err)
}
2019-01-21 03:53:45 +00:00
}
func main() {
outPath := flag.String("out", "config.yaml", "Output file")
flag.Parse()
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),
})
}
// ----------------------------------------------------------------------
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...)
dst.Hosts = append(dst.Hosts, &localconfig.Host{
Name: host.Name,
MACs: macs,
IPs: ips,
IPXE: ctx.Group.IPXE, // TODO render
Kernel: ctx.Group.Kernel,
Initrd: ctx.Group.Initrd,
Versions: ctx.Group.Versions,
Config: ctx.Config(),
})
}
// ----------------------------------------------------------------------
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)
}
}
func renderAddons(cluster *clustersconfig.Cluster) string {
2019-01-30 02:56:45 +00:00
if len(cluster.Addons) == 0 {
return ""
}
2018-12-10 13:44:05 +00:00
addons := src.Addons[cluster.Addons]
if addons == nil {
2018-12-10 13:45:33 +00:00
log.Fatalf("cluster %q: no addons with name %q", cluster.Name, cluster.Addons)
2018-12-10 13:44:05 +00:00
}
clusterAsMap := asMap(cluster)
clusterAsMap["kubernetes_svc_ip"] = cluster.KubernetesSvcIP().String()
clusterAsMap["dns_svc_ip"] = cluster.DNSSvcIP().String()
buf := &bytes.Buffer{}
for _, addon := range addons {
2019-01-22 05:43:46 +00:00
fmt.Fprintf(buf, "---\n# addon: %s\n", addon.Name)
2018-12-10 13:44:05 +00:00
err := addon.Execute(buf, clusterAsMap, nil)
if err != nil {
log.Fatalf("cluster %q: addons %q: failed to render %q: %v",
cluster.Name, cluster.Addons, addon.Name, err)
}
2019-01-22 05:43:46 +00:00
fmt.Fprintln(buf)
2018-12-10 13:44:05 +00:00
}
return buf.String()
2018-12-10 10:59:24 +00:00
}