diff options
author | Collin McNeese <cmcneese@chef.io> | 2021-02-15 11:48:38 -0600 |
---|---|---|
committer | Collin McNeese <cmcneese@chef.io> | 2021-02-15 11:48:38 -0600 |
commit | 51f868c243acd267f91acfa6f5feda5a3ea37143 (patch) | |
tree | f83612eb5fba21952a0644b0201ac4823d47a1b1 | |
parent | 4e2dd4ee7ff29e8233df30b473fd9acdd2079f9e (diff) | |
download | ohai-51f868c243acd267f91acfa6f5feda5a3ea37143.tar.gz |
updates habitat plugin to shell out with hab command rather than looking at filesystem.
Signed-off-by: Collin McNeese <cmcneese@chef.io>
-rw-r--r-- | lib/ohai/plugins/habitat.rb | 36 | ||||
-rw-r--r-- | spec/unit/plugins/habitat_spec.rb | 61 |
2 files changed, 63 insertions, 34 deletions
diff --git a/lib/ohai/plugins/habitat.rb b/lib/ohai/plugins/habitat.rb index 285e8856..0ae89865 100644 --- a/lib/ohai/plugins/habitat.rb +++ b/lib/ohai/plugins/habitat.rb @@ -19,25 +19,41 @@ Ohai.plugin(:Habitat) do provides "habitat" def fetch_habitat_version - shell_out("hab -V").stdout.gsub(/hab\s*/, "").strip + 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.") end def fetch_habitat_packages - if Dir.exist?("C:/hab/pkgs") - Dir.glob("C:/hab/pkgs/*/*/*/*/").sort.map { |pkg| pkg.gsub("C\:\/hab\/pkgs\/", "").chomp("/") } - elsif Dir.exist?("/hab/pkgs") - Dir.glob("/hab/pkgs/*/*/*/*/").sort.map { |pkg| pkg.gsub("\/hab\/pkgs\/", "").chomp("/") } + 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.") + end + + def load_habitat_service_via_cli(status_stdout) + # package type desired state elapsed (s) pid group + # core/httpd/2.4.35/20190307151146 standalone up up 158169 1410 httpd.default + @services = [] + lines = status_stdout.split("\n") + lines.each do |line| + @service = {} + fields = line.split(/\s+/) + next unless fields[0].match?(%r{.*/.*/.*/.*}) # ignore header line + @service = {} + @service[:identity] = fields[0] + @service[:topology] = fields[1] + @service[:state_desired] = fields[2] + @service[:state_actual] = fields[2] + (@services).push(@service) end + @services end def fetch_habitat_services - if Dir.exist?("C:/hab/svc") - Dir.glob("C:/hab/svc/*").sort.map { |svc| svc.gsub("C\:\/hab\/svc\/", "").chomp("/") } - elsif Dir.exist?("/hab/svc") - Dir.glob("/hab/svc/*").sort.map { |svc| svc.gsub("\/hab\/svc\/", "").chomp("/") } - end + services_shell_out = shell_out(["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.") end collect_data(:default) do diff --git a/spec/unit/plugins/habitat_spec.rb b/spec/unit/plugins/habitat_spec.rb index dcb35373..40396e10 100644 --- a/spec/unit/plugins/habitat_spec.rb +++ b/spec/unit/plugins/habitat_spec.rb @@ -18,35 +18,48 @@ require "spec_helper" describe "plugin habitat" do let(:plugin) { get_plugin("habitat") } - it "returns the installed version of Habitat" do - allow(plugin).to receive(:shell_out).with("hab -V").and_return(mock_shell_out(0, "hab 1.1.1/202001010000", "")) + before do + pkg_result = <<~PKG + line that would not match + origin1/package1/version1/release1 + origin2/package2/version2/release2 + PKG + svc_result = <<~SVC + package type desired state elapsed (s) pid group + origin1/package1/version1/release1 standalone up up 60 100 package1.default + origin2/package2/version2/release2 standalone up up 60 101 package2.default + SVC + allow(plugin).to receive(:shell_out).with(["hab"], + ["-V"]).and_return(mock_shell_out(0, "hab 1.1.1/202001010000", "")) + 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, "")) plugin.run + end + + it "returns the installed version of Habitat" do expect(plugin.habitat[:version]).to eql("1.1.1/202001010000") end - it "Creates arrays based on the installed Habitat services and packages on Linux" do - allow(Dir).to receive(:exist?).with("C:/hab/svc").and_return(false) - allow(Dir).to receive(:exist?).with("/hab/svc").and_return(true) - allow(Dir).to receive(:exist?).with("C:/hab/pkgs").and_return(false) - allow(Dir).to receive(:exist?).with("/hab/pkgs").and_return(true) - allow(Dir).to receive(:glob).with("/hab/svc/*").and_return(["/hab/svc/service1", "/hab/svc/service2"]) - allow(Dir).to receive(:glob).with("/hab/pkgs/*/*/*/*/").and_return(["/hab/pkgs/origin/package/version/number/"]) - plugin.run - expect(plugin.habitat[:services]).to include("service1") - expect(plugin.habitat[:services]).to include("service2") - expect(plugin.habitat[:packages]).to include("origin/package/version/number") + it "creates an array based on the installed Habitat packages" do + expect(plugin.habitat[:packages]).to_not include("line that would not match") + expect(plugin.habitat[:packages]).to include("origin1/package1/version1/release1") + expect(plugin.habitat[:packages]).to include("origin2/package2/version2/release2") end - it "Creates arrays based on the installed Habitat services and packages on Windows" do - allow(Dir).to receive(:exist?).with("C:/hab/svc").and_return(true) - allow(Dir).to receive(:exist?).with("/hab/svc").and_return(false) - allow(Dir).to receive(:exist?).with("C:/hab/pkgs").and_return(true) - allow(Dir).to receive(:exist?).with("/hab/pkgs").and_return(false) - allow(Dir).to receive(:glob).with("C:/hab/svc/*").and_return(["C:/hab/svc/service1", "C:/hab/svc/service2"]) - allow(Dir).to receive(:glob).with("C:/hab/pkgs/*/*/*/*/").and_return(["C:/hab/pkgs/origin/package/version/number/"]) - plugin.run - expect(plugin.habitat[:packages]).to include("origin/package/version/number") - expect(plugin.habitat[:services]).to include("service1") - expect(plugin.habitat[:services]).to include("service2") + it "creates an array based on the installed Habitat services" do + expect(plugin.habitat[:services]).to_not include("package type desired state elapsed (s) pid group") + expect(plugin.habitat[:services]).to include({ + identity: "origin1/package1/version1/release1", + state_actual: "up", + state_desired: "up", + topology: "standalone" + }) + expect(plugin.habitat[:services]).to include({ + identity: "origin2/package2/version2/release2", + state_actual: "up", + state_desired: "up", + topology: "standalone" + }) end end |