forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtctld.go
176 lines (150 loc) · 6.07 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
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
// Package vtctld contains all the code to expose a vtctld server
// based on the provided topo.Server.
package vtctld
import (
"flag"
"net/http"
"os"
"path"
"strings"
log "github.com/golang/glog"
"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")
// webDir2 is a temporary additional dir for a new, in-development UI.
webDir2 = flag.String("web_dir2", "", "directory from which to serve vtctld2 web interface resources")
enableRealtimeStats = flag.Bool("enable_realtime_stats", false, "Required for the Realtime Stats view. If set, vtctld will maintain a streaming RPC to each tablet (in all cells) to gather the realtime health stats.")
)
const (
appPrefix = "/app/"
// appPrefix2 is a temporary additional path for a new, in-development UI.
appPrefix2 = "/app2/"
)
// 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 app2 page.
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, appPrefix2, 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 static files for the vtctld2 web app.
// This is a temporary additional URL for serving the new,
// in-development UI side-by-side with the current one.
http.HandleFunc(appPrefix2, 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"
}
filePath := path.Join(*webDir2, rest)
if _, err := os.Stat(filePath); err != nil {
// The requested file doesn't exist.
if strings.ContainsAny(rest, "/.") {
// This looks like a real file path, so return Not Found.
http.NotFound(w, r)
return
}
// It looks like a virtual route path (for pages within the app).
// For these, we must serve index.html to initialize the app.
filePath = path.Join(*webDir2, "index.html")
}
http.ServeFile(w, r, filePath)
})
var realtimeStats *realtimeStats
if *enableRealtimeStats {
var err error
realtimeStats, err = newRealtimeStats(ts)
if err != nil {
log.Errorf("Failed to instantiate RealtimeStats at startup: %v", err)
}
}
// Serve the REST API for the vtctld web app.
initAPI(context.Background(), ts, actionRepo, realtimeStats)
// Init redirects for explorers
initExplorer(ts)
// Init workflow manager.
initWorkflowManager(ts)
}