/
generator.go
172 lines (150 loc) · 4.45 KB
/
generator.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package generation
import (
"errors"
"fmt"
"net"
"strings"
yaml "gopkg.in/yaml.v2"
"github.com/spf13/viper"
)
// TemplateLoader interface to provide data from file at path as dictionary
type TemplateLoader interface {
Load(path string) (map[string]interface{}, error)
}
// FileHelper interface to provide utility-funcations for common file-operations
type FileHelper interface {
Exists(path string) bool
Write(path, data string) error
}
// IPDetector interface to detect local outbound-ip-address
type IPDetector interface {
Detect() net.IP
}
// Generator capsulates generation-functionality with its dependencies
type Generator struct {
tmplLoader TemplateLoader
fileHelper FileHelper
ipDetector IPDetector
}
// CreateGenerator constructs Generator
func CreateGenerator() Generator {
return Generator{
tmplLoader: DefaultTemplateLoader{},
fileHelper: DefaultFileHelper{},
ipDetector: UplinkIPDetector{},
}
}
func (g Generator) checkPrerequisites(requestedScenario string) error {
if err := validateScenario("all", g.fileHelper); err != nil {
return err
}
if requestedScenario != "all" {
return validateScenario(requestedScenario, g.fileHelper)
}
return nil
}
func validateScenario(scenario string, fileHelper FileHelper) error {
if !viper.IsSet("scenarios." + scenario) {
return errors.New("'scenarios." + scenario + "' not configured'")
}
templateFolderPath := getTemplateFolderPath()
scenarioServices := viper.GetStringSlice("scenarios." + scenario)
if len(scenarioServices) == 0 {
return errors.New("'scenarios." + scenario + "' has no services")
}
for _, templateName := range scenarioServices {
tmplPath := templateFolderPath + templateName + ".yml"
if !fileHelper.Exists(tmplPath) {
return errors.New("template '" + tmplPath + "' not found")
}
}
return nil
}
// Generate generates docker-compose.yml based on scenario-config
func (g Generator) Generate(scenario string) error {
if len(scenario) == 0 {
scenario = "all"
}
fmt.Printf("generating '%v' environment ...\n", scenario)
if err := g.checkPrerequisites(scenario); err != nil {
return err
}
composeMap := createEmptyComposeMap()
missingServices := detectMissingServices(scenario)
if err := g.addServices(composeMap, scenario, missingServices); err != nil {
return err
}
if err := g.exportComposeMapAsYAML(composeMap); err != nil {
return err
}
fmt.Println("successfully generated 'docker-compose.yml'")
return nil
}
func detectMissingServices(scenario string) []string {
if scenario == "all" {
return []string{}
}
allServices := viper.GetStringSlice("scenarios.all")
scenarioServices := viper.GetStringSlice("scenarios." + scenario)
var missingServices []string
for _, allSvc := range allServices {
found := false
for _, scenSvc := range scenarioServices {
if allSvc == scenSvc {
found = true
}
}
if !found {
missingServices = append(missingServices, allSvc)
}
}
return missingServices
}
func (g Generator) addServices(composeMap map[string]interface{}, scenario string, missingServices []string) error {
services := viper.GetStringSlice("scenarios." + scenario)
servicesMap := composeMap["services"].(map[string]interface{})
for _, serviceName := range services {
path := fmt.Sprintf("%v%v.yml", getTemplateFolderPath(), serviceName)
serviceMap, err := g.tmplLoader.Load(path)
if err != nil {
return err
}
g.addExtraHosts(serviceMap, missingServices)
servicesMap[serviceName] = serviceMap
}
return nil
}
func (g Generator) addExtraHosts(serviceMap map[string]interface{}, missingServices []string) {
if len(missingServices) == 0 {
return
}
extraHosts := []string{}
for _, svc := range missingServices {
ipAddr := g.ipDetector.Detect()
extraHosts = append(extraHosts, fmt.Sprintf("%v:%v", svc, ipAddr.String()))
}
serviceMap["extra_hosts"] = extraHosts
}
func (g Generator) exportComposeMapAsYAML(composeMap map[string]interface{}) error {
composeYAML, err := yaml.Marshal(composeMap)
if err != nil {
return err
}
g.fileHelper.Write("docker-compose.yml", string(composeYAML))
return nil
}
func createEmptyComposeMap() map[string]interface{} {
return map[string]interface{}{
"version": "2",
"services": make(map[string]interface{}),
}
}
func getTemplateFolderPath() string {
templateFolderPath := viper.GetString("templateFolderPath")
return convertToFolderPath(templateFolderPath)
}
func convertToFolderPath(path string) string {
path = strings.TrimSpace(path)
path = strings.TrimRight(path, "/")
return path + "/"
}