-
Notifications
You must be signed in to change notification settings - Fork 6
/
object_stop.go
99 lines (95 loc) · 2.89 KB
/
object_stop.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
package omcmd
import (
"context"
"fmt"
"github.com/opensvc/om3/core/actioncontext"
"github.com/opensvc/om3/core/client"
"github.com/opensvc/om3/core/naming"
"github.com/opensvc/om3/core/object"
"github.com/opensvc/om3/core/objectaction"
"github.com/opensvc/om3/daemon/api"
"github.com/opensvc/om3/util/xsession"
)
type (
CmdObjectStop struct {
OptsGlobal
OptsAsync
OptsLock
OptsResourceSelector
OptTo
Force bool
NodeSelector string
}
)
func (t *CmdObjectStop) Run(selector, kind string) error {
mergedSelector := mergeSelector(selector, t.ObjectSelector, kind, "")
return objectaction.New(
objectaction.WithObjectSelector(mergedSelector),
objectaction.WithRID(t.RID),
objectaction.WithTag(t.Tag),
objectaction.WithSubset(t.Subset),
objectaction.WithLocal(t.Local),
objectaction.WithOutput(t.Output),
objectaction.WithColor(t.Color),
objectaction.WithAsyncTarget("stopped"),
objectaction.WithAsyncTime(t.Time),
objectaction.WithAsyncWait(t.Wait),
objectaction.WithAsyncWatch(t.Watch),
objectaction.WithProgress(!t.Quiet && t.Log == ""),
objectaction.WithRemoteNodes(t.NodeSelector),
objectaction.WithRemoteFunc(func(ctx context.Context, p naming.Path, nodename string) (interface{}, error) {
c, err := client.New(client.WithURL(t.Server))
if err != nil {
return nil, err
}
params := api.PostInstanceActionStopParams{}
if t.Force {
v := true
params.Force = &v
}
if t.OptsResourceSelector.RID != "" {
params.Rid = &t.OptsResourceSelector.RID
}
if t.OptsResourceSelector.Subset != "" {
params.Subset = &t.OptsResourceSelector.Subset
}
if t.OptsResourceSelector.Tag != "" {
params.Tag = &t.OptsResourceSelector.Tag
}
if t.OptTo.To != "" {
params.To = &t.OptTo.To
}
{
sid := xsession.ID
params.RequesterSid = &sid
}
response, err := c.PostInstanceActionStopWithResponse(ctx, nodename, p.Namespace, p.Kind, p.Name, ¶ms)
if err != nil {
return nil, err
}
switch {
case response.JSON200 != nil:
return *response.JSON200, nil
case response.JSON401 != nil:
return nil, fmt.Errorf("%s: node %s: %s", p, nodename, *response.JSON401)
case response.JSON403 != nil:
return nil, fmt.Errorf("%s: node %s: %s", p, nodename, *response.JSON403)
case response.JSON500 != nil:
return nil, fmt.Errorf("%s: node %s: %s", p, nodename, *response.JSON500)
default:
return nil, fmt.Errorf("%s: node %s: unexpected response: %s", p, nodename, response.Status())
}
}),
objectaction.WithLocalFunc(func(ctx context.Context, p naming.Path) (interface{}, error) {
o, err := object.NewActor(p)
if err != nil {
return nil, err
}
ctx = actioncontext.WithLockDisabled(ctx, t.Disable)
ctx = actioncontext.WithLockTimeout(ctx, t.Timeout)
ctx = actioncontext.WithTo(ctx, t.To)
ctx = actioncontext.WithForce(ctx, t.Force)
return nil, o.Stop(ctx)
}),
).Do()
}