-
Notifications
You must be signed in to change notification settings - Fork 6
/
core_action.go
474 lines (447 loc) · 13.1 KB
/
core_action.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package object
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"slices"
"sync"
"time"
"github.com/opensvc/om3/core/actioncontext"
"github.com/opensvc/om3/core/actionrollback"
"github.com/opensvc/om3/core/client"
"github.com/opensvc/om3/core/env"
"github.com/opensvc/om3/core/naming"
"github.com/opensvc/om3/core/rawconfig"
"github.com/opensvc/om3/core/resource"
"github.com/opensvc/om3/core/resourceselector"
"github.com/opensvc/om3/core/resourceset"
"github.com/opensvc/om3/core/status"
"github.com/opensvc/om3/core/statusbus"
"github.com/opensvc/om3/daemon/api"
"github.com/opensvc/om3/util/hostname"
"github.com/opensvc/om3/util/key"
"github.com/opensvc/om3/util/pg"
"github.com/opensvc/om3/util/xsession"
)
// Resources implementing setters
type (
confirmer interface {
SetConfirm(v bool)
}
forcer interface {
SetForce(v bool)
}
)
var (
ErrInvalidNode = errors.New("invalid node")
ErrDisabled = errors.New("object instance is disabled")
)
func (t *actor) validateAction() error {
node := rawconfig.GetNodeSection()
if t.Env() != "PRD" && node.Env == "PRD" {
return fmt.Errorf("%w: not allowed to run on this node (svc env=%s node env=%s)", ErrInvalidNode, t.Env(), node.Env)
}
if v, err := t.config.IsInNodes(hostname.Hostname()); err != nil {
return err
} else if v {
return nil
}
if v, err := t.config.IsInDRPNodes(hostname.Hostname()); err != nil {
return err
} else if v {
return nil
}
return fmt.Errorf("%w: the hostname '%s' is not a member of DEFAULT.nodes, DEFAULT.drpnode nor DEFAULT.drpnodes", ErrInvalidNode, hostname.Hostname())
}
func (t *actor) setenv(action string, leader bool) {
os.Setenv("OPENSVC_SVCPATH", t.path.String())
os.Setenv("OPENSVC_SVCNAME", t.path.Name)
os.Setenv("OPENSVC_NAMESPACE", t.path.Namespace)
os.Setenv("OPENSVC_ACTION", action)
if leader {
os.Setenv("OPENSVC_LEADER", "1")
} else {
os.Setenv("OPENSVC_LEADER", "0")
}
// each Setenv resource Driver will load its own env vars when actioned
}
func (t *actor) preAction(ctx context.Context) error {
if err := t.mayFreeze(ctx); err != nil {
return err
}
return nil
}
func (t *actor) needRollback(ctx context.Context) bool {
if actionrollback.Len(ctx) == 0 {
t.Log().Debugf("skip rollback: Empty stack")
return false
}
action := actioncontext.Props(ctx)
if !action.Rollback {
t.Log().Debugf("skip rollback: Not demanded by the %s action", action.Name)
return false
}
if actioncontext.IsRollbackDisabled(ctx) {
t.Log().Debugf("skip rollback: Disabled via the command flag")
return false
}
k := key.Parse("rollback")
if !t.Config().GetBool(k) {
t.Log().Debugf("skip rollback: Disabled via configuration keyword")
return false
}
return true
}
func (t *actor) rollback(ctx context.Context) error {
t.Log().Infof("rollback")
return actionrollback.Rollback(ctx)
}
func (t *actor) withTimeout(ctx context.Context) (context.Context, func()) {
props := actioncontext.Props(ctx)
timeout, source := t.actionTimeout(props.TimeoutKeywords)
t.log.Debugf("action timeout set to %s from keyword %s", timeout, source)
if timeout == 0 {
return ctx, func() {}
}
return context.WithTimeout(ctx, timeout)
}
func (t *actor) actionTimeout(kwNames []string) (time.Duration, string) {
for _, kwName := range kwNames {
k := key.Parse(kwName)
timeout := t.Config().GetDuration(k)
if timeout != nil {
return *timeout, kwName
}
}
return 0, ""
}
func (t *actor) abortWorker(ctx context.Context, r resource.Driver, q chan bool, wg *sync.WaitGroup) {
defer wg.Done()
a, ok := r.(resource.Aborter)
if !ok {
q <- false
return
}
r.Progress(ctx, "▶ run abort tests")
if a.Abort(ctx) {
t.log.Attr("rid", r.RID()).Errorf("resource %s denied start", r.RID())
r.Progress(ctx, rawconfig.Colorize.Error("deny start"))
q <- true
return
}
r.Progress(ctx, rawconfig.Colorize.Optimal("✓")+" allow start")
q <- false
}
// announceProgress signals the daemon that an action is in progress, using the
// POST /object/progress. This handler manages local expect:
// * set to "started" via InstanceMonitorUpdated event handler
// * set to "" if progress is idle
func (t *actor) announceProgress(ctx context.Context, progress string) error {
if env.HasDaemonMonitorOrigin() {
// no need to announce if the daemon started this action
return nil
}
c, err := client.New()
if err != nil {
return err
}
isPartial := !resourceselector.FromContext(ctx, nil).IsZero()
p := t.Path()
resp, err := c.PostInstanceProgress(ctx, p.Namespace, p.Kind, p.Name, api.PostInstanceProgress{
State: progress,
SessionID: xsession.ID,
IsPartial: &isPartial,
})
switch {
case errors.Is(err, os.ErrNotExist):
t.log.Debugf("skip announce progress: The daemon is not running")
return nil
case err != nil:
t.log.Errorf("announce %s state: %s", progress, err)
return err
case resp.StatusCode != http.StatusOK:
err := fmt.Errorf("unexpected post object progress status %s", resp.Status)
t.log.Errorf("announce %s state: %s", progress, err)
return err
}
t.Log().Debugf("announce %s state", progress)
return nil
}
func (t *actor) abortStart(ctx context.Context, l resourceLister) (err error) {
if actioncontext.Props(ctx).Name != "start" {
return nil
}
if err := t.abortStartAffinity(ctx); err != nil {
return err
}
return t.abortStartDrivers(ctx, l)
}
func (t *actor) abortStartAffinity(ctx context.Context) (err error) {
if env.HasDaemonMonitorOrigin() {
return nil
}
if actioncontext.IsForce(ctx) {
return nil
}
for _, pStr := range t.HardAffinity() {
p, err := naming.ParsePath(pStr)
if err != nil {
return fmt.Errorf("hard affinity object %s parse path: %w", p, err)
}
obj, err := NewCore(p, WithVolatile(true))
if err != nil {
return fmt.Errorf("hard affinity object %s init: %w", p, err)
}
instanceStatus, err := obj.Status(ctx)
if err != nil {
return fmt.Errorf("hard affinity object %s status: %w", p, err)
}
switch instanceStatus.Avail {
case status.Up:
case status.NotApplicable:
default:
return fmt.Errorf("hard affinity with %s is not satisfied (currently %s). use --force if you really want to start", p, instanceStatus.Avail)
}
}
for _, pStr := range t.HardAntiAffinity() {
p, err := naming.ParsePath(pStr)
if err != nil {
return fmt.Errorf("hard anti affinity object %s parse path: %w", p, err)
}
obj, err := NewCore(p, WithVolatile(true))
if err != nil {
return fmt.Errorf("hard anti affinity object %s init: %w", p, err)
}
instanceStatus, err := obj.Status(ctx)
if err != nil {
return fmt.Errorf("hard anti affinity object %s status: %w", p, err)
}
switch instanceStatus.Avail {
case status.Down:
case status.StandbyUp:
case status.StandbyDown:
case status.NotApplicable:
default:
return fmt.Errorf("hard anti affinity with %s is not satisfied (currently %s). use --force if you really want to start", p, instanceStatus.Avail)
}
}
return nil
}
func (t *actor) abortStartDrivers(ctx context.Context, l resourceLister) (err error) {
sb := statusbus.FromContext(ctx)
resources := l.Resources()
added := 0
q := make(chan bool, len(resources))
var wg sync.WaitGroup
for _, r := range resources {
currentState := sb.Get(r.RID())
if currentState.Is(status.Up, status.StandbyUp) {
continue
}
if r.IsDisabled() {
continue
}
wg.Add(1)
added = added + 1
go t.abortWorker(ctx, r, q, &wg)
}
wg.Wait()
var ret bool
for i := 0; i < added; i = i + 1 {
ret = ret || <-q
}
if ret {
return fmt.Errorf("abort start")
}
return nil
}
func (t *actor) action(ctx context.Context, fn resourceset.DoFunc) error {
if t.IsDisabled() {
return ErrDisabled
}
wd, _ := os.Getwd()
action := actioncontext.Props(ctx)
logger := t.log.
Attr("argv", os.Args).
Attr("cwd", wd).
Attr("action", action.Name).
Attr("origin", env.Origin())
logger.Infof("do %s (origin %s)", os.Args, env.Origin())
beginTime := time.Now()
defer func() {
logger.Attr("duration", time.Now().Sub(beginTime)).Infof("done %s in %s", os.Args, time.Now().Sub(beginTime))
}()
// daemon instance monitor updates
progress := actioncontext.Props(ctx).Progress
t.announceProgress(ctx, progress)
defer t.announceProgress(ctx, "idle") // TODO: failed cases ?
if mgr := pg.FromContext(ctx); mgr != nil {
mgr.Register(t.pg)
}
ctx, cancel := t.withTimeout(ctx)
defer cancel()
if err := t.preAction(ctx); err != nil {
return err
}
ctx, stop := statusbus.WithContext(ctx, t.path)
defer stop()
l := resourceselector.FromContext(ctx, t)
b := actioncontext.To(ctx)
progressWrap := func(fn resourceset.DoFunc) resourceset.DoFunc {
return func(ctx context.Context, r resource.Driver) error {
l := t.log.Attr("rid", r.RID())
ctx = l.WithContext(ctx)
err := fn(ctx, r)
switch {
case errors.Is(err, resource.ErrDisabled):
err = nil
case errors.Is(err, resource.ErrActionNotSupported):
err = nil
case errors.Is(err, resource.ErrActionPostponedToLinker):
err = nil
case err == nil:
r.Progress(ctx, rawconfig.Colorize.Optimal("✓"))
case r.IsOptional():
r.Progress(ctx, rawconfig.Colorize.Warning(err))
default:
r.Progress(ctx, rawconfig.Colorize.Error(err))
}
return err
}
}
linkWrap := func(fn resourceset.DoFunc) resourceset.DoFunc {
return func(ctx context.Context, r resource.Driver) error {
if linkToer, ok := r.(resource.LinkToer); ok {
if name := linkToer.LinkTo(); name != "" && l.Resources().HasRID(name) {
// will be handled by the targeted LinkNameser resource
return resource.ErrActionPostponedToLinker
}
}
if linkNameser, ok := r.(resource.LinkNameser); !ok {
// normal action for a non-linkable resource
return fn(ctx, r)
} else {
// Here, we handle a resource other resources can link to.
names := linkNameser.LinkNames()
rids := l.Resources().LinkersRID(names)
filter := func(fn resourceset.DoFunc) resourceset.DoFunc {
// filter applies the action only on linkers
return func(ctx context.Context, r resource.Driver) error {
if !slices.Contains(rids, r.RID()) {
return nil
}
return fn(ctx, r)
}
}
// On descending action, do action on linkers first.
if l.IsDesc() {
if err := t.ResourceSets().Do(ctx, l, b, "linked-"+action.Name, progressWrap(filter(fn))); err != nil {
return err
}
}
if err := fn(ctx, r); err != nil {
return err
}
// On ascending action, do action on linkers last.
if !l.IsDesc() {
if err := t.ResourceSets().Do(ctx, l, b, "linked-"+action.Name, progressWrap(filter(fn))); err != nil {
return err
}
}
return nil
}
}
}
// Pre action resource evaluation.
// For action requirements like fs#1(up)
sb := statusbus.FromContext(ctx)
evaluated := make(map[string]bool)
t.ResourceSets().Do(ctx, l, b, "pre-"+action.Name+" status", func(ctx context.Context, r resource.Driver) error {
for requiredRID := range r.Requires(action.Name).Requirements() {
if _, ok := evaluated[requiredRID]; ok {
continue
}
requiredResource := t.getConfiguredResourceByID(requiredRID)
if requiredResource == nil {
continue
}
sb.Post(requiredRID, resource.EvalStatus(ctx, requiredResource), false)
evaluated[requiredRID] = true
}
rid := r.RID()
sb.Post(rid, resource.EvalStatus(ctx, r), false)
evaluated[rid] = true
return nil
})
if err := t.abortStart(ctx, l); err != nil {
_, _ = t.statusEval(ctx)
return err
}
if err := t.ResourceSets().Do(ctx, l, b, action.Name, progressWrap(linkWrap(fn))); err != nil {
if t.needRollback(ctx) {
if errRollback := t.rollback(ctx); errRollback != nil {
t.Log().Errorf("rollback: %s", err)
}
}
return err
}
if action.Order.IsDesc() {
t.CleanPG(ctx)
}
return t.postStartStopStatusEval(ctx)
}
func (t *actor) postStartStopStatusEval(ctx context.Context) error {
action := actioncontext.Props(ctx)
instStatus, err := t.statusEval(ctx)
if err != nil {
return err
}
if !resourceselector.FromContext(ctx, nil).IsZero() {
// don't verify instance avail if a resource selection was requested
return nil
}
switch action.Name {
case "stop":
switch instStatus.Avail {
case status.Down, status.StandbyUp, status.StandbyUpWithDown, status.NotApplicable, status.StandbyDown:
default:
return fmt.Errorf("the stop action returned no error but end avail status is %s", instStatus.Avail)
}
case "start":
switch instStatus.Avail {
case status.Up, status.NotApplicable, status.StandbyUpWithUp:
default:
return fmt.Errorf("the start action returned no error but end avail status is %s", instStatus.Avail)
}
}
return nil
}
func (t *actor) mayFreeze(ctx context.Context) error {
action := actioncontext.Props(ctx)
if !action.Freeze {
return nil
}
if !resourceselector.FromContext(ctx, nil).IsZero() {
t.log.Debugf("skip freeze: Resource selection")
return nil
}
if !t.orchestrateWantsFreeze() {
t.log.Debugf("skip freeze: Orchestrate value")
return nil
}
if env.HasDaemonMonitorOrigin() {
t.log.Debugf("skip freeze: Action has daemon origin")
return nil
}
return t.Freeze(ctx)
}
func (t *actor) orchestrateWantsFreeze() bool {
switch t.Orchestrate() {
case "ha", "start":
return true
default:
return false
}
}