Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[commands] move Run behavior from cobra.RunE to options.Run() method #20

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions cmd/maintenance/complete/complete.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package complete

import (
"fmt"

"github.com/spf13/cobra"

"github.com/ydb-platform/ydbops/pkg/cli"
"github.com/ydb-platform/ydbops/pkg/cmdutil"
"github.com/ydb-platform/ydbops/pkg/prettyprint"
)

func New(f cmdutil.Factory) *cobra.Command {
Expand All @@ -23,14 +20,7 @@ func New(f cmdutil.Factory) *cobra.Command {
f.GetBaseOptions(), opts,
),
RunE: func(cmd *cobra.Command, args []string) error {
result, err := f.GetCMSClient().CompleteActions(opts.TaskID, opts.HostFQDNs)
if err != nil {
return err
}

fmt.Println(prettyprint.ResultToString(result))

return nil
return opts.Run(f)
},
})

Expand Down
13 changes: 13 additions & 0 deletions cmd/maintenance/complete/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"

"github.com/spf13/pflag"

"github.com/ydb-platform/ydbops/pkg/cmdutil"
"github.com/ydb-platform/ydbops/pkg/prettyprint"
)

type Options struct {
Expand All @@ -28,3 +31,13 @@ func (o *Options) Validate() error {
}
return nil
}

func (o *Options) Run(f cmdutil.Factory) error {
result, err := f.GetCMSClient().CompleteActions(o.TaskID, o.HostFQDNs)
if err != nil {
return err
}

fmt.Println(prettyprint.ResultToString(result))
return nil
}
26 changes: 1 addition & 25 deletions cmd/maintenance/create/create.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package create

import (
"fmt"
"time"

"github.com/google/uuid"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/durationpb"

"github.com/ydb-platform/ydbops/pkg/cli"
"github.com/ydb-platform/ydbops/pkg/client/cms"
"github.com/ydb-platform/ydbops/pkg/cmdutil"
"github.com/ydb-platform/ydbops/pkg/rolling"
)
Expand All @@ -28,25 +22,7 @@ func New(f cmdutil.Factory) *cobra.Command {
f.GetBaseOptions(), opts,
),
RunE: func(cmd *cobra.Command, args []string) error {
taskUID := cms.TaskUuidPrefix + uuid.New().String()
duration := time.Duration(opts.RestartOptions.RestartDuration) * time.Minute
taskId, err := f.GetCMSClient().CreateMaintenanceTask(cms.MaintenanceTaskParams{
Hosts: opts.RestartOptions.Hosts,
Duration: durationpb.New(duration),
AvailabilityMode: opts.RestartOptions.GetAvailabilityMode(),
ScopeType: cms.HostScope,
TaskUID: taskUID,
})
if err != nil {
return err
}

fmt.Printf(
"Your task id is:\n\n%s\n\nPlease write it down for refreshing and completing the task later.\n",
taskId.GetTaskUid(),
)

return nil
return opts.Run(f)
},
})

Expand Down
29 changes: 29 additions & 0 deletions cmd/maintenance/create/options.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package create

import (
"fmt"
"time"

"github.com/google/uuid"
"github.com/spf13/pflag"
"google.golang.org/protobuf/types/known/durationpb"

"github.com/ydb-platform/ydbops/pkg/client/cms"
"github.com/ydb-platform/ydbops/pkg/cmdutil"
"github.com/ydb-platform/ydbops/pkg/rolling"
)

Expand All @@ -17,3 +24,25 @@ func (o *Options) DefineFlags(fs *pflag.FlagSet) {
func (o *Options) Validate() error {
return o.RestartOptions.Validate()
}

func (o *Options) Run(f cmdutil.Factory) error {
taskUID := cms.TaskUuidPrefix + uuid.New().String()
duration := time.Duration(o.RestartOptions.RestartDuration) * time.Minute
taskId, err := f.GetCMSClient().CreateMaintenanceTask(cms.MaintenanceTaskParams{
Hosts: o.RestartOptions.Hosts,
Duration: durationpb.New(duration),
AvailabilityMode: o.RestartOptions.GetAvailabilityMode(),
ScopeType: cms.HostScope,
TaskUID: taskUID,
})
if err != nil {
return err
}

fmt.Printf(
"Your task id is:\n\n%s\n\nPlease write it down for refreshing and completing the task later.\n",
taskId.GetTaskUid(),
)

return nil
}
2 changes: 1 addition & 1 deletion cmd/maintenance/drop/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func New(f cmdutil.Factory) *cobra.Command {
f.GetBaseOptions(), taskIdOpts,
),
RunE: func(cmd *cobra.Command, args []string) error {
return f.GetCMSClient().DropTask(taskIdOpts.TaskID)
return taskIdOpts.Run(f)
},
})

Expand Down
6 changes: 6 additions & 0 deletions cmd/maintenance/drop/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"

"github.com/spf13/pflag"

"github.com/ydb-platform/ydbops/pkg/cmdutil"
)

type Options struct {
Expand All @@ -21,3 +23,7 @@ func (o *Options) Validate() error {
}
return nil
}

func (o *Options) Run(f cmdutil.Factory) error {
return f.GetCMSClient().DropTask(o.TaskID)
}
26 changes: 2 additions & 24 deletions cmd/maintenance/list/list.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package list

import (
"fmt"

"github.com/spf13/cobra"

"github.com/ydb-platform/ydbops/pkg/cli"
"github.com/ydb-platform/ydbops/pkg/cmdutil"
"github.com/ydb-platform/ydbops/pkg/prettyprint"
)

func New(f cmdutil.Factory) *cobra.Command {
_ = &Options{}
opts := &Options{}

cmd := cli.SetDefaultsOn(&cobra.Command{
Use: "list",
Expand All @@ -23,26 +20,7 @@ func New(f cmdutil.Factory) *cobra.Command {
f.GetBaseOptions(),
),
RunE: func(cmd *cobra.Command, args []string) error {
userSID, err := f.GetDiscoveryClient().WhoAmI()
if err != nil {
return err
}

tasks, err := f.GetCMSClient().MaintenanceTasks(userSID)
if err != nil {
return err
}

if len(tasks) == 0 {
fmt.Println("There are no maintenance tasks, associated with your user, at the moment.")
} else {
for _, task := range tasks {
taskInfo := prettyprint.TaskToString(task)
fmt.Println(taskInfo)
}
}

return nil
return opts.Run(f)
},
})

Expand Down
30 changes: 30 additions & 0 deletions cmd/maintenance/list/options.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
package list

import (
"fmt"

"github.com/ydb-platform/ydbops/pkg/cmdutil"
"github.com/ydb-platform/ydbops/pkg/prettyprint"
)

type Options struct{}

func (o *Options) Run(f cmdutil.Factory) error {
userSID, err := f.GetDiscoveryClient().WhoAmI()
if err != nil {
return err
}

tasks, err := f.GetCMSClient().MaintenanceTasks(userSID)
if err != nil {
return err
}

if len(tasks) == 0 {
fmt.Println("There are no maintenance tasks, associated with your user, at the moment.")
} else {
for _, task := range tasks {
taskInfo := prettyprint.TaskToString(task)
fmt.Println(taskInfo)
}
}

return nil
}
14 changes: 14 additions & 0 deletions cmd/maintenance/refresh/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"

"github.com/spf13/pflag"

"github.com/ydb-platform/ydbops/pkg/cmdutil"
"github.com/ydb-platform/ydbops/pkg/prettyprint"
)

type Options struct {
Expand All @@ -21,3 +24,14 @@ func (o *Options) Validate() error {
}
return nil
}

func (o *Options) Run(f cmdutil.Factory) error {
task, err := f.GetCMSClient().RefreshTask(o.TaskID)
if err != nil {
return err
}

fmt.Println(prettyprint.TaskToString(task))

return nil
}
12 changes: 1 addition & 11 deletions cmd/maintenance/refresh/refresh.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package refresh

import (
"fmt"

"github.com/spf13/cobra"

"github.com/ydb-platform/ydbops/pkg/cli"
"github.com/ydb-platform/ydbops/pkg/cmdutil"
"github.com/ydb-platform/ydbops/pkg/prettyprint"
)

func New(f cmdutil.Factory) *cobra.Command {
Expand All @@ -22,14 +19,7 @@ func New(f cmdutil.Factory) *cobra.Command {
f.GetBaseOptions(), taskIdOpts,
),
RunE: func(cmd *cobra.Command, args []string) error {
task, err := f.GetCMSClient().RefreshTask(taskIdOpts.TaskID)
if err != nil {
return err
}

fmt.Println(prettyprint.TaskToString(task))

return nil
return taskIdOpts.Run(f)
},
})

Expand Down
66 changes: 66 additions & 0 deletions cmd/restart/options.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package restart

import (
"time"

"github.com/spf13/pflag"
"go.uber.org/zap"

"github.com/ydb-platform/ydbops/pkg/cmdutil"
"github.com/ydb-platform/ydbops/pkg/options"
"github.com/ydb-platform/ydbops/pkg/rolling"
"github.com/ydb-platform/ydbops/pkg/rolling/restarters"
)

type Options struct {
Expand All @@ -13,3 +19,63 @@ type Options struct {
func (o *Options) DefineFlags(fs *pflag.FlagSet) {
o.RestartOptions.DefineFlags(fs)
}

func (o *Options) Run(f cmdutil.Factory) error {
var storageRestarter restarters.Restarter
var tenantRestarter restarters.Restarter

if o.RestartOptions.KubeconfigPath != "" {
storageRestarter = restarters.NewStorageK8sRestarter(
options.Logger,
&restarters.StorageK8sRestarterOptions{
K8sRestarterOptions: &restarters.K8sRestarterOptions{
KubeconfigPath: o.RestartOptions.KubeconfigPath,
Namespace: o.RestartOptions.K8sNamespace,
RestartDuration: time.Duration(o.RestartOptions.RestartDuration) * time.Second,
},
},
)
tenantRestarter = restarters.NewTenantK8sRestarter(
options.Logger,
&restarters.TenantK8sRestarterOptions{
K8sRestarterOptions: &restarters.K8sRestarterOptions{
KubeconfigPath: o.RestartOptions.KubeconfigPath,
Namespace: o.RestartOptions.K8sNamespace,
RestartDuration: time.Duration(o.RestartOptions.RestartDuration) * time.Second,
},
},
)
} else {
storageRestarter = restarters.NewStorageSSHRestarter(
options.Logger,
o.RestartOptions.SSHArgs,
o.RestartOptions.CustomSystemdUnitName,
)
tenantRestarter = restarters.NewTenantSSHRestarter(
options.Logger,
o.RestartOptions.SSHArgs,
o.RestartOptions.CustomSystemdUnitName,
)
}

bothUnspecified := !o.RestartOptions.Storage && !o.RestartOptions.Tenant

var executer rolling.Executer
var err error
if o.RestartOptions.Storage || bothUnspecified {
// TODO(shmel1k@): add logger to NewExecuter parameters
executer = rolling.NewExecuter(o.RestartOptions, zap.S(), f.GetCMSClient(), f.GetDiscoveryClient(), storageRestarter)
err = executer.Execute()
}

if err != nil {
return err
}

if o.RestartOptions.Tenant || bothUnspecified {
executer = rolling.NewExecuter(o.RestartOptions, zap.S(), f.GetCMSClient(), f.GetDiscoveryClient(), tenantRestarter)
err = executer.Execute()
}

return err
}
Loading
Loading