From 784e8ef85775f4f388d03453eba670a9cd0ca1f9 Mon Sep 17 00:00:00 2001 From: Alexander Chuprov Date: Mon, 8 Jul 2024 18:04:30 +0300 Subject: [PATCH] [#1209] cli: Add --quiet flag to healthcheck command Signed-off-by: Alexander Chuprov --- cmd/frostfs-cli/internal/commonflags/flags.go | 4 ++++ cmd/frostfs-cli/modules/control/healthcheck.go | 10 ++++++++++ cmd/frostfs-cli/modules/control/ir_healthcheck.go | 11 +++++++++++ 3 files changed, 25 insertions(+) diff --git a/cmd/frostfs-cli/internal/commonflags/flags.go b/cmd/frostfs-cli/internal/commonflags/flags.go index 23dbc8c2..cd46d63e 100644 --- a/cmd/frostfs-cli/internal/commonflags/flags.go +++ b/cmd/frostfs-cli/internal/commonflags/flags.go @@ -53,6 +53,10 @@ const ( AwaitFlag = "await" AwaitFlagUsage = "Wait for the operation to complete" + + QuietFlag = "quiet" + QuietFlagShorthand = "q" + QuietFlagUsage = "Print nothing and exit with non-zero code on failure" ) // Init adds common flags to the command: diff --git a/cmd/frostfs-cli/modules/control/healthcheck.go b/cmd/frostfs-cli/modules/control/healthcheck.go index 097fba54..2241a403 100644 --- a/cmd/frostfs-cli/modules/control/healthcheck.go +++ b/cmd/frostfs-cli/modules/control/healthcheck.go @@ -1,7 +1,10 @@ package control import ( + "os" + rawclient "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client" + "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key" commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control" @@ -24,6 +27,7 @@ func initControlHealthCheckCmd() { flags := healthCheckCmd.Flags() flags.Bool(healthcheckIRFlag, false, "Communicate with IR node") + flags.BoolP(commonflags.QuietFlag, commonflags.QuietFlagShorthand, false, commonflags.QuietFlagUsage) _ = flags.MarkDeprecated(healthcheckIRFlag, "for health check of inner ring nodes, use the 'control ir healthcheck' command instead.") } @@ -50,6 +54,12 @@ func healthCheck(cmd *cobra.Command, args []string) { commonCmd.ExitOnErr(cmd, "rpc error: %w", err) verifyResponse(cmd, resp.GetSignature(), resp.GetBody()) + if quietFlag, _ := cmd.Flags().GetBool(commonflags.QuietFlag); quietFlag { + if resp.GetBody().GetHealthStatus() == control.HealthStatus_READY { + return + } + os.Exit(1) + } cmd.Printf("Network status: %s\n", resp.GetBody().GetNetmapStatus()) cmd.Printf("Health status: %s\n", resp.GetBody().GetHealthStatus()) diff --git a/cmd/frostfs-cli/modules/control/ir_healthcheck.go b/cmd/frostfs-cli/modules/control/ir_healthcheck.go index e70538ce..4f272c1b 100644 --- a/cmd/frostfs-cli/modules/control/ir_healthcheck.go +++ b/cmd/frostfs-cli/modules/control/ir_healthcheck.go @@ -1,7 +1,10 @@ package control import ( + "os" + rawclient "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client" + "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key" commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common" ircontrol "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/ir" @@ -18,6 +21,8 @@ var irHealthCheckCmd = &cobra.Command{ func initControlIRHealthCheckCmd() { initControlFlags(irHealthCheckCmd) + flags := irHealthCheckCmd.Flags() + flags.BoolP(commonflags.QuietFlag, commonflags.QuietFlagShorthand, false, commonflags.QuietFlagUsage) } func irHealthCheck(cmd *cobra.Command, _ []string) { @@ -39,6 +44,12 @@ func irHealthCheck(cmd *cobra.Command, _ []string) { commonCmd.ExitOnErr(cmd, "rpc error: %w", err) verifyResponse(cmd, resp.GetSignature(), resp.GetBody()) + if quietFlag, _ := cmd.Flags().GetBool(commonflags.QuietFlag); quietFlag { + if resp.GetBody().GetHealthStatus() == ircontrol.HealthStatus_READY { + return + } + os.Exit(1) + } cmd.Printf("Health status: %s\n", resp.GetBody().GetHealthStatus()) }