forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtctld.go
126 lines (103 loc) · 4.34 KB
/
vtctld.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
// Package vtctld contains all the code to expose a vtctld server
// based on the provided topo.Server.
package vtctld
import (
"flag"
"net/http"
"path"
"strings"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/acl"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/wrangler"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
)
var (
webDir = flag.String("web_dir", "", "directory from which to serve vtctld web interface resources")
appPrefix = "/app/"
)
// InitVtctld initializes all the vtctld functionnality.
func InitVtctld(ts topo.Server) {
actionRepo := NewActionRepository(ts)
// keyspace actions
actionRepo.RegisterKeyspaceAction("ValidateKeyspace",
func(ctx context.Context, wr *wrangler.Wrangler, keyspace string, r *http.Request) (string, error) {
return "", wr.ValidateKeyspace(ctx, keyspace, false)
})
actionRepo.RegisterKeyspaceAction("ValidateSchemaKeyspace",
func(ctx context.Context, wr *wrangler.Wrangler, keyspace string, r *http.Request) (string, error) {
return "", wr.ValidateSchemaKeyspace(ctx, keyspace, nil, false)
})
actionRepo.RegisterKeyspaceAction("ValidateVersionKeyspace",
func(ctx context.Context, wr *wrangler.Wrangler, keyspace string, r *http.Request) (string, error) {
return "", wr.ValidateVersionKeyspace(ctx, keyspace)
})
actionRepo.RegisterKeyspaceAction("ValidatePermissionsKeyspace",
func(ctx context.Context, wr *wrangler.Wrangler, keyspace string, r *http.Request) (string, error) {
return "", wr.ValidatePermissionsKeyspace(ctx, keyspace)
})
// shard actions
actionRepo.RegisterShardAction("ValidateShard",
func(ctx context.Context, wr *wrangler.Wrangler, keyspace, shard string, r *http.Request) (string, error) {
return "", wr.ValidateShard(ctx, keyspace, shard, false)
})
actionRepo.RegisterShardAction("ValidateSchemaShard",
func(ctx context.Context, wr *wrangler.Wrangler, keyspace, shard string, r *http.Request) (string, error) {
return "", wr.ValidateSchemaShard(ctx, keyspace, shard, nil, false)
})
actionRepo.RegisterShardAction("ValidateVersionShard",
func(ctx context.Context, wr *wrangler.Wrangler, keyspace, shard string, r *http.Request) (string, error) {
return "", wr.ValidateVersionShard(ctx, keyspace, shard)
})
actionRepo.RegisterShardAction("ValidatePermissionsShard",
func(ctx context.Context, wr *wrangler.Wrangler, keyspace, shard string, r *http.Request) (string, error) {
return "", wr.ValidatePermissionsShard(ctx, keyspace, shard)
})
// tablet actions
actionRepo.RegisterTabletAction("Ping", "",
func(ctx context.Context, wr *wrangler.Wrangler, tabletAlias *topodatapb.TabletAlias, r *http.Request) (string, error) {
ti, err := wr.TopoServer().GetTablet(ctx, tabletAlias)
if err != nil {
return "", err
}
return "", wr.TabletManagerClient().Ping(ctx, ti.Tablet)
})
actionRepo.RegisterTabletAction("RefreshState", acl.ADMIN,
func(ctx context.Context, wr *wrangler.Wrangler, tabletAlias *topodatapb.TabletAlias, r *http.Request) (string, error) {
ti, err := wr.TopoServer().GetTablet(ctx, tabletAlias)
if err != nil {
return "", err
}
return "", wr.TabletManagerClient().RefreshState(ctx, ti.Tablet)
})
actionRepo.RegisterTabletAction("DeleteTablet", acl.ADMIN,
func(ctx context.Context, wr *wrangler.Wrangler, tabletAlias *topodatapb.TabletAlias, r *http.Request) (string, error) {
return "", wr.DeleteTablet(ctx, tabletAlias, false)
})
actionRepo.RegisterTabletAction("ReloadSchema", acl.ADMIN,
func(ctx context.Context, wr *wrangler.Wrangler, tabletAlias *topodatapb.TabletAlias, r *http.Request) (string, error) {
return "", wr.ReloadSchema(ctx, tabletAlias)
})
// Anything unrecognized gets redirected to the main app page.
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, appPrefix, http.StatusFound)
})
// Serve the static files for the vtctld web app.
http.HandleFunc(appPrefix, func(w http.ResponseWriter, r *http.Request) {
// Strip the prefix.
parts := strings.SplitN(r.URL.Path, "/", 3)
if len(parts) != 3 {
http.NotFound(w, r)
return
}
rest := parts[2]
if rest == "" {
rest = "index.html"
}
http.ServeFile(w, r, path.Join(*webDir, rest))
})
// Serve the REST API for the vtctld web app.
initAPI(context.Background(), ts, actionRepo)
// Init redirects for explorers
initExplorer(ts)
}