Skip to content

Commit

Permalink
Sync from server repo (e6321104cb0)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Spilchen committed May 7, 2024
1 parent 78ff367 commit 3cef18c
Show file tree
Hide file tree
Showing 22 changed files with 517 additions and 77 deletions.
2 changes: 2 additions & 0 deletions commands/cluster_command_launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ const (
removeSCSubCmd = "db_remove_subcluster"
stopSCSubCmd = "stop_subcluster"
addNodeSubCmd = "db_add_node"
startSCSubCmd = "start_subcluster"
stopNodeCmd = "stop_node"
removeNodeSubCmd = "db_remove_node"
restartNodeSubCmd = "restart_node"
Expand Down Expand Up @@ -512,6 +513,7 @@ func constructCmds() []*cobra.Command {
makeCmdAddSubcluster(),
makeCmdRemoveSubcluster(),
makeCmdStopSubcluster(),
makeCmdStartSubcluster(),
makeCmdSandboxSubcluster(),
makeCmdUnsandboxSubcluster(),
// node-scope cmds
Expand Down
2 changes: 1 addition & 1 deletion commands/cmd_add_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (c *CmdAddNode) Run(vcc vclusterops.ClusterCommands) error {
}

// write db info to vcluster config file
err := writeConfig(&vdb, vcc.GetLog())
err := writeConfig(&vdb)
if err != nil {
vcc.PrintWarning("fail to write config file, details: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/cmd_add_subcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (c *CmdAddSubcluster) Run(vcc vclusterops.ClusterCommands) error {
return err
}
// update db info in the config file
err = writeConfig(&vdb, vcc.GetLog())
err = writeConfig(&vdb)
if err != nil {
vcc.PrintWarning("fail to write config file, details: %s", err)
}
Expand Down
7 changes: 3 additions & 4 deletions commands/cmd_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ func (c *CmdBase) setDBPassword(opt *vclusterops.DatabaseOptions) error {
return nil
}

if c.passwordFile == "" {
return fmt.Errorf("password file path is empty")
}
password, err := c.passwordFileHelper(c.passwordFile)
if err != nil {
return err
Expand All @@ -292,10 +295,6 @@ func (c *CmdBase) setDBPassword(opt *vclusterops.DatabaseOptions) error {
}

func (c *CmdBase) passwordFileHelper(passwordFile string) (string, error) {
if passwordFile == "" {
return "", fmt.Errorf("password file path is empty")
}

// Read password from file
passwordBytes, err := os.ReadFile(passwordFile)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion commands/cmd_config_recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (c *CmdConfigRecover) Run(vcc vclusterops.ClusterCommands) error {
return err
}
// write db info to vcluster config file
err = writeConfig(&vdb, vcc.GetLog())
err = writeConfig(&vdb)
if err != nil {
return fmt.Errorf("fail to write config file, details: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/cmd_create_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (c *CmdCreateDB) Run(vcc vclusterops.ClusterCommands) error {
}

// write db info to vcluster config file
err := writeConfig(&vdb, vcc.GetLog())
err := writeConfig(&vdb)
if err != nil {
vcc.PrintWarning("fail to write config file, details: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/cmd_drop_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (c *CmdDropDB) Run(vcc vclusterops.ClusterCommands) error {
vcc.PrintInfo("Successfully dropped database %s", c.dropDBOptions.DBName)
// if the database is successfully dropped, the config file will be removed
// if failed to remove it, we will ask users to manually do it
err = removeConfig(vcc.GetLog())
err = removeConfig()
if err != nil {
vcc.PrintWarning("Fail to remove config file %q, "+
"please manually do it. Details: %v", c.dropDBOptions.ConfigPath, err)
Expand Down
2 changes: 1 addition & 1 deletion commands/cmd_remove_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (c *CmdRemoveNode) Run(vcc vclusterops.ClusterCommands) error {
}

// write db info to vcluster config file
err = writeConfig(&vdb, vcc.GetLog())
err = writeConfig(&vdb)
if err != nil {
vcc.PrintWarning("fail to write config file, details: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/cmd_remove_subcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *CmdRemoveSubcluster) Run(vcc vclusterops.ClusterCommands) error {
}

// write db info to vcluster config file
err = writeConfig(&vdb, vcc.GetLog())
err = writeConfig(&vdb)
if err != nil {
vcc.PrintWarning("fail to write config file, details: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/cmd_revive_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (c *CmdReviveDB) Run(vcc vclusterops.ClusterCommands) error {
}

// write db info to vcluster config file
err = writeConfig(vdb, vcc.GetLog())
err = writeConfig(vdb)
if err != nil {
vcc.PrintWarning("fail to write config file, details: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/cmd_start_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (c *CmdStartDB) Run(vcc vclusterops.ClusterCommands) error {
// for Eon database, update config file to fill nodes' subcluster information
if options.IsEon {
// write db info to vcluster config file
err := writeConfig(vdb, vcc.GetLog())
err := writeConfig(vdb)
if err != nil {
vcc.PrintWarning("fail to update config file, details: %s", err)
}
Expand Down
5 changes: 4 additions & 1 deletion commands/cmd_start_replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ This subcommand copies table or schema data directly from one Eon Mode
database's communal storage to another.
The --target-conn option serves as a collection file for gathering necessary
target information for replication. You need to run vcluster manage_connection
target information for replication. You need to run vcluster create_connection
to generate this connection file in order to use this option.
The --sandbox option is used to replicate from a sandbox to a target database
Expand Down Expand Up @@ -214,6 +214,9 @@ func (c *CmdStartReplication) parseTargetPassword() error {
options.TargetPassword = new(string)
}

if c.targetPasswordFile == "" {
return fmt.Errorf("target password file path is empty")
}
password, err := c.passwordFileHelper(c.targetPasswordFile)
if err != nil {
return err
Expand Down
142 changes: 142 additions & 0 deletions commands/cmd_start_subcluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
(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 (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/vertica/vcluster/vclusterops"
"github.com/vertica/vcluster/vclusterops/util"
"github.com/vertica/vcluster/vclusterops/vlog"
)

/* CmdStartSubcluster
*
* Implements ClusterCommand interface
*/
type CmdStartSubcluster struct {
startScOptions *vclusterops.VStartScOptions

CmdBase
}

func makeCmdStartSubcluster() *cobra.Command {
// CmdStartSubcluster
newCmd := &CmdStartSubcluster{}
opt := vclusterops.VStartScOptionsFactory()
newCmd.startScOptions = &opt

cmd := makeBasicCobraCmd(
newCmd,
startSCSubCmd,
"Start a subcluster",
`This subcommand starts a stopped subcluster in a running Eon database.
You must provide the subcluster name with the --subcluster option.
Examples:
# Start a subcluster with config file
vcluster start_subcluster --subcluster sc1 \
--config /opt/vertica/config/vertica_cluster.yaml
# Start a subcluster with user input
vcluster start_subcluster --db-name test_db \
--hosts 10.20.30.40,10.20.30.41,10.20.30.42 --subcluster sc1
`,
[]string{dbNameFlag, configFlag, hostsFlag, eonModeFlag, passwordFlag},
)

// local flags
newCmd.setLocalFlags(cmd)

// require name of subcluster to start
markFlagsRequired(cmd, []string{subclusterFlag})

// hide eon mode flag since we expect it to come from config file, not from user input
hideLocalFlags(cmd, []string{eonModeFlag})

return cmd
}

// setLocalFlags will set the local flags the command has
func (c *CmdStartSubcluster) setLocalFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(
&c.startScOptions.SubclusterToStart,
subclusterFlag,
"",
"Name of subcluster to start",
)
cmd.Flags().IntVar(
&c.startScOptions.StatePollingTimeout,
"timeout",
util.DefaultTimeoutSeconds,
"The timeout (in seconds) to wait for polling node state operation",
)
}

func (c *CmdStartSubcluster) Parse(inputArgv []string, logger vlog.Printer) error {
c.argv = inputArgv
logger.LogMaskedArgParse(c.argv)

// reset some options that are not included in user input
c.ResetUserInputOptions(&c.startScOptions.DatabaseOptions)

// start_subcluster only works for an Eon db so we assume the user always runs this subcommand
// on an Eon db. When Eon mode cannot be found in config file, we set its value to true.
if !viper.IsSet(eonModeKey) {
c.startScOptions.IsEon = true
}
return c.validateParse(logger)
}

func (c *CmdStartSubcluster) validateParse(logger vlog.Printer) error {
logger.Info("Called validateParse()")
err := c.getCertFilesFromCertPaths(&c.startScOptions.DatabaseOptions)
if err != nil {
return err
}

err = c.ValidateParseBaseOptions(&c.startScOptions.DatabaseOptions)
if err != nil {
return nil
}
return c.setDBPassword(&c.startScOptions.DatabaseOptions)
}

func (c *CmdStartSubcluster) Analyze(_ vlog.Printer) error {
return nil
}

func (c *CmdStartSubcluster) Run(vcc vclusterops.ClusterCommands) error {
vcc.V(1).Info("Called method Run()")

options := c.startScOptions

err := vcc.VStartSubcluster(options)
if err != nil {
return err
}

vcc.PrintInfo("Successfully started subcluster %s for database %s",
options.SubclusterToStart, options.DBName)

return nil
}

// SetDatabaseOptions will assign a vclusterops.DatabaseOptions instance to the one in CmdStartSubcluster
func (c *CmdStartSubcluster) SetDatabaseOptions(opt *vclusterops.DatabaseOptions) {
c.startScOptions.DatabaseOptions = *opt
}
3 changes: 2 additions & 1 deletion commands/cmd_stop_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func (c *CmdStopDB) setLocalFlags(cmd *cobra.Command) {
util.DefaultDrainSeconds,
util.GetEonFlagMsg("seconds to wait for user connections to close."+
" Default value is "+strconv.Itoa(util.DefaultDrainSeconds)+" seconds."+
" When the time expires, connections will be forcibly closed and the db will shut down"),
" When the time expires, connections will be forcibly closed and the db will shut down."+
" Set this to 0 for Eon database, if you want to forcibly stop the database."),
)
cmd.Flags().StringVar(
&c.stopDBOptions.Sandbox,
Expand Down
36 changes: 2 additions & 34 deletions commands/vcluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/spf13/viper"
"github.com/vertica/vcluster/vclusterops"
"github.com/vertica/vcluster/vclusterops/util"
"github.com/vertica/vcluster/vclusterops/vlog"
"gopkg.in/yaml.v3"
)

Expand All @@ -34,7 +33,6 @@ const (
// default file name that we'll use.
defConfigFileName = "vertica_cluster.yaml"
currentConfigFileVersion = "1.0"
configBackupName = "vertica_cluster.yaml.backup"
configFilePerm = 0600
)

Expand Down Expand Up @@ -189,7 +187,7 @@ func loadConfigToViper() error {

// writeConfig can write database information to vertica_cluster.yaml.
// It will be called in the end of some subcommands that will change the db state.
func writeConfig(vdb *vclusterops.VCoordinationDatabase, logger vlog.Printer) error {
func writeConfig(vdb *vclusterops.VCoordinationDatabase) error {
if dbOptions.ConfigPath == "" {
return fmt.Errorf("configuration file path is empty")
}
Expand All @@ -199,13 +197,6 @@ func writeConfig(vdb *vclusterops.VCoordinationDatabase, logger vlog.Printer) er
return err
}

// if the config file exists already,
// create its backup before overwriting it
err = backupConfigFile(dbOptions.ConfigPath, logger)
if err != nil {
return err
}

// update db config with the given database info
err = dbConfig.write(dbOptions.ConfigPath)
if err != nil {
Expand All @@ -217,15 +208,10 @@ func writeConfig(vdb *vclusterops.VCoordinationDatabase, logger vlog.Printer) er

// removeConfig remove the config file vertica_cluster.yaml.
// It will be called in the end of drop_db subcommands.
func removeConfig(logger vlog.Printer) error {
func removeConfig() error {
if dbOptions.ConfigPath == "" {
return fmt.Errorf("configuration file path is empty")
}
// back up the old config file
err := backupConfigFile(dbOptions.ConfigPath, logger)
if err != nil {
return err
}

// remove the old db config
return os.Remove(dbOptions.ConfigPath)
Expand Down Expand Up @@ -273,24 +259,6 @@ func readVDBToDBConfig(vdb *vclusterops.VCoordinationDatabase) (DatabaseConfig,
return dbConfig, nil
}

// backupConfigFile backs up config file before we update it.
// This function will add ".backup" suffix to previous config file.
func backupConfigFile(configFilePath string, logger vlog.Printer) error {
if util.CanReadAccessDir(configFilePath) == nil {
// copy file to vertica_cluster.yaml.backup
configDirPath := filepath.Dir(configFilePath)
configFileBackup := filepath.Join(configDirPath, configBackupName)
logger.Info("Configuration file exists and, creating a backup", "config file", configFilePath,
"backup file", configFileBackup)
err := util.CopyFile(configFilePath, configFileBackup, configFilePerm)
if err != nil {
return err
}
}

return nil
}

// read reads information from configFilePath to a DatabaseConfig object.
// It returns any read error encountered.
func readConfig() (dbConfig *DatabaseConfig, err error) {
Expand Down
1 change: 1 addition & 0 deletions vclusterops/cluster_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ type ClusterCommands interface {
VShowRestorePoints(options *VShowRestorePointsOptions) (restorePoints []RestorePoint, err error)
VStartDatabase(options *VStartDatabaseOptions) (vdbPtr *VCoordinationDatabase, err error)
VStartNodes(options *VStartNodesOptions) error
VStartSubcluster(startScOpt *VStartScOptions) error
VStopDatabase(options *VStopDatabaseOptions) error
VReplicateDatabase(options *VReplicationDatabaseOptions) error
VFetchCoordinationDatabase(options *VFetchCoordinationDatabaseOptions) (VCoordinationDatabase, error)
Expand Down
Loading

0 comments on commit 3cef18c

Please sign in to comment.