vitess.io/vitess@v0.16.2/go/cmd/zkctl/zkctl.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // zkctl initializes and controls ZooKeeper with Vitess-specific configuration.
    18  package main
    19  
    20  import (
    21  	"bufio"
    22  	"os"
    23  
    24  	"github.com/spf13/pflag"
    25  
    26  	"vitess.io/vitess/go/exit"
    27  	"vitess.io/vitess/go/vt/log"
    28  	"vitess.io/vitess/go/vt/logutil"
    29  	"vitess.io/vitess/go/vt/servenv"
    30  	"vitess.io/vitess/go/vt/zkctl"
    31  )
    32  
    33  var usage = `
    34  Commands:
    35  
    36  	init | start | shutdown | teardown
    37  `
    38  
    39  var (
    40  	// Reason for nolint : Used in line 54 (stdin = bufio.NewReader(os.Stdin)) in the init function
    41  	stdin *bufio.Reader //nolint
    42  	zkCfg = "6@<hostname>:3801:3802:3803"
    43  	myID  uint
    44  )
    45  
    46  func registerZkctlFlags(fs *pflag.FlagSet) {
    47  	fs.StringVar(&zkCfg, "zk.cfg", zkCfg,
    48  		"zkid@server1:leaderPort1:electionPort1:clientPort1,...)")
    49  	fs.UintVar(&myID, "zk.myid", myID,
    50  		"which server do you want to be? only needed when running multiple instance on one box, otherwise myid is implied by hostname")
    51  
    52  }
    53  func init() {
    54  	servenv.OnParse(registerZkctlFlags)
    55  	stdin = bufio.NewReader(os.Stdin)
    56  }
    57  
    58  func main() {
    59  	defer exit.Recover()
    60  	defer logutil.Flush()
    61  
    62  	fs := pflag.NewFlagSet("zkctl", pflag.ExitOnError)
    63  	log.RegisterFlags(fs)
    64  	logutil.RegisterFlags(fs)
    65  	args := servenv.ParseFlagsWithArgs("zkctl")
    66  
    67  	zkConfig := zkctl.MakeZkConfigFromString(zkCfg, uint32(myID))
    68  	zkd := zkctl.NewZkd(zkConfig)
    69  
    70  	action := args[0]
    71  	var err error
    72  	switch action {
    73  	case "init":
    74  		err = zkd.Init()
    75  	case "shutdown":
    76  		err = zkd.Shutdown()
    77  	case "start":
    78  		err = zkd.Start()
    79  	case "teardown":
    80  		err = zkd.Teardown()
    81  	default:
    82  		log.Errorf("invalid action: %v", action)
    83  		log.Errorf(usage)
    84  		exit.Return(1)
    85  	}
    86  	if err != nil {
    87  		log.Errorf("failed %v: %v", action, err)
    88  		exit.Return(1)
    89  	}
    90  }