Module: XenAPI::VirtualMachine

Included in:
Session
Defined in:
lib/xenapi/virtual_machine.rb

Instance Method Summary collapse

Instance Method Details

#adjust_vcpu_settings(vm_ref, new_settings) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/xenapi/virtual_machine.rb', line 152

def adjust_vcpu_settings(vm_ref, new_settings)
  parameters = self.VM.get_VCPUs_params(vm_ref)
  parameters.merge!(new_settings)
  self.VM.set_VCPUs_params(vm_ref, parameters)

  self
end

#cd_ref(vm_ref) ⇒ Object



90
91
92
93
94
95
# File 'lib/xenapi/virtual_machine.rb', line 90

def cd_ref(vm_ref)
  vbds(vm_ref, :include_cd => true).each_pair do |ref, record|
    return ref if record["type"] == "CD"
  end
  nil
end

#configure_network_interfaces_on(vm_ref) ⇒ Object



109
110
111
112
113
114
# File 'lib/xenapi/virtual_machine.rb', line 109

def configure_network_interfaces_on(vm_ref)
  vif_refs = self.VM.get_VIFs(vm_ref)
  raise "Template doesn't have any network interfaces" if vif_refs.nil? || vif_refs.empty?
  vif_record = self.VIF.get_record(vm_main_vif_ref(vm_ref))
  vif_record["MAC"]
end

#created_vifs(vm_ref) ⇒ Object



129
130
131
132
133
134
# File 'lib/xenapi/virtual_machine.rb', line 129

def created_vifs(vm_ref)
  vifs(vm_ref).to_a.inject({}) do |map, pair|
    map[pair.last["network_label"]] = pair.first
    map
  end
end

#exists_on_hypervisor?(uuid) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
# File 'lib/xenapi/virtual_machine.rb', line 116

def exists_on_hypervisor?(uuid)
  self.VM.get_by_uuid(uuid)
  true
rescue XenAPI::Error
  false
end

#export(vm_uuid, options = {}) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/xenapi/virtual_machine.rb', line 192

def export(vm_uuid, options = {})
  options = {:to => "/tmp/export_file"}.merge(options)
  file = File.open(options[:to], "wb")
  session_ref = self.key
  task_ref = self.task.create "export vm #{vm_uuid}", "export job"

  path = "/export?session_id=#{session_ref}&task_id=#{task_ref}&ref=#{self.vm_ref(vm_uuid)}"
  uri  = URI.parse "http://#{master_address}#{path}"

  Net::HTTP.get_response(uri) do |res|
    res.read_body {|chunk| file.write chunk }
  end

  options[:to]
ensure
  file.close rescue nil
  self.task.destroy(task_ref) rescue nil
end

#hdd_physical_utilisation(vm_ref) ⇒ Object



35
36
37
38
39
# File 'lib/xenapi/virtual_machine.rb', line 35

def hdd_physical_utilisation(vm_ref)
  vbds(vm_ref).values.inject(0) do |disk_size, vbd_record|
    disk_size += self.VDI.get_physical_utilisation(vbd_record['VDI']).to_i
  end / (1024**3) # in GB
end

#hdd_size(vm_ref) ⇒ Object



41
42
43
44
45
# File 'lib/xenapi/virtual_machine.rb', line 41

def hdd_size(vm_ref)
  vbds(vm_ref).values.inject(0) do |disk_size, vbd_record|
    disk_size += self.VDI.get_virtual_size(vbd_record['VDI']).to_i
  end / (1024**3) # in GB
end

#import(file_path, storage_uuid = nil) ⇒ Object



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
# File 'lib/xenapi/virtual_machine.rb', line 160

def import(file_path, storage_uuid = nil)
  file = File.open(file_path, "rb")

  session_ref = self.key
  storage_ref = storage_uuid ? self.SR.get_by_uuid(storage_uuid) : nil
  task_ref = self.task.create "import vm #{file_path}", "importat job"

  path = "/import?session_id=#{session_ref}&task_id=#{task_ref}"
  path += "&sr_id=#{storage_ref}" if storage_ref

  http = Net::HTTP.new(master_address, 80)
  request = Net::HTTP::Put.new(path, {})
  request.body_stream = file
  request.content_length = file.size
  begin
    http.request(request)
  rescue Errno::ECONNRESET
    Rails.logger.warn "VM import did a connection reset, but does not indicate an error"
  rescue Timeout::Error
    error = "Could not import VM due to timeout error: check if your image is valid"
    Rails.logger.error error
    raise error
  end

  task_rec = self.task.get_record task_ref
  vm_ref = task_rec["result"].gsub(/<.*?>/, "")
  self.VM.get_uuid(vm_ref)
ensure
  file.close rescue nil
  self.task.destroy(task_ref) rescue nil
end

#insert_iso_cd(cd_ref, iso_ref) ⇒ Object



97
98
99
100
101
# File 'lib/xenapi/virtual_machine.rb', line 97

def insert_iso_cd(cd_ref, iso_ref)
  self.VBD.set_bootable(cd_ref, false)
  self.VBD.insert(cd_ref, iso_ref)
  true
end

#master_addressObject



103
104
105
106
107
# File 'lib/xenapi/virtual_machine.rb', line 103

def master_address
  pool_ref = self.pool.get_all.first
  master_ref = self.pool.get_master pool_ref
  self.host.get_address master_ref
end

#next_disk_number(vm_ref) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/xenapi/virtual_machine.rb', line 72

def next_disk_number(vm_ref)
  device_map = {}

  vbds(vm_ref, :include_cd => true).each_pair do |vbd_ref, vbd_record|
    userdevice = vbd_record["userdevice"].to_i
    device_map[userdevice] = vbd_ref
  end

  disk_number = 0

  device_map.size.times do
    break if device_map[disk_number].nil?
    disk_number +=1
  end

  disk_number
end

#remove_disks_from_hypervisor(vm_ref) ⇒ Object



66
67
68
69
70
# File 'lib/xenapi/virtual_machine.rb', line 66

def remove_disks_from_hypervisor(vm_ref)
  vbds(vm_ref).each_value do |vbd_record|
    self.VDI.destroy(vbd_record['VDI'])
  end
end

#set_cpus_size(vm_ref, cpus) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/xenapi/virtual_machine.rb', line 136

def set_cpus_size(vm_ref, cpus)
  cpus = cpus.to_s
  max_cpus = self.VM.get_VCPUs_max(vm_ref).to_i

  # On upgrade, we want set VCPUS max before VCPUS at startup and vice versa
  if cpus.to_i > max_cpus
    self.VM.set_VCPUs_max(vm_ref, cpus)
    self.VM.set_VCPUs_at_startup(vm_ref, cpus)
  else
    self.VM.set_VCPUs_at_startup(vm_ref, cpus)
    self.VM.set_VCPUs_max(vm_ref, cpus)
  end

  self
end

#set_memory_size(vm_ref, memory_in_MB) ⇒ Object



123
124
125
126
127
# File 'lib/xenapi/virtual_machine.rb', line 123

def set_memory_size(vm_ref, memory_in_MB)
  memory_in_MB = memory_in_MB.to_s
  self.VM.set_memory_limits(vm_ref, memory_in_MB, memory_in_MB, memory_in_MB, memory_in_MB)
  self
end

#tools_outdated?(ref) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/xenapi/virtual_machine.rb', line 15

def tools_outdated?(ref)
  guest_ref = self.VM.get_guest_metrics(ref)
  guest_ref == "OpaqueRef:NULL" || !self.VM_guest_metrics.get_PV_drivers_up_to_date(guest_ref)
rescue Exception
  false
end

#vbds(ref, opts = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/xenapi/virtual_machine.rb', line 22

def vbds(ref, opts = {})
  vm_vbds_refs = self.VM.get_record(ref)["VBDs"]

  vm_vbds_refs.inject({}) do |disks, vm_vbd_ref|
    vm_vbd_record = self.VBD.get_record(vm_vbd_ref)

    disks[vm_vbd_ref] = vm_vbd_record if opts[:include_cd] || vm_vbd_record["type"] == "Disk"
    disks
  end
rescue => e
  {}
end

#vifs(ref) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/xenapi/virtual_machine.rb', line 53

def vifs(ref)
  vif_refs = self.VM.get_VIFs(ref)
  vif_refs.inject({}) do |interfaces, vif_ref|
    network_ref = self.VIF.get_network(vif_ref)
    network_label = self.network.get_name_label(network_ref)
    interfaces[vif_ref] = self.VIF.get_record(vif_ref).merge("network_label" => network_label)

    interfaces
  end
rescue
  {}
end

#vm_clone(ref_to_clone, name) ⇒ Object



11
12
13
# File 'lib/xenapi/virtual_machine.rb', line 11

def vm_clone(ref_to_clone, name)
  self.VM.clone(ref_to_clone, name)
end

#vm_main_vif_ref(ref) ⇒ Object



47
48
49
50
51
# File 'lib/xenapi/virtual_machine.rb', line 47

def vm_main_vif_ref(ref)
  self.VM.get_VIFs(ref).find do |vif_ref|
    self.VIF.get_device(vif_ref) == "0"
  end
end

#vm_record(ref) ⇒ Object



7
8
9
# File 'lib/xenapi/virtual_machine.rb', line 7

def vm_record(ref)
  self.VM.get_record(ref)
end

#vm_ref(uuid) ⇒ Object



3
4
5
# File 'lib/xenapi/virtual_machine.rb', line 3

def vm_ref(uuid)
  self.VM.get_by_uuid(uuid)
end