Skip to content

Commit

Permalink
[commands] refactor commands creation
Browse files Browse the repository at this point in the history
  • Loading branch information
shmel1k committed Jul 3, 2024
1 parent c5026f7 commit 6113c53
Show file tree
Hide file tree
Showing 44 changed files with 1,375 additions and 960 deletions.
31 changes: 0 additions & 31 deletions cmd/maintenance.go

This file was deleted.

58 changes: 0 additions & 58 deletions cmd/maintenance/host.go

This file was deleted.

42 changes: 42 additions & 0 deletions cmd/maintenance/host/host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package host

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/command"
"github.com/ydb-platform/ydbops/pkg/maintenance"
)

func NewHostCommand(f cmdutil.Factory) *cobra.Command {
opts := &MaintenanceHostOpts{
BaseOptions: &command.BaseOptions{},
}
return &cobra.Command{
Use: "host",
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: func(_ *cobra.Command, _ []string) error {
taskId, err := maintenance.RequestHost(f.GetCMSClient(), &maintenance.RequestHostParams{
AvailabilityMode: opts.GetAvailabilityMode(),
MaintenanceDuration: opts.GetMaintenanceDuration(),
HostFQDN: opts.HostFQDN,
})
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,
)

return nil
},
PreRunE: cli.PopulateProfileDefaultsAndValidate(opts.BaseOptions, opts),
}
}
59 changes: 59 additions & 0 deletions cmd/maintenance/host/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package host

import (
"fmt"
"strings"
"time"

"github.com/spf13/pflag"
"github.com/ydb-platform/ydb-go-genproto/draft/protos/Ydb_Maintenance"
"github.com/ydb-platform/ydbops/internal/collections"
"github.com/ydb-platform/ydbops/pkg/command"
"github.com/ydb-platform/ydbops/pkg/options"
"google.golang.org/protobuf/types/known/durationpb"
)

type MaintenanceHostOpts struct {
*command.BaseOptions
HostFQDN string
MaintenanceDuration int
AvailabilityMode string
}

const (
DefaultMaintenanceDurationSeconds = 3600
)

func (o *MaintenanceHostOpts) DefineFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.HostFQDN, "host-fqdn", "",
"Request the host with this FQDN from the cluster")

fs.StringVar(&o.AvailabilityMode, "availability-mode", "strong",
fmt.Sprintf("Availability mode. Available choices: %s", strings.Join(options.AvailabilityModes, ", ")))

fs.IntVar(&o.MaintenanceDuration, "duration", DefaultMaintenanceDurationSeconds,
fmt.Sprintf("The time you need to perform host maintenance, in seconds. Default: %v",
DefaultMaintenanceDurationSeconds))
}

func (o *MaintenanceHostOpts) GetMaintenanceDuration() *durationpb.Duration {
return durationpb.New(time.Second * time.Duration(o.MaintenanceDuration))
}

func (o *MaintenanceHostOpts) Validate() error {
if !collections.Contains(options.AvailabilityModes, o.AvailabilityMode) {
return fmt.Errorf("specified a non-existing availability mode: %s", o.AvailabilityMode)
}

if o.MaintenanceDuration < 0 {
return fmt.Errorf("specified invalid maintenance duration seconds: %d. Must be positive", o.MaintenanceDuration)
}
return nil
}

func (o *MaintenanceHostOpts) GetAvailabilityMode() Ydb_Maintenance.AvailabilityMode {
title := strings.ToUpper(fmt.Sprintf("availability_mode_%s", o.AvailabilityMode))
value := Ydb_Maintenance.AvailabilityMode_value[title]

return Ydb_Maintenance.AvailabilityMode(value)
}
32 changes: 32 additions & 0 deletions cmd/maintenance/maintenance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package maintenance

import (
"github.com/spf13/cobra"
"github.com/ydb-platform/ydbops/cmd/maintenance/host"
"github.com/ydb-platform/ydbops/pkg/cli"
"github.com/ydb-platform/ydbops/pkg/cmdutil"
"github.com/ydb-platform/ydbops/pkg/command"
)

type Options struct {
*command.BaseOptions
}

func New(f cmdutil.Factory) *cobra.Command {
options := &Options{}
c := cli.SetDefaultsOn(&cobra.Command{
Use: "maintenance",
Short: "Request hosts from the Cluster Management System",
Long: `ydbops maintenance [command]:
Manage host maintenance operations: request and return hosts
with performed maintenance back to the cluster.`,
PreRunE: cli.PopulateProfileDefaultsAndValidate(
options.BaseOptions, options,
),
RunE: cli.RequireSubcommand,
})

c.AddCommand(host.NewHostCommand(f))

return c
}
89 changes: 0 additions & 89 deletions cmd/restart.go

This file was deleted.

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

import (
"github.com/spf13/pflag"
"github.com/ydb-platform/ydbops/pkg/rolling"
)

type Options struct {
*rolling.RestartOptions
}

func (o *Options) DefineFlags(fs *pflag.FlagSet) {
o.RestartOptions.DefineFlags(fs)
}
Loading

0 comments on commit 6113c53

Please sign in to comment.