This repository was archived by the owner on Feb 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose-validate.go
More file actions
120 lines (103 loc) · 3.01 KB
/
Copy pathcompose-validate.go
File metadata and controls
120 lines (103 loc) · 3.01 KB
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
package main
import ("fmt"
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
var params = Init()
var configData = readData(*params.ConfigFile)
var validate = readData(*params.RuleFile)
var ruleSet RuleSet
var config Config
yaml.Unmarshal(validate, &ruleSet)
yaml.Unmarshal(configData, &config)
var ruleSetErrors = checkRuleSet(ruleSet)
if ruleSetErrors > 0 {
logError("found errors in rule set. Please fix the errors and restart validation.")
os.Exit(2)
}
var services = config.Services
fmt.Println(config)
if (*params.Verbose) {
fmt.Printf("\ndocker-compose file version: %s\n", config.Version)
fmt.Printf("Found %d rules\n", len(ruleSet.Rules))
}
var errors = validateRules(ruleSet.Rules, services, *params.Verbose)
printResult(errors)
}
func readData(fileName string) ([]byte) {
data, err := ioutil.ReadFile(fileName)
check(err)
return data
}
func validateServices(services map[string]Service, s string, verbose bool) (int) {
var errors int
if _, present := services[s]; present {
logVerbose("service " + s + " found", verbose)
} else {
logError("service " + s + " not found\n")
errors++
}
return errors
}
func validateLabel(name string, s Service, l string, verbose bool) (int) {
var errors int
if _, present := s.Labels[l]; present {
logVerbose("service " + name + " has label " + l, verbose)
} else {
logError("service " + name + " should have label " + l + ", but label is not present!")
errors++
}
return errors
}
func validateNetwork(name string, s Service, n string, verbose bool) (int) {
var errors int
if _, present := s.Networks[n]; present {
logVerbose("servicehas network " + n, verbose)
} else {
logError("service " + name + " should have network " + n + ", but network is not present!")
errors++
}
return errors
}
func validateNetworkMode(name string, s Service, net_mode string, verbose bool) (int) {
var errors int
if s.Network_mode == net_mode {
logVerbose("service " + name + " has network mode " + net_mode, verbose)
} else {
logError("service " + name + " should have network mode " + net_mode + ", but is " + s.Network_mode)
errors++
}
return errors
}
func validateRules(rules map[string]Rule, services map[string]Service, verbose bool) (int) {
var errors int
for name, rule := range rules {
fmt.Printf("\nValidating %s:\n", name)
for _, s := range rule.Services {
errors += validateServices(services, s, verbose)
for _, l := range rule.Labels {
errors += validateLabel(s, services[s], l, verbose)
}
for _, n := range rule.Networks {
errors += validateNetwork(s, services[s], n, verbose)
}
errors += validateNetworkMode(s, services[s], rule.Network_mode, verbose)
}
}
return errors
}
func printResult(errors int) {
if errors > 0 {
logError("\nvalidation failed\n\n")
os.Exit(1)
} else {
logSuccess("\nvalidation successful\n\n")
}
}