From 810aa8c98139d59be50cb558473c1efe07e74315 Mon Sep 17 00:00:00 2001
From: Kirill Smelkov <kirr@nexedi.com>
Date: Tue, 24 Nov 2015 11:18:18 +0300
Subject: [PATCH] NXD lib/tasks/gitlab/check: Exit with non-zero code, if
 something failed in a check task

This is handy for monitoring tools, which could e.g. periodically call check
tasks and instead of parsing output, rely on exit code.

The way we detect if something failed is via hooking into String#red, and if
anything was ever printed in red - that's an error.
---
 lib/tasks/gitlab/check.rake | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/lib/tasks/gitlab/check.rake b/lib/tasks/gitlab/check.rake
index a9a48f7188f..339a7eb89dc 100644
--- a/lib/tasks/gitlab/check.rake
+++ b/lib/tasks/gitlab/check.rake
@@ -1,3 +1,13 @@
+# if we ever print anything in red - that's an error
+$check_failed = false
+class String
+  alias_method :orig_red, :red
+  def red(*args)
+    $check_failed = true
+    orig_red(*args)
+  end
+end
+
 namespace :gitlab do
   desc "GitLab | Check the configuration of GitLab and its environment"
   task check: %w{gitlab:gitlab_shell:check
@@ -863,8 +873,9 @@ namespace :gitlab do
 
   def finished_checking(component)
     puts ""
-    puts "Checking #{component.color(:yellow)} ... #{"Finished".color(:green)}"
+    puts "Checking #{component.color(:yellow)} ... #{$check_failed ? "Failed".color(:red) : "OK".color(:green)}"
     puts ""
+    exit 1 if $check_failed
   end
 
   def see_database_guide
-- 
2.30.9