Class: Foreman::Model::Libvirt

Inherits:
ComputeResource show all
Defined in:
lib/foreman/model/libvirt.rb

Constant Summary

Constants inherited from ComputeResource

ComputeResource::PROVIDERS, ComputeResource::STI_PREFIX

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ComputeResource

#as_json, #available_images, #hardware_profile, #hardware_profiles, #new_interface, new_provider, #ping, #provider, #provider=, #provider_friendly_name, #save_vm, #start_vm, #stop_vm, #supports_update?, #to_label, #to_param, #update_required?, #vms

Methods included from Authorization

#enforce_create_permissions, #enforce_destroy_permissions, #enforce_edit_permissions, #enforce_permissions, included, #permission_failed?

Methods included from Taxonomix

included

Class Method Details

.model_nameObject


27
28
29
# File 'lib/foreman/model/libvirt.rb', line 27

def self.model_name
  ComputeResource.model_name
end

Instance Method Details

#capabilitiesObject


10
11
12
# File 'lib/foreman/model/libvirt.rb', line 10

def capabilities
  [:build]
end

#console(uuid) ⇒ Object


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/foreman/model/libvirt.rb', line 99

def console uuid
  vm = find_vm_by_uuid(uuid)
  raise "VM is not running!" unless vm.ready?
  password = random_password
  # Listen address cannot be updated while the guest is running
  # When we update the display password, we pass the existing listen address
  vm.update_display(:password => password, :listen => vm.display[:listen])
  WsProxy.start(:host => hypervisor.hostname, :host_port => vm.display[:port], :password => password).merge(:type =>  vm.display[:type].downcase, :name=> vm.name)
rescue ::Libvirt::Error => e
  if e.message =~ /cannot change listen address/
    logger.warn e
    raise "Unable to change VM display listen address, make sure the display is not attached to localhost only"
  else
    raise e
  end
end

#create_vm(args = { }) ⇒ Object


89
90
91
92
93
94
95
96
97
# File 'lib/foreman/model/libvirt.rb', line 89

def create_vm args = { }
  vm = new_vm(args)
  create_volumes :prefix => vm.name, :volumes => vm.volumes

  vm.save
rescue Fog::Errors::Error => e
  errors.add(:base, e.to_s)
  false
end

#destroy_vm(uuid, args = { }) ⇒ Object

we default to destroy the VM's storage as well.


21
22
23
24
25
# File 'lib/foreman/model/libvirt.rb', line 21

def destroy_vm uuid, args = { }
  find_vm_by_uuid(uuid).destroy({ :destroy_volumes => true }.merge(args))
rescue ActiveRecord::RecordNotFound
  true
end

#find_vm_by_uuid(uuid) ⇒ Object


14
15
16
17
18
# File 'lib/foreman/model/libvirt.rb', line 14

def find_vm_by_uuid uuid
  client.servers.get(uuid)
rescue ::Libvirt::RetrieveError => e
  raise(ActiveRecord::RecordNotFound)
end

#hypervisorObject


116
117
118
# File 'lib/foreman/model/libvirt.rb', line 116

def hypervisor
  client.nodes.first
end

#interfacesObject


63
64
65
# File 'lib/foreman/model/libvirt.rb', line 63

def interfaces
  client.interfaces rescue []
end

#max_cpu_countObject


31
32
33
# File 'lib/foreman/model/libvirt.rb', line 31

def max_cpu_count
  hypervisor.cpus
end

#max_memoryObject

libvirt reports in KB


36
37
38
39
40
41
# File 'lib/foreman/model/libvirt.rb', line 36

def max_memory
  hypervisor.memory * 1024
rescue => e
  logger.debug "unable to figure out free memory, guessing instead due to:#{e}"
  16*1024*1024*1024
end

#networksObject


67
68
69
# File 'lib/foreman/model/libvirt.rb', line 67

def networks
  client.networks rescue []
end

#new_nic(attr = { }) ⇒ Object


51
52
53
# File 'lib/foreman/model/libvirt.rb', line 51

def new_nic attr={ }
  client.nics.new attr
end

#new_vm(attr = { }) ⇒ Object


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/foreman/model/libvirt.rb', line 71

def new_vm attr={ }
  test_connection
  return unless errors.empty?
  opts = vm_instance_defaults.merge(attr.to_hash).symbolize_keys

  # convert rails nested_attributes into a plain hash
  [:nics, :volumes].each do |collection|
    nested_attrs = opts.delete("#{collection}_attributes".to_sym)
    opts[collection] = nested_attributes_for(collection, nested_attrs) if nested_attrs
  end

  opts.reject! { |k, v| v.nil? }

  vm = client.servers.new opts
  vm.memory = opts[:memory] if opts[:memory]
  vm
end

#new_volume(attr = { }) ⇒ Object


55
56
57
# File 'lib/foreman/model/libvirt.rb', line 55

def new_volume attr={ }
  client.volumes.new attr
end

#provided_attributesObject


6
7
8
# File 'lib/foreman/model/libvirt.rb', line 6

def provided_attributes
  super.merge({:mac => :mac})
end

#storage_poolsObject


59
60
61
# File 'lib/foreman/model/libvirt.rb', line 59

def storage_pools
  client.pools rescue []
end

#test_connection(options = {}) ⇒ Object


43
44
45
46
47
48
49
# File 'lib/foreman/model/libvirt.rb', line 43

def test_connection options = {}
  super
  errors[:url].empty? and hypervisor
rescue => e
  disconnect rescue nil
  errors[:base] << e.message
end