Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

miscellaneous cobras #14069

Merged
merged 11 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package cli

// This plugin imports consultopo to register the consul implementation of TopoServer.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package cli

// This plugin imports etcd2topo to register the etcd2 implementation of TopoServer.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package cli

import (
// Imports and register the zk2 TopologyServer
Expand Down
158 changes: 158 additions & 0 deletions go/cmd/topo2topo/cli/topo2topo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
Copyright 2023 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cli

import (
"context"
"fmt"

"github.com/spf13/cobra"

"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/vt/grpccommon"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/helpers"
)

var (
fromImplementation string
fromServerAddress string
fromRoot string
toImplementation string
toServerAddress string
toRoot string
compare bool
doKeyspaces bool
doShards bool
doShardReplications bool
doTablets bool
doRoutingRules bool

Main = &cobra.Command{
Use: "topo2topo",
Short: "topo2topo copies Vitess topology data from one topo server to another.",
Long: `topo2topo copies Vitess topology data from one topo server to another.
It can also be used to compare data between two topologies.`,
Args: cobra.NoArgs,
PreRunE: servenv.CobraPreRunE,
RunE: run,
}
)

func init() {
servenv.MoveFlagsToCobraCommand(Main)

Main.Flags().StringVar(&fromImplementation, "from_implementation", fromImplementation, "topology implementation to copy data from")
Main.Flags().StringVar(&fromServerAddress, "from_server", fromServerAddress, "topology server address to copy data from")
Main.Flags().StringVar(&fromRoot, "from_root", fromRoot, "topology server root to copy data from")
Main.Flags().StringVar(&toImplementation, "to_implementation", toImplementation, "topology implementation to copy data to")
Main.Flags().StringVar(&toServerAddress, "to_server", toServerAddress, "topology server address to copy data to")
Main.Flags().StringVar(&toRoot, "to_root", toRoot, "topology server root to copy data to")
Main.Flags().BoolVar(&compare, "compare", compare, "compares data between topologies")
Main.Flags().BoolVar(&doKeyspaces, "do-keyspaces", doKeyspaces, "copies the keyspace information")
Main.Flags().BoolVar(&doShards, "do-shards", doShards, "copies the shard information")
Main.Flags().BoolVar(&doShardReplications, "do-shard-replications", doShardReplications, "copies the shard replication information")
Main.Flags().BoolVar(&doTablets, "do-tablets", doTablets, "copies the tablet information")
Main.Flags().BoolVar(&doRoutingRules, "do-routing-rules", doRoutingRules, "copies the routing rules")

acl.RegisterFlags(Main.Flags())
grpccommon.RegisterFlags(Main.Flags())
}

func run(cmd *cobra.Command, args []string) error {
defer logutil.Flush()
servenv.Init()

fromTS, err := topo.OpenServer(fromImplementation, fromServerAddress, fromRoot)
if err != nil {
return fmt.Errorf("Cannot open 'from' topo %v: %w", fromImplementation, err)
}
toTS, err := topo.OpenServer(toImplementation, toServerAddress, toRoot)
if err != nil {
return fmt.Errorf("Cannot open 'to' topo %v: %w", toImplementation, err)
}

ctx := context.Background()

if compare {
return compareTopos(ctx, fromTS, toTS)
}

return copyTopos(ctx, fromTS, toTS)
}

func copyTopos(ctx context.Context, fromTS, toTS *topo.Server) error {
if doKeyspaces {
if err := helpers.CopyKeyspaces(ctx, fromTS, toTS); err != nil {
return err
}
}
if doShards {
if err := helpers.CopyShards(ctx, fromTS, toTS); err != nil {
return err
}
}
if doShardReplications {
if err := helpers.CopyShardReplications(ctx, fromTS, toTS); err != nil {
return err
}
}
if doTablets {
if err := helpers.CopyTablets(ctx, fromTS, toTS); err != nil {
return err
}
}
if doRoutingRules {
if err := helpers.CopyRoutingRules(ctx, fromTS, toTS); err != nil {
return err
}
}

return nil
}

func compareTopos(ctx context.Context, fromTS, toTS *topo.Server) (err error) {
if doKeyspaces {
err = helpers.CompareKeyspaces(ctx, fromTS, toTS)
if err != nil {
return fmt.Errorf("Compare keyspaces failed: %w", err)
}
}
if doShards {
err = helpers.CompareShards(ctx, fromTS, toTS)
if err != nil {
return fmt.Errorf("Compare shards failed: %w", err)
}
}
if doShardReplications {
err = helpers.CompareShardReplications(ctx, fromTS, toTS)
if err != nil {
return fmt.Errorf("Compare shard replications failed: %w", err)
}
}
if doTablets {
err = helpers.CompareTablets(ctx, fromTS, toTS)
if err != nil {
return fmt.Errorf("Compare tablets failed: %w", err)
}
}

fmt.Println("Topologies are in sync")
return nil
}
37 changes: 37 additions & 0 deletions go/cmd/topo2topo/docgen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2023 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the reason for calling this as main package and not docgen package?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


import (
"github.com/spf13/cobra"

"vitess.io/vitess/go/cmd/internal/docgen"
"vitess.io/vitess/go/cmd/topo2topo/cli"
)

func main() {
var dir string
cmd := cobra.Command{
Use: "docgen [-d <dir>]",
RunE: func(cmd *cobra.Command, args []string) error {
return docgen.GenerateMarkdownTree(cli.Main, dir)
},
}

cmd.Flags().StringVarP(&dir, "dir", "d", "doc", "output directory to write documentation")
_ = cmd.Execute()
}
123 changes: 3 additions & 120 deletions go/cmd/topo2topo/topo2topo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,132 +17,15 @@ limitations under the License.
package main

import (
"context"
"fmt"
"os"

"github.com/spf13/pflag"

"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/cmd/topo2topo/cli"
"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/vt/grpccommon"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/helpers"
)

var (
fromImplementation string
fromServerAddress string
fromRoot string
toImplementation string
toServerAddress string
toRoot string
compare bool
doKeyspaces bool
doShards bool
doShardReplications bool
doTablets bool
doRoutingRules bool
)

func init() {
servenv.OnParse(func(fs *pflag.FlagSet) {
fs.StringVar(&fromImplementation, "from_implementation", fromImplementation, "topology implementation to copy data from")
fs.StringVar(&fromServerAddress, "from_server", fromServerAddress, "topology server address to copy data from")
fs.StringVar(&fromRoot, "from_root", fromRoot, "topology server root to copy data from")
fs.StringVar(&toImplementation, "to_implementation", toImplementation, "topology implementation to copy data to")
fs.StringVar(&toServerAddress, "to_server", toServerAddress, "topology server address to copy data to")
fs.StringVar(&toRoot, "to_root", toRoot, "topology server root to copy data to")
fs.BoolVar(&compare, "compare", compare, "compares data between topologies")
fs.BoolVar(&doKeyspaces, "do-keyspaces", doKeyspaces, "copies the keyspace information")
fs.BoolVar(&doShards, "do-shards", doShards, "copies the shard information")
fs.BoolVar(&doShardReplications, "do-shard-replications", doShardReplications, "copies the shard replication information")
fs.BoolVar(&doTablets, "do-tablets", doTablets, "copies the tablet information")
fs.BoolVar(&doRoutingRules, "do-routing-rules", doRoutingRules, "copies the routing rules")

acl.RegisterFlags(fs)
})
}

func main() {
defer exit.RecoverAll()
defer logutil.Flush()

fs := pflag.NewFlagSet("topo2topo", pflag.ExitOnError)
grpccommon.RegisterFlags(fs)
log.RegisterFlags(fs)
logutil.RegisterFlags(fs)

servenv.ParseFlags("topo2topo")
servenv.Init()

fromTS, err := topo.OpenServer(fromImplementation, fromServerAddress, fromRoot)
if err != nil {
log.Exitf("Cannot open 'from' topo %v: %v", fromImplementation, err)
}
toTS, err := topo.OpenServer(toImplementation, toServerAddress, toRoot)
if err != nil {
log.Exitf("Cannot open 'to' topo %v: %v", toImplementation, err)
}

ctx := context.Background()

if compare {
compareTopos(ctx, fromTS, toTS)
return
}
copyTopos(ctx, fromTS, toTS)
}

func copyTopos(ctx context.Context, fromTS, toTS *topo.Server) {
if doKeyspaces {
helpers.CopyKeyspaces(ctx, fromTS, toTS)
}
if doShards {
helpers.CopyShards(ctx, fromTS, toTS)
}
if doShardReplications {
helpers.CopyShardReplications(ctx, fromTS, toTS)
}
if doTablets {
helpers.CopyTablets(ctx, fromTS, toTS)
}
if doRoutingRules {
helpers.CopyRoutingRules(ctx, fromTS, toTS)
}
}

func compareTopos(ctx context.Context, fromTS, toTS *topo.Server) {
var err error
if doKeyspaces {
err = helpers.CompareKeyspaces(ctx, fromTS, toTS)
if err != nil {
log.Exitf("Compare keyspaces failed: %v", err)
}
}
if doShards {
err = helpers.CompareShards(ctx, fromTS, toTS)
if err != nil {
log.Exitf("Compare shards failed: %v", err)
}
}
if doShardReplications {
err = helpers.CompareShardReplications(ctx, fromTS, toTS)
if err != nil {
log.Exitf("Compare shard replications failed: %v", err)
}
}
if doTablets {
err = helpers.CompareTablets(ctx, fromTS, toTS)
if err != nil {
log.Exitf("Compare tablets failed: %v", err)
}
}
if err == nil {
fmt.Println("Topologies are in sync")
os.Exit(0)
if err := cli.Main.Execute(); err != nil {
log.Exitf("%s", err)
}
}
Loading
Loading