-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
182 lines (162 loc) · 5.42 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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/flosch/pongo2/v6"
"github.com/google/go-jsonnet"
)
type Config struct {
Templates map[string]map[string]string `json:"templates"`
Clusters map[string]Cluster `json:"clusters"`
Nodes map[string]Node `json:"nodes"`
MacAddresses map[string]string `json:"macAddresses"`
}
type Cluster struct {
Nodes []string `json:"nodes"`
Templates map[string]string `json:"templates"`
InitrdUrl string `json:"initrdUrl"`
KernelUrl string `json:"kernelUrl"`
InstallRepoUrl string `json:"installRepoUrl"`
InstallKickstartUrl string `json:"installKickstartUrl"`
}
type Node struct {
MacAddress string `json:"macAddress"`
IgnoreDisks []string `json:"ignoreDisks"`
Network Network `json:"network"`
Cluster string `json:"cluster"`
Templates map[string]string `json:"templates"`
InitrdUrl string `json:"initrdUrl"`
KernelUrl string `json:"kernelUrl"`
InstallRepoUrl string `json:"installRepoUrl"`
InstallKickstartUrl string `json:"installKickstartUrl"`
}
type Network struct {
Device string `json:"device"`
BondSlaves string `json:"bondSlaves"`
}
func joinWithCommaFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
if !in.CanSlice() {
return nil, &pongo2.Error{
Sender: "filter:random",
OrigError: errors.New("input is not sliceable"),
}
}
slice := in.Interface().([]interface{})
strSlice := make([]string, len(slice))
for i, v := range slice {
// Convert each element in the slice to a string
strSlice[i] = pongo2.AsValue(v).String()
}
// Join the string slice with a comma
joined := strings.Join(strSlice, ",")
return pongo2.AsValue(joined), nil
}
func renderTemplate(w http.ResponseWriter, templateContent string, context pongo2.Context) {
tpl, err := pongo2.FromString(templateContent)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tpl.ExecuteWriter(context, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func fetchTemplate(macAddress string, config Config, templateType string) (string, string) {
nodeName, ok := config.MacAddresses[macAddress]
if !ok {
return "", "MAC not found"
}
node, ok := config.Nodes[nodeName]
if !ok {
return "", "Node not found"
}
cluster := node.Cluster
if cluster == "" {
return "", "Cluster not found"
}
templateName, ok := config.Clusters[cluster].Templates[templateType]
if !ok {
templateName = "default"
}
template, ok := config.Templates[templateType][templateName]
if !ok {
return "", "Template not found"
}
return template, ""
}
func main() {
// Default file path
filePath := "default.jsonnet"
// Check if environment variable is set for the file path
if val, ok := os.LookupEnv("CONFIG_PATH"); ok {
filePath = val
}
// Read Jsonnet file
data, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
return
}
// Create a Jsonnet VM and evaluate the string
vm := jsonnet.MakeVM()
jsonStr, err := vm.EvaluateSnippet(filePath, string(data))
if err != nil {
fmt.Printf("Error evaluating Jsonnet: %v\n", err)
return
}
fmt.Println("Generated JSON:")
fmt.Println(jsonStr)
var config Config
err = json.Unmarshal([]byte(jsonStr), &config)
if err != nil {
fmt.Printf("Error unmarshalling JSON: %v\n", err)
return
}
pongo2.RegisterFilter("join_with_comma", joinWithCommaFilter)
// fmt.Println("Templates:", config.Templates)
// fmt.Println("Cluster US1 Nodes:", config.Clusters["us1"].Nodes)
// fmt.Println("Node 'beastmode.cloud-fortress.net' MAC Address:", config.Nodes["beastmode.cloud-fortress.net"].MacAddress)
http.HandleFunc("/ipxe", func(w http.ResponseWriter, r *http.Request) {
macAddress := r.URL.Query().Get("mac_address")
if macAddress == "" {
http.Error(w, "MAC address is required", http.StatusBadRequest)
return
}
template, err := fetchTemplate(macAddress, config, "ipxe")
if err != "" {
http.Error(w, err, http.StatusNotFound)
return
}
renderTemplate(w, template, pongo2.Context{
"initrdUrl": "http://nas-01.cloud-fortress.net/isos/Rocky-9.3-x86_64-dvd/images/pxeboot/initrd.img",
"kernelUrl": "http://nas-01.cloud-fortress.net/isos/Rocky-9.3-x86_64-dvd/images/pxeboot/vmlinuz",
"installRepoUrl": "http://nas-01.cloud-fortress.net/isos/Rocky-9.3-x86_64-dvd",
"installKickstartUrl": "https://netboot.cloud-fortress.net/kickstart?mac_address${net0/mac}",
})
})
http.HandleFunc("/kickstart", func(w http.ResponseWriter, r *http.Request) {
macAddress := r.URL.Query().Get("mac_address")
if macAddress == "" {
http.Error(w, "MAC address is required", http.StatusBadRequest)
return
}
template, err := fetchTemplate(macAddress, config, "kickstart")
if err != "" {
http.Error(w, err, http.StatusNotFound)
return
}
renderTemplate(w, template, pongo2.Context{
"networkLink": "",
})
})
log.Println("Server is running on http://localhost:8080/")
http.ListenAndServe(":8080", nil)
}