Skip to content

Commit

Permalink
migrate zkctld 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 26dff0a commit 5d802ee
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 92 deletions.
100 changes: 100 additions & 0 deletions go/cmd/zkctld/cli/zkctld.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
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 (
"fmt"
"os"
"os/signal"
"syscall"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"vitess.io/vitess/go/acl"
"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 (
zkCfg = "6@<hostname>:3801:3802:3803"
myID uint
zkExtra []string

Main = &cobra.Command{
Use: "zkctld",
Short: "zkctld is a daemon that starts or initializes ZooKeeper with Vitess-specific configuration. It will stay running as long as the underlying ZooKeeper server, and will pass along SIGTERM.",
Args: cobra.NoArgs,
PersistentPreRunE: servenv.CobraPreRunE,
PostRun: func(cmd *cobra.Command, args []string) {
logutil.Flush()
},
RunE: run,
}
)

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

func registerFlags(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)")
acl.RegisterFlags(fs)
}

func run(cmd *cobra.Command, args []string) error {
servenv.Init()
zkConfig := zkctl.MakeZkConfigFromString(zkCfg, uint32(myID))
zkConfig.Extra = zkExtra
zkd := zkctl.NewZkd(zkConfig)

if zkd.Inited() {
log.Infof("already initialized, starting without init...")
if err := zkd.Start(); err != nil {
return fmt.Errorf("failed start: %v", err)
}
} else {
log.Infof("initializing...")
if err := zkd.Init(); err != nil {
return fmt.Errorf("failed init: %v", err)
}
}

log.Infof("waiting for signal or server shutdown...")
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
select {
case <-zkd.Done():
log.Infof("server shut down on its own")
case <-sig:
log.Infof("signal received, shutting down server")

// Action to perform if there is an error
if err := zkd.Shutdown(); err != nil {
return fmt.Errorf("error during shutdown:%v", err)
}
}

return nil
}
37 changes: 37 additions & 0 deletions go/cmd/zkctld/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/vttablet/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()
}
70 changes: 4 additions & 66 deletions go/cmd/zkctld/zkctld.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,77 +20,15 @@ limitations under the License.
package main

import (
"os"
"os/signal"
"syscall"

"github.com/spf13/pflag"

"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/cmd/zkctld/cli"
"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 (
zkCfg = "6@<hostname>:3801:3802:3803"
myID uint
zkExtra []string
)

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

func registerFlags(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)")
acl.RegisterFlags(fs)
}

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

servenv.ParseFlags("zkctld")
servenv.Init()
zkConfig := zkctl.MakeZkConfigFromString(zkCfg, uint32(myID))
zkConfig.Extra = zkExtra
zkd := zkctl.NewZkd(zkConfig)

if zkd.Inited() {
log.Infof("already initialized, starting without init...")
if err := zkd.Start(); err != nil {
log.Errorf("failed start: %v", err)
exit.Return(255)
}
} else {
log.Infof("initializing...")
if err := zkd.Init(); err != nil {
log.Errorf("failed init: %v", err)
exit.Return(255)
}
}

log.Infof("waiting for signal or server shutdown...")
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
select {
case <-zkd.Done():
log.Infof("server shut down on its own")
case <-sig:
log.Infof("signal received, shutting down server")

// Action to perform if there is an error
if err := zkd.Shutdown(); err != nil {
log.Errorf("error during shutdown:%v", err)
exit.Return(1)
}
if err := cli.Main.Execute(); err != nil {
log.Error(err)
exit.Return(1)
}
}
33 changes: 7 additions & 26 deletions go/flags/endtoend/zkctld.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
Usage of zkctld:
--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
--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)
--log_dir string If non-empty, write log files in this directory
--log_err_stacks log stack traces for errors
--log_rotate_max_size uint size in bytes at which logs are rotated (glog.MaxSize) (default 1887436800)
--logtostderr log to standard error instead of files
--pprof strings enable profiling
--purge_logs_interval duration how often try to remove old logs (default 1h0m0s)
--security_policy string the name of a registered security policy to use for controlling access to URLs - empty means allow all for anyone (built-in policies: deny-all, read-only)
--stderrthreshold severity logs at or above this threshold go to stderr (default 1)
--v Level log level for V logs
-v, --version print binary version
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
--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
zkctld is a daemon that starts or initializes ZooKeeper with Vitess-specific configuration. It will stay running as long as the underlying ZooKeeper server, and will pass along SIGTERM.

Usage:
zkctld [flags]

Flags:
-h, --help help for zkctld

0 comments on commit 5d802ee

Please sign in to comment.