Class: ForemanXen::Xenserver
- Inherits:
-
ComputeResource
- Object
- ComputeResource
- ForemanXen::Xenserver
- Defined in:
- app/models/foreman_xen/xenserver.rb
Class Method Summary collapse
Instance Method Summary collapse
- #associated_host(vm) ⇒ Object
- #builtin_templates ⇒ Object
- #capabilities ⇒ Object
- #console(uuid) ⇒ Object
- #create_vm(args = {}) ⇒ Object
- #create_vm_from_builtin(args) ⇒ Object
- #create_vm_from_custom(args) ⇒ Object
- #custom_templates ⇒ Object
-
#destroy_vm(ref, args = {}) ⇒ Object
we default to destroy the VM’s storage as well.
- #find_vm_by_uuid(ref) ⇒ Object
- #get_snapshots ⇒ Object
- #get_snapshots_for_vm(vm) ⇒ Object
- #hypervisor ⇒ Object
- #interfaces ⇒ Object
- #max_cpu_count ⇒ Object
- #max_memory ⇒ Object
- #networks ⇒ Object
- #new_nic(attr = {}) ⇒ Object
- #new_vm(attr = {}) ⇒ Object
- #new_volume(attr = {}) ⇒ Object
- #provided_attributes ⇒ Object
- #storage_pools ⇒ Object
- #templates ⇒ Object
- #test_connection(options = {}) ⇒ Object
Class Method Details
.model_name ⇒ Object
31 32 33 |
# File 'app/models/foreman_xen/xenserver.rb', line 31 def self.model_name ComputeResource.model_name end |
Instance Method Details
#associated_host(vm) ⇒ Object
121 122 123 |
# File 'app/models/foreman_xen/xenserver.rb', line 121 def associated_host(vm) associate_by("mac", vm.interfaces.map(&:mac)) end |
#builtin_templates ⇒ Object
116 117 118 119 |
# File 'app/models/foreman_xen/xenserver.rb', line 116 def builtin_templates tmps = client.servers.builtin_templates.select { |t| !t.is_a_snapshot } rescue [] tmps.sort { |a, b| a.name <=> b.name } end |
#capabilities ⇒ Object
12 13 14 |
# File 'app/models/foreman_xen/xenserver.rb', line 12 def capabilities [:build] end |
#console(uuid) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'app/models/foreman_xen/xenserver.rb', line 270 def console uuid vm = find_vm_by_uuid(uuid) raise 'VM is not running!' unless vm.ready? console = vm.service.consoles.find { |c| c.__vm == vm.reference && c.protocol == 'rfb' } raise "No console fore vm #{vm.name}" if console == nil session_ref = (vm.service.instance_variable_get :@connection).instance_variable_get :@credentials fullURL = "#{console.location}&session_id=#{session_ref}" tunnel = VNCTunnel.new fullURL tunnel.start logger.info 'VNCTunnel started' WsProxy.start(:host => tunnel.host, :host_port => tunnel.port, :password => '').merge(:type => 'vnc', :name => vm.name) rescue Error => e logger.warn e raise e end |
#create_vm(args = {}) ⇒ Object
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 |
# File 'app/models/foreman_xen/xenserver.rb', line 156 def create_vm(args = {}) custom_template_name = args[:custom_template_name] builtin_template_name = args[:builtin_template_name] raise 'you can select at most one template type' if builtin_template_name != '' and custom_template_name != '' begin vm = nil if custom_template_name != '' vm = create_vm_from_custom args else vm = create_vm_from_builtin args end vm.set_attribute('name_description', 'Provisioned by Foreman') cpus = args[:vcpus_max] if vm.vcpus_max.to_i < cpus.to_i vm.set_attribute('VCPUs_max', cpus) vm.set_attribute('VCPUs_at_startup', cpus) else vm.set_attribute('VCPUs_at_startup', cpus) vm.set_attribute('VCPUs_max', cpus) end vm.refresh return vm rescue => e logger.info e logger.info e.backtrace.join("\n") return false end end |
#create_vm_from_builtin(args) ⇒ Object
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 |
# File 'app/models/foreman_xen/xenserver.rb', line 227 def create_vm_from_builtin(args) host = client.hosts.first storage_repository = client.storage_repositories.find { |sr| sr.uuid == "#{args[:VBDs][:sr_uuid]}" } gb = 1073741824 #1gb in bytes size = args[:VBDs][:physical_size].to_i * gb vdi = client.vdis.create :name => "#{args[:name]}-disk1", :storage_repository => storage_repository, :description => "#{args[:name]}-disk_1", :virtual_size => size.to_s mem_max = args[:memory_max] mem_min = args[:memory_min] other_config = {} if args[:builtin_template_name] != '' template = client.servers.builtin_templates.find { |tmp| tmp.name == args[:builtin_template_name] } other_config = template.other_config other_config.delete 'disks' other_config.delete 'default_template' other_config['mac_seed'] = SecureRandom.uuid end vm = client.servers.new :name => args[:name], :affinity => host, :pv_bootloader => '', :hvm_boot_params => { :order => 'dnc' }, :other_config => other_config, :memory_static_max => mem_max, :memory_static_min => mem_min, :memory_dynamic_max => mem_max, :memory_dynamic_min => mem_min vm.save :auto_start => false client.vbds.create :server => vm, :vdi => vdi create_network(vm, args) vm.provision vm.set_attribute('HVM_boot_policy', 'BIOS order') vm.refresh vm end |
#create_vm_from_custom(args) ⇒ Object
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 |
# File 'app/models/foreman_xen/xenserver.rb', line 185 def create_vm_from_custom(args) mem_max = args[:memory_max] mem_min = args[:memory_min] raise 'Memory max cannot be lower than Memory min' if mem_min.to_i > mem_max.to_i vm = client.servers.new :name => args[:name], :template_name => args[:custom_template_name] vm.save :auto_start => false vm.provision vm.vifs.first.destroy rescue nil create_network(vm, args) args['xenstore']['vm-data']['ifs']['0']['mac'] = vm.vifs.first.mac xenstore_data = xenstore_hash_flatten(args['xenstore']) vm.set_attribute('xenstore_data', xenstore_data) if vm.memory_static_max.to_i < mem_max.to_i vm.set_attribute('memory_static_max', mem_max) vm.set_attribute('memory_dynamic_max', mem_max) vm.set_attribute('memory_dynamic_min', mem_min) vm.set_attribute('memory_static_min', mem_min) else vm.set_attribute('memory_static_min', mem_min) vm.set_attribute('memory_dynamic_min', mem_min) vm.set_attribute('memory_dynamic_max', mem_max) vm.set_attribute('memory_static_max', mem_max) end disks = vm.vbds.select { |vbd| vbd.type == 'Disk' } disks.sort! { |a, b| a.userdevice <=> b.userdevice } i = 0 disks.each do |vbd| vbd.vdi.set_attribute('name-label', "#{args[:name]}_#{i}") i+=1 end vm end |
#custom_templates ⇒ Object
111 112 113 114 |
# File 'app/models/foreman_xen/xenserver.rb', line 111 def custom_templates tmps = client.servers.custom_templates.select { |t| !t.is_a_snapshot } rescue [] tmps.sort { |a, b| a.name <=> b.name } end |
#destroy_vm(ref, args = {}) ⇒ Object
we default to destroy the VM’s storage as well.
25 26 27 28 29 |
# File 'app/models/foreman_xen/xenserver.rb', line 25 def destroy_vm(ref, args = {}) find_vm_by_uuid(ref).destroy rescue ActiveRecord::RecordNotFound true end |
#find_vm_by_uuid(ref) ⇒ Object
16 17 18 19 20 21 22 |
# File 'app/models/foreman_xen/xenserver.rb', line 16 def find_vm_by_uuid(ref) client.servers.get(ref) rescue Fog::XenServer::RequestFailed => e raise(ActiveRecord::RecordNotFound) if e..include?('HANDLE_INVALID') raise(ActiveRecord::RecordNotFound) if e..include?('VM.get_record: ["SESSION_INVALID"') raise e end |
#get_snapshots ⇒ Object
137 138 139 140 |
# File 'app/models/foreman_xen/xenserver.rb', line 137 def get_snapshots tmps = client.servers.templates.select { |t| t.is_a_snapshot } rescue [] tmps.sort { |a, b| a.name <=> b.name } end |
#get_snapshots_for_vm(vm) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'app/models/foreman_xen/xenserver.rb', line 125 def get_snapshots_for_vm(vm) if vm.snapshots.empty? return [] end tmps = client.servers.templates.select { |t| t.is_a_snapshot } rescue [] retval = [] tmps.each do | snapshot | retval << snapshot if vm.snapshots.include?(snapshot.reference) end retval end |
#hypervisor ⇒ Object
289 290 291 |
# File 'app/models/foreman_xen/xenserver.rb', line 289 def hypervisor client.hosts.first end |
#interfaces ⇒ Object
98 99 100 |
# File 'app/models/foreman_xen/xenserver.rb', line 98 def interfaces client.interfaces rescue [] end |
#max_cpu_count ⇒ Object
35 36 37 38 |
# File 'app/models/foreman_xen/xenserver.rb', line 35 def max_cpu_count ## 16 is a max number of cpus per vm according to XenServer doc [hypervisor.host_cpus.size, 16].min end |
#max_memory ⇒ Object
40 41 42 43 44 45 46 |
# File 'app/models/foreman_xen/xenserver.rb', line 40 def max_memory xenServerMaxDoc = 128*1024*1024*1024 [hypervisor.metrics.memory_total.to_i, xenServerMaxDoc].min rescue => e logger.debug "unable to figure out free memory, guessing instead due to:#{e}" 16*1024*1024*1024 end |
#networks ⇒ Object
102 103 104 105 |
# File 'app/models/foreman_xen/xenserver.rb', line 102 def networks networks = client.networks rescue [] networks.sort { |a, b| a.name <=> b.name } end |
#new_nic(attr = {}) ⇒ Object
56 57 58 |
# File 'app/models/foreman_xen/xenserver.rb', line 56 def new_nic(attr={}) client.networks.new attr end |
#new_vm(attr = {}) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'app/models/foreman_xen/xenserver.rb', line 142 def new_vm(attr={}) test_connection return unless errors.empty? opts = vm_instance_defaults.merge(attr.to_hash).symbolize_keys [:networks, :volumes].each do |collection| nested_attrs = opts.delete("#{collection}_attributes".to_sym) opts[collection] = nested_attributes_for(collection, nested_attrs) if nested_attrs end opts.reject! { |_, v| v.nil? } client.servers.new opts end |
#new_volume(attr = {}) ⇒ Object
60 61 62 |
# File 'app/models/foreman_xen/xenserver.rb', line 60 def new_volume(attr={}) client.storage_repositories.new attr end |
#provided_attributes ⇒ Object
5 6 7 8 9 10 |
# File 'app/models/foreman_xen/xenserver.rb', line 5 def provided_attributes super.merge( { :uuid => :reference, :mac => :mac }) end |
#storage_pools ⇒ Object
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 |
# File 'app/models/foreman_xen/xenserver.rb', line 64 def storage_pools results = Array.new storages = client.storage_repositories.select { |sr| sr.type!= 'udev' && sr.type!= 'iso'} rescue [] hosts = client.hosts storages.each do |sr| subresults = Hash.new() found = 0 hosts.each do |host| if (sr.reference == host.suspend_image_sr) found = 1 subresults[:name] = sr.name subresults[:display_name] = sr.name + '(' + host.hostname + ')' subresults[:uuid] = sr.uuid break end end if (found==0) subresults[:name] = sr.name subresults[:display_name] = sr.name subresults[:uuid] = sr.uuid end results.push(subresults) end results.sort_by!{|item| item[:display_name] } return results end |
#templates ⇒ Object
107 108 109 |
# File 'app/models/foreman_xen/xenserver.rb', line 107 def templates client.servers.templates rescue [] end |
#test_connection(options = {}) ⇒ Object
48 49 50 51 52 53 54 |
# File 'app/models/foreman_xen/xenserver.rb', line 48 def test_connection( = {}) super errors[:url].empty? and hypervisor rescue => e disconnect rescue nil errors[:base] << e. end |