Skip to content

Commit

Permalink
Sync from server repo (914bd414b6b)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Spilchen committed Apr 3, 2024
1 parent a64507c commit fff1224
Show file tree
Hide file tree
Showing 24 changed files with 701 additions and 99 deletions.
78 changes: 58 additions & 20 deletions commands/cluster_command_launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ const defaultExecutablePath = "/opt/vertica/bin/vcluster"

const CLIVersion = "1.2.0"
const vclusterLogPathEnv = "VCLUSTER_LOG_PATH"
const vclusterKeyPathEnv = "VCLUSTER_KEY_PATH"
const vclusterCertPathEnv = "VCLUSTER_CERT_PATH"
const vclusterKeyFileEnv = "VCLUSTER_KEY_FILE"
const vclusterCertFileEnv = "VCLUSTER_CERT_FILE"

// *Flag is for the flag name, *Key is for viper key name
// They are bound together
const (
dbNameFlag = "db-name"
dbNameKey = "dbName"
dbUserFlag = "db-user"
dbUserKey = "dbUser"
hostsFlag = "hosts"
hostsKey = "hosts"
catalogPathFlag = "catalog-path"
Expand All @@ -59,10 +61,10 @@ const (
configParamKey = "configParam"
logPathFlag = "log-path"
logPathKey = "logPath"
keyPathFlag = "key-path"
keyPathKey = "keyPath"
certPathFlag = "cert-path"
certPathKey = "certPath"
keyFileFlag = "key-file"
keyFileKey = "keyFile"
certFileFlag = "cert-file"
certFileKey = "certFile"
passwordFlag = "password"
passwordKey = "password"
passwordFileFlag = "password-file"
Expand All @@ -79,9 +81,26 @@ const (
sandboxFlag = "sandbox"
)

// Flag and key for database replication
const (
targetDBNameFlag = "target-db-name"
targetDBNameKey = "targetDBName"
targetHostFlag = "target-hosts"
targetHostKey = "targetHosts"
targetUserNameFlag = "target-db-user"
targetUserNameKey = "targetDBUser"
targetPasswordFileFlag = "target-password-file"
targetPasswordFileKey = "targetPasswordFile"
targetConnFlag = "target-conn"
targetConnKey = "targetConn"
sourceTLSConfigFlag = "source-tlsconfig"
sourceTLSConfigKey = "sourceTLSConfig"
)

// flags to viper key map
var flagKeyMap = map[string]string{
dbNameFlag: dbNameKey,
dbUserFlag: dbUserKey,
hostsFlag: hostsKey,
catalogPathFlag: catalogPathKey,
depotPathFlag: depotPathKey,
Expand All @@ -91,14 +110,19 @@ var flagKeyMap = map[string]string{
eonModeFlag: eonModeKey,
configParamFlag: configParamKey,
logPathFlag: logPathKey,
keyPathFlag: keyPathKey,
certPathFlag: certPathKey,
keyFileFlag: keyFileKey,
certFileFlag: certFileKey,
passwordFlag: passwordKey,
passwordFileFlag: passwordFileKey,
readPasswordFromPromptFlag: readPasswordFromPromptKey,
configFlag: configKey,
verboseFlag: verboseKey,
outputFileFlag: outputFileKey,
targetDBNameFlag: targetDBNameKey,
targetHostFlag: targetHostKey,
targetUserNameFlag: targetUserNameKey,
targetPasswordFileFlag: targetPasswordFileKey,
sourceTLSConfigFlag: sourceTLSConfigKey,
}

const (
Expand All @@ -108,6 +132,8 @@ const (
manageConfigSubCmd = "manage_config"
configRecoverSubCmd = "recover"
configShowSubCmd = "show"
replicationSubCmd = "replication"
startReplicationSubCmd = "start"
listAllNodesSubCmd = "list_allnodes"
startDBSubCmd = "start_db"
dropDBSubCmd = "drop_db"
Expand All @@ -130,8 +156,8 @@ const (
type cmdGlobals struct {
verbose bool
file *os.File
keyPath string
certPath string
keyFile string
certFile string
}

var (
Expand Down Expand Up @@ -223,10 +249,10 @@ func setDBOptionsUsingViper(flag string) error {
dbOptions.ConfigurationParameters = viper.GetStringMapString(configParamKey)
case logPathFlag:
*dbOptions.LogPath = viper.GetString(logPathKey)
case keyPathFlag:
globals.keyPath = viper.GetString(keyPathKey)
case certPathFlag:
globals.certPath = viper.GetString(certPathKey)
case keyFileFlag:
globals.keyFile = viper.GetString(keyFileKey)
case certFileFlag:
globals.certFile = viper.GetString(certFileKey)
case verboseFlag:
globals.verbose = viper.GetBool(verboseKey)
default:
Expand All @@ -244,12 +270,12 @@ func configViper(cmd *cobra.Command, flagsInConfig []string) error {

// log-path is a flag that all the subcommands need
flagsInConfig = append(flagsInConfig, logPathFlag)
// cert-path and key-path are not available for
// cert-file and key-file are not available for
// - manage_config
// - manage_config show
if cmd.CalledAs() != manageConfigSubCmd &&
cmd.CalledAs() != configShowSubCmd {
flagsInConfig = append(flagsInConfig, certPathFlag, keyPathFlag)
flagsInConfig = append(flagsInConfig, certFileFlag, keyFileFlag)
}

// bind viper keys to cobra flags
Expand All @@ -268,13 +294,13 @@ func configViper(cmd *cobra.Command, flagsInConfig []string) error {
if err != nil {
return fmt.Errorf("fail to bind viper key %q to environment variable %q: %w", logPathKey, vclusterLogPathEnv, err)
}
err = viper.BindEnv(keyPathKey, vclusterKeyPathEnv)
err = viper.BindEnv(keyFileKey, vclusterKeyFileEnv)
if err != nil {
return fmt.Errorf("fail to bind viper key %q to environment variable %q: %w", keyPathKey, vclusterKeyPathEnv, err)
return fmt.Errorf("fail to bind viper key %q to environment variable %q: %w", keyFileKey, vclusterKeyFileEnv, err)
}
err = viper.BindEnv(certPathKey, vclusterCertPathEnv)
err = viper.BindEnv(certFileKey, vclusterCertFileEnv)
if err != nil {
return fmt.Errorf("fail to bind viper key %q to environment variable %q: %w", certPathKey, vclusterCertPathEnv, err)
return fmt.Errorf("fail to bind viper key %q to environment variable %q: %w", certFileKey, vclusterCertFileEnv, err)
}

// load db options from config file to viper
Expand Down Expand Up @@ -389,6 +415,17 @@ func OldMakeBasicCobraCmd(i cmdInterface, use, short, long string) *cobra.Comman
return cmd
}

// makeSimpleCobraCmd can make a simple cobra command for some vcluster commands
// such as replication and manage_config
func makeSimpleCobraCmd(use, short, long string) *cobra.Command {
return &cobra.Command{
Use: use,
Short: short,
Long: long,
Args: cobra.NoArgs,
}
}

// constructCmds returns a list of commands that will be executed
// by the cluster command launcher.
func constructCmds() []*cobra.Command {
Expand Down Expand Up @@ -416,6 +453,7 @@ func constructCmds() []*cobra.Command {
// others
makeCmdScrutinize(),
makeCmdManageConfig(),
makeCmdReplication(),
}
}

Expand Down
66 changes: 41 additions & 25 deletions commands/cmd_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,26 @@ func (c *CmdBase) setCommonFlags(cmd *cobra.Command, flags []string) {
false,
"Show the details of VCluster run in the console",
)
// keyPath and certPath are flags that all subcommands require,
// keyFile and certFile are flags that all subcommands require,
// except for manage_config and `manage_config show`
if cmd.Name() != manageConfigSubCmd && // VER-92992: remove this line once manage_config is not runnable
cmd.Name() != configShowSubCmd {
cmd.Flags().StringVar(
&globals.keyPath,
keyPathFlag,
&globals.keyFile,
keyFileFlag,
"",
"Path to the key file",
)
markFlagsFileName(cmd, map[string][]string{keyPathFlag: {"key"}})
markFlagsFileName(cmd, map[string][]string{keyFileFlag: {"key"}})

cmd.Flags().StringVar(
&globals.certPath,
certPathFlag,
&globals.certFile,
certFileFlag,
"",
"Path to the cert file",
)
markFlagsFileName(cmd, map[string][]string{certPathFlag: {"pem", "crt"}})
cmd.MarkFlagsRequiredTogether(keyPathFlag, certPathFlag)
markFlagsFileName(cmd, map[string][]string{certFileFlag: {"pem", "crt"}})
cmd.MarkFlagsRequiredTogether(keyFileFlag, certFileFlag)
}
if util.StringInArray(outputFileFlag, flags) {
cmd.Flags().StringVarP(
Expand All @@ -138,6 +138,14 @@ func (c *CmdBase) setCommonFlags(cmd *cobra.Command, flags []string) {
"Write output to this file instead of stdout",
)
}
if util.StringInArray(dbUserFlag, flags) {
cmd.Flags().StringVar(
dbOptions.UserName,
dbUserFlag,
"",
"The username for connecting to the database",
)
}
}

// setConfigFlags sets the config flag as well as all the common flags that
Expand Down Expand Up @@ -291,27 +299,35 @@ func (c *CmdBase) setDBPassword(opt *vclusterops.DatabaseOptions) error {
return nil
}

password, err := c.passwordFileHelper(c.passwordFile)
if err != nil {
return err
}
*opt.Password = password
return nil
}

func (c *CmdBase) passwordFileHelper(passwordFile string) (string, error) {
if c.passwordFile == "" {
return fmt.Errorf("password file path is empty")
return "", fmt.Errorf("password file path is empty")
}
// hyphen(`-`) is used to indicate that input should come
// from stdin rather than from a file
if c.passwordFile == "-" {
if passwordFile == "-" {
password, err := readFromStdin()
if err != nil {
return err
}
*opt.Password = strings.TrimSuffix(password, "\n")
} else {
// Read password from file
passwordBytes, err := os.ReadFile(c.passwordFile)
if err != nil {
return fmt.Errorf("error reading password from file %q: %w", c.passwordFile, err)
return "", err
}
// Convert bytes to string, removing any newline characters
*opt.Password = strings.TrimSuffix(string(passwordBytes), "\n")
return strings.TrimSuffix(password, "\n"), nil
}
return nil

// Read password from file
passwordBytes, err := os.ReadFile(passwordFile)
if err != nil {
return "", fmt.Errorf("error reading password from file %q: %w", passwordFile, err)
}
// Convert bytes to string, removing any newline characters
return strings.TrimSuffix(string(passwordBytes), "\n"), nil
}

// usePassword returns true if at least one of the password
Expand Down Expand Up @@ -349,15 +365,15 @@ func (c *CmdBase) initCmdOutputFile() (*os.File, error) {

// getCertFilesFromPaths will update cert and key file from cert path options
func (c *CmdBase) getCertFilesFromCertPaths(opt *vclusterops.DatabaseOptions) error {
if globals.certPath != "" {
certData, err := os.ReadFile(globals.certPath)
if globals.certFile != "" {
certData, err := os.ReadFile(globals.certFile)
if err != nil {
return fmt.Errorf("failed to read certificate file, details %w", err)
}
opt.Cert = string(certData)
}
if globals.keyPath != "" {
keyData, err := os.ReadFile(globals.keyPath)
if globals.keyFile != "" {
keyData, err := os.ReadFile(globals.keyFile)
if err != nil {
return fmt.Errorf("failed to read private key file, details %w", err)
}
Expand Down
52 changes: 50 additions & 2 deletions commands/cmd_re_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package commands

import (
"fmt"

"github.com/spf13/cobra"
"github.com/vertica/vcluster/vclusterops"
"github.com/vertica/vcluster/vclusterops/vlog"
Expand Down Expand Up @@ -66,7 +68,7 @@ Examples:
)

// common db flags
newCmd.setCommonFlags(cmd, []string{dbNameFlag, hostsFlag, catalogPathFlag})
newCmd.setCommonFlags(cmd, []string{dbNameFlag, hostsFlag, catalogPathFlag, configFlag})

// local flags
newCmd.setLocalFlags(cmd)
Expand Down Expand Up @@ -112,16 +114,62 @@ func (c *CmdReIP) validateParse(logger vlog.Printer) error {

func (c *CmdReIP) Run(vcc vclusterops.ClusterCommands) error {
vcc.LogInfo("Called method Run()")
err := vcc.VReIP(c.reIPOptions)

options := c.reIPOptions

// load config info from the YAML config file
canUpdateConfig := true
dbConfig, err := readConfig()
if err != nil {
vcc.LogInfo("fail to read config file: %v", err)
canUpdateConfig = false
}

// VER-92369 should clean up the block below
// as the GetDBConfig function will be removed
config, err := options.GetDBConfig(vcc)
if err != nil {
return err
}
options.Config = config

err = vcc.VReIP(options)
if err != nil {
vcc.LogError(err, "fail to re-ip")
return err
}

vcc.PrintInfo("Re-ip is successfully completed")

// update config file after running re_ip
if canUpdateConfig {
c.UpdateConfig(dbConfig)
err = dbConfig.write(options.ConfigPath)
if err != nil {
fmt.Printf("Warning: fail to update config file, details %v\n", err)
}
}

return nil
}

// UpdateConfig will update node addresses in the config object after re_ip
func (c *CmdReIP) UpdateConfig(dbConfig *DatabaseConfig) {
nodeNameToAddress := make(map[string]string)
for _, reIPInfo := range c.reIPOptions.ReIPList {
if reIPInfo.TargetAddress != "" {
nodeNameToAddress[reIPInfo.NodeName] = reIPInfo.TargetAddress
}
}

for _, n := range dbConfig.Nodes {
newAddress, ok := nodeNameToAddress[n.Name]
if ok {
n.Address = newAddress
}
}
}

// SetDatabaseOptions will assign a vclusterops.DatabaseOptions instance to the one in CmdReIP
func (c *CmdReIP) SetDatabaseOptions(opt *vclusterops.DatabaseOptions) {
c.reIPOptions.DatabaseOptions = *opt
Expand Down
Loading

0 comments on commit fff1224

Please sign in to comment.