Class: OVIRT::VM

Inherits:
BaseObject show all
Defined in:
lib/ovirt/vm.rb

Instance Attribute Summary collapse

Attributes inherited from BaseObject

#client, #href, #id, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseObject

#parse_bool, #parse_version

Constructor Details

#initialize(client, xml) ⇒ VM

Returns a new instance of VM.



11
12
13
14
# File 'lib/ovirt/vm.rb', line 11

def initialize(client, xml)
  super(client, xml[:id], xml[:href], (xml/'name').first.text)
  parse_xml_attributes!(xml)
end

Instance Attribute Details

#cloneObject (readonly)

Returns the value of attribute clone.



8
9
10
# File 'lib/ovirt/vm.rb', line 8

def clone
  @clone
end

#clusterObject (readonly)

Returns the value of attribute cluster.



7
8
9
# File 'lib/ovirt/vm.rb', line 7

def cluster
  @cluster
end

#commentObject

Returns the value of attribute comment.



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

def comment
  @comment
end

#coresObject (readonly)

Returns the value of attribute cores.



8
9
10
# File 'lib/ovirt/vm.rb', line 8

def cores
  @cores
end

#creation_timeObject (readonly)

Returns the value of attribute creation_time.



8
9
10
# File 'lib/ovirt/vm.rb', line 8

def creation_time
  @creation_time
end

#descriptionObject (readonly)

Returns the value of attribute description.



7
8
9
# File 'lib/ovirt/vm.rb', line 7

def description
  @description
end

#displayObject (readonly)

Returns the value of attribute display.



7
8
9
# File 'lib/ovirt/vm.rb', line 7

def display
  @display
end

#haObject (readonly)

Returns the value of attribute ha.



8
9
10
# File 'lib/ovirt/vm.rb', line 8

def ha
  @ha
end

#ha_priorityObject (readonly)

Returns the value of attribute ha_priority.



8
9
10
# File 'lib/ovirt/vm.rb', line 8

def ha_priority
  @ha_priority
end

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/ovirt/vm.rb', line 7

def host
  @host
end

#interfacesObject

Returns the value of attribute interfaces.



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

def interfaces
  @interfaces
end

#ipsObject (readonly)

Returns the value of attribute ips.



8
9
10
# File 'lib/ovirt/vm.rb', line 8

def ips
  @ips
end

#memoryObject (readonly)

Returns the value of attribute memory.



7
8
9
# File 'lib/ovirt/vm.rb', line 7

def memory
  @memory
end

#osObject (readonly)

Returns the value of attribute os.



8
9
10
# File 'lib/ovirt/vm.rb', line 8

def os
  @os
end

#profileObject (readonly)

Returns the value of attribute profile.



7
8
9
# File 'lib/ovirt/vm.rb', line 7

def profile
  @profile
end

#quotaObject (readonly)

Returns the value of attribute quota.



8
9
10
# File 'lib/ovirt/vm.rb', line 8

def quota
  @quota
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/ovirt/vm.rb', line 7

def status
  @status
end

#storageObject (readonly)

Returns the value of attribute storage.



8
9
10
# File 'lib/ovirt/vm.rb', line 8

def storage
  @storage
end

#templateObject (readonly)

Returns the value of attribute template.



7
8
9
# File 'lib/ovirt/vm.rb', line 7

def template
  @template
end

#vncObject (readonly)

Returns the value of attribute vnc.



8
9
10
# File 'lib/ovirt/vm.rb', line 8

def vnc
  @vnc
end

#volumesObject

Returns the value of attribute volumes.



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

def volumes
  @volumes
end

Class Method Details

.cloudinit(opts = {}) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/ovirt/vm.rb', line 146

def self.cloudinit(opts={})
  hostname            = opts[:hostname]
  ip                  = opts[:ip]
  netmask             = opts[:netmask]
  dns                 = opts[:dns]
  gateway             = opts[:gateway]
  domain              = opts[:domain]
  nicname             = opts[:nicname]
  nicsdef             = opts[:nicsdef]
  user                = opts[:user] || 'root'
  password            = opts[:password]
  ssh_authorized_keys = opts[:ssh_authorized_keys]
  fileslist           = opts[:files]
  runcmd              = opts[:runcmd]
  cluster_major, cluster_minor = opts[:cluster_version]
  api_major,api_minor, api_build, api_revision = opts[:api_version]
  extracmd            = nil 
  unless opts[:phone_home].nil?
    phone_home = \
    "phone_home:\n" \
    "  url: #{opts[:phone_home]['url']}\n" \
    "  post: #{opts[:phone_home]['post']}\n"
    extracmd   = phone_home
  end
  cmdlist             = 'runcmd:'
  unless runcmd.nil?
    runcmd.each do |cmd|
      cmdlist = \
	  "#{cmdlist}\n" \
      "- #{cmd}\n"
    end
    if extracmd.nil?
      extracmd = cmdlist
    else
      extracmd = extracmd +cmdlist
    end
  end
  builder   = Nokogiri::XML::Builder.new do
    action {
      # An API has changed in version 3.5.5. Make sure
      # <use_cloud_init>true</use_cloud_init> is used only if the endpoint
      # if of that version or higher and the cluster the VM is provisioned
      # to is of version 3.5 or higher.
      # NOTE: RHEV-m/OVirt versions prior to 3.6.0 contain a bug
      # (https://bugzilla.redhat.com/show_bug.cgi?id=1206068) which causes
      # that build and revision parameters ov API version are set to 0.
      # This may have an impact on conditional inclusion of <use_cloud_init>
      # and thus leaving the guest unconfigured.
      # You can either upgrade to RHEV-m/OVirt 3.6+ or work this around by
      # manually forcing the ovirt engine to report an appropriate version:
      # % rhevm-config -s VdcVersion=<major>.<minor>.<build>.<revision>
      # % service ovirt-engine restart
      # Please replace the placeholders with appropriate values. 
      if api_major > 3 || (api_major == 3 && api_minor > 5) || (api_major == 3 && api_minor == 5 && api_build >= 5)
        use_cloud_init true if cluster_major > 3 || (cluster_major == 3 && cluster_minor >= 5)
      end
      vm {
        initialization {
          unless runcmd.nil?
            custom_script cmdlist
          end
          unless phone_home.nil?
            custom_script phone_home
          end
          cloud_init {
            unless hostname.nil?
              host { address hostname  }
            end
            unless password.nil?
              users { 
                user {  
                  user_name user
                  password password
                }
              }
            end
            unless ssh_authorized_keys.nil?
              authorized_keys {
                authorized_key {
                  user { user_name user }
                  ssh_authorized_keys.each do |sshkey|
                    key sshkey    
                  end
                }
              }
            end
            network_configuration {
              if !nicname.nil?
                nics {
                  nic {
                    name_ nicname
                    unless ip.nil? || netmask.nil? || gateway.nil?
                      network { ip(:'address'=> ip , :'netmask'=> netmask, :'gateway'=> gateway ) }
                      boot_protocol 'STATIC'
                      on_boot 'true'
                    end 
                  }
                }
              elsif nicsdef.is_a?(Array) && !nicsdef.empty? && nicsdef.all? { |n| n.is_a?(Hash) }
                nics {
                  nicsdef.each do |n|
                    nic {
                      name_(n[:nicname])
                      boot_protocol_(n[:boot_protocol] || 'NONE') # Defaults to NONE boot proto
                      on_boot_(n[:on_boot] || 'true') # Defaults to 'true'
                      unless n[:ip].nil? || n[:netmask].nil?
                        network_ {
                          n[:gateway].nil? ?
                          ip_(:address => n[:ip], :netmask => n[:netmask]) :
                          ip_(:address => n[:ip], :netmask => n[:netmask], :gateway => n[:gateway])
                        }
                      end
                    }
                  end
                }
              end
              dns { 
                unless dns.nil? 
                  servers {
                    dns.each do |dnsentry|
                      host {  address dnsentry }
                    end
                  }
                end
                unless domain.nil? 
                  search_domains { host {  address domain }}
                end
              }
            }
          regenerate_ssh_keys 'true'
          files {
            unless extracmd.nil?
              file_ {
                name_   'ignored'
                content extracmd
                type    'PLAINTEXT'
               }
            end
            unless fileslist.nil?
              fileslist.each do |fileentry|
                file {
                  name_   fileentry['path']
                  content fileentry['content']
                  type    'PLAINTEXT'
                }
              end
            end
          }
         }
        }
      }
    }
  end
  Nokogiri::XML(builder.to_xml).root.to_xml
end

.ticket(options = {}) ⇒ Object



42
43
44
45
46
47
# File 'lib/ovirt/vm.rb', line 42

def self.ticket options={}
  builder = Nokogiri::XML::Builder.new do
    action_{ ticket_{ expiry_(options[:expiry] || 120) } }
  end
  Nokogiri::XML(builder.to_xml).root.to_s
end

.to_xml(opts = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ovirt/vm.rb', line 49

def self.to_xml(opts={})
  builder = Nokogiri::XML::Builder.new do
    vm{
      name_ opts[:name] || "i-#{Time.now.to_i}"
      description_ opts[:description] || ""
      if opts[:comment]
        comment_ opts[:comment]
      end
      if opts[:template] && !opts[:template].empty?
        template_ :id => (opts[:template])
      elsif opts[:template_name] && !opts[:template_name].empty?
        template_{ name_(opts[:template_name])}
      else
        template_{name_('Blank')}
      end
      if opts[:quota]
        quota_( :id => opts[:quota])
      end
      if opts[:cluster]
        cluster_( :id => opts[:cluster])
      elsif opts[:cluster_name]
        cluster_{ name_(opts[:cluster_name])}
      end
      type_ opts[:hwp_id] || 'Server'
      if opts[:memory]
          memory opts[:memory]
      end
      if opts[:cores]
         cpu {
           topology( :cores => (opts[:cores] || '1'), :sockets => '1' )
         }
      end
      # os element must not be sent when template is present (RHBZ 1104235)
      if opts[:template].nil? || opts[:template].empty?
        os_opts = opts[:os] ? opts[:os].dup : {}
        os_opts[:type] ||= opts[:os_type] || 'unassigned'
        os_opts[:boot] ||= [opts.fetch(:boot_dev1, 'network'), opts.fetch(:boot_dev2, 'hd')]
        os_opts[:kernel] ||= opts[:os_kernel]
        os_opts[:initrd] ||= opts[:os_initrd]
        os_opts[:cmdline] ||= opts[:os_cmdline]
        if opts[:first_boot_dev]
          os_opts[:boot] = os_opts[:boot].sort_by.with_index do |device, index|
            device == opts[:first_boot_dev] ? -1 : index
          end
        end
        os(:type => os_opts[:type]) do
          os_opts[:boot].each { |device| boot(:dev => device) }
          kernel os_opts[:kernel]
          initrd os_opts[:initrd]
          cmdline os_opts[:cmdline]
        end
      end
      if !opts[:ha].nil? || !opts[:ha_priority].nil?
        high_availability_{
          enabled_(opts[:ha]) unless opts[:ha].nil?
          priority_(opts[:ha_priority]) unless opts[:ha_priority].nil?
        }
      end
      disks_ {
        clone_(opts[:clone]) if opts[:clone]
        if opts[:disks]
          opts[:disks].each do |d|
            disk(:id => d[:id]) {
              storage_domains { storage_domain(:id => d[:storagedomain]) }
            }
          end
        end
      }
      display_{
        type_(opts[:display][:type])
      } if opts[:display]
      custom_properties {
        custom_property({
          :name => "floppyinject",
          :value => "#{opts[:fileinject_path] || OVIRT::FILEINJECT_PATH}:#{opts[:user_data]}",
          :regexp => "^([^:]+):(.*)$"})
      } if(opts[:user_data_method] && opts[:user_data_method] == :custom_property)
      payloads {
        payload(:type => 'floppy') {
          file(:name => "#{opts[:fileinject_path] || OVIRT::FILEINJECT_PATH}") { content(Base64::decode64(opts[:user_data])) }
        }
      } if(opts[:user_data_method] && opts[:user_data_method] == :payload)
      payloads {
        payload(:type => 'floppy') {
          files {
            file {
              name_ "#{opts[:fileinject_path] || OVIRT::FILEINJECT_PATH}"
              content Base64::decode64(opts[:user_data])
            }
          }
        }
      } if(opts[:user_data_method] && opts[:user_data_method] == :payload_v3_3)
    }
  end
  Nokogiri::XML(builder.to_xml).root.to_s
end

Instance Method Details

#ready?Boolean

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.

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'lib/ovirt/vm.rb', line 22

def ready?
  return false unless @status =~ /down/i
  volumes.each do |volume|
    return false if volume.status =~ /locked/i
  end
  true
end

#running?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/ovirt/vm.rb', line 16

def running?
  !(@status =~ /down/i) && !(@status =~ /wait_for_launch/i)
end