From fb6636b5efef95febc8b2d8c3677beeb90a4e1c7 Mon Sep 17 00:00:00 2001 From: Maksim Zinal Date: Wed, 12 Mar 2025 12:53:59 +0300 Subject: [PATCH 1/2] added the command --- .../unreleased/Added-20250312-124859.yaml | 3 ++ .gitignore | 1 + cmd/nodes/list/list.go | 29 ++++++++++++ cmd/nodes/list/options.go | 44 +++++++++++++++++++ cmd/nodes/nodes.go | 25 +++++++++++ cmd/root.go | 2 + 6 files changed, 104 insertions(+) create mode 100644 .changes/unreleased/Added-20250312-124859.yaml create mode 100644 cmd/nodes/list/list.go create mode 100644 cmd/nodes/list/options.go create mode 100644 cmd/nodes/nodes.go diff --git a/.changes/unreleased/Added-20250312-124859.yaml b/.changes/unreleased/Added-20250312-124859.yaml new file mode 100644 index 0000000..5740264 --- /dev/null +++ b/.changes/unreleased/Added-20250312-124859.yaml @@ -0,0 +1,3 @@ +kind: Added +body: New command `ydbops nodes list` +time: 2025-03-12T12:48:59.010415078+03:00 diff --git a/.gitignore b/.gitignore index 40b69df..0754367 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ ydbops vendor bin +/.vscode/ \ No newline at end of file diff --git a/cmd/nodes/list/list.go b/cmd/nodes/list/list.go new file mode 100644 index 0000000..9d0a2dd --- /dev/null +++ b/cmd/nodes/list/list.go @@ -0,0 +1,29 @@ +package list + +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 { + opts := &Options{} + + cmd := cli.SetDefaultsOn(&cobra.Command{ + Use: "list", + Short: "List the cluster nodes", + Long: `ydbops cluster list: + Obtain the list of cluster nodes from the Configuration Management System.`, + PreRunE: cli.PopulateProfileDefaultsAndValidate( + f.GetBaseOptions(), opts, + ), + RunE: func(cmd *cobra.Command, args []string) error { + return opts.Run(f) + }, + }) + + opts.DefineFlags(cmd.PersistentFlags()) + + return cmd +} diff --git a/cmd/nodes/list/options.go b/cmd/nodes/list/options.go new file mode 100644 index 0000000..b09b24b --- /dev/null +++ b/cmd/nodes/list/options.go @@ -0,0 +1,44 @@ +package list + +import ( + "fmt" + + "github.com/spf13/pflag" + + "github.com/ydb-platform/ydbops/pkg/cmdutil" +) + +type Options struct { +} + +const ( + DefaultMaintenanceDurationSeconds = 3600 +) + +func (o *Options) DefineFlags(fs *pflag.FlagSet) { +} + +func (o *Options) Validate() error { + return nil +} + +func (o *Options) Run(f cmdutil.Factory) error { + nodes, err := f.GetCMSClient().Nodes() + if err != nil { + return err + } + + fmt.Printf("--- begin nodes ---\n") + for _, node := range nodes { + nodeType := "UNKNOWN" + if node.GetStorage() != nil { + nodeType = "STORAGE" + } else if node.GetDynamic() != nil { + nodeType = "DATABASE" + } + fmt.Printf("%d\t%s:%d\t%s\n", node.GetNodeId(), node.GetHost(), node.GetPort(), nodeType) + } + fmt.Printf("--- end nodes ---\n") + + return nil +} diff --git a/cmd/nodes/nodes.go b/cmd/nodes/nodes.go new file mode 100644 index 0000000..81c04e4 --- /dev/null +++ b/cmd/nodes/nodes.go @@ -0,0 +1,25 @@ +package cluster + +import ( + "github.com/spf13/cobra" + + "github.com/ydb-platform/ydbops/cmd/nodes/list" + "github.com/ydb-platform/ydbops/pkg/cli" + "github.com/ydb-platform/ydbops/pkg/cmdutil" +) + +func New(f cmdutil.Factory) *cobra.Command { + cmd := cli.SetDefaultsOn(&cobra.Command{ + Use: "nodes", + Short: "Node operations with the Cluster Management System", + Long: `ydbops nodes [command]: + Perform node operations.`, + RunE: cli.RequireSubcommand, + }) + + cmd.AddCommand( + list.New(f), + ) + + return cmd +} diff --git a/cmd/root.go b/cmd/root.go index 1e9e248..a6d0817 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,6 +10,7 @@ import ( "go.uber.org/zap/zapcore" "github.com/ydb-platform/ydbops/cmd/maintenance" + nodes "github.com/ydb-platform/ydbops/cmd/nodes" "github.com/ydb-platform/ydbops/cmd/restart" "github.com/ydb-platform/ydbops/cmd/run" "github.com/ydb-platform/ydbops/cmd/version" @@ -95,6 +96,7 @@ func NewRootCommand( func InitRootCommandTree(root *cobra.Command, f cmdutil.Factory) { root.AddCommand( restart.New(f), + nodes.New(f), maintenance.New(f), run.New(f), version.New(), From f60b67aae7519369e02e436f111cd82d5730294c Mon Sep 17 00:00:00 2001 From: Maksim Zinal Date: Wed, 12 Mar 2025 12:56:14 +0300 Subject: [PATCH 2/2] removed extra var --- cmd/nodes/list/options.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cmd/nodes/list/options.go b/cmd/nodes/list/options.go index b09b24b..d4a010c 100644 --- a/cmd/nodes/list/options.go +++ b/cmd/nodes/list/options.go @@ -11,10 +11,6 @@ import ( type Options struct { } -const ( - DefaultMaintenanceDurationSeconds = 3600 -) - func (o *Options) DefineFlags(fs *pflag.FlagSet) { }