Skip to content

Commit

Permalink
[commands] finish refactoring with working tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shmel1k committed Jun 27, 2024
1 parent d175a2b commit f183813
Show file tree
Hide file tree
Showing 16 changed files with 296 additions and 243 deletions.
16 changes: 8 additions & 8 deletions cmd/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import (

type MaintenanceCommand struct {
*command.Base
description *command.Description
preRunCallback cli.PreRunCallback
commandOptions *options.RestartOptions
cobraCommand *cobra.Command
}

func (r *MaintenanceCommand) RegisterSubcommands(c ...command.Command) {
for _, v := range c {
v.RegisterOptions()
cli.SetDefaultsOn(v.ToCobraCommand())
r.ToCobraCommand().AddCommand(v.ToCobraCommand())
}
}

func (r *MaintenanceCommand) RegisterOptions() {
Expand All @@ -36,23 +39,20 @@ func (r *MaintenanceCommand) ToCobraCommand() *cobra.Command {
PreRunE: cli.PopulateProfileDefaultsAndValidate(
r.Base.GetBaseOptions(), r.commandOptions,
),
RunE: cli.RequireSubcommand,
RunE: r.RunCallback(),
})
return cmd
r.cobraCommand = cmd
return r.cobraCommand
}

func (r *MaintenanceCommand) RunCallback() func(*cobra.Command, []string) error {
return cli.RequireSubcommand
}

func NewMaintenanceCommand(
description *command.Description,
base *command.Base,
preRunCallback cli.PreRunCallback, // TODO(shmel1k@): change to validation callback name or smth
) command.Command {
return &MaintenanceCommand{
description: description,
preRunCallback: preRunCallback,
commandOptions: &options.RestartOptions{},
Base: base,
}
Expand Down
22 changes: 5 additions & 17 deletions cmd/maintenance/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,26 @@ import (
"github.com/spf13/cobra"

"github.com/ydb-platform/ydbops/pkg/cli"
"github.com/ydb-platform/ydbops/pkg/client"
"github.com/ydb-platform/ydbops/pkg/cmdutil"
"github.com/ydb-platform/ydbops/pkg/command"
"github.com/ydb-platform/ydbops/pkg/maintenance"
"github.com/ydb-platform/ydbops/pkg/options"
)

func NewHostCommand(
description *command.Description,
rootCommand *command.Base,
preRunCallback cli.PreRunCallback,
) command.Command {
return &HostCommand{
Base: rootCommand,
preRunCallback: preRunCallback,
commandOptions: &options.MaintenanceHostOpts{},
}
}

type HostCommand struct {
*command.Base
description *command.Description
preRunCallback cli.PreRunCallback
commandOptions *options.MaintenanceHostOpts
cobraCommand *cobra.Command
f cmdutil.Factory
}

func (h *HostCommand) ToCobraCommand() *cobra.Command {
Expand All @@ -42,7 +38,8 @@ func (h *HostCommand) ToCobraCommand() *cobra.Command {
Short: "Request host from the CMS (Cluster Management System)",
Long: `ydbops maintenance host:
Make a request to take the host out of the cluster.`,
RunE: h.RunCallback(),
RunE: h.RunCallback(),
PreRunE: cli.PopulateProfileDefaultsAndValidate(h.GetBaseOptions(), h.commandOptions),
}

return h.cobraCommand
Expand All @@ -61,16 +58,7 @@ func (h *HostCommand) RegisterSubcommands(c ...command.Command) {

func (h *HostCommand) RunCallback() func(*cobra.Command, []string) error {
return func(_ *cobra.Command, _ []string) error {
err := client.InitConnectionFactory(
*h.GetBaseOptions(),
options.Logger,
options.DefaultRetryCount,
)
if err != nil {
return err
}

taskId, err := maintenance.RequestHost(h.commandOptions)
taskId, err := maintenance.RequestHost(h.f.GetCMSClient(), h.commandOptions)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ type RestartCommand struct {
func NewRestartCommand(
description *command.Description,
rootCommand *command.Base,
preRunCallback cli.PreRunCallback,
f cmdutil.Factory,
) command.Command {
return &RestartCommand{
description: description,
preRunCallback: preRunCallback,
preRunCallback: cli.PopulateProfileDefaultsAndValidate,
commandOptions: &options.RestartOptions{},
Base: rootCommand,
}
Expand Down
21 changes: 12 additions & 9 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,29 @@ import (
"github.com/ydb-platform/ydbops/pkg/options"
"github.com/ydb-platform/ydbops/pkg/rolling"
"github.com/ydb-platform/ydbops/pkg/rolling/restarters"
"go.uber.org/zap"
)

type RunCommand struct {
*command.Base
description *command.Description
commandOptions *options.RunOptions
cobraCommand *cobra.Command
restarter *restarters.RunRestarter // TODO(shmel1k@): move to restarter interface.
f cmdutil.Factory
}

func NewRunCommand(
description *command.Description,
rootCommand *command.Base,
restarter *restarters.RunRestarter,
f cmdutil.Factory,
) command.Command {
return &RunCommand{
description: description,
commandOptions: &options.RunOptions{
RestartOptions: &options.RestartOptions{},
}, // TODO(shmel1k@): remove from options package.
restarter: restarters.NewRunRestarter(options.Logger), // TODO(shmel1k@): remove link to global variable
Base: rootCommand,
Base: rootCommand,
f: f,
}
}

Expand All @@ -55,17 +54,21 @@ func (r *RunCommand) RunCallback() func(*cobra.Command, []string) error {

bothUnspecified := !r.commandOptions.Storage && !r.commandOptions.Tenant

restarter := restarters.NewRunRestarter(zap.S(), &restarters.RunRestarterParams{
PayloadFilePath: r.commandOptions.PayloadFilePath,
})

var executer rolling.Executer
var err error
if r.commandOptions.Storage || bothUnspecified {
r.restarter.SetStorageOnly()
executer = rolling.NewExecuter(*r.commandOptions.RestartOptions, options.Logger, r.f.GetCMSClient(), r.f.GetDiscoveryClient(), r.restarter)
restarter.SetStorageOnly()
executer = rolling.NewExecuter(*r.commandOptions.RestartOptions, options.Logger, r.f.GetCMSClient(), r.f.GetDiscoveryClient(), restarter)
err = executer.Execute()
}

if err == nil && (r.commandOptions.Tenant || bothUnspecified) {
r.restarter.SetDynnodeOnly()
executer = rolling.NewExecuter(*r.commandOptions.RestartOptions, options.Logger, r.f.GetCMSClient(), r.f.GetDiscoveryClient(), r.restarter)
restarter.SetDynnodeOnly()
executer = rolling.NewExecuter(*r.commandOptions.RestartOptions, options.Logger, r.f.GetCMSClient(), r.f.GetDiscoveryClient(), restarter)
err = executer.Execute()
}

Expand All @@ -81,7 +84,7 @@ func (r *RunCommand) ToCobraCommand() *cobra.Command {
Use: r.description.GetUse(),
Short: r.description.GetShortDescription(),
Long: r.description.GetLongDescription(),
PreRunE: cli.PopulateProfileDefaultsAndValidate(r.GetBaseOptions(), r.restarter.Opts),
PreRunE: cli.PopulateProfileDefaultsAndValidate(r.GetBaseOptions(), r.commandOptions),
RunE: r.RunCallback(),
}
return r.cobraCommand
Expand Down
116 changes: 0 additions & 116 deletions examples/overridden_restarter/main.go

This file was deleted.

Loading

0 comments on commit f183813

Please sign in to comment.