forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook.go
55 lines (45 loc) · 1.67 KB
/
hook.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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wrangler
import (
"fmt"
"strings"
log "github.com/golang/glog"
hk "github.com/youtube/vitess/go/vt/hook"
"github.com/youtube/vitess/go/vt/topo"
"golang.org/x/net/context"
pb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// ExecuteHook will run the hook on the tablet
func (wr *Wrangler) ExecuteHook(ctx context.Context, tabletAlias *pb.TabletAlias, hook *hk.Hook) (hookResult *hk.HookResult, err error) {
if strings.Contains(hook.Name, "/") {
return nil, fmt.Errorf("hook name cannot have a '/' in it")
}
ti, err := wr.ts.GetTablet(ctx, tabletAlias)
if err != nil {
return nil, err
}
return wr.ExecuteTabletInfoHook(ctx, ti, hook)
}
// ExecuteTabletInfoHook will run the hook on the tablet described by
// TabletInfo
func (wr *Wrangler) ExecuteTabletInfoHook(ctx context.Context, ti *topo.TabletInfo, hook *hk.Hook) (hookResult *hk.HookResult, err error) {
return wr.tmc.ExecuteHook(ctx, ti, hook)
}
// ExecuteOptionalTabletInfoHook executes a hook and returns an error
// only if the hook failed, not if the hook doesn't exist.
func (wr *Wrangler) ExecuteOptionalTabletInfoHook(ctx context.Context, ti *topo.TabletInfo, hook *hk.Hook) (err error) {
hr, err := wr.ExecuteTabletInfoHook(ctx, ti, hook)
if err != nil {
return err
}
if hr.ExitStatus == hk.HOOK_DOES_NOT_EXIST {
log.Infof("Hook %v doesn't exist on tablet %v", hook.Name, ti.AliasString())
return nil
}
if hr.ExitStatus != hk.HOOK_SUCCESS {
return fmt.Errorf("Hook %v failed(%v): %v", hook.Name, hr.ExitStatus, hr.Stderr)
}
return nil
}