Class: Foreman::Model::Ovirt

Inherits:
ComputeResource show all
Defined in:
lib/foreman/model/ovirt.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, #find_vm_by_uuid, new_provider, #ping, #provider, #provider=, #provider_friendly_name, #stop_vm, #to_label, #to_param, #vm_instance_defaults, #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


9
10
11
# File 'lib/foreman/model/ovirt.rb', line 9

def self.model_name
  ComputeResource.model_name
end

Instance Method Details

#capabilitiesObject


13
14
15
# File 'lib/foreman/model/ovirt.rb', line 13

def capabilities
  [:build]
end

#clustersObject


42
43
44
# File 'lib/foreman/model/ovirt.rb', line 42

def clusters
  client.clusters
end

#console(uuid) ⇒ Object


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/foreman/model/ovirt.rb', line 148

def console(uuid)
  vm = find_vm_by_uuid(uuid)
  raise "VM is not running!" if vm.status == "down"
  if vm.display[:type] =~ /spice/i
    xpi_opts = {:name => vm.name, :address => vm.display[:address], :secure_port => vm.display[:secure_port], :ca_cert => cacert}
    opts = if vm.display[:secure_port]
             { :host_port => vm.display[:secure_port], :ssl_target => true }
           else
             { :host_port => vm.display[:port] }
           end
    WsProxy.start(opts.merge(:host => vm.display[:address], :password => vm.ticket)).merge(xpi_opts).merge(:type => 'spice')
  else
    WsProxy.start(:host => vm.display[:address], :host_port => vm.display[:port], :password => vm.ticket).merge(:name => vm.name, :type => 'vnc')
  end
end

#create_vm(args = {}) ⇒ Object


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/foreman/model/ovirt.rb', line 97

def create_vm(args = {})
  #ovirt doesn't accept '.' in vm name.
  args[:name] = args[:name].parameterize
  vm = super({ :first_boot_dev => 'network' }.merge(args))
  begin
    create_interfaces(vm, args[:interfaces_attributes])
    create_volumes(vm, args[:volumes_attributes])
  rescue => e
    destroy_vm vm.id
    raise e
  end
  vm
end

#datacenters(options = {}) ⇒ Object


76
77
78
# File 'lib/foreman/model/ovirt.rb', line 76

def datacenters(options={})
  client.datacenters(options).map { |dc| [dc[:name], dc[:id]] }
end

#destroy_vm(uuid) ⇒ Object


138
139
140
141
142
143
144
145
146
# File 'lib/foreman/model/ovirt.rb', line 138

def destroy_vm(uuid)
  begin
    find_vm_by_uuid(uuid).destroy
  rescue OVIRT::OvirtException => e
    #404 error are ignored on delete.
    raise e unless e.message =~ /404/
  end
  true
end

#hardware_profile(id) ⇒ Object


38
39
40
# File 'lib/foreman/model/ovirt.rb', line 38

def hardware_profile(id)
  client.templates.get(id) || raise(ActiveRecord::RecordNotFound)
end

#hardware_profiles(opts = {}) ⇒ Object


34
35
36
# File 'lib/foreman/model/ovirt.rb', line 34

def hardware_profiles(opts={})
  client.templates
end

#max_cpu_countObject

FIXME


26
27
28
# File 'lib/foreman/model/ovirt.rb', line 26

def max_cpu_count
  8
end

#max_memoryObject


30
31
32
# File 'lib/foreman/model/ovirt.rb', line 30

def max_memory
  16*1024*1024*1024
end

#networks(opts = {}) ⇒ Object


80
81
82
83
84
85
86
87
# File 'lib/foreman/model/ovirt.rb', line 80

def networks(opts ={})
  if opts[:cluster_id]
    client.clusters.get(opts[:cluster_id]).networks
  else
    []
  end

end

#new_interface(attr = {}) ⇒ Object


120
121
122
# File 'lib/foreman/model/ovirt.rb', line 120

def new_interface(attr={})
  Fog::Compute::Ovirt::Interface.new(attr)
end

#new_vm(attr = {}) ⇒ Object


111
112
113
114
115
116
117
118
# File 'lib/foreman/model/ovirt.rb', line 111

def new_vm(attr={})
  vm = super
  interfaces = nested_attributes_for :interfaces, attr[:interfaces_attributes]
  interfaces.map{ |i| vm.interfaces << new_interface(i)}
  volumes = nested_attributes_for :volumes, attr[:volumes_attributes]
  volumes.map{ |v| vm.volumes << new_volume(v)}
  vm
end

#new_volume(attr = {}) ⇒ Object


124
125
126
# File 'lib/foreman/model/ovirt.rb', line 124

def new_volume(attr={})
  Fog::Compute::Ovirt::Volume.new(attr)
end

#provided_attributesObject


21
22
23
# File 'lib/foreman/model/ovirt.rb', line 21

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

#save_vm(uuid, attr) ⇒ Object


128
129
130
131
132
133
134
135
136
# File 'lib/foreman/model/ovirt.rb', line 128

def save_vm(uuid, attr)
  vm = find_vm_by_uuid(uuid)
  vm.attributes.merge!(attr.symbolize_keys)
  update_interfaces(vm, attr[:interfaces_attributes])
  update_volumes(vm, attr[:volumes_attributes])
  vm.interfaces
  vm.volumes
  vm.save
end

#start_vm(uuid) ⇒ Object


93
94
95
# File 'lib/foreman/model/ovirt.rb', line 93

def start_vm(uuid)
  find_vm_by_uuid(uuid).start(:blocking => true)
end

#storage_domains(opts = {}) ⇒ Object


89
90
91
# File 'lib/foreman/model/ovirt.rb', line 89

def storage_domains(opts ={})
  client.storage_domains({:role => 'data'}.merge(opts))
end

#supports_update?Boolean


17
18
19
# File 'lib/foreman/model/ovirt.rb', line 17

def supports_update?
  true
end

#test_connection(options = {}) ⇒ Object


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/foreman/model/ovirt.rb', line 60

def test_connection options = {}
  super
  errors[:url].empty? && datacenters && test_https_required
rescue => e
  case e.message
    when /404/
      errors[:url] << e.message
    when /302/
      errors[:url] << 'HTTPS URL is required for API access'
    when /401/
      errors[:user] << e.message
    else
      errors[:base] << e.message
  end
end

#update_required?(old_attrs, new_attrs) ⇒ Boolean


164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/foreman/model/ovirt.rb', line 164

def update_required?(old_attrs, new_attrs)
  return true if super(old_attrs, new_attrs)

  new_attrs[:interfaces_attributes].each do |key, interface|
    return true if (interface[:id].blank? || interface[:_delete] == '1') && key != 'new_interfaces' #ignore the template
  end if new_attrs[:interfaces_attributes]

  new_attrs[:volumes_attributes].each do |key, volume|
    return true if (volume[:id].blank? || volume[:_delete] == '1') && key != 'new_volumes' #ignore the template
  end if new_attrs[:volumes_attributes]

  false
end