// NewVtctlPipe creates a new VtctlPipe based on the given topo server. func NewVtctlPipe(t *testing.T, ts topo.Server) *VtctlPipe { // Register all vtctl commands servenvInitialized.Do(func() { // make sure we use the right protocol flag.Set("vtctl_client_protocol", "grpc") // Enable all query groups flag.Set("enable_queries", "true") servenv.FireRunHooks() }) // Listen on a random port listener, err := net.Listen("tcp", ":0") if err != nil { t.Fatalf("Cannot listen: %v", err) } // Create a gRPC server and listen on the port server := grpc.NewServer() grpcvtctlserver.StartServer(server, ts) go server.Serve(listener) // Create a VtctlClient gRPC client to talk to the fake server client, err := vtctlclient.New(listener.Addr().String(), 30*time.Second) if err != nil { t.Fatalf("Cannot create client: %v", err) } return &VtctlPipe{ listener: listener, client: client, t: t, } }
func main() { defer exit.RecoverAll() defer logutil.Flush() flag.Parse() args := flag.Args() if len(args) == 0 { flag.Usage() exit.Return(1) } action := args[0] startMsg := fmt.Sprintf("USER=%v SUDO_USER=%v %v", os.Getenv("USER"), os.Getenv("SUDO_USER"), strings.Join(os.Args, " ")) if syslogger, err := syslog.New(syslog.LOG_INFO, "vtctl "); err == nil { syslogger.Info(startMsg) } else { log.Warningf("cannot connect to syslog: %v", err) } servenv.FireRunHooks() topoServer := topo.GetServer() defer topo.CloseServers() ctx, cancel := context.WithTimeout(context.Background(), *waitTime) wr := wrangler.New(logutil.NewConsoleLogger(), topoServer, tmclient.NewTabletManagerClient()) installSignalHandlers(cancel) err := vtctl.RunCommand(ctx, wr, args) cancel() switch err { case vtctl.ErrUnknownCommand: flag.Usage() exit.Return(1) case nil: // keep going default: log.Errorf("action failed: %v %v", action, err) exit.Return(255) } }