Class: Fog::Hyperv::Compute::Server
- Inherits:
-
Compute::Server
- Object
- Compute::Server
- Fog::Hyperv::Compute::Server
- Extended by:
- ModelExtends
- Includes:
- ModelIncludes
- Defined in:
- lib/fog/hyperv/compute/models/server.rb
Overview
A Hyper-V VM
Constant Summary collapse
- VM_STATE_ENUM_VALUES =
Note:
Defined by Microsoft.HyperV.PowerShell.VMState
VM runtime state
{ Other: 1, Running: 2, Off: 3, Stopping: 4, Saved: 6, Paused: 9, Starting: 10, Reset: 11, Saving: 32_773, Pausing: 32_776, Resuming: 32_777, FastSaved: 32_779, FastSaving: 32_780, ForceShutdown: 32_781, ForceReboot: 32_782, Hibernated: 32_783, ComponentServicing: 32_784, RunningCritical: 32_785, OffCritical: 32_786, StoppingCritical: 32_787, SavedCritical: 32_788, PausedCritical: 32_789, StartingCritical: 32_790, ResetCritical: 32_791, SavingCritical: 32_792, PausingCritical: 32_793, ResumingCritical: 32_794, FastSavedCritical: 32_795, FastSavingCritical: 32_796 }.freeze
- VM_STATUS_ENUM_VALUES =
Note:
Defined by Microsoft.HyperV.PowerShell.VMOperationalStatus
VM object status
{ Ok: 2, Degraded: 3, PredictiveFailure: 5, InService: 11, Dormant: 15, SupportingEntityInError: 16, CreatingSnapshot: 32_768, ApplyingSnapshot: 23_769, DeletingSnapshot: 32_770, WaitingToStart: 32_771, MergingDisks: 32_772, ExportingVirtualMachine: 32_773, MigratingVirtualMachine: 32_774, BackingUpVirtualMachine: 32_776, ModifyingUpVirtualMachine: 32_777, StorageMigrationPhaseOne: 32_778, StorageMigrationPhaseTwo: 32_779, MigratingPlannedVm: 32_780, CheckingCompatibility: 32_781, ApplicationCriticalState: 32_782, CommunicationTimedOut: 32_783, CommunicationFailed: 32_784, NoIommu: 32_785, NoIovSupportInNic: 32_786, SwitchNotInIovMode: 32_787, IovBlockedByPolicy: 32_788, IovNoAvailResources: 32_789, IovGuestDriversNeeded: 32_790, CriticalIoError: 32_795 }.freeze
- VM_GENERATION_VALUES =
VM firmware generation - i.e. BIOS/UEFI
{ BIOS: 1, UEFI: 2 }.freeze
Instance Attribute Summary collapse
-
#bios ⇒ Bios, Firmware
(also: #firmware)
readonly
BIOS/UEFI configuration depending on generation.
-
#com_ports ⇒ Array<ComPort>
The COM ports on the VM.
-
#computer_name ⇒ String
readonly
The name of the host running the VM.
-
#creation_time ⇒ Time
readonly
The time the VM was created.
-
#dvd_drives ⇒ Array<DvdDrive>
The DVD drives on the VM.
-
#dynamic_memory_enabled ⇒ Boolean
If memory is dynamically handled - sliding between #memory_minimum and #memory_maximum.
-
#floppy_drives ⇒ Array<FloppyDrive>
The floppy drives on the VM.
-
#generation ⇒ Symbol
readonly
The VM firmware generation.
-
#hard_drives ⇒ Array<HardDrive>
The hard drives on the VM.
-
#id ⇒ String
(also: #vm_id)
readonly
The GUID of the VM.
-
#is_clustered ⇒ Boolean
readonly
Iv the VM is clustered.
-
#memory_assigned ⇒ Integer
readonly
The memory assigned to the VM on startup.
-
#memory_maximum ⇒ Integer
The maximum amount of memory the VM can assign when #dynamic_memory_enabled.
-
#memory_minimum ⇒ Integer
The minimum amount of memory the VM can assign when #dynamic_memory_enabled.
-
#memory_startup ⇒ Integer
The starting amount of memory the VM will be assigned.
-
#name ⇒ String
(also: #vm_name)
The name of the VM.
-
#network_adapters ⇒ Array<NetworkAdapter>
The network adapters on the VM.
-
#notes ⇒ String
User-specified notes for the VM.
-
#primary_operational_status ⇒ Symbol
readonly
The primary status of the VM.
-
#processor_count ⇒ Integer
The number of processors in the VM.
-
#secondary_operational_status ⇒ Symbol
readonly
The secondary status of the VM.
-
#security ⇒ Security
readonly
UEFI security configuration, if #generation is
:UEFI. -
#state ⇒ Symbol
readonly
The state of the VM.
-
#tpm_enabled ⇒ Boolean
If a vTPM is enabled on the VM, only available if #generation is
:UEFI. -
#uptime ⇒ Time
readonly
The amount of time the VM has been online.
-
#vhds ⇒ Array<Vhd>
The VHD images in use by the VM.
Instance Method Summary collapse
-
#build_vhd_path(filename) ⇒ String
Build a path for where to store a VHD of a given name.
-
#create(boot_device: nil, switch_name: nil, **attrs) ⇒ Object
Create the VM object if it doesn’t exist.
-
#destroy ⇒ Object
Remove the VM object from Hyper-V.
-
#hibernate ⇒ Object
Hibernate the VM, i.e.
-
#initialize(attrs = {}) ⇒ Server
constructor
A new instance of Server.
-
#ip_addresses ⇒ Array<String>
The IP addresses of all attached network adapters.
-
#mac_addresses ⇒ Array<String>
The MAC addresses of all attached network adapters.
-
#public_ip_address ⇒ String?
The first public IP address of the VM - if any.
-
#public_ip_addresses ⇒ Array<String>
The public (not link-local) IP addresses of all attached network adapters.
-
#ready? ⇒ Boolean
If the VM is ready? (i.e. #state is
:Running). -
#reload ⇒ self?
Reload the VM attributes from the Hyper-V server.
-
#restart ⇒ Object
(also: #reboot)
Restart the VM.
-
#resume ⇒ Object
Resume suspended VM execution.
-
#start ⇒ Object
Start the VM.
-
#stop(turn_off: false) ⇒ Object
Stop the VM.
-
#suspend ⇒ Object
Suspend VM execution.
- #update ⇒ Object
-
#update_version ⇒ Object
Update the VM object to the latest version.
-
#username ⇒ String
Get the username of the main system account.
Methods included from ModelExtends
Methods included from ModelIncludes
#cluster, #computer, #dirty, #dirty?, #vm
Constructor Details
#initialize(attrs = {}) ⇒ Server
Returns a new instance of Server.
174 175 176 177 178 179 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 174 def initialize(attrs = {}) @cluster = attrs.delete :cluster @computer = attrs.delete :computer super end |
Instance Attribute Details
#bios ⇒ Bios, Firmware (readonly) Also known as: firmware
Returns BIOS/UEFI configuration depending on generation.
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 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 183 def bios associations[:bios] ||= begin requires :generation, :id if generation == :BIOS klass = Fog::Hyperv::Compute::Bios method = :get_vm_bios else klass = Fog::Hyperv::Compute::Firmware method = :get_vm_firmware end klass.new( **service.public_send( method, computer_name:, vm_id:, _return_fields: klass.attributes ), vm: self, service: @service, computer: @computer, cluster: @cluster ) end end |
#com_ports ⇒ Array<ComPort>
Returns the COM ports on the VM.
153 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 153 collection :com_ports |
#computer_name ⇒ String (readonly)
Returns the name of the host running the VM.
99 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 99 attribute :computer_name |
#creation_time ⇒ Time (readonly)
Returns the time the VM was created.
107 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 107 attribute :creation_time, type: :hypervdatetime |
#dvd_drives ⇒ Array<DvdDrive>
Returns the DVD drives on the VM.
156 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 156 collection :dvd_drives |
#dynamic_memory_enabled ⇒ Boolean
Returns if memory is dynamically handled - sliding between #memory_minimum and #memory_maximum.
110 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 110 attribute :dynamic_memory_enabled, type: :boolean, default: false |
#floppy_drives ⇒ Array<FloppyDrive>
only supported on #generation :BIOS
Returns the floppy drives on the VM.
160 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 160 collection :floppy_drives |
#generation ⇒ Symbol (readonly)
Returns the VM firmware generation.
113 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 113 attribute :generation, type: :hypervenum, values: VM_GENERATION_VALUES, default: :UEFI |
#hard_drives ⇒ Array<HardDrive>
Returns the hard drives on the VM.
163 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 163 collection :hard_drives |
#id ⇒ String (readonly) Also known as: vm_id
Returns the GUID of the VM.
94 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 94 identity :id |
#is_clustered ⇒ Boolean (readonly)
Returns iv the VM is clustered.
116 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 116 attribute :is_clustered, type: :boolean, default: false |
#memory_assigned ⇒ Integer (readonly)
Returns the memory assigned to the VM on startup.
131 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 131 attribute :memory_assigned, type: :integer |
#memory_maximum ⇒ Integer
Returns the maximum amount of memory the VM can assign when #dynamic_memory_enabled.
134 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 134 attribute :memory_maximum, type: :integer, default: 17_179_869_184 |
#memory_minimum ⇒ Integer
Returns the minimum amount of memory the VM can assign when #dynamic_memory_enabled.
137 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 137 attribute :memory_minimum, type: :integer, default: 536_870_912 |
#memory_startup ⇒ Integer
Returns the starting amount of memory the VM will be assigned.
140 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 140 attribute :memory_startup, type: :integer, default: 536_870_912 |
#name ⇒ String Also known as: vm_name
Returns the name of the VM.
103 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 103 attribute :name |
#network_adapters ⇒ Array<NetworkAdapter>
Returns the network adapters on the VM.
166 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 166 collection :network_adapters |
#notes ⇒ String
Returns user-specified notes for the VM.
143 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 143 attribute :notes, type: :string |
#primary_operational_status ⇒ Symbol (readonly)
Returns the primary status of the VM.
124 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 124 attribute :primary_operational_status, type: :hypervenum, values: VM_STATUS_ENUM_VALUES |
#processor_count ⇒ Integer
Returns the number of processors in the VM.
146 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 146 attribute :processor_count, type: :integer, default: 1 |
#secondary_operational_status ⇒ Symbol (readonly)
Returns the secondary status of the VM.
128 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 128 attribute :secondary_operational_status, type: :hypervenum, values: VM_STATUS_ENUM_VALUES |
#security ⇒ Security (readonly)
Returns UEFI security configuration, if #generation is :UEFI.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 214 def security requires :generation, :id return nil unless generation == :UEFI associations[:security] ||= Fog::Hyperv::Compute::Security.new( **service.get_vm_security( computer_name:, vm_id:, _return_fields: Fog::Hyperv::Compute::Security.attributes ), vm: self, service: @service, computer: @computer, cluster: @cluster ) end |
#state ⇒ Symbol (readonly)
Returns the state of the VM.
120 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 120 attribute :state, type: :hypervenum, values: VM_STATE_ENUM_VALUES |
#tpm_enabled ⇒ Boolean
Returns if a vTPM is enabled on the VM, only available if #generation is :UEFI.
235 236 237 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 235 def tpm_enabled security&.tpm_enabled end |
#uptime ⇒ Time (readonly)
Returns the amount of time the VM has been online.
149 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 149 attribute :uptime, type: :hypervtimespan |
#vhds ⇒ Array<Vhd>
Returns the VHD images in use by the VM.
169 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 169 collection :vhds |
Instance Method Details
#build_vhd_path(filename) ⇒ String
Build a path for where to store a VHD of a given name
425 426 427 428 429 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 425 def build_vhd_path(filename) requires :name [computer.virtual_hard_disk_path, '\\', name, '\\', filename].join end |
#create(boot_device: nil, switch_name: nil, **attrs) ⇒ Object
Create the VM object if it doesn’t exist
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 321 def create(boot_device: nil, switch_name: nil, **attrs) attrs[:no_vhd] = true unless attrs[:new_vhd_path] # Attributes that can't be set as part of the New-VM call post_create_attributes = { processor_count: processor_count > 1 ? processor_count : nil, notes: notes.nil? || notes == '' ? nil : notes } if dynamic_memory_enabled post_create_attributes.merge!( dynamic_memory_enabled: true, memory_minimum_bytes: memory_minimum, memory_maximum_bytes: memory_maximum ) end post_create_attributes.compact! merge_attributes( service.new_vm( computer_name:, name:, generation: generation_num, memory_startup_bytes: memory_startup, boot_device:, switch_name:, **attrs, _return_fields: self.class.attributes ) ) attributes.merge! post_create_attributes if post_create_attributes.any? save if dirty? # Save any associations that have been manually assigned before VM creation self.class.associations.each_key do |assoc| next unless associations.key? assoc associations[assoc].select do |obj| obj.computer_name = computer_name if obj.respond_to?(:computer_name=) obj.vm_id = id if obj.respond_to?(:vm_id=) obj.save if obj.dirty? || !obj.persisted? next if assoc != :vhd || hard_drives.none? { |hdd| hdd.path == vhd.path } hard_drives.create(path: vhd.path) end associations[assoc].clear end self end |
#destroy ⇒ Object
if the VM has VHDs, make sure to remove them first to not leave the VM data remaining on disk
Remove the VM object from Hyper-V
310 311 312 313 314 315 316 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 310 def destroy requires :id stop turn_off: true if ready? service.remove_vm(computer_name:, id:) true end |
#hibernate ⇒ Object
Hibernate the VM, i.e. save VM state to disk and turn it off
282 283 284 285 286 287 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 282 def hibernate requires :id service.save_vm(computer_name:, id:) true end |
#ip_addresses ⇒ Array<String>
Returns the IP addresses of all attached network adapters.
448 449 450 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 448 def ip_addresses network_adapters.map(&:ip_addresses).flatten end |
#mac_addresses ⇒ Array<String>
Returns the MAC addresses of all attached network adapters.
443 444 445 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 443 def mac_addresses network_adapters.map(&:mac_address) end |
#public_ip_address ⇒ String?
Returns the first public IP address of the VM - if any.
461 462 463 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 461 def public_ip_address public_ip_addresses.first end |
#public_ip_addresses ⇒ Array<String>
Returns the public (not link-local) IP addresses of all attached network adapters.
453 454 455 456 457 458 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 453 def public_ip_addresses ip_addresses .map { |a| IPAddr.new a } .reject(&:link_local?) .map(&:to_s) end |
#ready? ⇒ Boolean
Returns if the VM is ready? (i.e. #state is :Running).
438 439 440 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 438 def ready? state_num == 2 end |
#reload ⇒ self?
Reload the VM attributes from the Hyper-V server
411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 411 def reload requires :id data = service.get_vm computer_name:, id:, _return_fields: self.class.attributes return unless data merge_attributes(data) @old = data self end |
#restart ⇒ Object Also known as: reboot
This method will always cause a hard reset, i.e. power off and on without waiting for OS shutdown
Restart the VM
291 292 293 294 295 296 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 291 def restart requires :id service.restart_vm(computer_name:, id:, force:) true end |
#resume ⇒ Object
Resume suspended VM execution
274 275 276 277 278 279 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 274 def resume requires :id service.resume_vm(computer_name:, id:) true end |
#start ⇒ Object
Start the VM
249 250 251 252 253 254 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 249 def start requires :id service.start_vm(computer_name:, id:) true end |
#stop(turn_off: false) ⇒ Object
Stop the VM
258 259 260 261 262 263 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 258 def stop(turn_off: false) requires :id service.stop_vm(computer_name:, id:, turn_off:) true end |
#suspend ⇒ Object
Suspend VM execution
266 267 268 269 270 271 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 266 def suspend requires :id service.suspend_vm(computer_name:, id:) true end |
#update ⇒ Object
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 375 def update requires :id changes = { processor_count: changed!(:processor_count), memory_startup_bytes: changed!(:memory_startup), new_vm_name: changed!(:name) }.compact if dynamic_memory_enabled changes[:dynamic_memory] = true if changed?(:dynamic_memory_enabled) changes[:memory_minimum_bytes] = memory_minimum if changed?(:dynamic_memory_enabled, :memory_minimum) changes[:memory_maximum_bytes] = memory_maximum if changed?(:dynamic_memory_enabled, :memory_maximum) elsif changed?(:dynamic_memory_enabled) changes[:static_memory] = true end changes.compact! changes[:notes] = notes || '' if changed? :notes return self unless changes.any? merge_attributes( service.set_vm( computer_name: old.computer_name, id: old.id, **changes, _always_include: changes.keys, _return_fields: self.class.attributes ) ) end |
#update_version ⇒ Object
Update the VM object to the latest version
300 301 302 303 304 305 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 300 def update_version requires :id service.update_vm(computer_name:, id:) true end |
#username ⇒ String
Get the username of the main system account
433 434 435 |
# File 'lib/fog/hyperv/compute/models/server.rb', line 433 def username @username || 'Administrator' end |