diff options
author | Tim Smith <tsmith@chef.io> | 2016-12-20 11:24:32 -0800 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2017-04-06 10:02:19 -0700 |
commit | b693539a55cd8bc4494f0f76ac9b08c8a15b48b8 (patch) | |
tree | 99086617ac68debab70f891d62a1a319cf4bceb9 | |
parent | daeb73c1089d8b611bc5fde3bfddddad28f304eb (diff) | |
download | ohai-b693539a55cd8bc4494f0f76ac9b08c8a15b48b8.tar.gz |
Move the “Virtualization” plugin into libvirt namespace
This plugin isn’t actually a virtualization plugin. It’s actually a libvirt plugin. Move the data out of the Virtualization mash and stick it into its own location. Also rename the plugin LibVirt. I need to get a system setup with libvirt to test this and probably tweak it further / add tests.
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r-- | lib/ohai/plugins/libvirt.rb | 81 | ||||
-rw-r--r-- | lib/ohai/plugins/virtualization.rb | 83 |
2 files changed, 81 insertions, 83 deletions
diff --git a/lib/ohai/plugins/libvirt.rb b/lib/ohai/plugins/libvirt.rb new file mode 100644 index 00000000..46587b01 --- /dev/null +++ b/lib/ohai/plugins/libvirt.rb @@ -0,0 +1,81 @@ +# +# Author:: Benjamin Black (<bb@chef.io>) +# Copyright:: Copyright (c) 2009-2016 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(:LibVirt) do + %w{ uri capabilities nodeinfo domains networks storage }.each do |info| + provides "libvirt/#{info}" + end + + collect_data do + if virtualization[:role].eql?("host") + begin + require "libvirt" # this is the ruby-libvirt gem not the libvirt gem + + libvirt Mash.new + emu = (virtualization[:system].eql?("kvm") ? "qemu" : libvirt[:system]) + libvirt[:libvirt_version] = Libvirt.version(emu)[0].to_s + + virtconn = Libvirt.open_read_only("#{emu}:///system") + + libvirt[:uri] = virtconn.uri + libvirt[:capabilities] = Mash.new + + libvirt[:nodeinfo] = Mash.new + ni = virtconn.node_get_info + %w{cores cpus memory mhz model nodes sockets threads}.each { |a| libvirt[:nodeinfo][a] = ni.send(a) } + + libvirt[:domains] = Mash.new + virtconn.list_domains.each do |d| + dv = virtconn.lookup_domain_by_id d + libvirt[:domains][dv.name] = Mash.new + libvirt[:domains][dv.name][:id] = d + %w{os_type uuid}.each { |a| libvirt[:domains][dv.name][a] = dv.send(a) } + %w{cpu_time max_mem memory nr_virt_cpu state}.each { |a| libvirt[:domains][dv.name][a] = dv.info.send(a) } + + end + + libvirt[:networks] = Mash.new + virtconn.list_networks.each do |n| + nv = virtconn.lookup_network_by_name n + libvirt[:networks][n] = Mash.new + %w{bridge_name uuid}.each { |a| libvirt[:networks][n][a] = nv.send(a) } + end + + libvirt[:storage] = Mash.new + virtconn.list_storage_pools.each do |pool| + sp = virtconn.lookup_storage_pool_by_name pool + libvirt[:storage][pool] = Mash.new + %w{autostart uuid}.each { |a| libvirt[:storage][pool][a] = sp.send(a) } + %w{allocation available capacity state}.each { |a| libvirt[:storage][pool][a] = sp.info.send(a) } + + libvirt[:storage][pool][:volumes] = Mash.new + sp.list_volumes.each do |v| + libvirt[:storage][pool][:volumes][v] = Mash.new + sv = sp.lookup_volume_by_name v + %w{key name path}.each { |a| libvirt[:storage][pool][:volumes][v][a] = sv.send(a) } + %w{allocation capacity type}.each { |a| libvirt[:storage][pool][:volumes][v][a] = sv.info.send(a) } + end + end + + virtconn.close + rescue LoadError => e + Ohai::Log.debug("Plugin LibVirt: Can't load gem ruby-libvirt. Cannot continue.") + end + end + end +end diff --git a/lib/ohai/plugins/virtualization.rb b/lib/ohai/plugins/virtualization.rb deleted file mode 100644 index 7e199b93..00000000 --- a/lib/ohai/plugins/virtualization.rb +++ /dev/null @@ -1,83 +0,0 @@ -# -# Author:: Benjamin Black (<bb@chef.io>) -# Copyright:: Copyright (c) 2009-2016 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. -# - -# Note: despite the name this is really a libvirt plugin. -# perhaps we'd be better off renaming it? -tsmith - -Ohai.plugin(:VirtualizationInfo) do - %w{ uri capabilities nodeinfo domains networks storage }.each do |info| - provides "virtualization/#{info}" - end - - collect_data do - unless virtualization.nil? || !(virtualization[:role].eql?("host")) - begin - require "libvirt" # this is the ruby-libvirt gem not the libvirt gem - - emu = (virtualization[:system].eql?("kvm") ? "qemu" : virtualization[:system]) - virtualization[:libvirt_version] = Libvirt.version(emu)[0].to_s - - virtconn = Libvirt.open_read_only("#{emu}:///system") - - virtualization[:uri] = virtconn.uri - virtualization[:capabilities] = Mash.new - - virtualization[:nodeinfo] = Mash.new - ni = virtconn.node_get_info - %w{cores cpus memory mhz model nodes sockets threads}.each { |a| virtualization[:nodeinfo][a] = ni.send(a) } - - virtualization[:domains] = Mash.new - virtconn.list_domains.each do |d| - dv = virtconn.lookup_domain_by_id d - virtualization[:domains][dv.name] = Mash.new - virtualization[:domains][dv.name][:id] = d - %w{os_type uuid}.each { |a| virtualization[:domains][dv.name][a] = dv.send(a) } - %w{cpu_time max_mem memory nr_virt_cpu state}.each { |a| virtualization[:domains][dv.name][a] = dv.info.send(a) } - - end - - virtualization[:networks] = Mash.new - virtconn.list_networks.each do |n| - nv = virtconn.lookup_network_by_name n - virtualization[:networks][n] = Mash.new - %w{bridge_name uuid}.each { |a| virtualization[:networks][n][a] = nv.send(a) } - end - - virtualization[:storage] = Mash.new - virtconn.list_storage_pools.each do |pool| - sp = virtconn.lookup_storage_pool_by_name pool - virtualization[:storage][pool] = Mash.new - %w{autostart uuid}.each { |a| virtualization[:storage][pool][a] = sp.send(a) } - %w{allocation available capacity state}.each { |a| virtualization[:storage][pool][a] = sp.info.send(a) } - - virtualization[:storage][pool][:volumes] = Mash.new - sp.list_volumes.each do |v| - virtualization[:storage][pool][:volumes][v] = Mash.new - sv = sp.lookup_volume_by_name v - %w{key name path}.each { |a| virtualization[:storage][pool][:volumes][v][a] = sv.send(a) } - %w{allocation capacity type}.each { |a| virtualization[:storage][pool][:volumes][v][a] = sv.info.send(a) } - end - end - - virtconn.close - rescue LoadError => e - Ohai::Log.debug("Plugin Virtualization: Can't load gem: #{e}. Cannot continue.") - end - end - end -end |