-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpacemaker.go
385 lines (328 loc) · 8.98 KB
/
pacemaker.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
// The pacemaker package provides an API for reading the Pacemaker cluster configuration (CIB).
// Copyright (C) 2017 Kristoffer Gronlund <[email protected]>
// See LICENSE for license.
package pacemaker
import (
"fmt"
"runtime"
"strings"
"unsafe"
)
/*
#cgo pkg-config: libxml-2.0 glib-2.0 libqb pacemaker pacemaker-cib
#include <crm/cib.h>
#include <crm/services.h>
#include <crm/common/util.h>
#include <crm/common/xml.h>
#include <crm/common/mainloop.h>
// Flags returned by go_cib_register_notify_callbacks
// indicating which notifications were actually
// available to register (different connection types
// enable different sets of notifications)
#define GO_CIB_NOTIFY_DESTROY 0x1
#define GO_CIB_NOTIFY_ADDREMOVE 0x2
extern int go_cib_signon(cib_t* cib, const char* name, enum cib_conn_type type);
extern int go_cib_signoff(cib_t* cib);
extern int go_cib_query(cib_t * cib, const char *section, xmlNode ** output_data, int call_options);
extern unsigned int go_cib_register_notify_callbacks(cib_t * cib);
extern void go_add_idle_scheduler(GMainLoop* loop);
*/
import "C"
// Error type returned by the functions in this package.
type CibError struct {
msg string
}
func (e *CibError) Error() string {
return e.msg
}
// Internal function used to create a CibError instance
// from a pacemaker return code.
func formatErrorRc(rc int) *CibError {
errorname := C.pcmk_errorname(C.int(rc))
strerror := C.pcmk_strerror(C.int(rc))
if errorname == nil {
errorname = C.CString("")
defer C.free(unsafe.Pointer(errorname))
}
if strerror == nil {
strerror = C.CString("")
defer C.free(unsafe.Pointer(strerror))
}
return &CibError{fmt.Sprintf("%d: %s %s", rc, C.GoString(errorname), C.GoString(strerror))}
}
// When connecting to Pacemaker, we have
// to declare which type of connection to
// use. Since the API is read-only at the
// moment, it only really makes sense to
// pass Query to functions that take a
// CibConnection parameter.
type CibConnection int
const (
Query CibConnection = C.cib_query
Command CibConnection = C.cib_command
NoConnection CibConnection = C.cib_no_connection
CommandNonBlocking CibConnection = C.cib_command_nonblocking
)
type CibOpenConfig struct {
connection CibConnection
file string
shadow string
server string
user string
passwd string
port int
encrypted bool
}
func ForQuery(config *CibOpenConfig) {
config.connection = Query
}
func ForCommand(config *CibOpenConfig) {
config.connection = Command
}
func ForNoConnection(config *CibOpenConfig) {
config.connection = NoConnection
}
func ForCommandNonBlocking(config *CibOpenConfig) {
config.connection = CommandNonBlocking
}
func FromFile(file string) func(*CibOpenConfig) {
return func(config *CibOpenConfig) {
config.file = file
}
}
func FromShadow(shadow string) func(*CibOpenConfig) {
return func(config *CibOpenConfig) {
config.shadow = shadow
}
}
func FromRemote(server, user, passwd string, port int, encrypted bool) func(*CibOpenConfig) {
return func(config *CibOpenConfig) {
config.server = server
config.user = user
config.passwd = passwd
config.port = port
config.encrypted = encrypted
}
}
type Element struct {
Type string
Id string
Attr map[string]string
Elements []*Element
}
type CibEvent int
const (
UpdateEvent CibEvent = 0
DestroyEvent CibEvent = 1
)
//go:generate stringer -type=CibEvent
type CibEventFunc func(event CibEvent, doc *CibDocument)
type subscriptionData struct {
Id int
Callback CibEventFunc
}
// Root entity representing the CIB. Can be
// populated with CIB data if the Decode
// method is used.
type Cib struct {
cCib *C.cib_t
subscribers map[int]CibEventFunc
notifications uint
}
type CibVersion struct {
AdminEpoch int32
Epoch int32
NumUpdates int32
}
type CibDocument struct {
xml *C.xmlNode
}
func (ver *CibVersion) String() string {
return fmt.Sprintf("%d:%d:%d", ver.AdminEpoch, ver.Epoch, ver.NumUpdates)
}
func OpenCib(options ...func(*CibOpenConfig)) (*Cib, error) {
var cib Cib
config := CibOpenConfig{}
for _, opt := range options {
opt(&config)
}
if config.connection != Query && config.connection != Command {
config.connection = Query
}
if config.file != "" {
s := C.CString(config.file)
defer C.free(unsafe.Pointer(s))
cib.cCib = C.cib_file_new(s)
} else if config.shadow != "" {
s := C.CString(config.shadow)
defer C.free(unsafe.Pointer(s))
cib.cCib = C.cib_shadow_new(s)
} else if config.server != "" {
s := C.CString(config.server)
u := C.CString(config.user)
p := C.CString(config.passwd)
defer C.free(unsafe.Pointer(s))
defer C.free(unsafe.Pointer(u))
defer C.free(unsafe.Pointer(p))
var e C.int = 0
if config.encrypted {
e = 1
}
cib.cCib = C.cib_remote_new(s, u, p, (C.int)(config.port), (C.gboolean)(e))
} else {
cib.cCib = C.cib_new()
}
rc := C.go_cib_signon(cib.cCib, C.crm_system_name, (uint32)(config.connection))
if rc != C.pcmk_ok {
return nil, formatErrorRc((int)(rc))
}
return &cib, nil
}
func GetShadowFile(name string) string {
s := C.CString(name)
defer C.free(unsafe.Pointer(s))
return C.GoString(C.get_shadow_file(s))
}
func (cib *Cib) Close() error {
rc := C.go_cib_signoff(cib.cCib)
if rc != C.pcmk_ok {
return formatErrorRc((int)(rc))
}
C.cib_delete(cib.cCib)
cib.cCib = nil
return nil
}
func (doc *CibDocument) Version() *CibVersion {
var admin_epoch C.int
var epoch C.int
var num_updates C.int
ok := C.cib_version_details(doc.xml, (*C.int)(unsafe.Pointer(&admin_epoch)), (*C.int)(unsafe.Pointer(&epoch)), (*C.int)(unsafe.Pointer(&num_updates)))
if ok == 1 {
return &CibVersion{(int32)(admin_epoch), (int32)(epoch), (int32)(num_updates)}
}
return nil
}
func (doc *CibDocument) ToString() string {
buffer := C.dump_xml_unformatted(doc.xml)
defer C.free(unsafe.Pointer(buffer))
return C.GoString(buffer)
}
func (doc *CibDocument) Close() {
C.free_xml(doc.xml)
}
func (cib *Cib) queryImpl(xpath string, nochildren bool) (*C.xmlNode, error) {
var root *C.xmlNode
var rc C.int
var opts C.int
opts = C.cib_sync_call + C.cib_scope_local
if xpath != "" {
opts += C.cib_xpath
}
if nochildren {
opts += C.cib_no_children
}
if xpath != "" {
xp := C.CString(xpath)
defer C.free(unsafe.Pointer(xp))
rc = C.go_cib_query(cib.cCib, xp, (**C.xmlNode)(unsafe.Pointer(&root)), opts)
} else {
rc = C.go_cib_query(cib.cCib, nil, (**C.xmlNode)(unsafe.Pointer(&root)), opts)
}
if rc != C.pcmk_ok {
return nil, formatErrorRc((int)(rc))
}
return root, nil
}
func (cib *Cib) Version() (*CibVersion, error) {
var admin_epoch C.int
var epoch C.int
var num_updates C.int
root, err := cib.queryImpl("/cib", true)
if err != nil {
return nil, err
}
defer C.free_xml(root)
ok := C.cib_version_details(root, (*C.int)(unsafe.Pointer(&admin_epoch)), (*C.int)(unsafe.Pointer(&epoch)), (*C.int)(unsafe.Pointer(&num_updates)))
if ok == 1 {
return &CibVersion{(int32)(admin_epoch), (int32)(epoch), (int32)(num_updates)}, nil
}
return nil, &CibError{"Failed to get CIB version details"}
}
func (cib *Cib) Query() (*CibDocument, error) {
var root *C.xmlNode
root, err := cib.queryImpl("", false)
if err != nil {
return nil, err
}
return &CibDocument{root}, nil
}
func (cib *Cib) QueryNoChildren() (*CibDocument, error) {
var root *C.xmlNode
root, err := cib.queryImpl("", true)
if err != nil {
return nil, err
}
return &CibDocument{root}, nil
}
func (cib *Cib) QueryXPath(xpath string) (*CibDocument, error) {
var root *C.xmlNode
root, err := cib.queryImpl(xpath, false)
if err != nil {
return nil, err
}
return &CibDocument{root}, nil
}
func (cib *Cib) QueryXPathNoChildren(xpath string) (*CibDocument, error) {
var root *C.xmlNode
root, err := cib.queryImpl(xpath, true)
if err != nil {
return nil, err
}
return &CibDocument{root}, nil
}
func init() {
s := C.CString("go-pacemaker")
C.crm_log_init(s, C.LOG_CRIT, 0, 0, 0, nil, 1)
C.free(unsafe.Pointer(s))
}
func IsTrue(bstr string) bool {
sl := strings.ToLower(bstr)
return sl == "true" || sl == "on" || sl == "yes" || sl == "y" || sl == "1"
}
var the_cib *Cib
func (cib *Cib) Subscribers() map[int]CibEventFunc {
return cib.subscribers
}
func (cib *Cib) Subscribe(callback CibEventFunc) (uint, error) {
the_cib = cib
if cib.subscribers == nil {
cib.subscribers = make(map[int]CibEventFunc)
flags := C.go_cib_register_notify_callbacks(cib.cCib)
cib.notifications = uint(flags)
}
id := len(cib.subscribers)
cib.subscribers[id] = callback
return cib.notifications, nil
}
//export diffNotifyCallback
func diffNotifyCallback(current_cib *C.xmlNode) {
for _, callback := range the_cib.subscribers {
callback(UpdateEvent, &CibDocument{current_cib})
}
}
//export destroyNotifyCallback
func destroyNotifyCallback() {
for _, callback := range the_cib.subscribers {
callback(DestroyEvent, nil)
}
}
//export goMainloopSched
func goMainloopSched() {
runtime.Gosched()
}
func Mainloop() {
mainloop := C.g_main_loop_new(nil, C.FALSE)
C.go_add_idle_scheduler(mainloop)
C.g_main_loop_run(mainloop)
C.g_main_loop_unref(mainloop)
}