Class: Fog::Hyperv::Compute::Server

Inherits:
Compute::Server
  • Object
show all
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

Instance Method Summary collapse

Methods included from ModelExtends

collection

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

#biosBios, Firmware (readonly) Also known as: firmware

Returns BIOS/UEFI configuration depending on generation.

Returns:

  • (Bios, Firmware)

    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_portsArray<ComPort>

Returns the COM ports on the VM.

Returns:

  • (Array<ComPort>)

    the COM ports on the VM



153
# File 'lib/fog/hyperv/compute/models/server.rb', line 153

collection :com_ports

#computer_nameString (readonly)

Returns the name of the host running the VM.

Returns:

  • (String)

    the name of the host running the VM



99
# File 'lib/fog/hyperv/compute/models/server.rb', line 99

attribute :computer_name

#creation_timeTime (readonly)

Returns the time the VM was created.

Returns:

  • (Time)

    the time the VM was created



107
# File 'lib/fog/hyperv/compute/models/server.rb', line 107

attribute :creation_time, type: :hypervdatetime

#dvd_drivesArray<DvdDrive>

Returns the DVD drives on the VM.

Returns:

  • (Array<DvdDrive>)

    the DVD drives on the VM



156
# File 'lib/fog/hyperv/compute/models/server.rb', line 156

collection :dvd_drives

#dynamic_memory_enabledBoolean

Returns if memory is dynamically handled - sliding between #memory_minimum and #memory_maximum.

Returns:

  • (Boolean)

    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_drivesArray<FloppyDrive>

Note:

only supported on #generation :BIOS

Returns the floppy drives on the VM.

Returns:



160
# File 'lib/fog/hyperv/compute/models/server.rb', line 160

collection :floppy_drives

#generationSymbol (readonly)

Returns the VM firmware generation.

Returns:

  • (Symbol)

    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_drivesArray<HardDrive>

Returns the hard drives on the VM.

Returns:

  • (Array<HardDrive>)

    the hard drives on the VM



163
# File 'lib/fog/hyperv/compute/models/server.rb', line 163

collection :hard_drives

#idString (readonly) Also known as: vm_id

Returns the GUID of the VM.

Returns:

  • (String)

    the GUID of the VM



94
# File 'lib/fog/hyperv/compute/models/server.rb', line 94

identity :id

#is_clusteredBoolean (readonly)

Returns iv the VM is clustered.

Returns:

  • (Boolean)

    iv the VM is clustered



116
# File 'lib/fog/hyperv/compute/models/server.rb', line 116

attribute :is_clustered, type: :boolean, default: false

#memory_assignedInteger (readonly)

Returns the memory assigned to the VM on startup.

Returns:

  • (Integer)

    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_maximumInteger

Returns the maximum amount of memory the VM can assign when #dynamic_memory_enabled.

Returns:

  • (Integer)

    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_minimumInteger

Returns the minimum amount of memory the VM can assign when #dynamic_memory_enabled.

Returns:

  • (Integer)

    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_startupInteger

Returns the starting amount of memory the VM will be assigned.

Returns:

  • (Integer)

    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

#nameString Also known as: vm_name

Returns the name of the VM.

Returns:

  • (String)

    the name of the VM



103
# File 'lib/fog/hyperv/compute/models/server.rb', line 103

attribute :name

#network_adaptersArray<NetworkAdapter>

Returns the network adapters on the VM.

Returns:



166
# File 'lib/fog/hyperv/compute/models/server.rb', line 166

collection :network_adapters

#notesString

Returns user-specified notes for the VM.

Returns:

  • (String)

    user-specified notes for the VM



143
# File 'lib/fog/hyperv/compute/models/server.rb', line 143

attribute :notes, type: :string

#primary_operational_statusSymbol (readonly)

Returns the primary status of the VM.

Returns:

  • (Symbol)

    the primary status of the VM

See Also:



124
# File 'lib/fog/hyperv/compute/models/server.rb', line 124

attribute :primary_operational_status, type: :hypervenum, values: VM_STATUS_ENUM_VALUES

#processor_countInteger

Returns the number of processors in the VM.

Returns:

  • (Integer)

    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_statusSymbol (readonly)

Returns the secondary status of the VM.

Returns:

  • (Symbol)

    the secondary status of the VM

See Also:



128
# File 'lib/fog/hyperv/compute/models/server.rb', line 128

attribute :secondary_operational_status, type: :hypervenum, values: VM_STATUS_ENUM_VALUES

#securitySecurity (readonly)

Returns UEFI security configuration, if #generation is :UEFI.

Returns:

  • (Security)

    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

#stateSymbol (readonly)

Returns the state of the VM.

Returns:

  • (Symbol)

    the state of the VM

See Also:



120
# File 'lib/fog/hyperv/compute/models/server.rb', line 120

attribute :state, type: :hypervenum, values: VM_STATE_ENUM_VALUES

#tpm_enabledBoolean

Returns if a vTPM is enabled on the VM, only available if #generation is :UEFI.

Returns:

  • (Boolean)

    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

#uptimeTime (readonly)

Returns the amount of time the VM has been online.

Returns:

  • (Time)

    the amount of time the VM has been online



149
# File 'lib/fog/hyperv/compute/models/server.rb', line 149

attribute :uptime, type: :hypervtimespan

#vhdsArray<Vhd>

Returns the VHD images in use by the VM.

Returns:

  • (Array<Vhd>)

    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

Returns:

  • (String)

    the absolute path for the VHD



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

Parameters:

  • boot_device (Symbol) (defaults to: nil)

    the default boot device to configure the VM with, one of BOOT_DEVICE

  • switch_name (String) (defaults to: nil)

    the name of a Switch to connect the VM to on creation



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

#destroyObject

Note:

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

#hibernateObject

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_addressesArray<String>

Returns the IP addresses of all attached network adapters.

Returns:

  • (Array<String>)

    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_addressesArray<String>

Returns the MAC addresses of all attached network adapters.

Returns:

  • (Array<String>)

    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_addressString?

Returns the first public IP address of the VM - if any.

Returns:

  • (String, nil)

    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_addressesArray<String>

Returns the public (not link-local) IP addresses of all attached network adapters.

Returns:

  • (Array<String>)

    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).

Returns:

  • (Boolean)

    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

#reloadself?

Reload the VM attributes from the Hyper-V server

Returns:

  • (self)

    if model successfully reloaded

  • (nil)

    if something went wrong or the model was not found



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

#restartObject Also known as: reboot

Note:

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

#resumeObject

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

#startObject

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

Parameters:

  • turn_off (Boolean) (defaults to: false)

    perform an instant power off instead of an ACPI shutdown



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

#suspendObject

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

#updateObject



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_versionObject

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

#usernameString

Get the username of the main system account

Returns:

  • (String)

    the system account username - usually “Administrator” or “root”



433
434
435
# File 'lib/fog/hyperv/compute/models/server.rb', line 433

def username
  @username || 'Administrator'
end