/
pipower.go
424 lines (388 loc) · 10.7 KB
/
pipower.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* pipower.go: a power control module for the proprietary BitScope power control module
*
* 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 ../../core/proto/src -I . --gogo_out=plugins=grpc:. pipower.proto
/*
* This module will manipulate the PhysState state field.
* It will be restricted to Arch = aarch64 & Platform = rpi3.
* This requires at least Chassis & Rank to be set in the RPi3 extension.
* This nodename format is a key to knowing which PiPower server to talk to.
*/
package pipower
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"reflect"
"strconv"
"strings"
"sync"
"time"
"github.com/gogo/protobuf/proto"
ptypes "github.com/gogo/protobuf/types"
"github.com/kraken-hpc/kraken/core"
cpb "github.com/kraken-hpc/kraken/core/proto"
pipb "github.com/kraken-hpc/kraken/extensions/rpi3"
"github.com/kraken-hpc/kraken/lib/types"
"github.com/kraken-hpc/kraken/lib/util"
)
const (
ChassisURL string = "type.googleapis.com/RPi3.Pi/Chassis"
RankURL string = "type.googleapis.com/RPi3.Pi/Rank"
)
// ppNode is the PiPower node struct
type ppNode struct {
ID string `json:"id,omitempty"`
State string `json:"state,omitempty"`
}
// ppmut helps us succinctly define our mutations
type ppmut struct {
f cpb.Node_PhysState // from
t cpb.Node_PhysState // to
timeout string // timeout
// everything fails to PHYS_HANG
}
// our mutation definitions
// also we discover anything we can migrate to
var muts = map[string]ppmut{
"UKtoOFF": {
f: cpb.Node_PHYS_UNKNOWN,
t: cpb.Node_POWER_OFF,
timeout: "5s",
},
"OFFtoON": {
f: cpb.Node_POWER_OFF,
t: cpb.Node_POWER_ON,
timeout: "5s",
},
"ONtoOFF": {
f: cpb.Node_POWER_ON,
t: cpb.Node_POWER_OFF,
timeout: "5s",
},
"HANGtoOFF": {
f: cpb.Node_PHYS_HANG,
t: cpb.Node_POWER_OFF,
timeout: "10s", // we need a longer timeout, because we let it sit cold for a few seconds
},
"UKtoHANG": { // this one should never happen; just making sure HANG gets connected in our graph
f: cpb.Node_PHYS_UNKNOWN,
t: cpb.Node_PHYS_HANG,
timeout: "0s",
},
}
// modify these if you want different requires for mutations
var reqs = map[string]reflect.Value{
"/Arch": reflect.ValueOf("aarch64"),
"/Platform": reflect.ValueOf("rpi3"),
}
// modify this if you want excludes
var excs = map[string]reflect.Value{}
////////////////////
// PiPower Object /
//////////////////
// PiPower provides a power on/off interface to the proprietary BitScope power control plane
type PiPower struct {
api types.ModuleAPIClient
mutex *sync.Mutex
queue map[string][2]string // map[<nodename>][<mutation>, <nodeidstr>]
cfg *Config
mchan <-chan types.Event
dchan chan<- types.Event
ticker *time.Ticker
}
/*
*types.Module
*/
var _ types.Module = (*PiPower)(nil)
// Name returns the FQDN of the module
func (*PiPower) Name() string { return "github.com/kraken-hpc/kraken/modules/pipower" }
/*
* types.ModuleWithConfig
*/
var _ types.ModuleWithConfig = (*PiPower)(nil)
// NewConfig returns a fully initialized default config
func (*PiPower) NewConfig() proto.Message {
r := &Config{
Servers: map[string]*Server{
"c0": {
Name: "c0",
Ip: "127.0.0.1",
Port: 8000,
},
},
Tick: "1s",
}
return r
}
// UpdateConfig updates the running config
func (pp *PiPower) UpdateConfig(cfg proto.Message) (e error) {
if ppcfg, ok := cfg.(*Config); ok {
pp.cfg = ppcfg
if pp.ticker != nil {
pp.ticker.Stop()
dur, _ := time.ParseDuration(pp.cfg.Tick)
pp.ticker = time.NewTicker(dur)
}
return
}
return fmt.Errorf("invalid config type")
}
// ConfigURL gives the any resolver URL for the config
func (*PiPower) ConfigURL() string {
cfg := &Config{}
any, _ := ptypes.MarshalAny(cfg)
return any.GetTypeUrl()
}
/*
* types.ModuleWithMutations & types.ModuleWithDiscovery
*/
var _ types.ModuleWithMutations = (*PiPower)(nil)
var _ types.ModuleWithDiscovery = (*PiPower)(nil)
// SetMutationChan sets the current mutation channel
// this is generally done by the API
func (pp *PiPower) SetMutationChan(c <-chan types.Event) { pp.mchan = c }
// SetDiscoveryChan sets the current discovery channel
// this is generally done by the API
func (pp *PiPower) SetDiscoveryChan(c chan<- types.Event) { pp.dchan = c }
/*
* types.ModuleSelfService
*/
var _ types.ModuleSelfService = (*PiPower)(nil)
// Entry is the module's executable entrypoint
func (pp *PiPower) Entry() {
url := util.NodeURLJoin(pp.api.Self().String(),
util.URLPush(util.URLPush("/Services", "pipower"), "State"))
pp.dchan <- core.NewEvent(
types.Event_DISCOVERY,
url,
&core.DiscoveryEvent{
URL: url,
ValueID: "RUN",
},
)
// main loop
for {
// fire a timer that will do our next work
dur, _ := time.ParseDuration(pp.cfg.GetTick())
pp.ticker = time.NewTicker(dur)
select {
case <-pp.ticker.C: // time to do work
go pp.fireChanges()
break
case m := <-pp.mchan: // mutation request
go pp.handleMutation(m)
break
}
}
}
// Init is used to intialize an executable module prior to entrypoint
func (pp *PiPower) Init(api types.ModuleAPIClient) {
pp.api = api
pp.mutex = &sync.Mutex{}
pp.queue = make(map[string][2]string)
pp.cfg = pp.NewConfig().(*Config)
}
// Stop should perform a graceful exit
func (pp *PiPower) Stop() {
os.Exit(0)
}
////////////////////////
// Unexported methods /
//////////////////////
// this might get some strange results if you don't stick to the c<num>n<num> scheme
func (*PiPower) parseNodeName(nn string) (chassis, node string) {
// c0n12 -> c0, 12
s := strings.Split(nn, "n")
return s[0], s[1]
}
func (pp *PiPower) fireChanges() {
on := map[string][]string{}
off := map[string][]string{}
stat := map[string][]string{}
idmap := map[string]string{}
pp.mutex.Lock()
for m := range pp.queue {
c, n := pp.parseNodeName(m)
idmap[m] = pp.queue[m][1]
switch pp.queue[m][0] {
case "UKtoOFF": // this actually just forces discovery
stat[c] = append(stat[c], n)
break
case "OFFtoON":
on[c] = append(on[c], n)
break
case "ONtoOFF":
fallthrough
case "HANGtoOFF":
off[c] = append(off[c], n)
break
}
}
pp.queue = make(map[string][2]string)
pp.mutex.Unlock()
for c := range on {
pp.fire(c, on[c], "/state/on", idmap)
}
for c := range off {
pp.fire(c, off[c], "/state/off", idmap)
}
for c := range stat {
pp.fire(c, stat[c], "/state", idmap)
}
}
func (pp *PiPower) fire(c string, ns []string, cmd string, idmap map[string]string) {
srv, ok := pp.cfg.Servers[c]
if !ok {
pp.api.Logf(types.LLERROR, "cannot control power for unknown chassis: %s", c)
return
}
addr := srv.Ip + ":" + strconv.Itoa(int(srv.Port))
nlist := strings.Join(ns, ",")
url := "http://" + addr + "/nodes/" + nlist + cmd
resp, e := http.Get(url)
if e != nil {
pp.api.Logf(types.LLERROR, "http GET to API failed: %v", e)
return
}
defer resp.Body.Close()
body, e := ioutil.ReadAll(resp.Body)
if e != nil {
pp.api.Logf(types.LLERROR, "http GET failed to read body: %v", e)
return
}
rs := []ppNode{}
e = json.Unmarshal(body, &rs)
if e != nil {
pp.api.Logf(types.LLERROR, "got invalid JSON response: %v", e)
return
}
for _, r := range rs {
url := util.NodeURLJoin(idmap[c+"n"+r.ID], "/PhysState")
vid := "POWER_OFF"
if r.State == "on" {
vid = "POWER_ON"
}
v := core.NewEvent(
types.Event_DISCOVERY,
url,
&core.DiscoveryEvent{
URL: url,
ValueID: vid,
},
)
pp.dchan <- v
}
}
func (pp *PiPower) handleMutation(m types.Event) {
if m.Type() != types.Event_STATE_MUTATION {
pp.api.Log(types.LLINFO, "got an unexpected event type on mutation channel")
}
me := m.Data().(*core.MutationEvent)
//nodename := me.NodeCfg.Message().(*cpb.Node).Nodename
vs, e := me.NodeCfg.GetValues([]string{ChassisURL, RankURL})
if e != nil {
pp.api.Logf(types.LLERROR, "error getting values for node: %v", e)
}
// we make a speciall "nodename" consisting of <chassis>n<rank> to key by
// mostly for historical convenience
if len(vs) != 2 {
pp.api.Logf(types.LLERROR, "incomplete RPi3 data for power control: %v", vs)
return
}
nodename := vs[ChassisURL].String() + "n" + strconv.FormatUint(vs[RankURL].Uint(), 10)
switch me.Type {
case core.MutationEvent_MUTATE:
switch me.Mutation[1] {
case "UKtoOFF": // this actually just forces discovery
fallthrough
case "OFFtoON":
fallthrough
case "ONtoOFF":
fallthrough
case "HANGtoOFF":
pp.mutex.Lock()
pp.queue[nodename] = [2]string{me.Mutation[1], me.NodeCfg.ID().String()}
pp.mutex.Unlock()
/*
url := util.NodeURLJoin(me.NodeCfg.ID().String(), "/RunState")
ev := core.NewEvent(
types.Event_DISCOVERY,
url,
&core.DiscoveryEvent{
URL: url,
ValueID: "RUN_UK",
},
)
pp.dchan <- ev
url := util.NodeURLJoin(me.NodeCfg.ID().String(), "type.googleapis.com/RPi3.Pi/Pxe")
ev := core.NewEvent(
types.Event_DISCOVERY,
url,
&core.DiscoveryEvent{
URL: url,
ValueID: "PXE_NONE",
},
)
pp.dchan <- ev
*/
break
case "UKtoHANG": // we don't actually do this
fallthrough
default:
pp.api.Logf(types.LLDEBUG, "unexpected event: %s", me.Mutation[1])
}
break
case core.MutationEvent_INTERRUPT:
pp.mutex.Lock()
delete(pp.queue, nodename)
pp.mutex.Unlock()
break
}
}
// initialization
func init() {
module := &PiPower{}
mutations := make(map[string]types.StateMutation)
discovers := make(map[string]map[string]reflect.Value)
drstate := make(map[string]reflect.Value)
si := core.NewServiceInstance("pipower", module.Name(), module.Entry)
for m := range muts {
dur, _ := time.ParseDuration(muts[m].timeout)
mutations[m] = core.NewStateMutation(
map[string][2]reflect.Value{
"/PhysState": {
reflect.ValueOf(muts[m].f),
reflect.ValueOf(muts[m].t),
},
},
reqs,
excs,
types.StateMutationContext_CHILD,
dur,
[3]string{si.ID(), "/PhysState", "PHYS_HANG"},
)
drstate[cpb.Node_PhysState_name[int32(muts[m].t)]] = reflect.ValueOf(muts[m].t)
}
discovers["/PhysState"] = drstate
discovers["/RunState"] = map[string]reflect.Value{
"RUN_UK": reflect.ValueOf(cpb.Node_UNKNOWN),
}
discovers["type.googleapis.com/RPi3.Pi/Pxe"] = map[string]reflect.Value{
"PXE_NONE": reflect.ValueOf(pipb.Pi_NONE),
}
discovers["/Services/pipower/State"] = 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)
}