Revision 96ede451
Added by Amos Benari about 12 years ago
- ID 96ede451fc45d6d8e5786d62d4c92adc97c46ad3
lib/foreman/model/ovirt.rb | ||
---|---|---|
client.templates.get(id) || raise(ActiveRecord::RecordNotFound)
|
||
end
|
||
|
||
def find_vm_by_uuid uuid
|
||
client.servers.get(uuid) || raise(ActiveRecord::RecordNotFound)
|
||
end
|
||
|
||
def clusters
|
||
client.clusters
|
||
end
|
||
... | ... | |
|
||
end
|
||
|
||
def start_vm uuid
|
||
def storage_domains(opts ={})
|
||
client.storage_domains({:role => 'data'}.merge(opts))
|
||
end
|
||
|
||
def start_vm(uuid)
|
||
find_vm_by_uuid(uuid).start(:blocking => true)
|
||
end
|
||
|
||
def create_vm args = {}
|
||
def create_vm(args = {})
|
||
#ovirt doesn't accept '.' in vm name.
|
||
args[:name] = args[:name].parameterize
|
||
args[:display] = 'VNC'
|
||
vm = super args
|
||
vm = super args
|
||
begin
|
||
set_interfaces(vm, args[:interfaces_attributes])
|
||
create_interfaces(vm, args[:interfaces_attributes])
|
||
create_volumes(vm, args[:volumes_attributes])
|
||
rescue => e
|
||
destroy_vm vm.id
|
||
raise e
|
||
... | ... | |
vm
|
||
end
|
||
|
||
def new_vm attr={}
|
||
def new_vm(attr={})
|
||
vm = client.servers.new vm_instance_defaults.merge(attr)
|
||
attr[:interfaces_attributes].each do |key, interface|
|
||
vm.interfaces << new_interface(interface) unless (interface[:_delete] =='1' && interface[:id].nil?) || key == 'new_interfaces' #ignore the template
|
||
end if attr[:interfaces_attributes]
|
||
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
|
||
|
||
def new_interface attr={}
|
||
def new_interface(attr={})
|
||
Fog::Compute::Ovirt::Interface.new(attr)
|
||
end
|
||
|
||
def save_vm uuid, attr
|
||
def new_volume(attr={})
|
||
Fog::Compute::Ovirt::Volume.new(attr)
|
||
end
|
||
|
||
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
|
||
|
||
def destroy_vm uuid
|
||
def destroy_vm(uuid)
|
||
begin
|
||
find_vm_by_uuid(uuid).destroy
|
||
rescue OVIRT::OvirtException => e
|
||
... | ... | |
|
||
protected
|
||
|
||
def bootstrap args
|
||
def bootstrap(args)
|
||
client.servers.bootstrap vm_instance_defaults.merge(args.to_hash)
|
||
rescue Fog::Errors::Error => e
|
||
errors.add(:base, e.to_s)
|
||
... | ... | |
new_attrs[:interfaces_attributes].each do |key, interface|
|
||
return true if (interface[:id].nil? || 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].nil? || volume[:_delete] == '1') && key != 'new_volumes' #ignore the template
|
||
end if new_attrs[:volumes_attributes]
|
||
|
||
false
|
||
end
|
||
|
||
def console uuid
|
||
vm = find_vm_by_uuid(uuid)
|
||
raise "VM is not running!" if vm.status == "down"
|
||
raise "Spice display is not supported at the moment" if vm.display[:type] =~ /spice/i
|
||
VNCProxy.start :host => vm.display[:address], :host_port => vm.display[:port], :password => vm.ticket
|
||
def console(uuid)
|
||
vm = find_vm_by_uuid(uuid)
|
||
raise "VM is not running!" if vm.status == "down"
|
||
raise "Spice display is not supported at the moment" if vm.display[:type] =~ /spice/i
|
||
VNCProxy.start(:host => vm.display[:address], :host_port => vm.display[:port], :password => vm.ticket)
|
||
end
|
||
|
||
private
|
||
def set_interfaces(vm, attrs)
|
||
def create_interfaces(vm, attrs)
|
||
#first remove all existing interfaces
|
||
vm.interfaces.each do |interface|
|
||
#The blocking true is a work-around for ovirt bug, it should be removed.
|
||
vm.destroy_interface(:id => interface.id, :blocking => true)
|
||
end if vm.interfaces
|
||
#add interfaces
|
||
attrs.each do |key, interface|
|
||
vm.add_interface(interface) if interface[:id].nil? && interface[:_delete] != '1' && key != 'new_interfaces'
|
||
end if attrs
|
||
interfaces = nested_attributes_for :interfaces, attrs
|
||
interfaces.map{ |i| vm.add_interface(i)}
|
||
vm.interfaces.reload
|
||
end
|
||
|
||
def create_volumes(vm, attrs)
|
||
#add volumes
|
||
volumes = nested_attributes_for :volumes, attrs
|
||
#if no volume was set as bootable set the first volume.
|
||
volumes.first[:bootable] = true if volumes.first && volumes.map{|vol| true if vol[:bootable] == '1'}.compact().empty?
|
||
#The blocking true is a work-around for ovirt bug, it should be removed.
|
||
volumes.map{ |vol| vm.add_volume(vol.merge(:blocking => true)) if vol[:storage_domain]}
|
||
vm.volumes.reload
|
||
end
|
||
|
||
def update_interfaces(vm, attrs)
|
||
attrs.each do |key, interface|
|
||
unless key == 'new_interfaces' #ignore the template
|
||
... | ... | |
end if attrs
|
||
end
|
||
|
||
def update_volumes(vm, attrs)
|
||
attrs.each do |key, volume|
|
||
unless key == 'new_volumes' #ignore the template
|
||
vm.destroy_volume(:id => volume[:id]) if volume[:_delete] == '1' && volume[:id]
|
||
vm.add_volume(volume) if volume[:id].nil?
|
||
end
|
||
end if attrs
|
||
end
|
||
|
||
end
|
||
end
|
Also available in: Unified diff
fixes #1574 - ovirt - add remove and show volumes