Skip to content

Commit

Permalink
migrate zkctl binary to cobra
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Mason <[email protected]>
  • Loading branch information
Andrew Mason committed Sep 25, 2023
1 parent 886f19d commit 26dff0a
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 61 deletions.
32 changes: 32 additions & 0 deletions go/cmd/zkctl/command/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
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 command

import "github.com/spf13/cobra"

var Init = &cobra.Command{
Use: "init",
Short: "Generates a new config and then starts zookeeper.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return zkd.Init()
},
}

func init() {
Root.AddCommand(Init)
}
63 changes: 63 additions & 0 deletions go/cmd/zkctl/command/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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 command

import (
"github.com/spf13/cobra"

"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/zkctl"
)

var (
zkCfg = "6@<hostname>:3801:3802:3803"
myID uint
zkExtra []string

zkd *zkctl.Zkd

Root = &cobra.Command{
Use: "zkctl",
Short: "Initializes and controls zookeeper with Vitess-specific configuration.",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := servenv.CobraPreRunE(cmd, args); err != nil {
return err
}

zkConfig := zkctl.MakeZkConfigFromString(zkCfg, uint32(myID))
zkConfig.Extra = zkExtra
zkd = zkctl.NewZkd(zkConfig)

return nil
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
logutil.Flush()
},
}
)

func init() {
Root.PersistentFlags().StringVar(&zkCfg, "zk.cfg", zkCfg,
"zkid@server1:leaderPort1:electionPort1:clientPort1,...)")
Root.PersistentFlags().UintVar(&myID, "zk.myid", myID,
"which server do you want to be? only needed when running multiple instance on one box, otherwise myid is implied by hostname")
Root.PersistentFlags().StringArrayVar(&zkExtra, "zk.extra", zkExtra,
"extra config line(s) to append verbatim to config (flag can be specified more than once)")

servenv.MovePersistentFlagsToCobraCommand(Root)
}
32 changes: 32 additions & 0 deletions go/cmd/zkctl/command/shutdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
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 command

import "github.com/spf13/cobra"

var Shutdown = &cobra.Command{
Use: "shutdown",
Short: "Terminates a zookeeper server but keeps its data dir intact.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return zkd.Shutdown()
},
}

func init() {
Root.AddCommand(Shutdown)
}
32 changes: 32 additions & 0 deletions go/cmd/zkctl/command/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
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 command

import "github.com/spf13/cobra"

var Start = &cobra.Command{
Use: "start",
Short: "Runs an already initialized zookeeper server.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return zkd.Start()
},
}

func init() {
Root.AddCommand(Start)
}
32 changes: 32 additions & 0 deletions go/cmd/zkctl/command/teardown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
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 command

import "github.com/spf13/cobra"

var Teardown = &cobra.Command{
Use: "teardown",
Short: "Shuts down the zookeeper server and removes its data dir.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return zkd.Teardown()
},
}

func init() {
Root.AddCommand(Teardown)
}
37 changes: 37 additions & 0 deletions go/cmd/zkctl/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

import (
"github.com/spf13/cobra"

"vitess.io/vitess/go/cmd/internal/docgen"
"vitess.io/vitess/go/cmd/zkctl/command"
)

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

cmd.Flags().StringVarP(&dir, "dir", "d", "doc", "output directory to write documentation")
_ = cmd.Execute()
}
62 changes: 3 additions & 59 deletions go/cmd/zkctl/zkctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,75 +14,19 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// zkctl initializes and controls ZooKeeper with Vitess-specific configuration.
package main

import (
"github.com/spf13/pflag"

"vitess.io/vitess/go/cmd/zkctl/command"
"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/zkctl"
)

var usage = `
Commands:
init | start | shutdown | teardown
`

var (
zkCfg = "6@<hostname>:3801:3802:3803"
myID uint
zkExtra []string
)

func registerZkctlFlags(fs *pflag.FlagSet) {
fs.StringVar(&zkCfg, "zk.cfg", zkCfg,
"zkid@server1:leaderPort1:electionPort1:clientPort1,...)")
fs.UintVar(&myID, "zk.myid", myID,
"which server do you want to be? only needed when running multiple instance on one box, otherwise myid is implied by hostname")
fs.StringArrayVar(&zkExtra, "zk.extra", zkExtra,
"extra config line(s) to append verbatim to config (flag can be specified more than once)")
}

func init() {
servenv.OnParse(registerZkctlFlags)
}

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

fs := pflag.NewFlagSet("zkctl", pflag.ExitOnError)
log.RegisterFlags(fs)
logutil.RegisterFlags(fs)
args := servenv.ParseFlagsWithArgs("zkctl")

zkConfig := zkctl.MakeZkConfigFromString(zkCfg, uint32(myID))
zkConfig.Extra = zkExtra
zkd := zkctl.NewZkd(zkConfig)

action := args[0]
var err error
switch action {
case "init":
err = zkd.Init()
case "shutdown":
err = zkd.Shutdown()
case "start":
err = zkd.Start()
case "teardown":
err = zkd.Teardown()
default:
log.Errorf("invalid action: %v", action)
log.Errorf(usage)
exit.Return(1)
}
if err != nil {
log.Errorf("failed %v: %v", action, err)
if err := command.Root.Execute(); err != nil {
log.Error(err)
exit.Return(1)
}
}
19 changes: 17 additions & 2 deletions go/flags/endtoend/zkctl.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
Usage of zkctl:
Initializes and controls zookeeper with Vitess-specific configuration.

Usage:
zkctl [command]

Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
init Generates a new config and then starts zookeeper.
shutdown Terminates a zookeeper server but keeps its data dir intact.
start Runs an already initialized zookeeper server.
teardown Shuts down the zookeeper server and removes its data dir.

Flags:
--alsologtostderr log to standard error as well as files
--config-file string Full path of the config file (with extension) to use. If set, --config-path, --config-type, and --config-name are ignored.
--config-file-not-found-handling ConfigFileNotFoundHandling Behavior when a config file is not found. (Options: error, exit, ignore, warn) (default warn)
--config-name string Name of the config file (without extension) to search for. (default "vtconfig")
--config-path strings Paths to search for config files in. (default [{{ .Workdir }}])
--config-persistence-min-interval duration minimum interval between persisting dynamic config changes back to disk (if no change has occurred, nothing is done). (default 1s)
--config-type string Config file type (omit to infer config type from file extension).
-h, --help display usage and exit
-h, --help help for zkctl
--keep_logs duration keep logs for this long (using ctime) (zero to keep forever)
--keep_logs_by_mtime duration keep logs for this long (using mtime) (zero to keep forever)
--log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0)
Expand All @@ -23,3 +36,5 @@ Usage of zkctl:
--zk.cfg string zkid@server1:leaderPort1:electionPort1:clientPort1,...) (default "6@<hostname>:3801:3802:3803")
--zk.extra stringArray extra config line(s) to append verbatim to config (flag can be specified more than once)
--zk.myid uint which server do you want to be? only needed when running multiple instance on one box, otherwise myid is implied by hostname

Use "zkctl [command] --help" for more information about a command.

0 comments on commit 26dff0a

Please sign in to comment.