Class: ForemanOvirt::Ovirt
- Inherits:
-
ComputeResource
- Object
- ComputeResource
- ForemanOvirt::Ovirt
- Defined in:
- app/models/foreman_ovirt/ovirt.rb
Constant Summary collapse
- ALLOWED_DISPLAY_TYPES =
%w[vnc spice].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #associated_host(vm) ⇒ Object
- #available_clusters ⇒ Object
- #available_images ⇒ Object
- #available_networks(cluster_id = nil) ⇒ Object
- #available_operating_systems ⇒ Object
- #available_storage_domains(_cluster_id = nil) ⇒ Object
- #capabilities ⇒ Object
- #connect(options = {}) ⇒ Object
- #connection_properties_valid? ⇒ Boolean
- #console(uuid) ⇒ Object
- #create_vm(args = {}) ⇒ Object
- #datacenters(options = {}) ⇒ Object
- #destroy_vm(uuid) ⇒ Object
- #determine_os_type(host) ⇒ Object
- #display_type ⇒ Object
- #display_type=(display) ⇒ Object
- #display_types ⇒ Object
- #editable_network_interfaces? ⇒ Boolean
- #find_vm_by_uuid(uuid) ⇒ Object
- #get_datacenter_uuid(datacenter) ⇒ Object
- #get_ovirt_id(argument_list, argument_key, argument_value) ⇒ Object
- #host_compute_attrs(host) ⇒ Object
- #image_exists?(image) ⇒ Boolean
- #instance_type(id) ⇒ Object
- #keyboard_layout ⇒ Object
- #keyboard_layout=(layout) ⇒ Object
- #networks(opts = {}) ⇒ Object
- #new_interface(attr = {}) ⇒ Object
- #new_vm(attr = {}) ⇒ Object
- #new_volume(attr = {}) ⇒ Object
- #nictypes ⇒ Object
- #normalize_vm_attrs(vm_attrs) ⇒ Object
- #ovirt_quota ⇒ Object
- #ovirt_quota=(ovirt_quota_id) ⇒ Object
- #parse_vms_list_params(params) ⇒ Object
- #preallocate_and_clone_disks(args, template) ⇒ Object
- #provided_attributes ⇒ Object
- #public_key ⇒ Object
- #public_key=(key) ⇒ Object
- #sanitize_inherited_vm_attributes(args, template, instance_type) ⇒ Object
- #save_vm(uuid, attr) ⇒ Object
- #start_vm(uuid) ⇒ Object
- #start_with_cloudinit(uuid, user_data = nil) ⇒ Object
- #storage_domains(opts = {}) ⇒ Object
- #supports_operating_systems? ⇒ Boolean
- #supports_update? ⇒ Boolean
- #supports_vms_pagination? ⇒ Boolean
- #template(id) ⇒ Object
- #test_connection(options = {}) ⇒ Object
- #update_required?(old_attrs, new_attrs) ⇒ Boolean
- #user_data_supported? ⇒ Boolean
- #validate_quota ⇒ Object
- #vm_instance_defaults ⇒ Object
- #vnic_profiles ⇒ Object
Class Method Details
.available? ⇒ Boolean
22 23 24 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 22 def self.available? Fog::Compute.providers.include?(:ovirt) end |
.model_name ⇒ Object
26 27 28 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 26 def self.model_name ComputeResource.model_name end |
.provider_friendly_name ⇒ Object
440 441 442 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 440 def self.provider_friendly_name 'oVirt' end |
Instance Method Details
#associated_host(vm) ⇒ Object
436 437 438 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 436 def associated_host(vm) associate_by('mac', vm.interfaces.map(&:mac)) end |
#available_clusters ⇒ Object
229 230 231 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 229 def available_clusters clusters end |
#available_images ⇒ Object
128 129 130 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 128 def available_images templates end |
#available_networks(cluster_id = nil) ⇒ Object
233 234 235 236 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 233 def available_networks(cluster_id = nil) raise ::Foreman::Exception, N_('Cluster ID is required to list available networks') if cluster_id.nil? networks({ cluster_id: cluster_id }) end |
#available_operating_systems ⇒ Object
111 112 113 114 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 111 def return attrs[:available_operating_systems] if attrs.key?(:available_operating_systems) raise Foreman::Exception, 'Listing operating systems is not supported by the current version' end |
#available_storage_domains(_cluster_id = nil) ⇒ Object
238 239 240 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 238 def available_storage_domains(_cluster_id = nil) storage_domains end |
#capabilities ⇒ Object
40 41 42 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 40 def capabilities %i[build image new_volume] end |
#connect(options = {}) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 173 def connect( = {}) return unless connection_properties_valid? update_public_key datacenters && test_https_required rescue StandardError => e case e. when /404/ errors.add(:url, e.) when /302/ errors.add(:url, _('HTTPS URL is required for API access')) when /401/ errors.add(:user, e.) else errors.add(:base, e.) end end |
#connection_properties_valid? ⇒ Boolean
191 192 193 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 191 def connection_properties_valid? errors[:url].empty? && errors[:username].empty? && errors[:password].empty? end |
#console(uuid) ⇒ Object
410 411 412 413 414 415 416 417 418 419 420 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 410 def console(uuid) vm = find_vm_by_uuid(uuid) raise 'VM is not running!' if vm.status == 'down' 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(name: vm.name, type: vm.display[:type]) end |
#create_vm(args = {}) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 281 def create_vm(args = {}) args[:comment] = args[:user_data] if args[:user_data] args[:template] = args[:image_id] if args[:image_id] template = template(args[:template]) if args[:template] instance_type = instance_type(args[:instance_type]) unless args[:instance_type].empty? args[:cluster] = get_ovirt_id(clusters, 'cluster', args[:cluster]) args[:template] = args.delete(:vm_template) if args.key?(:vm_template) sanitize_inherited_vm_attributes(args, template, instance_type) preallocate_and_clone_disks(args, template) if args[:volumes_attributes].present? && template.present? vm = super({ first_boot_dev: 'network', quota: ovirt_quota }.merge(args)) begin create_interfaces(vm, args[:interfaces_attributes], args[:cluster]) unless args[:interfaces_attributes].empty? create_volumes(vm, args[:volumes_attributes]) unless args[:volumes_attributes].empty? rescue StandardError => e destroy_vm vm.id raise e end vm end |
#datacenters(options = {}) ⇒ Object
195 196 197 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 195 def datacenters( = {}) client.datacenters().map { |dc| [dc[:name], dc[:id]] } end |
#destroy_vm(uuid) ⇒ Object
390 391 392 393 394 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 390 def destroy_vm(uuid) find_vm_by_uuid(uuid).destroy rescue ActiveRecord::RecordNotFound true end |
#determine_os_type(host) ⇒ Object
76 77 78 79 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 106 107 108 109 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 76 def determine_os_type(host) return nil unless host return host.params['ovirt_ostype'] if host.params['ovirt_ostype'] ret = 'other_linux' return ret unless host. os_name = os_name_mapping(host) arch_name = arch_name_mapping(host) available = .select { |os| os[:name].present? } match_found = false best_matches = available.sort_by do |os| = 0.0 if os[:name].include?(os_name) match_found = true += 100 # prefer the shorter names a bit in case we have not found more important some specifics += (1.0 / os[:name].length) # bonus for major or major_minor += 10 if os[:name].include?("#{os_name}_#{host..major}") += 5 if os[:name].include?("#{os_name}_#{host..major}_#{host..minor}") # bonus for architecture += 10 if arch_name && os[:name].include?(arch_name) end end unless match_found logger.debug { 'No oVirt OS type found, returning other OS' } return available.first[:name] end logger.debug { "Available oVirt OS types: #{best_matches.map { |x| x[:name] }.join(',')}" } best_matches.last[:name] if best_matches.last end |
#display_type ⇒ Object
444 445 446 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 444 def display_type attrs[:display].presence || 'vnc' end |
#display_type=(display) ⇒ Object
448 449 450 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 448 def display_type=(display) attrs[:display] = display.downcase end |
#display_types ⇒ Object
150 151 152 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 150 def display_types ALLOWED_DISPLAY_TYPES end |
#editable_network_interfaces? ⇒ Boolean
211 212 213 214 215 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 211 def editable_network_interfaces? # we can't decide whether the networks are available when we # don't know the cluster_id, assuming it's possible true end |
#find_vm_by_uuid(uuid) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 44 def find_vm_by_uuid(uuid) vm = super return unless vm vm.define_singleton_method(:vm_template) do template end vm rescue Fog::Ovirt::Errors::OvirtEngineError raise(ActiveRecord::RecordNotFound) end |
#get_datacenter_uuid(datacenter) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 199 def get_datacenter_uuid(datacenter) return @datacenter_uuid if @datacenter_uuid if Foreman.is_uuid?(datacenter) @datacenter_uuid = datacenter else @datacenter_uuid = datacenters.select { |dc| dc[0] == datacenter } raise ::Foreman::Exception, N_('Datacenter was not found') if @datacenter_uuid.empty? @datacenter_uuid = @datacenter_uuid.first[1] end @datacenter_uuid end |
#get_ovirt_id(argument_list, argument_key, argument_value) ⇒ Object
306 307 308 309 310 311 312 313 314 315 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 306 def get_ovirt_id(argument_list, argument_key, argument_value) return argument_value if argument_value.blank? if argument_list.none? do |a| a.name == argument_value || a.id == argument_value end raise Foreman::Exception, "The #{argument_key} #{argument_value} is not valid, enter a correct id or name" end argument_list.detect { |a| a.name == argument_value }.try(:id) || argument_value end |
#host_compute_attrs(host) ⇒ Object
34 35 36 37 38 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 34 def host_compute_attrs(host) super.tap do |attrs| attrs[:os] = { type: determine_os_type(host) } if end end |
#image_exists?(image) ⇒ Boolean
132 133 134 135 136 137 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 132 def image_exists?(image) client.templates.get(image).present? rescue StandardError => e Foreman::Logging.exception('Error while checking if image exists', e) false end |
#instance_type(id) ⇒ Object
146 147 148 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 146 def instance_type(id) client.instance_types.get(id) || raise(ActiveRecord::RecordNotFound) end |
#keyboard_layout ⇒ Object
452 453 454 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 452 def keyboard_layout attrs[:keyboard_layout].presence || 'en-us' end |
#keyboard_layout=(layout) ⇒ Object
456 457 458 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 456 def keyboard_layout=(layout) attrs[:keyboard_layout] = layout.downcase end |
#networks(opts = {}) ⇒ Object
221 222 223 224 225 226 227 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 221 def networks(opts = {}) if opts[:cluster_id] client.clusters.get(opts[:cluster_id]).networks else [] end end |
#new_interface(attr = {}) ⇒ Object
368 369 370 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 368 def new_interface(attr = {}) Fog::Ovirt::Compute::Interface.new(attr) end |
#new_vm(attr = {}) ⇒ Object
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 350 def new_vm(attr = {}) vm = super vm.define_singleton_method(:vm_template) do template end vm.define_singleton_method(:vm_template=) do |value| self.template = value end 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
372 373 374 375 376 377 378 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 372 def new_volume(attr = {}) set_preallocated_attributes!(attr, attr[:preallocate]) raise ::Foreman::Exception, N_('VM volume attributes are not set properly') unless attr.all? do |_key, value| value.is_a? String end Fog::Ovirt::Compute::Volume.new(attr) end |
#nictypes ⇒ Object
501 502 503 504 505 506 507 508 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 501 def nictypes [ OpenStruct.new({ id: 'virtio', name: 'VirtIO' }), OpenStruct.new({ id: 'rtl8139', name: 'rtl8139' }), OpenStruct.new({ id: 'e1000', name: 'e1000' }), OpenStruct.new({ id: 'pci_passthrough', name: 'PCI Passthrough' }), ] end |
#normalize_vm_attrs(vm_attrs) ⇒ Object
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 468 def normalize_vm_attrs(vm_attrs) normalized = slice_vm_attributes(vm_attrs, %w[cores interfaces_attributes memory]) normalized['cluster_id'] = get_ovirt_id(clusters, 'cluster', vm_attrs['cluster']) normalized['cluster_name'] = clusters.detect { |c| c.id == normalized['cluster_id'] }.try(:name) normalized['template_id'] = get_ovirt_id(templates, 'template', vm_attrs['template']) normalized['template_name'] = templates.detect { |t| t.id == normalized['template_id'] }.try(:name) cluster_networks = networks(cluster_id: normalized['cluster_id']) interface_attrs = vm_attrs['interfaces_attributes'] || {} normalized['interfaces_attributes'] = interface_attrs.inject({}) do |interfaces, (key, nic)| interfaces.update(key => { 'name' => nic['name'], 'network_id' => nic['network'], 'network_name' => cluster_networks.detect do |n| n.id == nic['network'] end.try(:name) }) end volume_attrs = vm_attrs['volumes_attributes'] || {} normalized['volumes_attributes'] = volume_attrs.inject({}) do |volumes, (key, vol)| volumes.update(key => { 'size' => memory_gb_to_bytes(vol['size_gb']).to_s, 'storage_domain_id' => vol['storage_domain'], 'storage_domain_name' => storage_domains.detect do |d| d.id == vol['storage_domain'] end.try(:name), 'preallocate' => to_bool(vol['preallocate']), 'bootable' => to_bool(vol['bootable']) }) end normalized end |
#ovirt_quota ⇒ Object
124 125 126 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 124 def ovirt_quota attrs[:ovirt_quota_id].presence end |
#ovirt_quota=(ovirt_quota_id) ⇒ Object
120 121 122 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 120 def ovirt_quota=(ovirt_quota_id) attrs[:ovirt_quota_id] = ovirt_quota_id end |
#parse_vms_list_params(params) ⇒ Object
400 401 402 403 404 405 406 407 408 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 400 def parse_vms_list_params(params) max = (params['length'] || 10).to_i { search: params['search']['value'] || '', max: max, page: (params['start'].to_i / max) + 1, without_details: true, } end |
#preallocate_and_clone_disks(args, template) ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 317 def preallocate_and_clone_disks(args, template) volumes_to_change = args[:volumes_attributes].values.select { |x| x[:id].present? } return if volumes_to_change.blank? template_disks = template.volumes disks = volumes_to_change.map do |volume| if volume[:preallocate] == '1' { id: volume[:id], sparse: 'false', format: 'raw', storage_domain: volume[:storage_domain] } else template_volume = template_disks.detect { |v| v.id == volume['id'] } if template_volume.storage_domain != volume['storage_domain'] { id: volume['id'], storage_domain: volume['storage_domain'] } end end end.compact args.merge!(clone: true, disks: disks) if disks.present? end |
#provided_attributes ⇒ Object
116 117 118 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 116 def provided_attributes super.merge({ mac: :mac }) end |
#public_key ⇒ Object
460 461 462 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 460 def public_key attrs[:public_key] end |
#public_key=(key) ⇒ Object
464 465 466 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 464 def public_key=(key) attrs[:public_key] = key end |
#sanitize_inherited_vm_attributes(args, template, instance_type) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 261 def sanitize_inherited_vm_attributes(args, template, instance_type) # Override memory an cores values if template and/or instance type is/are provided. # Take template values if blank values for VM attributes, # because oVirt will fail if empty values are present in VM definition # Instance type values always take precedence on templates or vm provided values if template template_cores = template.cores.to_i if template.cores.present? template_memory = template.memory.to_i if template.memory.present? args[:cores] = template_cores if template_cores && args[:cores].blank? args[:memory] = template_memory if template_memory && args[:memory].blank? end return unless instance_type instance_type_cores = instance_type.cores.to_i if instance_type.cores.present? instance_type_sockets = instance_type.sockets.to_i if instance_type.sockets.present? instance_type_memory = instance_type.memory.to_i if instance_type.memory.present? args[:cores] = instance_type_cores if instance_type_cores args[:sockets] = instance_type_sockets if instance_type_sockets args[:memory] = instance_type_memory if instance_type_memory end |
#save_vm(uuid, attr) ⇒ Object
380 381 382 383 384 385 386 387 388 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 380 def save_vm(uuid, attr) vm = find_vm_by_uuid(uuid) vm.attributes.deep_merge!(attr.deep_symbolize_keys).deep_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
246 247 248 249 250 251 252 253 254 255 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 246 def start_vm(uuid) vm = find_vm_by_uuid(uuid) if %r{cloud-config|^#!/}.match?(vm.comment.to_s) vm.start_with_cloudinit(blocking: true, user_data: vm.comment, use_custom_script: true) vm.comment = '' vm.save else vm.start(blocking: true) end end |
#start_with_cloudinit(uuid, user_data = nil) ⇒ Object
257 258 259 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 257 def start_with_cloudinit(uuid, user_data = nil) find_vm_by_uuid(uuid).start_with_cloudinit(blocking: true, user_data: user_data, use_custom_script: true) end |
#storage_domains(opts = {}) ⇒ Object
242 243 244 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 242 def storage_domains(opts = {}) client.storage_domains({ role: %w[data volume] }.merge(opts)) end |
#supports_operating_systems? ⇒ Boolean
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 61 def if client.respond_to?(:operating_systems) unless attrs.key?(:available_operating_systems) save end attrs[:available_operating_systems] != :unsupported else false end rescue Foreman::FingerprintException logger.info 'Unable to verify OS capabilities, SSL certificate verification failed' false end |
#supports_update? ⇒ Boolean
57 58 59 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 57 def supports_update? true end |
#supports_vms_pagination? ⇒ Boolean
396 397 398 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 396 def supports_vms_pagination? true end |
#template(id) ⇒ Object
139 140 141 142 143 144 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 139 def template(id) compute = client.templates.get(id) || raise(ActiveRecord::RecordNotFound) compute.interfaces compute.volumes compute end |
#test_connection(options = {}) ⇒ Object
168 169 170 171 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 168 def test_connection( = {}) super connect() end |
#update_required?(old_attrs, new_attrs) ⇒ Boolean
422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 422 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' end new_attrs[:volumes_attributes]&.each do |key, volume| return true if (volume[:id].blank? || volume[:_delete] == '1') && key != 'new_volumes' # ignore the template end false end |
#user_data_supported? ⇒ Boolean
30 31 32 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 30 def user_data_supported? true end |
#validate_quota ⇒ Object
510 511 512 513 514 515 516 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 510 def validate_quota attrs[:ovirt_quota_id] = if attrs[:ovirt_quota_id].nil? client.quotas.first.id else get_ovirt_id(client.quotas, 'quota', attrs[:ovirt_quota_id]) end end |
#vm_instance_defaults ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 338 def vm_instance_defaults super.merge( memory: 1024.megabytes, cores: '1', sockets: '1', display: { type: display_type, keyboard_layout: keyboard_layout, port: -1, monitors: 1 } ) end |
#vnic_profiles ⇒ Object
217 218 219 |
# File 'app/models/foreman_ovirt/ovirt.rb', line 217 def vnic_profiles client.list_vnic_profiles end |