-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
107 lines (88 loc) · 2.5 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"time"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclwrite"
dupont "github.com/thomas-maurice/dupont/pkg"
"github.com/thomas-maurice/dupont/pkg/config"
"go.uber.org/zap"
)
var (
configFile string
configFormat string
what string
)
func init() {
flag.StringVar(&configFile, "config", "config.hcl", "Configuration file to use")
flag.StringVar(&configFormat, "config-format", "hcl", "Configuration format (hcl/yaml)")
flag.StringVar(&what, "what", "apply", "What to do ? (apply/delete/generate)")
}
func main() {
flag.Parse()
log, err := zap.NewProduction()
if err != nil {
panic(err)
}
switch what {
case "apply":
cfg, err := config.LoadConfig(log, configFile, configFormat)
if err != nil {
log.Fatal("could not load config", zap.Error(err))
}
log.Info("loaded config", zap.Any("config", cfg))
err = dupont.ApplyConfiguration(log, cfg)
if err != nil {
log.Fatal("could not apply config", zap.Error(err))
}
case "delete":
cfg, err := config.LoadConfig(log, configFile, configFormat)
if err != nil {
log.Fatal("could not load config", zap.Error(err))
}
log.Info("loaded config", zap.Any("config", cfg))
err = dupont.TearDownConfiguration(log, cfg)
if err != nil {
log.Fatal("could not tear down config", zap.Error(err))
}
case "generate":
cfg, err := config.LoadTopology(log, configFile, configFormat)
if err != nil {
log.Fatal("could not load topology", zap.Error(err))
}
log.Info("loaded topololgy", zap.Any("topololgy", cfg))
topology, err := dupont.GenerateTopology(cfg)
if err != nil {
log.Fatal("could not generate topology", zap.Error(err))
}
for hostName, hostConfig := range topology {
hclFile := hclwrite.NewEmptyFile()
gohcl.EncodeIntoBody(hostConfig, hclFile.Body())
wr := bytes.NewBuffer(nil)
wr.WriteString(fmt.Sprintf(`/*
Auto generated configuration ! You probably should not touch this.
Generated on: %v
Topology ID : %s
*/
`, time.Now(), cfg.ID()))
hclFile.WriteTo(wr)
if _, err := os.Stat(cfg.ID()); os.IsNotExist(err) {
err := os.Mkdir(cfg.ID(), 0700)
if err != nil {
log.Fatal("could not create topology dir", zap.Error(err))
}
}
err := ioutil.WriteFile(path.Join(cfg.ID(), hostName+".hcl"), wr.Bytes(), 0600)
if err != nil {
log.Fatal("could not create topology config file", zap.Error(err))
}
}
default:
log.Fatal("unknown operation", zap.String("operation", what))
}
}