Skip to content

Commit

Permalink
Merge pull request #9 from shmel1k/feature/configurable_parameters_fo…
Browse files Browse the repository at this point in the history
…r_commands

Feature/configurable parameters for commands
  • Loading branch information
shmel1k authored Jul 4, 2024
2 parents 06eba36 + c0debac commit 183037d
Show file tree
Hide file tree
Showing 61 changed files with 1,720 additions and 1,275 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ydbops
vendor
31 changes: 0 additions & 31 deletions cmd/maintenance.go

This file was deleted.

60 changes: 0 additions & 60 deletions cmd/maintenance/complete.go

This file was deleted.

40 changes: 40 additions & 0 deletions cmd/maintenance/complete/complete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 {
opts := &Options{}

cmd := cli.SetDefaultsOn(&cobra.Command{
Use: "complete",
Short: "Declare the maintenance task completed",
Long: `ydbops maintenance complete:
Any hosts that have been given to you within the task will be considered returned to the cluster.
You must not perform any host maintenance after you called this command.`,
PreRunE: cli.PopulateProfileDefaultsAndValidate(
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
},
})

opts.DefineFlags(cmd.PersistentFlags())

return cmd
}
30 changes: 30 additions & 0 deletions cmd/maintenance/complete/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package complete

import (
"fmt"

"github.com/spf13/pflag"
)

type Options struct {
TaskID string
HostFQDNs []string
}

func (o *Options) DefineFlags(fs *pflag.FlagSet) {
fs.StringSliceVar(&o.HostFQDNs, "hosts", []string{},
"FQDNs of hosts with completed maintenance")
fs.StringVar(&o.TaskID, "task-id", "",
"ID of your maintenance task (result of `ydbops maintenance host`)")
}

func (o *Options) Validate() error {
// TODO(shmel1k@): remove copypaste between drop, create & refresh methods.
if len(o.HostFQDNs) == 0 {
return fmt.Errorf("--hosts unspecified")
}
if o.TaskID == "" {
return fmt.Errorf("--task-id unspecified, argument required")
}
return nil
}
58 changes: 0 additions & 58 deletions cmd/maintenance/create.go

This file was deleted.

56 changes: 56 additions & 0 deletions cmd/maintenance/create/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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"
)

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

cmd := cli.SetDefaultsOn(&cobra.Command{
Use: "create",
Short: "Create a maintenance task to obtain a set of hosts",
Long: `ydbops maintenance create:
Create a maintenance task, which allows taking the set of hosts out of the cluster.`,
PreRunE: cli.PopulateProfileDefaultsAndValidate(
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
},
})

opts.DefineFlags(cmd.PersistentFlags())

return cmd
}
18 changes: 18 additions & 0 deletions cmd/maintenance/create/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package create

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)
}

func (o *Options) Validate() error {
return o.RestartOptions.Validate()
}
53 changes: 0 additions & 53 deletions cmd/maintenance/drop.go

This file was deleted.

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

import (
"github.com/spf13/cobra"

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

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

cmd := cli.SetDefaultsOn(&cobra.Command{
Use: "drop",
Short: "Drop an existing maintenance task",
Long: `ydbops maintenance drop:
Drops the maintenance task, meaning two things:
1. Any hosts given within the maintenance task will be considered returned.
2. Any hosts requested, but not yet given, will not be reserved for you any longer.`,
PreRunE: cli.PopulateProfileDefaultsAndValidate(
f.GetBaseOptions(), taskIdOpts,
),
RunE: func(cmd *cobra.Command, args []string) error {
return f.GetCMSClient().DropTask(taskIdOpts.TaskID)
},
})

taskIdOpts.DefineFlags(cmd.PersistentFlags())

return cmd
}
Loading

0 comments on commit 183037d

Please sign in to comment.