# File lib/fog/libvirt/compute.rb, line 78 def initialize(options={}) @uri = ::Fog::Compute::LibvirtUtil::URI.new(enhance_uri(options[:libvirt_uri])) @ip_command = options[:libvirt_ip_command] # libvirt is part of the gem => ruby-libvirt begin require 'libvirt' rescue LoadError => e retry if require('rubygems') raise e.message end begin if options[:libvirt_username] and options[:libvirt_password] @client = ::Libvirt::open_auth(uri.uri, [::Libvirt::CRED_AUTHNAME, ::Libvirt::CRED_PASSPHRASE]) do |cred| case cred['type'] when ::Libvirt::CRED_AUTHNAME options[:libvirt_username] when ::Libvirt::CRED_PASSPHRASE options[:libvirt_password] end end else @client = ::Libvirt::open(uri.uri) end rescue ::Libvirt::ConnectionError raise Fog::Errors::Error.new("Error making a connection to libvirt URI #{uri.uri}:\n#{$!}") end end
# File lib/fog/libvirt/requests/compute/clone_volume.rb, line 5 def clone_volume (pool_name, xml, name) vol = client.lookup_storage_pool_by_name(pool_name).lookup_volume_by_name(name) client.lookup_storage_pool_by_name(pool_name).create_vol_xml_from(xml, vol) end
# File lib/fog/libvirt/requests/compute/create_domain.rb, line 5 def create_domain(xml) client.create_domain_xml(xml) end
# File lib/fog/libvirt/requests/compute/create_volume.rb, line 5 def create_volume(pool_name, xml) client.lookup_storage_pool_by_name(pool_name).create_vol_xml(xml) end
# File lib/fog/libvirt/requests/compute/define_domain.rb, line 5 def define_domain(xml) client.define_domain_xml(xml) end
# File lib/fog/libvirt/requests/compute/define_pool.rb, line 5 def define_pool(xml) client.define_storage_pool_xml(xml) end
shutdown the interface
# File lib/fog/libvirt/requests/compute/destroy_interface.rb, line 6 def destroy_interface(uuid) client.lookup_interface_by_uuid(uuid).destroy end
# File lib/fog/libvirt/requests/compute/destroy_network.rb, line 5 def destroy_network(uuid) client.lookup_network_by_uuid(uuid).destroy end
# File lib/fog/libvirt/compute.rb, line 113 def enhance_uri(uri) require 'cgi' append="" # on macosx, chances are we are using libvirt through homebrew # the client will default to a socket location based on it's own location (/opt) # we conveniently point it to /var/run/libvirt/libvirt-sock # if no socket option has been specified explicitly if RUBY_PLATFORM =~ /darwin/ querystring=::URI.parse(uri).query if querystring.nil? append="?socket=/var/run/libvirt/libvirt-sock" else if !::CGI.parse(querystring).key?("socket") append="&socket=/var/run/libvirt/libvirt-sock" end end end uri+append end
# File lib/fog/libvirt/requests/compute/get_node_info.rb, line 5 def get_node_info node_hash = Hash.new node_info = client.node_get_info [:model, :memory, :cpus, :mhz, :nodes, :sockets, :cores, :threads].each do |param| node_hash[param] = node_info.send(param) rescue nil end [:type, :version, :node_free_memory, :max_vcpus].each do |param| node_hash[param] = client.send(param) rescue nil end node_hash[:uri] = client.uri xml = client.sys_info rescue nil [:uuid, :manufacturer, :product, :serial].each do |attr| node_hash[attr] = node_attr(attr, xml) rescue nil end if xml node_hash[:hostname] = client.hostname [node_hash] end
# File lib/fog/libvirt/requests/compute/list_domains.rb, line 5 def list_domains(filter = { }) data=[] if filter.key?(:uuid) data << client.lookup_domain_by_uuid(filter[:uuid]) elsif filter.key?(:name) data << client.lookup_domain_by_name(filter[:name]) else client.list_defined_domains.each { |name| data << client.lookup_domain_by_name(name) } unless filter[:defined] == false client.list_domains.each { |id| data << client.lookup_domain_by_id(id) } unless filter[:active] == false end data.compact.map { |d| domain_to_attributes d } end
# File lib/fog/libvirt/requests/compute/list_interfaces.rb, line 5 def list_interfaces(filter = { }) data=[] if filter.keys.empty? active_networks = client.list_interfaces rescue [] defined_networks = client.list_defined_interfaces rescue [] (active_networks + defined_networks).each do |ifname| data << interface_to_attributes(client.lookup_interface_by_name(ifname)) end else data = [interface_to_attributes(get_interface_by_filter(filter))] end data.compact end
# File lib/fog/libvirt/requests/compute/list_networks.rb, line 5 def list_networks(filter = { }) data=[] if filter.keys.empty? (client.list_networks + client.list_defined_networks).each do |network_name| data << network_to_attributes(client.lookup_network_by_name(network_name)) end else data = [network_to_attributes(get_network_by_filter(filter))] end data end
# File lib/fog/libvirt/requests/compute/list_pool_volumes.rb, line 5 def list_pool_volumes(uuid) pool = client.lookup_storage_pool_by_uuid uuid pool.list_volumes.map do |volume_name| volume_to_attributes(pool.lookup_volume_by_name(volume_name)) end end
# File lib/fog/libvirt/requests/compute/list_pools.rb, line 5 def list_pools(filter = { }) data=[] if filter.key?(:name) data << find_pool_by_name(filter[:name]) elsif filter.key?(:uuid) data << find_pool_by_uuid(filter[:uuid]) else (client.list_storage_pools + client.list_defined_storage_pools).each do |name| data << find_pool_by_name(name) end end data.compact end
# File lib/fog/libvirt/requests/compute/list_volumes.rb, line 5 def list_volumes(filter = { }) data = [] if filter.keys.empty? raw_volumes do |pool| pool.list_volumes.each do |volume_name| data << volume_to_attributes(pool.lookup_volume_by_name(volume_name)) end end else return [get_volume(filter)] end data.compact end
# File lib/fog/libvirt/requests/compute/pool_action.rb, line 5 def pool_action(uuid, action) pool = client.lookup_storage_pool_by_uuid uuid pool.send(action) true end
# File lib/fog/libvirt/compute.rb, line 109 def terminate @client.close if @client and !@client.closed? end
# File lib/fog/libvirt/requests/compute/update_display.rb, line 5 def update_display(options = { }) raise ArgumentError, "uuid is a required parameter" unless options.key? :uuid display = { } display[:type] = options[:type] || 'vnc' display[:port] = (options[:port] || -1).to_s display[:listen] = options[:listen].to_s if options[:listen] display[:passwd] = options[:password].to_s if options[:password] display[:autoport] = 'yes' if display[:port] == '-1' builder = Nokogiri::XML::Builder.new { graphics_ (display) } xml = Nokogiri::XML(builder.to_xml).root.to_s client.lookup_domain_by_uuid(options[:uuid]).update_device(xml, 0) # if we got no exceptions, then we're good' true end
# File lib/fog/libvirt/requests/compute/vm_action.rb, line 5 def vm_action(uuid, action) domain = client.lookup_domain_by_uuid(uuid) domain.send(action) true end
# File lib/fog/libvirt/requests/compute/volume_action.rb, line 5 def volume_action(key, action, options={}) get_volume({:key => key}, true).send(action) true end
# File lib/fog/libvirt/requests/compute/list_volumes.rb, line 37 def bytes_to_gb bytes bytes / 1024**3 end
# File lib/fog/libvirt/requests/compute/list_pools.rb, line 35 def find_pool_by_name name pool_to_attributes(client.lookup_storage_pool_by_name(name)) rescue ::Libvirt::RetrieveError nil end
# File lib/fog/libvirt/requests/compute/list_pools.rb, line 41 def find_pool_by_uuid uuid pool_to_attributes(client.lookup_storage_pool_by_uuid(uuid)) rescue ::Libvirt::RetrieveError nil end
Retrieve the interface by mac or by name
# File lib/fog/libvirt/requests/compute/list_interfaces.rb, line 21 def get_interface_by_filter(filter) case filter.keys.first when :mac client.lookup_interface_by_mac(filter[:mac]) when :name client.lookup_interface_by_name(filter[:name]) end end
Retrieve the network by uuid or name
# File lib/fog/libvirt/requests/compute/list_networks.rb, line 19 def get_network_by_filter(filter) case filter.keys.first when :uuid client.lookup_network_by_uuid(filter[:uuid]) when :name client.lookup_network_by_name(filter[:name]) end end
# File lib/fog/libvirt/requests/compute/list_volumes.rb, line 48 def get_volume filter = { }, raw = false raw_volumes do |pool| vol = case filter.keys.first when :name pool.lookup_volume_by_name(filter[:name]) rescue nil when :key pool.lookup_volume_by_key(filter[:key]) rescue nil when :path pool.lookup_volume_by_path(filter[:path]) rescue nil end if vol return raw ? vol : volume_to_attributes(vol) end end { } end
# File lib/fog/libvirt/requests/compute/list_interfaces.rb, line 30 def interface_to_attributes(net) return if net.nil? || net.name == 'lo' { :mac => net.mac, :name => net.name, :active => net.active? } end
# File lib/fog/libvirt/requests/compute/list_networks.rb, line 28 def network_to_attributes(net) return if net.nil? { :uuid => net.uuid, :name => net.name, :bridge_name => net.bridge_name } end
# File lib/fog/libvirt/requests/compute/get_node_info.rb, line 26 def node_attr attr, xml xml_element(xml, "sysinfo/system/entry[@name='#{attr}']").strip end
# File lib/fog/libvirt/requests/compute/list_pools.rb, line 20 def pool_to_attributes(pool) states=[:inactive, :building, :running, :degrated, :inaccessible] { :uuid => pool.uuid, :persistent => pool.persistent?, :autostart => pool.autostart?, :active => pool.active?, :name => pool.name, :allocation => pool.info.allocation, :capacity => pool.info.capacity, :num_of_volumes => pool.num_of_volumes, :state => states[pool.info.state] } end
# File lib/fog/libvirt/requests/compute/list_volumes.rb, line 41 def raw_volumes client.list_storage_pools.each do |pool_name| pool = client.lookup_storage_pool_by_name(pool_name) yield(pool) end end
# File lib/fog/libvirt/requests/compute/list_volumes.rb, line 21 def volume_to_attributes(vol) format_type = xml_element(vol.xml_desc, "/volume/target/format", "type") rescue nil # not all volumes have types, e.g. LVM return nil if format_type == "dir" { :pool_name => vol.pool.name, :key => vol.key, :id => vol.key, :path => vol.path, :name => vol.name, :format_type => format_type, :allocation => bytes_to_gb(vol.info.allocation), :capacity => bytes_to_gb(vol.info.capacity), } end