From 7c9551abde1e04b7f46f7f0e8295a43306263fff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Kar=C3=A9kinian?= Date: Sun, 3 Nov 2013 14:38:03 +0100 Subject: [PATCH] Exit with the proper status (0 for success, 1 for failure) --- lib/chef/knife/cookbook_inspect.rb | 4 ++-- lib/chef/knife/data_bag_inspect.rb | 6 +++--- lib/chef/knife/environment_inspect.rb | 4 ++-- lib/chef/knife/inspect.rb | 6 +++++- lib/chef/knife/role_inspect.rb | 4 ++-- lib/health_inspector/checklists/base.rb | 10 +++++++++- 6 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/chef/knife/cookbook_inspect.rb b/lib/chef/knife/cookbook_inspect.rb index 8241789..a197945 100644 --- a/lib/chef/knife/cookbook_inspect.rb +++ b/lib/chef/knife/cookbook_inspect.rb @@ -22,9 +22,9 @@ def run # api_endpoint = env ? "environments/#{env}/cookbooks/#{cookbook_name}" : "cookbooks/#{cookbook_name}" validator = HealthInspector::Checklists::Cookbooks.new(self) - validator.validate_item( validator.load_item(cookbook_name) ) + exit validator.validate_item( validator.load_item(cookbook_name) ) when 0 # We are inspecting all the cookbooks - HealthInspector::Checklists::Cookbooks.run(self) + exit HealthInspector::Checklists::Cookbooks.run(self) end end end diff --git a/lib/chef/knife/data_bag_inspect.rb b/lib/chef/knife/data_bag_inspect.rb index 8109c6d..656c4b1 100644 --- a/lib/chef/knife/data_bag_inspect.rb +++ b/lib/chef/knife/data_bag_inspect.rb @@ -17,17 +17,17 @@ def run item_name = @name_args[1] validator = HealthInspector::Checklists::DataBagItems.new(self) - validator.validate_item( validator.load_item("#{bag_name}/#{item_name}") ) + exit validator.validate_item( validator.load_item("#{bag_name}/#{item_name}") ) when 1 # We are inspecting a data bag bag_name = @name_args[0] validator = HealthInspector::Checklists::DataBags.new(self) - validator.validate_item( validator.load_item(bag_name) ) + exit validator.validate_item( validator.load_item(bag_name) ) when 0 # We are inspecting all the data bags HealthInspector::Checklists::DataBags.run(self) - HealthInspector::Checklists::DataBagItems.run(self) + exit HealthInspector::Checklists::DataBagItems.run(self) end end end diff --git a/lib/chef/knife/environment_inspect.rb b/lib/chef/knife/environment_inspect.rb index 8b272b5..6b2c8ee 100644 --- a/lib/chef/knife/environment_inspect.rb +++ b/lib/chef/knife/environment_inspect.rb @@ -15,9 +15,9 @@ def run when 1 # We are inspecting a environment environment_name = @name_args[0] validator = HealthInspector::Checklists::Environments.new(self) - validator.validate_item( validator.load_item(environment_name) ) + exit validator.validate_item( validator.load_item(environment_name) ) when 0 # We are inspecting all the environments - HealthInspector::Checklists::Environments.run(self) + exit HealthInspector::Checklists::Environments.run(self) end end end diff --git a/lib/chef/knife/inspect.rb b/lib/chef/knife/inspect.rb index f4ac12a..a9b080f 100644 --- a/lib/chef/knife/inspect.rb +++ b/lib/chef/knife/inspect.rb @@ -4,6 +4,8 @@ class Chef class Knife class Inspect < Knife + CHECKLISTS = %w[Cookbooks DataBags DataBagItems Environments Roles] + deps do require "health_inspector" end @@ -11,9 +13,11 @@ class Inspect < Knife banner "knife inspect" def run - %w[ Cookbooks DataBags DataBagItems Environments Roles ].each do |checklist| + results = CHECKLISTS.map do |checklist| HealthInspector::Checklists.const_get(checklist).run(self) end + + exit ! results.include?(false) end end end diff --git a/lib/chef/knife/role_inspect.rb b/lib/chef/knife/role_inspect.rb index f96d5a0..69f6069 100644 --- a/lib/chef/knife/role_inspect.rb +++ b/lib/chef/knife/role_inspect.rb @@ -15,9 +15,9 @@ def run when 1 # We are inspecting a role role_name = @name_args[0] validator = HealthInspector::Checklists::Roles.new(self) - validator.validate_item( validator.load_item(role_name) ) + exit validator.validate_item( validator.load_item(role_name) ) when 0 # We are inspecting all the roles - HealthInspector::Checklists::Roles.run(self) + exit HealthInspector::Checklists::Roles.run(self) end end end diff --git a/lib/health_inspector/checklists/base.rb b/lib/health_inspector/checklists/base.rb index e5e07ab..f5ecb97 100644 --- a/lib/health_inspector/checklists/base.rb +++ b/lib/health_inspector/checklists/base.rb @@ -41,9 +41,13 @@ def each_item def run banner "Inspecting #{self.class.title}" + results = [] + each_item do |item| - validate_item(item) + results << validate_item(item) end + + return ! results.include?(false) end def validate_item(item) @@ -52,8 +56,12 @@ def validate_item(item) if failures.empty? print_success(item.name) # unless @context.quiet_success + + true else print_failures(item.name, failures) + + false end end