diff options
author | Tim Smith <tsmith@chef.io> | 2021-02-19 16:28:48 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-19 16:28:48 -0800 |
commit | f220c434bbc81bced91a8971b33973b5177cfec5 (patch) | |
tree | 54c9e4fd1090da9324a3188136e4a6eec95d45f1 | |
parent | f26c3ce26c63eb06d714073f920b1f75c4feef25 (diff) | |
parent | 7afd8cf17b3c27f07cbab6b1da1124fdfbfced6b (diff) | |
download | ohai-f220c434bbc81bced91a8971b33973b5177cfec5.tar.gz |
Merge pull request #1623 from collinmcneese/habitat_plugin
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r-- | lib/ohai/plugins/habitat.rb | 71 | ||||
-rw-r--r-- | spec/unit/plugins/habitat_spec.rb | 66 |
2 files changed, 137 insertions, 0 deletions
diff --git a/lib/ohai/plugins/habitat.rb b/lib/ohai/plugins/habitat.rb new file mode 100644 index 00000000..7a50a2c0 --- /dev/null +++ b/lib/ohai/plugins/habitat.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true +# +# Copyright:: Copyright (c) Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# +Ohai.plugin(:Habitat) do + provides "habitat" + + def habitat_binary + @habitat_binary ||= which("hab") + end + + def fetch_habitat_version + shell_out([habitat_binary, "-V"]).stdout.gsub(/hab\s*/, "").strip + rescue Ohai::Exceptions::Exec + logger.trace("Plugin Habitat: Unable to determine the installed version of Habitat, skipping collection.") + end + + def fetch_habitat_packages + shell_out([habitat_binary, "pkg", "list", "--all"]).stdout.split.sort.select { |pkg| pkg.match?(%r{.*/.*/.*/.*}) } + rescue Ohai::Exceptions::Exec + logger.trace("Plugin Habitat: Unable to determine the installed Habitat packages, 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 = [] + status_stdout.each_line do |line| + 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 + services_shell_out = shell_out([habitat_binary, "svc", "status"]).stdout + load_habitat_service_via_cli(services_shell_out) if services_shell_out + rescue Ohai::Exceptions::Exec + logger.trace("Plugin Habitat: Unable to determine the installed Habitat services, skipping collection.") + end + + collect_data(:default) do + return unless habitat_binary + + habitat Mash.new + habitat["version"] = fetch_habitat_version + habitat["packages"] = fetch_habitat_packages + habitat["services"] = fetch_habitat_services + end +end diff --git a/spec/unit/plugins/habitat_spec.rb b/spec/unit/plugins/habitat_spec.rb new file mode 100644 index 00000000..b9c9e2c6 --- /dev/null +++ b/spec/unit/plugins/habitat_spec.rb @@ -0,0 +1,66 @@ +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "spec_helper" + +describe "plugin habitat" do + let(:plugin) { get_plugin("habitat") } + + 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(:habitat_binary).and_return("/some/path/hab") + allow(plugin).to receive(:shell_out).with(["/some/path/hab", + "-V"]).and_return(mock_shell_out(0, "hab 1.1.1/202001010000", "")) + allow(plugin).to receive(:shell_out).with(["/some/path/hab", "pkg", "list", + "--all"]).and_return(mock_shell_out(0, pkg_result, "")) + allow(plugin).to receive(:shell_out).with(["/some/path/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 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 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 |