Skip to content

Commit

Permalink
Sync from server repo (ea2191cdef7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Spilchen committed Mar 24, 2024
1 parent 15f8a02 commit 45acd3e
Show file tree
Hide file tree
Showing 18 changed files with 615 additions and 177 deletions.
29 changes: 21 additions & 8 deletions commands/cluster_command_launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,24 @@ var flagKeyMap = map[string]string{
const (
createDBSubCmd = "create_db"
stopDBSubCmd = "stop_db"
reviveDBSubCmd = "revive_db"
manageConfigSubCmd = "manage_config"
configRecoverSubCmd = "recover"
configShowSubCmd = "show"
listAllNodesSubCmd = "list_allnodes"
startDBSubCmd = "start_db"
dropDBSubCmd = "drop_db"
reviveDBSubCmd = "revive_db"
addSCSubCmd = "db_add_subcluster"
removeSCSubCmd = "db_remove_subcluster"
addNodeSubCmd = "db_add_node"
removeNodeSubCmd = "db_remove_node"
restartNodeSubCmd = "restart_node"
reIPSubCmd = "re_ip"
listAllNodesSubCmd = "list_allnodes"
sandboxSubCmd = "sandbox_subcluster"
unsandboxSubCmd = "unsandbox_subcluster"
scrutinizeSubCmd = "scrutinize"
showRestorePointsSubCmd = "show_restore_points"
installPkgSubCmd = "install_packages"
configSubCmd = "config"
)

// cmdGlobals holds global variables shared by multiple
Expand Down Expand Up @@ -239,10 +241,14 @@ 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 config subcmd
if cmd.CalledAs() != configSubCmd {
// cert-path and key-path are not available for
// - manage_config
// - manage_config show
if cmd.CalledAs() != manageConfigSubCmd &&
cmd.CalledAs() != configShowSubCmd {
flagsInConfig = append(flagsInConfig, certPathFlag, keyPathFlag)
}

// bind viper keys to cobra flags
for _, flag := range flagsInConfig {
if _, ok := flagKeyMap[flag]; !ok {
Expand Down Expand Up @@ -270,13 +276,20 @@ func configViper(cmd *cobra.Command, flagsInConfig []string) error {

// load db options from config file to viper
// note: config file is not available for create_db and revive_db
if cmd.CalledAs() != createDBSubCmd && cmd.CalledAs() != reviveDBSubCmd {
// manage_config does not need viper to load config file info
if cmd.CalledAs() != createDBSubCmd &&
cmd.CalledAs() != reviveDBSubCmd &&
cmd.CalledAs() != manageConfigSubCmd {
err = loadConfigToViper()
if err != nil {
return err
}
}

return handleViperUserInput(flagsInConfig)
}

func handleViperUserInput(flagsInConfig []string) error {
// if a flag is set in viper through user input, env var or config file, we assign its viper value
// to database options. viper can automatically retrieve the correct value following below order:
// 1. user input
Expand All @@ -289,7 +302,7 @@ func configViper(cmd *cobra.Command, flagsInConfig []string) error {
continue
}
if viper.IsSet(flagKeyMap[flag]) {
err = setDBOptionsUsingViper(flag)
err := setDBOptionsUsingViper(flag)
if err != nil {
return fmt.Errorf("fail to set flag %q using viper: %w", flag, err)
}
Expand Down Expand Up @@ -396,7 +409,7 @@ func constructCmds() []*cobra.Command {
makeCmdRemoveNode(),
// others
makeCmdScrutinize(),
makeCmdConfig(),
makeCmdManageConfig(),
}
}

Expand Down
6 changes: 4 additions & 2 deletions commands/cmd_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ 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, except for the config subcommand
if cmd.Name() != configSubCmd {
// keyPath and certPath are flags that all subcommands require,
// except for manage_config and `manage_config show`
if cmd.Name() != manageConfigSubCmd &&
cmd.Name() != configShowSubCmd {
cmd.Flags().StringVar(
&globals.keyPath,
keyPathFlag,
Expand Down
113 changes: 0 additions & 113 deletions commands/cmd_config.go

This file was deleted.

126 changes: 126 additions & 0 deletions commands/cmd_config_recover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
(c) Copyright [2023-2024] Open Text.
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 commands

import (
"fmt"

"github.com/spf13/cobra"
"github.com/vertica/vcluster/vclusterops"
"github.com/vertica/vcluster/vclusterops/vlog"
)

/* CmdConfigRecover
*
* A subcommand recovering the YAML config file
* in the default or a specified location.
*
* Implements ClusterCommand interface
*/
type CmdConfigRecover struct {
recoverConfigOptions *vclusterops.VFetchCoordinationDatabaseOptions
CmdBase
}

func makeCmdConfigRecover() *cobra.Command {
newCmd := &CmdConfigRecover{}
opt := vclusterops.VRecoverConfigOptionsFactory()
newCmd.recoverConfigOptions = &opt

cmd := makeBasicCobraCmd(
newCmd,
configRecoverSubCmd,
"recover the content of the config file",
`This subcommand is used to recover the content of the config file.
You must provide the all hosts that participate in the database.
If there is an existing file at the provided config file location, the recover function
will not create a new config file unless you explicitly specify --overwrite.
Examples:
# Recover the config file to the default location
vcluster manage_config recover --db-name test_db \
--hosts 10.20.30.41,10.20.30.42,10.20.30.43 \
--catalog-path /data --depot-path /data
# Recover the config file to /tmp/vertica_cluster.yaml
vcluster manage_config recover --db-name test_db \
--hosts 10.20.30.41,10.20.30.42,10.20.30.43 \
--catalog-path /data --depot-path /data \
--config /tmp/vertica_cluster.yaml
`,
[]string{dbNameFlag, hostsFlag, catalogPathFlag, depotPathFlag, ipv6Flag, configFlag},
)

// require db-name, hosts, catalog-path, and data-path
markFlagsRequired(cmd, []string{dbNameFlag, hostsFlag, catalogPathFlag})

// local flags
newCmd.setLocalFlags(cmd)

return cmd
}

// setLocalFlags will set the local flags the command has
func (c *CmdConfigRecover) setLocalFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(
&c.recoverConfigOptions.Overwrite,
"overwrite",
false,
"overwrite the existing config file",
)
}

func (c *CmdConfigRecover) Parse(inputArgv []string, logger vlog.Printer) error {
c.argv = inputArgv
logger.LogArgParse(&c.argv)

return c.validateParse(logger)
}

// all validations of the arguments should go in here
func (c *CmdConfigRecover) validateParse(logger vlog.Printer) error {
logger.Info("Called validateParse()")
err := c.ValidateParseBaseOptions(&c.recoverConfigOptions.DatabaseOptions)
if err != nil {
return err
}

return nil
}

func (c *CmdConfigRecover) Run(vcc vclusterops.ClusterCommands) error {
vdb, err := vcc.VFetchCoordinationDatabase(c.recoverConfigOptions)
if err != nil {
vcc.LogError(err, "failed to recover the config file")
return err
}
// write db info to vcluster config file
err = writeConfig(&vdb, vcc.GetLog())
if err != nil {
return fmt.Errorf("fail to write config file, details: %s", err)
}
vcc.PrintInfo("Recovered config file for database %s at %s", vdb.Name,
c.recoverConfigOptions.ConfigPath)

return nil
}

// SetDatabaseOptions will assign a vclusterops.DatabaseOptions instance
func (c *CmdConfigRecover) SetDatabaseOptions(opt *vclusterops.DatabaseOptions) {
c.recoverConfigOptions.DatabaseOptions = *opt
}
Loading

0 comments on commit 45acd3e

Please sign in to comment.