/
rhprovider.go
410 lines (364 loc) · 9.04 KB
/
rhprovider.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
package rhprovider
import (
"bufio"
"bytes"
"encoding/xml"
"errors"
"fmt"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/sgotti/fence"
"github.com/sgotti/fence/utils"
)
type RHAgentProvider struct {
config *RHAgentProviderConfig
agents map[string]*RHAgent
}
type RHAgentProviderConfig struct {
Glob string
}
type RHAgent struct {
Command string
*fence.Agent
}
func newRHAgent() *RHAgent {
return &RHAgent{Agent: fence.NewAgent()}
}
const (
defaultGlob = "/usr/sbin/fence_*"
)
var defaultConfig = &RHAgentProviderConfig{Glob: defaultGlob}
func New(config *RHAgentProviderConfig) *RHAgentProvider {
p := &RHAgentProvider{agents: make(map[string]*RHAgent)}
if config != nil {
p.config = config
} else {
p.config = defaultConfig
}
return p
}
type RHResourceAgent struct {
Command string
XMLName xml.Name `xml:"resource-agent"`
Name string `xml:"name,attr"`
ShortDesc string `xml:"shortdesc,attr"`
LongDesc string `xml:"longdesc"`
VendorUrl string `xml:"vendor-url"`
Parameters []RHParameter `xml:"parameters>parameter"`
Actions []RHAction `xml:"actions>action"`
}
type RHParameter struct {
Name string `xml:"name,attr"`
Unique bool `xml:"unique,attr"`
Required bool `xml:"required,attr"`
ShortDesc string `xml:"shortdesc"`
Content RHContent `xml:"content"`
}
type RHContent struct {
ContentType string `xml:"type,attr"`
Default string `xml:"default,attr"`
Options []RHContentOptions `xml:"option"`
}
type RHContentOptions struct {
Value string `xml:"value,attr"`
}
type RHAction struct {
Name string `xml:"name,attr"`
OnTarget string `xml:"on_target,attr"`
Automatic string `xml:"automatic,attr"`
}
func parseMetadata(mdxml []byte) (*RHResourceAgent, error) {
v := &RHResourceAgent{}
err := xml.Unmarshal(mdxml, v)
if err != nil {
return nil, err
}
return v, nil
}
func (r *RHResourceAgent) toResourceAgent() (*RHAgent, error) {
a := newRHAgent()
a.Command = r.Command
a.Name = r.Name
a.ShortDesc = r.ShortDesc
a.LongDesc = r.LongDesc
for _, mdp := range r.Parameters {
// If "action" parameter ignore it and set agent's DefaultAction
if mdp.Name == "action" && mdp.Content.Default != "" {
fa, err := StringToAction(mdp.Content.Default)
if err != nil {
// Ignore bad default action
} else {
a.DefaultAction = fa
}
continue
}
// If "port" parameter ignore it and set agent's DefaultAction
if mdp.Name == "port" {
a.MultiplePorts = true
continue
}
// If "port" parameter ignore it and set agent's DefaultAction
if mdp.Name == "separator" {
continue
}
// TODO. All the metadatas reports unique = "0" but I think they should be unique...
p := &fence.Parameter{Name: mdp.Name, Unique: mdp.Unique, Required: mdp.Required, Desc: mdp.ShortDesc}
switch mdp.Content.ContentType {
case "boolean":
p.ContentType = fence.Boolean
if mdp.Content.Default != "" {
value, err := strconv.ParseBool(mdp.Content.Default)
if err != nil {
return nil, err
}
p.Default = value
}
case "string":
p.ContentType = fence.String
if mdp.Content.Default != "" {
p.Default = mdp.Content.Default
}
case "select":
p.HasOptions = true
p.ContentType = fence.String
if mdp.Content.Default != "" {
p.Default = mdp.Content.Default
}
default:
return nil, fmt.Errorf("Agent: %s, parameter: %s. Wrong content type: %s", a.Name, p.Name, mdp.Content.ContentType)
}
for _, o := range mdp.Content.Options {
p.Options = append(p.Options, o.Value)
}
a.Parameters[p.Name] = p
}
for _, mda := range r.Actions {
if mda.Name == "on" {
if mda.Automatic == "1" {
a.UnfenceAction = fence.On
}
if mda.OnTarget == "1" {
a.UnfenceOnTarget = true
}
}
fa, err := StringToAction(mda.Name)
if err != nil {
// Ignore unknown action
continue
}
a.Actions = append(a.Actions, fa)
}
return a, nil
}
func (p *RHAgentProvider) LoadAgents(timeout time.Duration) error {
p.agents = make(map[string]*RHAgent)
files, err := filepath.Glob(p.config.Glob)
if err != nil {
return err
}
t := time.Now()
nexttimeout := 0 * time.Second
// TODO Detected duplicate agents? (agents returning the same name in metadata)
for _, file := range files {
if timeout != 0 {
nexttimeout = timeout - time.Since(t)
if nexttimeout < 0 {
return errors.New("timeout")
}
}
a, err := p.LoadAgent(file, nexttimeout)
if err != nil {
continue
}
p.agents[a.Name] = a
}
return nil
}
func (p *RHAgentProvider) LoadAgent(file string, timeout time.Duration) (*RHAgent, error) {
cmd := exec.Command(file, "-o", "metadata")
var b bytes.Buffer
cmd.Stdout = &b
err := cmd.Start()
if err != nil {
return nil, err
}
if timeout == time.Duration(0) {
err = cmd.Wait()
} else {
err = utils.WaitTimeout(cmd, timeout)
}
if err != nil {
return nil, err
}
mdxml := b.Bytes()
mda, err := parseMetadata(mdxml)
if err != nil {
return nil, err
}
a, err := mda.toResourceAgent()
if err != nil {
return nil, fmt.Errorf("Agent \"%s\": %s", mda.Name, err)
}
a.Command = file
return a, nil
}
func (p *RHAgentProvider) getRHAgent(name string) (*RHAgent, error) {
a, ok := p.agents[name]
if !ok {
return nil, fmt.Errorf("Unknown agent: %s", name)
}
return a, nil
}
func (p *RHAgentProvider) GetAgents() (fence.Agents, error) {
fagents := make(fence.Agents)
for _, a := range p.agents {
fagents[a.Name] = a.Agent
}
return fagents, nil
}
func (p *RHAgentProvider) GetAgent(name string) (*fence.Agent, error) {
a, ok := p.agents[name]
if !ok {
return nil, fmt.Errorf("Unknown agent: %s", name)
}
return a.Agent, nil
}
func ActionToString(action fence.Action) string {
switch action {
case fence.On:
return "on"
case fence.Off:
return "off"
case fence.Reboot:
return "reboot"
case fence.Status:
return "status"
case fence.List:
return "list"
case fence.Monitor:
return "monitor"
}
return ""
}
func StringToAction(action string) (fence.Action, error) {
switch action {
case "on", "enable":
return fence.On, nil
case "off", "disable":
return fence.Off, nil
case "reboot":
return fence.Reboot, nil
case "status":
return fence.Status, nil
case "list":
return fence.List, nil
case "monitor":
return fence.Monitor, nil
}
return 0, fmt.Errorf("Unknown fence action: %s", action)
}
func (p *RHAgentProvider) run(ac *fence.AgentConfig, action string, timeout time.Duration) ([]byte, error) {
a, err := p.getRHAgent(ac.Name)
if err != nil {
return nil, err
}
command := a.Command
cmd := exec.Command(command)
cmdstdin, err := cmd.StdinPipe()
var b bytes.Buffer
cmd.Stdout = &b
if err != nil {
return nil, err
}
err = cmd.Start()
if err != nil {
return nil, err
}
_, err = cmdstdin.Write([]byte(fmt.Sprintf("action=%s\n", action)))
if err != nil {
return nil, err
}
if ac.Port != "" {
_, err = cmdstdin.Write([]byte(fmt.Sprintf("port=%s\n", ac.Port)))
if err != nil {
return nil, err
}
}
for name, values := range ac.Parameters {
for _, value := range values {
_, err = cmdstdin.Write([]byte(fmt.Sprintf("%s=%s\n", name, value)))
if err != nil {
return nil, err
}
}
}
cmdstdin.Close()
if timeout == time.Duration(0) {
err = cmd.Wait()
} else {
err = utils.WaitTimeout(cmd, timeout)
}
if err != nil {
return b.Bytes(), err
}
return b.Bytes(), nil
}
func (p *RHAgentProvider) Status(ac *fence.AgentConfig, timeout time.Duration) (fence.DeviceStatus, error) {
_, err := p.run(ac, "status", timeout)
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
// Process exited with exit code != 0
return fence.Ko, nil
}
return fence.Ko, err
}
return fence.Ok, nil
}
func (p *RHAgentProvider) Monitor(ac *fence.AgentConfig, timeout time.Duration) (fence.DeviceStatus, error) {
_, err := p.run(ac, "monitor", timeout)
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
// Process exited with exit code != 0
return fence.Ko, nil
}
return fence.Ko, err
}
return fence.Ok, nil
}
func (p *RHAgentProvider) List(ac *fence.AgentConfig, timeout time.Duration) (fence.PortList, error) {
out, err := p.run(ac, "list", timeout)
if err != nil {
return nil, err
}
portList := make(fence.PortList, 0)
reader := bufio.NewReader(bytes.NewReader(out))
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
var portName fence.PortName
line := scanner.Text() // Println will add back the final '\n'
split := strings.Split(line, ",")
switch len(split) {
case 1:
portName = fence.PortName{Name: split[0]}
case 2:
portName = fence.PortName{Name: split[0], Alias: split[1]}
default:
return nil, fmt.Errorf("Wrong list format")
}
portList = append(portList, portName)
}
return portList, nil
}
func (p *RHAgentProvider) Run(ac *fence.AgentConfig, action fence.Action, timeout time.Duration) error {
// Specify action only if action !- fence.None,
// elsewhere the agent will run the default action
var actionstr string
if action != fence.None {
actionstr = ActionToString(action)
}
_, err := p.run(ac, actionstr, timeout)
return err
}