Class: Fog::Compute::Libvirt::Real

Inherits:
Object
  • Object
show all
Includes:
Shared
Defined in:
lib/rackspace-fog/libvirt/compute.rb,
lib/rackspace-fog/libvirt/requests/compute/vm_action.rb,
lib/rackspace-fog/libvirt/requests/compute/list_pools.rb,
lib/rackspace-fog/libvirt/requests/compute/define_pool.rb,
lib/rackspace-fog/libvirt/requests/compute/pool_action.rb,
lib/rackspace-fog/libvirt/requests/compute/list_domains.rb,
lib/rackspace-fog/libvirt/requests/compute/list_volumes.rb,
lib/rackspace-fog/libvirt/requests/compute/create_domain.rb,
lib/rackspace-fog/libvirt/requests/compute/create_volume.rb,
lib/rackspace-fog/libvirt/requests/compute/define_domain.rb,
lib/rackspace-fog/libvirt/requests/compute/get_node_info.rb,
lib/rackspace-fog/libvirt/requests/compute/list_networks.rb,
lib/rackspace-fog/libvirt/requests/compute/volume_action.rb,
lib/rackspace-fog/libvirt/requests/compute/update_display.rb,
lib/rackspace-fog/libvirt/requests/compute/destroy_network.rb,
lib/rackspace-fog/libvirt/requests/compute/list_interfaces.rb,
lib/rackspace-fog/libvirt/requests/compute/destroy_interface.rb,
lib/rackspace-fog/libvirt/requests/compute/list_pool_volumes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Fog::Compute::LibvirtUtil

#randomized_name, #to_xml, #xml_element, #xml_elements

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rackspace-fog/libvirt/compute.rb', line 80

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
  require 'libvirt'

  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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



75
76
77
# File 'lib/rackspace-fog/libvirt/compute.rb', line 75

def client
  @client
end

#ip_commandObject (readonly)

Returns the value of attribute ip_command.



77
78
79
# File 'lib/rackspace-fog/libvirt/compute.rb', line 77

def ip_command
  @ip_command
end

#uriObject (readonly)

Returns the value of attribute uri.



76
77
78
# File 'lib/rackspace-fog/libvirt/compute.rb', line 76

def uri
  @uri
end

Instance Method Details

#create_domain(xml) ⇒ Object



5
6
7
# File 'lib/rackspace-fog/libvirt/requests/compute/create_domain.rb', line 5

def create_domain(xml)
  client.create_domain_xml(xml)
end

#create_volume(pool_name, xml) ⇒ Object



5
6
7
# File 'lib/rackspace-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

#define_domain(xml) ⇒ Object



5
6
7
# File 'lib/rackspace-fog/libvirt/requests/compute/define_domain.rb', line 5

def define_domain(xml)
  client.define_domain_xml(xml)
end

#define_pool(xml) ⇒ Object



5
6
7
# File 'lib/rackspace-fog/libvirt/requests/compute/define_pool.rb', line 5

def define_pool(xml)
  client.define_storage_pool_xml(xml)
end

#destroy_interface(uuid) ⇒ Object

shutdown the interface



6
7
8
# File 'lib/rackspace-fog/libvirt/requests/compute/destroy_interface.rb', line 6

def destroy_interface(uuid)
  client.lookup_interface_by_uuid(uuid).destroy
end

#destroy_network(uuid) ⇒ Object



5
6
7
# File 'lib/rackspace-fog/libvirt/requests/compute/destroy_network.rb', line 5

def destroy_network(uuid)
  client.lookup_network_by_uuid(uuid).destroy
end

#enhance_uri(uri) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rackspace-fog/libvirt/compute.rb', line 111

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).has_key?("socket")
        append="&socket=/var/run/libvirt/libvirt-sock"
      end
    end
  end
  uri+append
end

#get_node_infoObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rackspace-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)
  end if xml

  node_hash[:hostname] = client.hostname
  [node_hash]
end

#list_domains(filter = { }) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rackspace-fog/libvirt/requests/compute/list_domains.rb', line 5

def list_domains(filter = { })
  data=[]

  if filter.has_key?(:uuid)
    data << client.lookup_domain_by_uuid(filter[:uuid])
  elsif filter.has_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

#list_interfaces(filter = { }) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rackspace-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

#list_networks(filter = { }) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rackspace-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

#list_pool_volumes(uuid) ⇒ Object



5
6
7
8
9
10
# File 'lib/rackspace-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

#list_pools(filter = { }) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rackspace-fog/libvirt/requests/compute/list_pools.rb', line 5

def list_pools(filter = { })
  data=[]
  if filter.has_key?(:name)
    data << find_pool_by_name(filter[:name])
  elsif filter.has_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

#list_volumes(filter = { }) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rackspace-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

#pool_action(uuid, action) ⇒ Object



5
6
7
8
9
# File 'lib/rackspace-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

#terminateObject



107
108
109
# File 'lib/rackspace-fog/libvirt/compute.rb', line 107

def terminate
  @client.close if @client and !@client.closed?
end

#update_display(options = { }) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rackspace-fog/libvirt/requests/compute/update_display.rb', line 5

def update_display(options = { })
  raise ArgumentError, "uuid is a required parameter" unless options.has_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

#vm_action(uuid, action) ⇒ Object



5
6
7
8
9
# File 'lib/rackspace-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

#volume_action(key, action, options = {}) ⇒ Object



5
6
7
8
# File 'lib/rackspace-fog/libvirt/requests/compute/volume_action.rb', line 5

def volume_action(key, action, options={})
  get_volume({:key => key}, true).send(action)
  true
end