-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[commands] refactor commands creation
- Loading branch information
Showing
44 changed files
with
1,375 additions
and
960 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.