-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
208 lines (180 loc) · 5.39 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"dario.cat/mergo"
"fmt"
"github.com/akamensky/argparse"
"gopkg.in/yaml.v3"
"net"
"os"
"path/filepath"
"strings"
)
type ClassTableEntry map[string]interface{}
type ClassTable map[string]ClassTableEntry
type DataTable map[string]interface{}
// production/*.yml
type Component struct {
Classes ClassTable
Data DataTable
Parameters DataTable
Implies []string
}
type ResolutionResult struct {
Classes ClassTable
Data DataTable
Parameters DataTable
}
// nodes.yml
type NodeSpec struct {
Fallback Node
Nodes map[string]Node
}
type Node struct {
Environment string
Implies []string
}
type Classification struct {
Classes ClassTable
Data DataTable
Parameters DataTable
Environment string
}
func loadComponent(name string, componentsBase string) (*Component, error) {
nameComponents := strings.Split(strings.ToLower(name)+".yml", "/")
data, err := os.ReadFile(filepath.Join(componentsBase, filepath.Join(nameComponents...)))
if err != nil {
return nil, err
}
dst := &Component{}
err = yaml.Unmarshal(data, dst)
if err != nil {
return nil, err
}
if dst.Classes == nil {
dst.Classes = ClassTable{}
}
// for ease of configuration allow to define the classes with without a value, we'll fix them to empty objects.
for i := range dst.Classes {
if dst.Classes[i] == nil {
dst.Classes[i] = ClassTableEntry{}
}
}
if dst.Data == nil {
dst.Data = DataTable{}
}
if dst.Parameters == nil {
dst.Parameters = DataTable{}
}
return dst, nil
}
func resolveClasses(dst *ResolutionResult, implications []string, componentsBase string, strictMode bool, seen map[string]interface{}) {
for i := range implications {
implication := strings.ToLower(implications[i])
_, seenBefore := seen[implication]
if !seenBefore {
seen[implication] = true
fmt.Printf("# component: %s\n", implication)
component, err := loadComponent(implication, componentsBase)
if err == nil {
// first merge the implications
resolveClasses(dst, component.Implies, componentsBase, strictMode, seen)
// then merge the current component (post-order) to prioritize explicitly configured values higher up the tree
err = mergo.Merge(&dst.Classes, component.Classes, mergo.WithOverride)
if err != nil {
fmt.Printf("# Failed to merge classes!\n")
if strictMode {
panic("Failed to merge classes in strict mode!")
}
}
err = mergo.Merge(&dst.Data, component.Data, mergo.WithOverride)
if err != nil {
fmt.Printf("# Failed to merge data!\n")
if strictMode {
panic("Failed to merge data in strict mode!")
}
}
err = mergo.Merge(&dst.Parameters, component.Parameters, mergo.WithOverride)
if err != nil {
fmt.Printf("# Failed to merge parameters!\n")
if strictMode {
panic("Failed to merge parameters in strict mode!")
}
}
} else {
if strictMode {
panic("Failed to load a component in strict mode!")
}
fmt.Printf("# Error loading component %s: %s\n", implication, err)
}
}
}
}
func resolveNodeName(nodeName string) {
ips, _ := net.LookupIP(nodeName)
stringIps := make([]string, len(ips))
for i, ip := range ips {
stringIps[i] = ip.String()
}
}
func classify(dst *Classification, nodeName string, nodesFile string, componentsBase string, strictMode bool) error {
nodesData, err := os.ReadFile(nodesFile)
nodes := NodeSpec{}
if err == nil {
err = yaml.Unmarshal(nodesData, &nodes)
if err != nil {
return err
}
} else {
return err
}
fallback := nodes.Fallback
node, found := nodes.Nodes[nodeName]
if !found {
if strictMode {
panic("Node not found in strict mode!")
}
fmt.Println("# Node is not known, falling back!")
node = fallback
}
if node.Environment == "" {
node.Environment = fallback.Environment
}
if strings.Contains(componentsBase, "%s") {
componentsBase = fmt.Sprintf(componentsBase, node.Environment)
}
result := ResolutionResult{Data: DataTable{}, Classes: ClassTable{}}
resolveClasses(&result, node.Implies, componentsBase, strictMode, map[string]interface{}{})
dst.Classes = result.Classes
dst.Data = result.Data
dst.Parameters = result.Parameters
dst.Environment = node.Environment
return nil
}
func main() {
parser := argparse.NewParser("classyfy", "A Puppet external node classifier (ENC)")
componentsBase := parser.String("c", "production-base", &argparse.Options{Default: "production", Required: false, Help: "The base path for configuration production."})
nodesFile := parser.String("N", "nodes-file", &argparse.Options{Default: "nodes.yml", Required: false, Help: "The path to the node specification file."})
nodeName := parser.String("n", "node", &argparse.Options{Required: true, Help: "The hostname of the node to classify."})
dataOnly := parser.Flag("d", "data", &argparse.Options{Help: "Only output the data."})
strictMode := parser.Flag("s", "strict", &argparse.Options{Help: "Fail on a inconsistent model."})
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
os.Exit(1)
}
classification := Classification{}
err = classify(&classification, *nodeName, *nodesFile, *componentsBase, *strictMode)
if err != nil {
fmt.Println("Failed to classify the given node!")
fmt.Println(err)
os.Exit(1)
}
var data interface{}
if *dataOnly {
data = classification.Data
} else {
data = classification
}
response, _ := yaml.Marshal(data)
fmt.Printf("---\n%s\n", string(response))
}