-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
193 lines (169 loc) · 4.65 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
package main
import (
"encoding/json"
"io"
"log"
"os"
"sort"
"strings"
"text/template"
"github.com/resinstack/netbox-dnsmasq/netbox"
)
// DHCPHost contains the information required to generate a matching
// line in a dhcp configuration daemon.
type DHCPHost struct {
DeviceID int64
HWAddr []string
Name string
Addr string
IPMIMAC string
IPMIAddr string
}
// ShoelacesHost is a host that can be mapped without user input in
// shoelaces.
type ShoelacesHost struct {
Network string `json:"network"`
Script ShoeScript `json:"script"`
}
// ShoeScript is the nested script type for shoelaces. It is only
// supported to specify by name.
type ShoeScript struct {
Name string `json:"name"`
}
// ShoelacesNetworkMap matches the structure that shoelaces expects to
// be able to read in.
type ShoelacesNetworkMap struct {
NetworkMaps []ShoelacesHost `json:"networkMaps"`
}
func main() {
if _, verbose := os.LookupEnv("VERBOSE"); !verbose {
log.SetOutput(io.Discard)
}
hostTmplStr := "{{JoinStrings .HWAddr \",\"}},{{.Addr}}\n{{ if and .IPMIMAC .IPMIAddr }}{{.IPMIMAC}},{{.IPMIAddr}}\n{{ end }}"
if hts := os.Getenv("DNSMASQ_TEMPLATE"); hts != "" {
hostTmplStr = hts
}
hostTmpl := template.New("dhcp-host")
hostTmpl = hostTmpl.Funcs(template.FuncMap{"JoinStrings": strings.Join})
hostTmpl, err := hostTmpl.Parse(hostTmplStr)
if err != nil {
log.Println("Error parsing dhcp-host template", err)
os.Exit(1)
}
token := os.Getenv("NETBOX_TOKEN")
if token == "" {
log.Println("Please provide netbox API token via env var NETBOX_TOKEN")
os.Exit(1)
}
netboxURL := os.Getenv("NETBOX_URL")
if netboxURL == "" {
log.Println("Please provide netbox url via NETBOX_URL")
os.Exit(1)
}
nb, err := netbox.NewClient(netbox.WithNetBoxURL(netboxURL), netbox.WithToken(token))
if err != nil {
log.Println("Error initializing client:", err)
os.Exit(1)
}
site := os.Getenv("NETBOX_SITE")
tag := os.Getenv("NETBOX_TAG")
shoetag := os.Getenv("SHOELACES_TAG_PREFIX")
devices, err := nb.ListDevices(site, tag)
if err != nil {
log.Println("Error listing devices:", err)
os.Exit(1)
}
hosts := make(map[int64]*DHCPHost, len(devices))
shoenets := []ShoelacesHost{}
for _, dev := range devices {
if dev.PrimaryIPv4.Address == "" {
continue
}
ipaddr := strings.SplitN(dev.PrimaryIPv4.Address, "/", 2)[0]
ifres, err := nb.ListInterfaces(dev.ID, false)
if err != nil {
log.Printf("Error pulling interfaces for %s: %v", dev.Name, err)
continue
}
if len(ifres) == 0 {
log.Printf("No interface available for PXE! (%s)", dev.Name)
continue
}
hwaddrs := make([]string, len(ifres))
for i, int := range ifres {
if int.MACAddress == "" {
continue
}
hwaddrs[i] = strings.ToLower(int.MACAddress)
}
// Fetch in OOB address information. Useful for
// supplying DHCP to an IPMI fabric.
ipmiAddr := strings.SplitN(dev.IPMIAddress.Address, "/", 2)[0]
ipmiRes, _ := nb.ListInterfaces(dev.ID, true)
ipmiMAC := ""
if len(ipmiRes) == 1 {
ipmiMAC = strings.ToLower(ipmiRes[0].MACAddress)
}
log.Println(dev.ID, dev.Name, ipaddr, hwaddrs, ipmiAddr, ipmiMAC)
hosts[dev.ID] = &DHCPHost{
DeviceID: dev.ID,
Name: dev.Name,
Addr: ipaddr,
HWAddr: hwaddrs,
IPMIAddr: ipmiAddr,
IPMIMAC: ipmiMAC,
}
// Construct the shoelaces mapping if enabled.
if shoetag != "" {
for _, tag := range dev.Tags {
if strings.HasPrefix(tag.Slug, shoetag) {
// Map this script for this host's IPs
shoehost := ShoelacesHost{
Network: ipaddr + "/32",
Script: ShoeScript{
Name: strings.TrimPrefix(tag.Slug, shoetag) + ".ipxe",
},
}
shoenets = append(shoenets, shoehost)
break
}
}
}
}
if os.Getenv("DNSMASQ_HOSTSFILE") != "" {
tHosts := make([]*DHCPHost, len(hosts))
i := 0
for _, host := range hosts {
sort.Strings(host.HWAddr)
tHosts[i] = host
i++
}
sort.Slice(tHosts, func(i, j int) bool {
return tHosts[i].HWAddr[0] < tHosts[j].HWAddr[0]
})
f, err := os.Create(os.Getenv("DNSMASQ_HOSTSFILE"))
if err != nil {
log.Println("Error writing out hosts file", err)
os.Exit(1)
}
defer f.Close()
for _, host := range tHosts {
if err := hostTmpl.Execute(f, host); err != nil {
log.Println("Error executing template", err)
}
}
}
if os.Getenv("SHOELACES_MAPFILE") != "" {
f, err := os.Create(os.Getenv("SHOELACES_MAPFILE"))
if err != nil {
log.Println("Error opening shoelaces map file", err)
os.Exit(1)
}
defer f.Close()
enc := json.NewEncoder(f)
if err := enc.Encode(ShoelacesNetworkMap{NetworkMaps: shoenets}); err != nil {
log.Println("Error writing shoelaces mappings", err)
os.Exit(1)
}
}
}