summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavide Cavalca <davide125@tiscali.it>2016-09-07 12:12:19 -0700
committerPhil Dibowitz <phil@ipom.com>2016-09-07 12:12:19 -0700
commit77f6fd2edcc70a012f606c0a1dfd298c863b32a9 (patch)
treef89b2e50c10439bbd94f921a182a711b94a1bd64
parent3455869e625b96cc8207b9162f54b6ca293c5aea (diff)
downloadohai-77f6fd2edcc70a012f606c0a1dfd298c863b32a9.tar.gz
add a plugin for machine information (#867)
* add a plugin for machine information * split machine into hostnamectl and machine_id for clarity * Fix machineid spec
-rw-r--r--lib/ohai/plugins/linux/hostnamectl.rb34
-rw-r--r--lib/ohai/plugins/linux/machineid.rb35
-rw-r--r--spec/unit/plugins/linux/hostnamectl_spec.rb59
-rw-r--r--spec/unit/plugins/linux/machineid_spec.rb46
4 files changed, 174 insertions, 0 deletions
diff --git a/lib/ohai/plugins/linux/hostnamectl.rb b/lib/ohai/plugins/linux/hostnamectl.rb
new file mode 100644
index 00000000..960b7c42
--- /dev/null
+++ b/lib/ohai/plugins/linux/hostnamectl.rb
@@ -0,0 +1,34 @@
+#
+# Author:: Davide Cavalca (<dcavalca@fb.com>)
+# Copyright:: Copyright (c) 2016 Facebook
+# 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(:Hostnamectl) do
+ provides "hostnamectl"
+
+ collect_data(:linux) do
+ hostnamectl Mash.new unless hostnamectl
+
+ hostnamectl_path = which("hostnamectl")
+ if hostnamectl_path
+ hostnamectl_cmd = shell_out(hostnamectl_path)
+ hostnamectl_cmd.stdout.split("\n").each do |line|
+ key, val = line.split(":")
+ hostnamectl[key.chomp.lstrip.tr(" ", "_").downcase] = val.chomp.lstrip
+ end
+ end
+ end
+end
diff --git a/lib/ohai/plugins/linux/machineid.rb b/lib/ohai/plugins/linux/machineid.rb
new file mode 100644
index 00000000..b946c418
--- /dev/null
+++ b/lib/ohai/plugins/linux/machineid.rb
@@ -0,0 +1,35 @@
+#
+# Author:: Davide Cavalca (<dcavalca@fb.com>)
+# Copyright:: Copyright (c) 2016 Facebook
+# 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(:Machineid) do
+ provides "machine_id"
+
+ collect_data(:linux) do
+ mid = nil
+
+ if File.exists?("/etc/machine-id")
+ mid = File.read("/etc/machine-id").chomp
+ elsif File.exists?("/var/lib/dbus/machine-id")
+ mid = File.read("/var/lib/dbus/machine-id").chomp
+ end
+
+ if mid
+ machine_id mid
+ end
+ end
+end
diff --git a/spec/unit/plugins/linux/hostnamectl_spec.rb b/spec/unit/plugins/linux/hostnamectl_spec.rb
new file mode 100644
index 00000000..bfa72790
--- /dev/null
+++ b/spec/unit/plugins/linux/hostnamectl_spec.rb
@@ -0,0 +1,59 @@
+#
+# Author:: Davide Cavalca <dcavalca@fb.com>
+# Copyright:: Copyright (c) 2016 Facebook
+# 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 File.expand_path(File.dirname(__FILE__) + "/../../../spec_helper.rb")
+
+describe Ohai::System, "Linux hostnamectl plugin" do
+ let(:plugin) { get_plugin("linux/hostnamectl") }
+
+ before(:each) do
+ allow(plugin).to receive(:collect_os).and_return(:linux)
+ end
+
+ it "should populate hostnamectl if hostnamectl is available" do
+ hostnamectl_out = <<-HOSTNAMECTL_OUT
+ Static hostname: foo
+ Icon name: computer-laptop
+ Chassis: laptop
+ Machine ID: 6f702523e2fc7499eb1dc68e5314dacf
+ Boot ID: e085ae9e65e245a8a7b62912adeebe97
+ Operating System: Debian GNU/Linux 8 (jessie)
+ Kernel: Linux 4.3.0-0.bpo.1-amd64
+ Architecture: x86-64
+HOSTNAMECTL_OUT
+
+ allow(plugin).to receive(:which).with("hostnamectl").and_return("/bin/hostnamectl")
+ allow(plugin).to receive(:shell_out).with("/bin/hostnamectl").and_return(mock_shell_out(0, hostnamectl_out, ""))
+ plugin.run
+ expect(plugin[:hostnamectl].to_hash).to eq({
+ "static_hostname" => "foo",
+ "icon_name" => "computer-laptop",
+ "chassis" => "laptop",
+ "machine_id" => "6f702523e2fc7499eb1dc68e5314dacf",
+ "boot_id" => "e085ae9e65e245a8a7b62912adeebe97",
+ "operating_system" => "Debian GNU/Linux 8 (jessie)",
+ "kernel" => "Linux 4.3.0-0.bpo.1-amd64",
+ "architecture" => "x86-64",
+ })
+ end
+
+ it "should not populate hostnamectl if hostnamectl is not available" do
+ allow(plugin).to receive(:which).with("hostnamectl").and_return(false)
+ expect(plugin[:hostnamectl]).to eq(nil)
+ end
+end
diff --git a/spec/unit/plugins/linux/machineid_spec.rb b/spec/unit/plugins/linux/machineid_spec.rb
new file mode 100644
index 00000000..12267079
--- /dev/null
+++ b/spec/unit/plugins/linux/machineid_spec.rb
@@ -0,0 +1,46 @@
+#
+# Author:: Davide Cavalca <dcavalca@fb.com>
+# Copyright:: Copyright (c) 2016 Facebook
+# 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 File.expand_path(File.dirname(__FILE__) + "/../../../spec_helper.rb")
+
+describe Ohai::System, "Machine id plugin" do
+ let(:plugin) { get_plugin("linux/machineid") }
+
+ before(:each) do
+ allow(plugin).to receive(:collect_os).and_return(:linux)
+ end
+
+ it "should read /etc/machine-id if available" do
+ machine_id = "6f702523e2fc7499eb1dc68e5314dacf"
+
+ allow(File).to receive(:exists?).with("/etc/machine-id").and_return(true)
+ allow(File).to receive(:read).with("/etc/machine-id").and_return(machine_id)
+ plugin.run
+ expect(plugin[:machine_id]).to eq(machine_id)
+ end
+
+ it "should read /var/lib/dbus/machine-id if available" do
+ machine_id = "6f702523e2fc7499eb1dc68e5314dacf"
+
+ allow(File).to receive(:exists?).with("/etc/machine-id").and_return(false)
+ allow(File).to receive(:exists?).with("/var/lib/dbus/machine-id").and_return(true)
+ allow(File).to receive(:read).with("/var/lib/dbus/machine-id").and_return(machine_id)
+ plugin.run
+ expect(plugin[:machine_id]).to eq(machine_id)
+ end
+end