summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCollin McNeese <cmcneese@chef.io>2021-02-19 08:25:06 -0600
committerCollin McNeese <cmcneese@chef.io>2021-02-19 08:25:06 -0600
commit55a38e0e6acec50e871dbb9df45a1f1d2527e2c9 (patch)
treec9e16db0b969ebbed52408fe74437ce9c75de69d
parentb44d0fbda9fba8f3a099c4c72ab4bd3a60ee9138 (diff)
downloadohai-55a38e0e6acec50e871dbb9df45a1f1d2527e2c9.tar.gz
updates Habitat plugin to guard for hab binary existing
Signed-off-by: Collin McNeese <cmcneese@chef.io>
-rw-r--r--lib/ohai/plugins/habitat.rb12
-rw-r--r--spec/unit/plugins/habitat_spec.rb1
2 files changed, 10 insertions, 3 deletions
diff --git a/lib/ohai/plugins/habitat.rb b/lib/ohai/plugins/habitat.rb
index a9e7ff2d..26bbddef 100644
--- a/lib/ohai/plugins/habitat.rb
+++ b/lib/ohai/plugins/habitat.rb
@@ -18,16 +18,20 @@
Ohai.plugin(:Habitat) do
provides "habitat"
+ def habitat_exists?
+ which("habitat")
+ end
+
def fetch_habitat_version
shell_out(["hab"], ["-V"]).stdout.gsub(/hab\s*/, "").strip
rescue Ohai::Exceptions::Exec
- logger.trace("Plugin Habitat: No detected version of hab binary found in PATH, skipping collection.")
+ logger.trace("Plugin Habitat: Unable to determine the installed version of Habitat, skipping collection.")
end
def fetch_habitat_packages
shell_out(["hab", "pkg", "list", "--all"]).stdout.split.sort.select { |pkg| pkg.match?(%r{.*/.*/.*/.*}) }
rescue Ohai::Exceptions::Exec
- logger.trace("Plugin Habitat: No detected version of hab binary found in PATH, skipping collection.")
+ logger.trace("Plugin Habitat: Unable to determine the installed Habitat packages, skipping collection.")
end
def load_habitat_service_via_cli(status_stdout)
@@ -54,10 +58,12 @@ Ohai.plugin(:Habitat) do
services_shell_out = shell_out(%w{hab svc status}).stdout
load_habitat_service_via_cli(services_shell_out) if services_shell_out
rescue Ohai::Exceptions::Exec
- logger.trace("Plugin Habitat: No detected version of hab binary found in PATH, skipping collection.")
+ logger.trace("Plugin Habitat: Unable to determine the installed Habitat services, skipping collection.")
end
collect_data(:default) do
+ return unless habitat_exists?
+
habitat Mash.new
habitat["version"] = fetch_habitat_version
habitat["packages"] = fetch_habitat_packages
diff --git a/spec/unit/plugins/habitat_spec.rb b/spec/unit/plugins/habitat_spec.rb
index 175ae3b8..183a2174 100644
--- a/spec/unit/plugins/habitat_spec.rb
+++ b/spec/unit/plugins/habitat_spec.rb
@@ -34,6 +34,7 @@ describe "plugin habitat" do
allow(plugin).to receive(:shell_out).with(["hab", "pkg", "list",
"--all"]).and_return(mock_shell_out(0, pkg_result, ""))
allow(plugin).to receive(:shell_out).with(%w{hab svc status}).and_return(mock_shell_out(0, svc_result, ""))
+ allow(plugin).to receive(:habitat_exists?).and_return(true)
plugin.run
end