vitess.io/vitess@v0.16.2/go/vt/mysqlctl/mycnf_flag.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  package mysqlctl
    18  
    19  import (
    20  	"github.com/spf13/pflag"
    21  
    22  	"vitess.io/vitess/go/vt/log"
    23  	"vitess.io/vitess/go/vt/servenv"
    24  )
    25  
    26  // This file handles using command line flags to create a Mycnf object.
    27  // Since whoever links with this module doesn't necessarely need the flags,
    28  // RegisterFlags needs to be called explicitly to set the flags up.
    29  
    30  var (
    31  	// the individual command line parameters
    32  	flagServerID              int
    33  	flagMysqlPort             int
    34  	flagDataDir               string
    35  	flagInnodbDataHomeDir     string
    36  	flagInnodbLogGroupHomeDir string
    37  	flagSocketFile            string
    38  	flagGeneralLogPath        string
    39  	flagErrorLogPath          string
    40  	flagSlowLogPath           string
    41  	flagRelayLogPath          string
    42  	flagRelayLogIndexPath     string
    43  	flagRelayLogInfoPath      string
    44  	flagBinLogPath            string
    45  	flagMasterInfoFile        string
    46  	flagPidFile               string
    47  	flagTmpDir                string
    48  	flagSecureFilePriv        string
    49  
    50  	// the file to use to specify them all
    51  	flagMycnfFile string
    52  )
    53  
    54  // RegisterFlags registers the command line flags for
    55  // specifying the values of a mycnf config file. See NewMycnfFromFlags
    56  // to get the supported modes.
    57  func RegisterFlags() {
    58  	servenv.OnParse(func(fs *pflag.FlagSet) {
    59  		fs.IntVar(&flagServerID, "mycnf_server_id", flagServerID, "mysql server id of the server (if specified, mycnf-file will be ignored)")
    60  		fs.IntVar(&flagMysqlPort, "mycnf_mysql_port", flagMysqlPort, "port mysql is listening on")
    61  		fs.StringVar(&flagDataDir, "mycnf_data_dir", flagDataDir, "data directory for mysql")
    62  		fs.StringVar(&flagInnodbDataHomeDir, "mycnf_innodb_data_home_dir", flagInnodbDataHomeDir, "Innodb data home directory")
    63  		fs.StringVar(&flagInnodbLogGroupHomeDir, "mycnf_innodb_log_group_home_dir", flagInnodbLogGroupHomeDir, "Innodb log group home directory")
    64  		fs.StringVar(&flagSocketFile, "mycnf_socket_file", flagSocketFile, "mysql socket file")
    65  		fs.StringVar(&flagGeneralLogPath, "mycnf_general_log_path", flagGeneralLogPath, "mysql general log path")
    66  		fs.StringVar(&flagErrorLogPath, "mycnf_error_log_path", flagErrorLogPath, "mysql error log path")
    67  		fs.StringVar(&flagSlowLogPath, "mycnf_slow_log_path", flagSlowLogPath, "mysql slow query log path")
    68  		fs.StringVar(&flagRelayLogPath, "mycnf_relay_log_path", flagRelayLogPath, "mysql relay log path")
    69  		fs.StringVar(&flagRelayLogIndexPath, "mycnf_relay_log_index_path", flagRelayLogIndexPath, "mysql relay log index path")
    70  		fs.StringVar(&flagRelayLogInfoPath, "mycnf_relay_log_info_path", flagRelayLogInfoPath, "mysql relay log info path")
    71  		fs.StringVar(&flagBinLogPath, "mycnf_bin_log_path", flagBinLogPath, "mysql binlog path")
    72  		fs.StringVar(&flagMasterInfoFile, "mycnf_master_info_file", flagMasterInfoFile, "mysql master.info file")
    73  		fs.StringVar(&flagPidFile, "mycnf_pid_file", flagPidFile, "mysql pid file")
    74  		fs.StringVar(&flagTmpDir, "mycnf_tmp_dir", flagTmpDir, "mysql tmp directory")
    75  		fs.StringVar(&flagSecureFilePriv, "mycnf_secure_file_priv", flagSecureFilePriv, "mysql path for loading secure files")
    76  
    77  		fs.StringVar(&flagMycnfFile, "mycnf-file", flagMycnfFile, "path to my.cnf, if reading all config params from there")
    78  	})
    79  }
    80  
    81  // NewMycnfFromFlags creates a Mycnf object from the command line flags.
    82  //
    83  // Multiple modes are supported:
    84  //   - at least mycnf_server_id is set on the command line
    85  //     --> then we read all parameters from the command line, and not from
    86  //     any my.cnf file.
    87  //   - mycnf_server_id is not passed in, but mycnf-file is passed in
    88  //     --> then we read that mycnf file
    89  //   - mycnf_server_id and mycnf-file are not passed in:
    90  //     --> then we use the default location of the my.cnf file for the
    91  //     provided uid and read that my.cnf file.
    92  //
    93  // RegisterCommandLineFlags should have been called before calling
    94  // this, otherwise we'll panic.
    95  func NewMycnfFromFlags(uid uint32) (mycnf *Mycnf, err error) {
    96  	if flagServerID != 0 {
    97  		log.Info("mycnf_server_id is specified, using command line parameters for mysql config")
    98  		return &Mycnf{
    99  			ServerID:              uint32(flagServerID),
   100  			MysqlPort:             int32(flagMysqlPort),
   101  			DataDir:               flagDataDir,
   102  			InnodbDataHomeDir:     flagInnodbDataHomeDir,
   103  			InnodbLogGroupHomeDir: flagInnodbLogGroupHomeDir,
   104  			SocketFile:            flagSocketFile,
   105  			GeneralLogPath:        flagGeneralLogPath,
   106  			ErrorLogPath:          flagErrorLogPath,
   107  			SlowLogPath:           flagSlowLogPath,
   108  			RelayLogPath:          flagRelayLogPath,
   109  			RelayLogIndexPath:     flagRelayLogIndexPath,
   110  			RelayLogInfoPath:      flagRelayLogInfoPath,
   111  			BinLogPath:            flagBinLogPath,
   112  			MasterInfoFile:        flagMasterInfoFile,
   113  			PidFile:               flagPidFile,
   114  			TmpDir:                flagTmpDir,
   115  			SecureFilePriv:        flagSecureFilePriv,
   116  
   117  			// This is probably not going to be used by anybody,
   118  			// but fill in a default value. (Note it's used by
   119  			// mysqld.Start, in which case it is correct).
   120  			Path: MycnfFile(uint32(flagServerID)),
   121  		}, nil
   122  	}
   123  
   124  	if flagMycnfFile == "" {
   125  		flagMycnfFile = MycnfFile(uid)
   126  		log.Infof("No mycnf_server_id, no mycnf-file specified, using default config for server id %v: %v", uid, flagMycnfFile)
   127  	} else {
   128  		log.Infof("No mycnf_server_id specified, using mycnf-file file %v", flagMycnfFile)
   129  	}
   130  	mycnf = NewMycnf(uid, 0)
   131  	mycnf.Path = flagMycnfFile
   132  	return ReadMycnf(mycnf)
   133  }