-
Notifications
You must be signed in to change notification settings - Fork 3
/
pxe.go
369 lines (322 loc) · 9.73 KB
/
pxe.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* pxe.go: provides generic PXE/iPXE-boot capabilities
* this manages both DHCP and TFTP/HTTP services.
* If <file> doesn't exist, but <file>.tpl does, tftp will fill it as as template.
*
* Author: J. Lowell Wofford <lowell@lanl.gov>
*
* This software is open source software available under the BSD-3 license.
* Copyright (c) 2018, Triad National Security, LLC
* See LICENSE file for details.
*/
//go:generate protoc -I ../../kraken/core/proto/src -I . --gogo_out=plugins=grpc:. pxemodule.proto
package pxe
import (
"fmt"
"net"
"os"
"reflect"
"sync"
"time"
"github.com/google/gopacket/layers"
"github.com/mdlayher/raw"
ptypes "github.com/gogo/protobuf/types"
"github.com/gogo/protobuf/proto"
pxepb "github.com/kraken-hpc/kraken-layercake/extensions/pxe"
"github.com/kraken-hpc/kraken/core"
cpb "github.com/kraken-hpc/kraken/core/proto"
ipv4t "github.com/kraken-hpc/kraken/extensions/ipv4/customtypes"
"github.com/kraken-hpc/kraken/lib/types"
"github.com/kraken-hpc/kraken/lib/util"
)
const (
PXEStateURL = "type.googleapis.com/PXE.Client/State"
SrvStateURL = "/Services/pxe/State"
)
type pxmut struct {
f pxepb.Client_State
t pxepb.Client_State
reqs map[string]reflect.Value
timeout string
}
var muts = map[string]pxmut{
"NONEtoWAIT": {
f: pxepb.Client_NONE,
t: pxepb.Client_WAIT,
reqs: reqs,
timeout: "10s",
},
}
// modify these if you want different requires for mutations
var reqs = map[string]reflect.Value{
"/PhysState": reflect.ValueOf(cpb.Node_POWER_ON),
}
// modify this if you want excludes
var excs = map[string]reflect.Value{}
/* we use channels and a node manager rather than locking
to make our node store safe. This is a simpple query
language for that service */
type nodeQueryBy string
const (
queryByIP nodeQueryBy = "IP"
queryByMAC nodeQueryBy = "MAC"
)
//////////////////
// PXE Object /
////////////////
// PXE provides PXE-boot capabilities
type PXE struct {
api types.ModuleAPIClient
cfg *Config
mchan <-chan types.Event
dchan chan<- types.Event
selfIP net.IP
selfNet net.IP
options layers.DHCPOptions
leaseTime time.Duration
iface *net.Interface
rawHandle *raw.Conn
// for maintaining our list of currently booting nodes
mutex sync.RWMutex
nodeBy map[nodeQueryBy]map[string]types.Node
}
/*
* concurrency safe accessors for nodeBy
*/
// NodeGet gets a node that we know about -- concurrency safe
func (px *PXE) NodeGet(qb nodeQueryBy, q string) (n types.Node) { // returns nil for not found
var ok bool
px.mutex.RLock()
if n, ok = px.nodeBy[qb][q]; !ok {
px.api.Logf(types.LLERROR, "tried to acquire node that doesn't exist: %s %s", qb, q)
px.mutex.RUnlock()
return
}
px.mutex.RUnlock()
return
}
// NodeDelete deletes a node that we know about -- cuncurrency safe
func (px *PXE) NodeDelete(qb nodeQueryBy, q string) { // silently ignores non-existent nodes
var n types.Node
var ok bool
px.mutex.Lock()
if n, ok = px.nodeBy[qb][q]; !ok {
px.mutex.Unlock()
return
}
v, e := n.GetValues([]string{px.cfg.IpUrl, px.cfg.MacUrl})
if e != nil {
px.api.Logf(types.LLERROR, "error getting values for node: %v", e)
}
ip := v[px.cfg.IpUrl].Interface().(ipv4t.IP).IP
mac := v[px.cfg.MacUrl].Interface().(ipv4t.MAC).HardwareAddr
delete(px.nodeBy[queryByIP], ip.String())
delete(px.nodeBy[queryByMAC], mac.String())
px.mutex.Unlock()
}
// NodeCreate creates a new node in our node pool -- concurrency safe
func (px *PXE) NodeCreate(n types.Node) (e error) {
v, e := n.GetValues([]string{px.cfg.IpUrl, px.cfg.MacUrl})
if e != nil {
px.api.Logf(types.LLERROR, "error getting values for node: %v", e)
}
if len(v) != 2 {
return fmt.Errorf("missing ip or mac for node, aborting")
}
ip := v[px.cfg.IpUrl].Interface().(ipv4t.IP).IP
mac := v[px.cfg.MacUrl].Interface().(ipv4t.MAC).HardwareAddr
if ip == nil || mac == nil { // incomplete node
return fmt.Errorf("won't add incomplete node: ip: %v, mac: %v", ip, mac)
}
px.mutex.Lock()
px.nodeBy[queryByIP][ip.String()] = n
px.nodeBy[queryByMAC][mac.String()] = n
px.mutex.Unlock()
return
}
/*
* types.Module
*/
var _ types.Module = (*PXE)(nil)
// Name returns the FQDN of the module
func (*PXE) Name() string { return "github.com/kraken-hpc/kraken-layercake/modules/pxe" }
/*
* types.ModuleWithConfig
*/
var _ types.Module = (*PXE)(nil)
// NewConfig returns a fully initialized default config
func (*PXE) NewConfig() proto.Message {
r := &Config{
SrvIfaceUrl: "type.googleapis.com/IPv4.IPv4OverEthernet/Ifaces/kraken/Eth/Iface",
SrvIpUrl: "type.googleapis.com/IPv4.IPv4OverEthernet/Ifaces/kraken/Ip/Ip",
IpUrl: "type.googleapis.com/IPv4.IPv4OverEthernet/Ifaces/kraken/Ip/Ip",
NmUrl: "type.googleapis.com/IPv4.IPv4OverEthernet/Ifaces/kraken/Ip/Subnet",
SubnetUrl: "type.googleapis.com/IPv4.IPv4OverEthernet/Ifaces/kraken/Ip/Subnet",
MacUrl: "type.googleapis.com/IPv4.IPv4OverEthernet/Ifaces/kraken/Eth/Mac",
TftpDir: "tftp",
}
return r
}
// UpdateConfig updates the running config
func (px *PXE) UpdateConfig(cfg proto.Message) (e error) {
if pxcfg, ok := cfg.(*Config); ok {
px.cfg = pxcfg
return
}
return fmt.Errorf("invalid config type")
}
// ConfigURL gives the any resolver URL for the config
func (*PXE) ConfigURL() string {
cfg := &Config{}
any, _ := ptypes.MarshalAny(cfg)
return any.GetTypeUrl()
}
/*
* types.ModuleWithMutations & types.ModuleWithDiscovery
*/
var _ types.ModuleWithMutations = (*PXE)(nil)
var _ types.ModuleWithDiscovery = (*PXE)(nil)
// SetMutationChan sets the current mutation channel
// this is generally done by the API
func (px *PXE) SetMutationChan(c <-chan types.Event) { px.mchan = c }
// SetDiscoveryChan sets the current discovery channel
// this is generally done by the API
func (px *PXE) SetDiscoveryChan(c chan<- types.Event) { px.dchan = c }
/*
* types.ModuleSelfService
*/
var _ types.ModuleSelfService = (*PXE)(nil)
// Entry is the module's executable entrypoint
func (px *PXE) Entry() {
nself, _ := px.api.QueryRead(px.api.Self().String())
v, _ := nself.GetValue(px.cfg.SrvIpUrl)
px.selfIP = v.Interface().(ipv4t.IP).IP
v, _ = nself.GetValue(px.cfg.SubnetUrl)
px.selfNet = v.Interface().(ipv4t.IP).IP
v, _ = nself.GetValue(px.cfg.SrvIfaceUrl)
go px.StartDHCP(v.String(), px.selfIP)
go px.StartTFTP(px.selfIP)
url := util.NodeURLJoin(px.api.Self().String(), SrvStateURL)
ev := core.NewEvent(
types.Event_DISCOVERY,
url,
&core.DiscoveryEvent{
URL: url,
ValueID: "RUN",
},
)
px.dchan <- ev
for {
select {
case v := <-px.mchan:
if v.Type() != types.Event_STATE_MUTATION {
px.api.Log(types.LLERROR, "got unexpected non-mutation event")
break
}
m := v.Data().(*core.MutationEvent)
go px.handleMutation(m)
break
}
}
}
// Init is used to intialize an executable module prior to entrypoint
func (px *PXE) Init(api types.ModuleAPIClient) {
px.api = api
px.mutex = sync.RWMutex{}
px.nodeBy = make(map[nodeQueryBy]map[string]types.Node)
px.nodeBy[queryByIP] = make(map[string]types.Node)
px.nodeBy[queryByMAC] = make(map[string]types.Node)
px.cfg = px.NewConfig().(*Config)
}
// Stop should perform a graceful exit
func (px *PXE) Stop() {
os.Exit(0)
}
////////////////////////
// Unexported methods /
//////////////////////
func (px *PXE) handleMutation(m *core.MutationEvent) {
switch m.Type {
case core.MutationEvent_MUTATE:
switch m.Mutation[1] {
case "NONEtoWAIT": // starting a new mutation, register the node
if e := px.NodeCreate(m.NodeCfg); e != nil {
px.api.Logf(types.LLERROR, "%v", e)
break
}
url := util.NodeURLJoin(m.NodeCfg.ID().String(), PXEStateURL)
ev := core.NewEvent(
types.Event_DISCOVERY,
url,
&core.DiscoveryEvent{
URL: url,
ValueID: "WAIT",
},
)
px.dchan <- ev
case "WAITtoINIT": // we're initializing, but don't do anything (more for discovery/timeout)
}
case core.MutationEvent_INTERRUPT: // on any interrupt, we remove the node
v, e := m.NodeCfg.GetValue(px.cfg.IpUrl)
if e != nil || !v.IsValid() {
break
}
ip := v.Interface().(ipv4t.IP)
px.NodeDelete(queryByIP, ip.String())
}
}
func init() {
module := &PXE{}
mutations := make(map[string]types.StateMutation)
discovers := make(map[string]map[string]reflect.Value)
dpxe := make(map[string]reflect.Value)
si := core.NewServiceInstance("pxe", module.Name(), module.Entry)
for m := range muts {
dur, _ := time.ParseDuration(muts[m].timeout)
mutations[m] = core.NewStateMutation(
map[string][2]reflect.Value{
PXEStateURL: {
reflect.ValueOf(muts[m].f),
reflect.ValueOf(muts[m].t),
},
},
reqs,
excs,
types.StateMutationContext_CHILD,
dur,
[3]string{si.ID(), "/PhysState", "PHYS_HANG"},
)
dpxe[pxepb.Client_State_name[int32(muts[m].t)]] = reflect.ValueOf(muts[m].t)
}
mutations["WAITtoINIT"] = core.NewStateMutation(
map[string][2]reflect.Value{
PXEStateURL: {
reflect.ValueOf(pxepb.Client_WAIT),
reflect.ValueOf(pxepb.Client_INIT),
},
"/RunState": {
reflect.ValueOf(cpb.Node_UNKNOWN),
reflect.ValueOf(cpb.Node_INIT),
},
},
reqs,
excs,
types.StateMutationContext_CHILD,
time.Second*90,
[3]string{si.ID(), "/PhysState", "PHYS_HANG"},
)
dpxe["INIT"] = reflect.ValueOf(pxepb.Client_INIT)
discovers[PXEStateURL] = dpxe
discovers["/RunState"] = map[string]reflect.Value{
"NODE_INIT": reflect.ValueOf(cpb.Node_INIT),
}
discovers["/PhysState"] = map[string]reflect.Value{
"PHYS_HANG": reflect.ValueOf(cpb.Node_PHYS_HANG),
}
discovers[SrvStateURL] = map[string]reflect.Value{
"RUN": reflect.ValueOf(cpb.ServiceInstance_RUN)}
// Register it all
core.Registry.RegisterModule(module)
core.Registry.RegisterServiceInstance(module, map[string]types.ServiceInstance{si.ID(): si})
core.Registry.RegisterDiscoverable(si, discovers)
core.Registry.RegisterMutations(si, mutations)
}