-
Notifications
You must be signed in to change notification settings - Fork 6
/
node_eval.go
106 lines (99 loc) · 2.67 KB
/
node_eval.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
package omcmd
import (
"context"
"fmt"
"github.com/opensvc/om3/core/actioncontext"
"github.com/opensvc/om3/core/client"
"github.com/opensvc/om3/core/nodeaction"
"github.com/opensvc/om3/core/nodeselector"
"github.com/opensvc/om3/core/object"
"github.com/opensvc/om3/core/output"
"github.com/opensvc/om3/core/rawconfig"
"github.com/opensvc/om3/daemon/api"
"github.com/opensvc/om3/util/hostname"
)
type (
CmdNodeEval struct {
OptsGlobal
OptsLock
Keywords []string
Impersonate string
NodeSelector string
}
)
func (t *CmdNodeEval) Run() error {
if t.Local {
return t.doNodeAction()
}
c, err := client.New()
if err != nil {
return err
}
nodenames := []string{hostname.Hostname()}
if t.NodeSelector != "" {
sel := nodeselector.New(t.NodeSelector)
if l, err := sel.Expand(); err != nil {
return err
} else {
nodenames = l
}
}
l := make(api.KeywordItems, 0)
for _, nodename := range nodenames {
params := api.GetNodeConfigGetParams{}
params.Kw = &t.Keywords
v := true
params.Evaluate = &v
if t.Impersonate != "" {
params.Impersonate = &t.Impersonate
}
response, err := c.GetNodeConfigGetWithResponse(context.Background(), nodename, ¶ms)
if err != nil {
return err
}
switch {
case response.JSON200 != nil:
l = append(l, response.JSON200.Items...)
case response.JSON400 != nil:
return fmt.Errorf("%s: %s", nodename, *response.JSON400)
case response.JSON401 != nil:
return fmt.Errorf("%s: %s", nodename, *response.JSON401)
case response.JSON403 != nil:
return fmt.Errorf("%s: %s", nodename, *response.JSON403)
case response.JSON500 != nil:
return fmt.Errorf("%s: %s", nodename, *response.JSON500)
default:
return fmt.Errorf("%s: unexpected response: %s", nodename, response.Status())
}
}
defaultOutput := "tab=NODE:meta.node,KEYWORD:meta.keyword,VALUE:data.value,EVALUATED_AS:meta.evaluated_as"
output.Renderer{
DefaultOutput: defaultOutput,
Output: t.Output,
Color: t.Color,
Data: api.KeywordList{Items: l, Kind: "KeywordList"},
Colorize: rawconfig.Colorize,
}.Print()
return nil
}
func (t *CmdNodeEval) doNodeAction() error {
return nodeaction.New(
nodeaction.WithLocal(t.Local),
nodeaction.WithFormat(t.Output),
nodeaction.WithColor(t.Color),
nodeaction.WithServer(t.Server),
nodeaction.WithLocalFunc(func() (interface{}, error) {
n, err := object.NewNode()
if err != nil {
return nil, err
}
ctx := context.Background()
ctx = actioncontext.WithLockDisabled(ctx, t.Disable)
ctx = actioncontext.WithLockTimeout(ctx, t.Timeout)
for _, s := range t.Keywords {
return n.EvalAs(ctx, s, t.Impersonate)
}
return nil, nil
}),
).Do()
}