class OVIRT::VM

Attributes

clone[R]
cluster[R]
comment[RW]
cores[R]
creation_time[R]
description[R]
display[R]
ha[R]
ha_priority[R]
host[R]
instance_type[R]
interfaces[RW]
ips[R]
memory[R]
os[R]
profile[R]
quota[R]
status[R]
storage[R]
template[R]
vnc[R]
volumes[RW]

Public Class Methods

cloudinit(opts={}) click to toggle source
    # File lib/ovirt/vm.rb
149 def self.cloudinit(opts={})
150   hostname            = opts[:hostname]
151   ip                  = opts[:ip]
152   netmask             = opts[:netmask]
153   dns                 = opts[:dns]
154   gateway             = opts[:gateway]
155   domain              = opts[:domain]
156   nicname             = opts[:nicname]
157   nicsdef             = opts[:nicsdef]
158   user                = opts[:user] || 'root'
159   password            = opts[:password]
160   ssh_authorized_keys = opts[:ssh_authorized_keys]
161   fileslist           = opts[:files]
162   runcmd              = opts[:runcmd]
163   cluster_major, cluster_minor = opts[:cluster_version]
164   api_major,api_minor, api_build, api_revision = opts[:api_version]
165   extracmd            = nil 
166   unless opts[:phone_home].nil?
167     phone_home = \
168     "phone_home:\n" \
169     "  url: #{opts[:phone_home]['url']}\n" \
170     "  post: #{opts[:phone_home]['post']}\n"
171     extracmd   = phone_home
172   end
173   cmdlist             = 'runcmd:'
174   unless runcmd.nil?
175     runcmd.each do |cmd|
176       cmdlist = \
177       "#{cmdlist}\n" \
178       "- #{cmd}\n"
179     end
180     if extracmd.nil?
181       extracmd = cmdlist
182     else
183       extracmd = extracmd +cmdlist
184     end
185   end
186   builder   = Nokogiri::XML::Builder.new do
187     action {
188       # An API has changed in version 3.5.5. Make sure
189       # <use_cloud_init>true</use_cloud_init> is used only if the endpoint
190       # if of that version or higher and the cluster the VM is provisioned
191       # to is of version 3.5 or higher.
192       # NOTE: RHEV-m/OVirt versions prior to 3.6.0 contain a bug
193       # (https://bugzilla.redhat.com/show_bug.cgi?id=1206068) which causes
194       # that build and revision parameters ov API version are set to 0.
195       # This may have an impact on conditional inclusion of <use_cloud_init>
196       # and thus leaving the guest unconfigured.
197       # You can either upgrade to RHEV-m/OVirt 3.6+ or work this around by
198       # manually forcing the ovirt engine to report an appropriate version:
199       # % rhevm-config -s VdcVersion=<major>.<minor>.<build>.<revision>
200       # % service ovirt-engine restart
201       # Please replace the placeholders with appropriate values.
202       if api_major > 3 || (api_major == 3 && api_minor > 5) || (api_major == 3 && api_minor == 5 && api_build >= 5)
203         use_cloud_init true if cluster_major > 3 || (cluster_major == 3 && cluster_minor >= 5)
204       end
205       vm {
206         initialization {
207           unless runcmd.nil?
208             custom_script cmdlist
209           end
210           unless phone_home.nil?
211             custom_script phone_home
212           end
213           cloud_init {
214             unless hostname.nil?
215               host { address hostname  }
216             end
217             unless password.nil?
218               users { 
219                 user {  
220                   user_name user
221                   password password
222                 }
223               }
224             end
225             unless ssh_authorized_keys.nil?
226               authorized_keys {
227                 authorized_key {
228                   user { user_name user }
229                   ssh_authorized_keys.each do |sshkey|
230                     key sshkey    
231                   end
232                 }
233               }
234             end
235             network_configuration {
236               if !nicname.nil?
237                 nics {
238                   nic {
239                     name_ nicname
240                     unless ip.nil? || netmask.nil? || gateway.nil?
241                       network { ip(:'address'=> ip , :'netmask'=> netmask, :'gateway'=> gateway ) }
242                       boot_protocol 'STATIC'
243                       on_boot 'true'
244                     end 
245                   }
246                 }
247               elsif nicsdef.is_a?(Array) && !nicsdef.empty? && nicsdef.all? { |n| n.is_a?(Hash) }
248                 nics {
249                   nicsdef.each do |n|
250                     nic {
251                       name_(n[:nicname])
252                       boot_protocol_(n[:boot_protocol] || 'NONE') # Defaults to NONE boot proto
253                       on_boot_(n[:on_boot] || 'true') # Defaults to 'true'
254                       unless n[:ip].nil? || n[:netmask].nil?
255                         network_ {
256                           n[:gateway].nil? ?
257                           ip_(:address => n[:ip], :netmask => n[:netmask]) :
258                           ip_(:address => n[:ip], :netmask => n[:netmask], :gateway => n[:gateway])
259                         }
260                       end
261                     }
262                   end
263                 }
264               end
265               dns { 
266                 if dns.is_a?(String)
267                   servers { host { address dns }}
268                 elsif dns.is_a?(Array) && dns.all? {|n| n.is_a?(String) }
269                   servers { host { address dns.join(' ') }}
270                 end
271                 if domain.is_a?(String)
272                   search_domains { host {  address domain }}
273                 elsif domain.is_a?(Array) && domain.all? {|n| n.is_a?(String) }
274                   search_domains { host { address domain.join(' ')}}
275                 end
276               }
277             }
278           regenerate_ssh_keys 'true'
279           files {
280             unless extracmd.nil?
281               file_ {
282                 name_   'ignored'
283                 content extracmd
284                 type    'PLAINTEXT'
285                }
286             end
287             unless fileslist.nil?
288               fileslist.each do |fileentry|
289                 file {
290                   name_   fileentry['path']
291                   content fileentry['content']
292                   type    'PLAINTEXT'
293                 }
294               end
295             end
296           }
297          }
298         }
299       }
300     }
301   end
302   Nokogiri::XML(builder.to_xml).root.to_xml
303 end
new(client, xml) click to toggle source
Calls superclass method OVIRT::BaseObject::new
   # File lib/ovirt/vm.rb
11 def initialize(client, xml)
12   super(client, xml[:id], xml[:href], (xml/'name').first.text)
13   parse_xml_attributes!(xml)
14 end
ticket(options={}) click to toggle source
   # File lib/ovirt/vm.rb
42 def self.ticket options={}
43   builder = Nokogiri::XML::Builder.new do
44     action_{ ticket_{ expiry_(options[:expiry] || 120) } }
45   end
46   Nokogiri::XML(builder.to_xml).root.to_s
47 end
to_xml(opts={}) click to toggle source
    # File lib/ovirt/vm.rb
 49 def self.to_xml(opts={})
 50   builder = Nokogiri::XML::Builder.new do
 51     vm{
 52       name_ opts[:name] || "i-#{Time.now.to_i}"
 53       description_ opts[:description] || ""
 54       if opts[:comment]
 55         comment_ opts[:comment]
 56       end
 57       if opts[:template] && !opts[:template].empty?
 58         template_ :id => (opts[:template])
 59       elsif opts[:template_name] && !opts[:template_name].empty?
 60         template_{ name_(opts[:template_name])}
 61       else
 62         template_{name_('Blank')}
 63       end
 64       if opts[:instance_type] && !opts[:instance_type].empty?
 65         instance_type( :id => opts[:instance_type])
 66       end
 67       if opts[:quota]
 68         quota_( :id => opts[:quota])
 69       end
 70       if opts[:cluster]
 71         cluster_( :id => opts[:cluster])
 72       elsif opts[:cluster_name]
 73         cluster_{ name_(opts[:cluster_name])}
 74       end
 75       type_ opts[:hwp_id] || 'Server'
 76       if opts[:memory]
 77           memory opts[:memory]
 78       end
 79       if opts[:cores]
 80          cpu {
 81            topology( :cores => (opts[:cores] || '1'), :sockets => '1' )
 82          }
 83       end
 84       # os element must not be sent when template is present (RHBZ 1104235)
 85       if opts[:template].nil? || opts[:template].empty?
 86         os_opts = opts[:os] ? opts[:os].dup : {}
 87         os_opts[:type] ||= opts[:os_type] || 'unassigned'
 88         os_opts[:boot] ||= [opts.fetch(:boot_dev1, 'network'), opts.fetch(:boot_dev2, 'hd')]
 89         os_opts[:kernel] ||= opts[:os_kernel]
 90         os_opts[:initrd] ||= opts[:os_initrd]
 91         os_opts[:cmdline] ||= opts[:os_cmdline]
 92         if opts[:first_boot_dev]
 93           os_opts[:boot] = os_opts[:boot].sort_by.with_index do |device, index|
 94             device == opts[:first_boot_dev] ? -1 : index
 95           end
 96         end
 97         os(:type => os_opts[:type]) do
 98           os_opts[:boot].each { |device| boot(:dev => device) }
 99           kernel os_opts[:kernel]
100           initrd os_opts[:initrd]
101           cmdline os_opts[:cmdline]
102         end
103       end
104       if !opts[:ha].nil? || !opts[:ha_priority].nil?
105         high_availability_{
106           enabled_(opts[:ha]) unless opts[:ha].nil?
107           priority_(opts[:ha_priority]) unless opts[:ha_priority].nil?
108         }
109       end
110       disks_ {
111         clone_(opts[:clone]) if opts[:clone]
112         if opts[:disks]
113           opts[:disks].each do |d|
114             disk(:id => d[:id]) {
115               storage_domains { storage_domain(:id => d[:storagedomain]) }
116             }
117           end
118         end
119       }
120       display_{
121         type_(opts[:display][:type])
122       } if opts[:display]
123       custom_properties {
124         custom_property({
125           :name => "floppyinject",
126           :value => "#{opts[:fileinject_path] || OVIRT::FILEINJECT_PATH}:#{opts[:user_data]}",
127           :regexp => "^([^:]+):(.*)$"})
128       } if(opts[:user_data_method] && opts[:user_data_method] == :custom_property)
129       payloads {
130         payload(:type => 'floppy') {
131           file(:name => "#{opts[:fileinject_path] || OVIRT::FILEINJECT_PATH}") { content(Base64::decode64(opts[:user_data])) }
132         }
133       } if(opts[:user_data_method] && opts[:user_data_method] == :payload)
134       payloads {
135         payload(:type => 'floppy') {
136           files {
137             file {
138               name_ "#{opts[:fileinject_path] || OVIRT::FILEINJECT_PATH}"
139               content Base64::decode64(opts[:user_data])
140             }
141           }
142         }
143       } if(opts[:user_data_method] && opts[:user_data_method] == :payload_v3_3)
144     }
145   end
146   Nokogiri::XML(builder.to_xml).root.to_s
147 end

Public Instance Methods

ready?() click to toggle source

In oVirt 3.1 a vm can be marked down and not locked while its volumes are locked. This method indicates if it is safe to launch the vm.

   # File lib/ovirt/vm.rb
22 def ready?
23   return false unless @status =~ /down/i
24   volumes.each do |volume|
25     return false if volume.status =~ /locked/i
26   end
27   true
28 end
running?() click to toggle source
   # File lib/ovirt/vm.rb
16 def running?
17   !(@status =~ /down/i) && !(@status =~ /wait_for_launch/i)
18 end

Private Instance Methods

parse_xml_attributes!(xml) click to toggle source
    # File lib/ovirt/vm.rb
307 def parse_xml_attributes!(xml)
308   @description = ((xml/'description').first.text rescue '')
309   @comment = ((xml/'comment').first.text rescue '')
310   @status = ((xml/'status').first.text rescue 'unknown')
311   @memory = (xml/'memory').first.text
312   @profile = (xml/'type').first.text
313   @template = Link::new(@client, (xml/'template').first[:id], (xml/'template').first[:href])
314   @instance_type = Link::new(@client, (xml/'instance_type').first[:id], (xml/'instance_type').first[:href]) rescue nil
315   @host = Link::new(@client, (xml/'host').first[:id], (xml/'host').first[:href]) rescue nil
316   @cluster = Link::new(@client, (xml/'cluster').first[:id], (xml/'cluster').first[:href])
317   @display = {
318     :type => ((xml/'display/type').first.text rescue ''),
319     :address => ((xml/'display/address').first.text rescue nil),
320     :port => ((xml/'display/port').first.text rescue nil),
321     :secure_port => ((xml/'display/secure_port').first.text rescue nil),
322     :subject => ((xml/'display/certificate/subject').first.text rescue nil),
323     :monitors => ((xml/'display/monitors').first.text rescue 0)
324   }
325   @cores = ((xml/'cpu/topology').first[:cores].to_i * (xml/'cpu/topology').first[:sockets].to_i rescue nil)
326   @storage = ((xml/'disks/disk/size').first.text rescue nil)
327   @creation_time = (xml/'creation_time').text
328   @ips = (xml/'guest_info/ips/ip').map { |ip| ip[:address] }
329   @vnc = {
330     :address => ((xml/'display/address').first.text rescue "127.0.0.1"),
331     :port => ((xml/'display/port').first.text rescue "5890")
332   } unless @ip
333   @os = {
334       :type => (xml/'os').first[:type],
335       :boot => (xml/'os/boot').collect {|boot| boot[:dev] }
336   }
337   @ha = ((xml/'high_availability/enabled').first.text rescue nil)
338   @ha_priority = ((xml/'high_availability/priority').first.text rescue nil)
339   @quota = ((xml/'quota').first[:id] rescue nil)
340 
341   disks = xml/'disks/disk'
342   @volumes = disks.length > 0 ? disks.collect {|disk| OVIRT::Volume::new(@client, disk)} : nil
343 
344   interfaces = xml/'nics/nic'
345   @interfaces = interfaces.length > 0 ? interfaces.collect {|nic| OVIRT::Interface::new(@client, nic)} : nil
346 
347   @clone = ((xml/'disks/clone').first.text rescue nil)
348 end