Skip to content

Commit

Permalink
Sync from server repo (a6c03ba28b)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Spilchen committed Oct 20, 2023
1 parent 0487bd6 commit fb3153c
Show file tree
Hide file tree
Showing 67 changed files with 753 additions and 891 deletions.
4 changes: 2 additions & 2 deletions cmd/vcluster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func main() {
// use fmt for print info in this function, because the step of
// setting up logs could error out
fmt.Println("---{vcluster begin}---")
launcher := commands.MakeClusterCommandLauncher()
runError := launcher.Run(os.Args)
launcher, vcc := commands.MakeClusterCommandLauncher()
runError := launcher.Run(os.Args, vcc)
if runError != nil {
fmt.Printf("Error during execution: %s\n", runError)
os.Exit(1)
Expand Down
11 changes: 7 additions & 4 deletions commands/cluster_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@

package commands

import "github.com/vertica/vcluster/vclusterops/vlog"
import (
"github.com/vertica/vcluster/vclusterops"
"github.com/vertica/vcluster/vclusterops/vlog"
)

type ClusterCommand interface {
CommandType() string
Parse(argv []string) error
Parse(argv []string, log vlog.Printer) error

/* TODO: Analyze information about the state of
* the cluster. The information could be
* cached in a config file or constructed through
* cluster discovery.
*/
Analyze() error
Run(log vlog.Printer) error
Analyze(log vlog.Printer) error
Run(vcc vclusterops.VClusterCommands) error
PrintUsage(string)
}
63 changes: 39 additions & 24 deletions commands/cluster_command_launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"os"

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

Expand Down Expand Up @@ -52,38 +53,43 @@ type ClusterCommandLauncher struct {
*/
const minArgs = 2
const helpString = "help"
const defaultLogPath = "/opt/vertica/log/vcluster.log"

/* ClusterCommandLauncherFactory()
* Returns a new instance of a ClusterCommandLauncher
* with some reasonable defaults.
*/
func MakeClusterCommandLauncher() ClusterCommandLauncher {
func MakeClusterCommandLauncher() (ClusterCommandLauncher, vclusterops.VClusterCommands) {
// setup logs for command launcher initialization
logPath := vlog.ParseLogPathArg(os.Args, vlog.DefaultLogPath)
vlog.SetupOrDie(logPath)
vlog.LogInfoln("New vcluster command initialization")
userCommandString := os.Args[1]
log := vlog.Printer{}
logPath := parseLogPathArg(os.Args, defaultLogPath)
log.SetupOrDie(logPath)
vcc := vclusterops.VClusterCommands{
Log: log.WithName(userCommandString),
}
vcc.Log.Info("New vcluster command initialization")
newLauncher := ClusterCommandLauncher{}

allCommands := constructCmds()
allCommands := constructCmds(vcc.Log)

newLauncher.commands = map[string]ClusterCommand{}
for _, c := range allCommands {
_, existsInMap := newLauncher.commands[c.CommandType()]
if existsInMap {
// shout loud if there's a programmer error
vlog.LogPrintError("Programmer Error: tried add command %s to the commands index twice. Check cluster_command_launcher.go",
vcc.Log.PrintError("Programmer Error: tried to add command %s to the commands index twice. Check cluster_command_launcher.go",
c.CommandType())
os.Exit(1)
}
newLauncher.commands[c.CommandType()] = c
}

return newLauncher
return newLauncher, vcc
}

// constructCmds returns a list of commands that will be executed
// by the cluster command launcher.
func constructCmds() []ClusterCommand {
func constructCmds(_ vlog.Printer) []ClusterCommand {
return []ClusterCommand{
// db-scope cmds
makeCmdCreateDB(),
Expand Down Expand Up @@ -118,24 +124,26 @@ func constructCmds() []ClusterCommand {
* + Calls Run() for the sub-command
* + Returns any errors to the caller after writing the error to the log
*/
func (c ClusterCommandLauncher) Run(inputArgv []string) error {
func (c ClusterCommandLauncher) Run(inputArgv []string, vcc vclusterops.VClusterCommands) error {
userCommandString := os.Args[1]
c.argv = inputArgv
minArgsError := checkMinimumInput(c.argv)

if minArgsError != nil {
vlog.LogError(minArgsError.Error())
vcc.Log.Error(minArgsError, "fail to check minimum argument")
return minArgsError
}

subCommand, idError := identifySubcommand(c.commands)
subCommand, idError := identifySubcommand(c.commands, userCommandString, vcc.Log)

if idError != nil {
vlog.LogError(idError.Error())
vcc.Log.Error(idError, "fail to recognize command")
return idError
}

parseError := subCommand.Parse(inputArgv[2:])
parseError := subCommand.Parse(inputArgv[2:], vcc.Log)
if parseError != nil {
vlog.LogError(parseError.Error())
vcc.Log.Error(parseError, "fail to parse command")
return parseError
}

Expand All @@ -152,31 +160,29 @@ func (c ClusterCommandLauncher) Run(inputArgv []string) error {
/* TODO: this is where we would read a
* configuration file. Not currently implemented.
*/
analyzeError := subCommand.Analyze()
analyzeError := subCommand.Analyze(vcc.Log)

if analyzeError != nil {
vlog.LogError(analyzeError.Error())
vcc.Log.Error(analyzeError, "fail to analyze command")
return analyzeError
}
log := vlog.GetGlobalLogger().Printer

runError := subCommand.Run(log)
runError := subCommand.Run(vcc)
if runError != nil {
vlog.LogError(runError.Error())
vcc.Log.Error(runError, "fail to run command")
}
return runError
}

func identifySubcommand(commands map[string]ClusterCommand) (ClusterCommand, error) {
userCommandString := os.Args[1]
func identifySubcommand(commands map[string]ClusterCommand, userCommandString string,
log vlog.Printer) (ClusterCommand, error) {
command, ok := commands[userCommandString]

if !ok {
return nil, fmt.Errorf("unrecognized command '%s'",
userCommandString)
}

vlog.LogInfo("Recognized command: %s\n", userCommandString)
log.Log.Info("Recognized command", "cmd", userCommandString)
return command, nil
}

Expand All @@ -188,3 +194,12 @@ func checkMinimumInput(inputArgv []string) error {
minArgs,
len(inputArgv))
}

func parseLogPathArg(argInput []string, defaultPath string) (logPath string) {
for idx, arg := range argInput {
if arg == "--log-path" {
return argInput[idx+1]
}
}
return defaultPath
}
23 changes: 10 additions & 13 deletions commands/cmd_add_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ func (c *CmdAddNode) CommandType() string {
return "db_add_node"
}

func (c *CmdAddNode) Parse(inputArgv []string) error {
func (c *CmdAddNode) Parse(inputArgv []string, log vlog.Printer) error {
c.argv = inputArgv
err := c.ValidateParseArgv(c.CommandType())
err := c.ValidateParseArgv(c.CommandType(), log)
if err != nil {
return err
}
Expand All @@ -105,11 +105,11 @@ func (c *CmdAddNode) Parse(inputArgv []string) error {
if !util.IsOptionSet(c.parser, "eon-mode") {
c.CmdBase.isEon = nil
}
return c.validateParse()
return c.validateParse(log)
}

func (c *CmdAddNode) validateParse() error {
vlog.LogInfoln("Called validateParse()")
func (c *CmdAddNode) validateParse(log vlog.Printer) error {
log.Info("Called validateParse()")

err := c.parseNewHostList()
if err != nil {
Expand Down Expand Up @@ -150,20 +150,17 @@ func (c *CmdAddNode) parseNodeNameList() error {
return nil
}

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

func (c *CmdAddNode) Run(log vlog.Printer) error {
vcc := vclusterops.VClusterCommands{
Log: log.WithName(c.CommandType()),
}
func (c *CmdAddNode) Run(vcc vclusterops.VClusterCommands) error {
vcc.Log.V(1).Info("Called method Run()")

options := c.addNodeOptions

// get config from vertica_cluster.yaml
config, err := options.GetDBConfig()
config, err := options.GetDBConfig(vcc)
if err != nil {
return err
}
Expand All @@ -174,9 +171,9 @@ func (c *CmdAddNode) Run(log vlog.Printer) error {
return addNodeError
}
// write cluster information to the YAML config file
err = vdb.WriteClusterConfig(options.ConfigDirectory)
err = vdb.WriteClusterConfig(options.ConfigDirectory, vcc.Log)
if err != nil {
vlog.LogPrintWarning("fail to write config file, details: %s", err)
vcc.Log.PrintWarning("fail to write config file, details: %s", err)
}
vcc.Log.PrintInfo("Added nodes %s to database %s", *c.newHostListStr, *options.DBName)
return nil
Expand Down
23 changes: 10 additions & 13 deletions commands/cmd_add_subcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ func (c *CmdAddSubcluster) CommandType() string {
return "db_add_subcluster"
}

func (c *CmdAddSubcluster) Parse(inputArgv []string) error {
func (c *CmdAddSubcluster) Parse(inputArgv []string, log vlog.Printer) error {
c.argv = inputArgv
err := c.ValidateParseArgv(c.CommandType())
err := c.ValidateParseArgv(c.CommandType(), log)
if err != nil {
return err
}
Expand All @@ -110,30 +110,27 @@ func (c *CmdAddSubcluster) Parse(inputArgv []string) error {
c.addSubclusterOptions.ConfigDirectory = nil
}

return c.validateParse()
return c.validateParse(log)
}

// all validations of the arguments should go in here
func (c *CmdAddSubcluster) validateParse() error {
vlog.LogInfoln("Called validateParse()")
func (c *CmdAddSubcluster) validateParse(log vlog.Printer) error {
log.Info("Called validateParse()")
return c.ValidateParseBaseOptions(&c.addSubclusterOptions.DatabaseOptions)
}

func (c *CmdAddSubcluster) Analyze() error {
vlog.LogInfoln("Called method Analyze()")
func (c *CmdAddSubcluster) Analyze(log vlog.Printer) error {
log.Info("Called method Analyze()")
return nil
}

func (c *CmdAddSubcluster) Run(log vlog.Printer) error {
vcc := vclusterops.VClusterCommands{
Log: log.WithName(c.CommandType()),
}
func (c *CmdAddSubcluster) Run(vcc vclusterops.VClusterCommands) error {
vcc.Log.V(1).Info("Called method Run()")

options := c.addSubclusterOptions

// get config from vertica_cluster.yaml
config, err := options.GetDBConfig()
config, err := options.GetDBConfig(vcc)
if err != nil {
return err
}
Expand All @@ -145,6 +142,6 @@ func (c *CmdAddSubcluster) Run(log vlog.Printer) error {
return err
}

vlog.LogPrintInfo("Added subcluster %s to database %s", *options.SCName, *options.DBName)
vcc.Log.PrintInfo("Added subcluster %s to database %s", *options.SCName, *options.DBName)
return nil
}
8 changes: 4 additions & 4 deletions commands/cmd_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ func (c *CmdBase) ParseArgv() error {
}

// validate and parse argv
func (c *CmdBase) ValidateParseArgv(commandType string) error {
vlog.LogArgParse(&c.argv)
func (c *CmdBase) ValidateParseArgv(commandType string, log vlog.Printer) error {
log.LogArgParse(&c.argv)
return c.ValidateParseArgvHelper(commandType)
}

// validate and parse masked argv
// Some database actions, such as createDB and reviveDB, need to mask sensitive parameters in the log
func (c *CmdBase) ValidateParseMaskedArgv(commandType string) error {
vlog.LogMaskedArgParse(c.argv)
func (c *CmdBase) ValidateParseMaskedArgv(commandType string, log vlog.Printer) error {
log.LogMaskedArgParse(c.argv)
return c.ValidateParseArgvHelper(commandType)
}

Expand Down
17 changes: 8 additions & 9 deletions commands/cmd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func (c *CmdConfig) CommandType() string {
return "config"
}

func (c *CmdConfig) Parse(inputArgv []string) error {
vlog.LogArgParse(&inputArgv)
func (c *CmdConfig) Parse(inputArgv []string, log vlog.Printer) error {
log.LogArgParse(&inputArgv)

if c.parser == nil {
return fmt.Errorf("unexpected nil - the parser was nil")
Expand All @@ -68,28 +68,27 @@ func (c *CmdConfig) Parse(inputArgv []string) error {
return err
}

return c.validateParse()
return c.validateParse(log)
}

func (c *CmdConfig) validateParse() error {
vlog.LogInfoln("Called validateParse()")

func (c *CmdConfig) validateParse(log vlog.Printer) error {
log.Info("Called validateParse()")
// if directory is not provided, then use the current directory
return c.validateDirectory()
}

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

func (c *CmdConfig) Run(_ vlog.Printer) error {
func (c *CmdConfig) Run(vcc vclusterops.VClusterCommands) error {
if *c.show {
configFilePath := filepath.Join(*c.directory, vclusterops.ConfigFileName)
fileBytes, err := os.ReadFile(configFilePath)
if err != nil {
return fmt.Errorf("fail to read config file, details: %w", err)
}
vlog.LogPrintInfo("Content of the config file:\n%s", string(fileBytes))
vcc.Log.PrintInfo("Content of the config file:\n%s", string(fileBytes))
}

return nil
Expand Down
Loading

0 comments on commit fb3153c

Please sign in to comment.