class Fog::Vsphere::Compute::Server

Public Class Methods

new(attributes = {}) click to toggle source
Calls superclass method
# File lib/fog/vsphere/models/compute/server.rb, line 62
def initialize(attributes = {})
  super defaults.merge(attributes)
  self.instance_uuid ||= id # TODO: remvoe instance_uuid as it can be replaced with simple id
  initialize_interfaces
  initialize_customvalues
  initialize_scsi_controllers
  initialize_nvme_controllers
  initialize_volumes
end

Public Instance Methods

acquire_ticket(_type = nil) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 337
def acquire_ticket(_type = nil)
  service.tickets(server: self).create
end
add_interface(attrs) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 234
def add_interface(attrs)
  Fog::Logger.deprecation('<server>.add_interface is deprecated. Call <server>.interfaces.create instead.')

  interfaces.create(attrs)
end
cdrom(key) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 279
def cdrom(key)
  cdroms.get(key)
end
cdroms(opts = {}) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 275
def cdroms(opts = {})
  service.cdroms(instance_uuid: id).all(opts)
end
clone(options = {}) click to toggle source

Clone from a server object

Parameters

*<~Hash>:

* 'name'<~String> - *REQUIRED* Name of the _new_ VirtualMachine
* See more options in vm_clone request/compute/vm_clone.rb
# File lib/fog/vsphere/models/compute/server.rb, line 159
def clone(options = {})
  requires :name, :datacenter, :path

  # Convert symbols to strings
  req_options = options.each_with_object({}) { |(k, v), hsh| hsh[k.to_s] = v; }

  # Give our path to the request
  req_options['template_path'] = "#{relative_path}/#{name}"
  req_options['template_datacenter'] = datacenter.to_s
  req_options['datacenter'] ||= datacenter.to_s

  # Perform the actual clone
  clone_results = service.vm_clone(req_options)

  # We need to assign the service, otherwise we can't reload the model
  # Create the new VM model. TODO This only works when "wait=true"
  new_vm = self.class.new(clone_results['new_vm'].merge(service: service))

  # We need to assign the collection otherwise we
  # cannot reload the model.
  new_vm.collection = collection

  # Return the new VM model.
  new_vm
end
config_vnc(options = {}) click to toggle source

defines VNC attributes on the hypervisor

# File lib/fog/vsphere/models/compute/server.rb, line 203
def config_vnc(options = {})
  requires :instance_uuid
  service.vm_config_vnc(options.merge('instance_uuid' => instance_uuid))
end
customvalues() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 288
def customvalues
  attributes[:customvalues] ||= id.nil? ? [] : service.customvalues(vm: self)
end
destroy(options = {}) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 129
def destroy(options = {})
  requires :instance_uuid
  if ready?
    # need to turn it off before destroying
    stop(options)
    wait_for { !ready? }
  end
  service.vm_destroy('instance_uuid' => instance_uuid)
end
destroy_interface(attrs) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 246
def destroy_interface(attrs)
  Fog::Logger.deprecation('<server>.destroy_vm_interface is deprecated. Call <server>.interfaces.get(:key => <nic_key>).destroy instead.')

  interfaces.get(attrs[:key] || attrs['key']).destroy
end
find_snapshot(snapshot_ref) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 260
def find_snapshot(snapshot_ref)
  snapshots.get(snapshot_ref)
end
folder() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 304
def folder
  return nil unless datacenter && path
  attributes[:folder] ||= service.folders(datacenter: datacenter, type: :vm).get(path)
end
guest_processes(opts = {}) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 283
def guest_processes(opts = {})
  raise 'VM tools must be running' unless tools_running?
  service.list_processes(id, opts)
end
interface_ready?(attrs) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 230
def interface_ready?(attrs)
  (attrs.is_a?(Hash) && attrs[:blocking]) || attrs.is_a?(Fog::Vsphere::Compute::Interface)
end
interfaces() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 226
def interfaces
  attributes[:interfaces] ||= id.nil? ? [] : service.interfaces(server_id: id)
end
mac() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 222
def mac
  interfaces.first.mac unless interfaces.empty?
end
memory() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 214
def memory
  memory_mb * 1024 * 1024
end
migrate(options = {}) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 139
def migrate(options = {})
  options = { priority: 'defaultPriority' }.merge(options)
  requires :instance_uuid

  # Convert symbols to strings
  req_options = options.each_with_object({}) { |(k, v), hsh| hsh[k.to_s] = v; }
  req_options['cluster'] ||= cluster
  req_options['datacenter'] = datacenter.to_s
  req_options['instance_uuid'] = instance_uuid

  service.vm_migrate(req_options)
end
new?() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 319
def new?
  id.nil?
end
nvme_controllers() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 296
def nvme_controllers
  attributes[:nvme_controllers] ||= service.list_vm_nvme_controllers(id)
end
ready?() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 190
def ready?
  power_state == 'poweredOn'
end
reboot(options = {}) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 123
def reboot(options = {})
  options = { force: false }.merge(options)
  requires :instance_uuid
  service.vm_reboot('instance_uuid' => instance_uuid, 'force' => options[:force])
end
relative_path() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 331
def relative_path
  requires :path, :datacenter

  (path.split('/').reject(&:empty?) - ['Datacenters', datacenter, 'vm']).join('/')
end
reload() click to toggle source
Calls superclass method
# File lib/fog/vsphere/models/compute/server.rb, line 323
def reload
  # reload does not re-read assoiciated attributes, so we clear it manually
  %i[interfaces volumes].each do |attr|
    attributes.delete(attr)
  end
  super
end
revert_snapshot(snapshot) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 264
def revert_snapshot(snapshot)
  case snapshot
  when Snapshot
    service.revert_to_snapshot(snapshot)
  when String
    service.revert_to_snapshot(find_snapshot(snapshot))
  else
    raise ArgumentError, 'snapshot has to be kind of Snapshot or String class'
  end
end
save() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 309
def save
  requires :name, :cluster, :datacenter
  if persisted?
    service.update_vm(self)
  else
    self.id = service.create_vm(attributes)
  end
  reload
end
scsi_controller() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 300
def scsi_controller
  scsi_controllers.first
end
scsi_controllers() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 292
def scsi_controllers
  attributes[:scsi_controllers] ||= service.list_vm_scsi_controllers(id)
end
snapshots(opts = {}) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 256
def snapshots(opts = {})
  service.snapshots(server_id: id).all(opts)
end
sockets() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 218
def sockets
  cpus / corespersocket
end
start(_options = {}) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 106
def start(_options = {})
  requires :instance_uuid
  service.vm_power_on('instance_uuid' => instance_uuid) unless ready?
end
stop(options = {}) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 111
def stop(options = {})
  options = { force: !tools_installed? || !tools_running? }.merge(options)
  requires :instance_uuid
  service.vm_power_off('instance_uuid' => instance_uuid, 'force' => options[:force]) unless power_state == 'poweredOff'
end
suspend(options = {}) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 117
def suspend(options = {})
  options = { force: !tools_installed? || !tools_running? }.merge(options)
  requires :instance_uuid
  service.vm_suspend('instance_uuid' => instance_uuid, 'force' => options[:force])
end
take_snapshot(options = {}) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 185
def take_snapshot(options = {})
  requires :instance_uuid
  service.vm_take_snapshot(options.merge('instance_uuid' => instance_uuid))
end
tools_installed?() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 194
def tools_installed?
  !(tools_state == 'toolsNotInstalled' || tools_version == 'guestToolsNotInstalled')
end
tools_running?() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 198
def tools_running?
  %w[toolsOk toolsOld].include? tools_state
end
update_interface(attrs) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 240
def update_interface(attrs)
  wait_for { !ready? } if interface_ready? attrs
  attrs[:datacenter] = datacenter unless attrs.key? :datacenter
  service.update_vm_interface(id, attrs)
end
vm_reconfig_cpus(_options = {}) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 91
def vm_reconfig_cpus(_options = {})
  requires :instance_uuid, :cpus, :corespersocket
  service.vm_reconfig_cpus('instance_uuid' => instance_uuid, 'cpus' => cpus, 'corespersocket' => corespersocket)
end
vm_reconfig_hardware(hardware_spec, _options = {}) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 101
def vm_reconfig_hardware(hardware_spec, _options = {})
  requires :instance_uuid
  service.vm_reconfig_hardware('instance_uuid' => instance_uuid, 'hardware_spec' => hardware_spec)
end
vm_reconfig_memory(_options = {}) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 86
def vm_reconfig_memory(_options = {})
  requires :instance_uuid, :memory
  service.vm_reconfig_memory('instance_uuid' => instance_uuid, 'memory' => memory_mb)
end
vm_reconfig_volumes(_options = {}) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 96
def vm_reconfig_volumes(_options = {})
  requires :instance_uuid, :volumes
  service.vm_reconfig_volumes('instance_uuid' => instance_uuid, 'volumes' => volumes)
end
vm_rename() click to toggle source

End Lazy Loaded Attributes

# File lib/fog/vsphere/models/compute/server.rb, line 81
def vm_rename
  requires :instance_uuid, :name
  service.vm_rename('instance_uuid' => instance_uuid, 'name' => name)
end
vnc() click to toggle source

returns a hash of VNC attributes required for service

# File lib/fog/vsphere/models/compute/server.rb, line 209
def vnc
  requires :instance_uuid
  service.vm_get_vnc(instance_uuid)
end
volumes() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 252
def volumes
  attributes[:volumes] ||= id.nil? ? [] : service.volumes(server_id: id)
end

Private Instance Methods

defaults() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 343
def defaults
  {
    cpus: 1,
    #            :corespersocket => 1,
    memory_mb: 512,
    guest_id: 'otherGuest',
    path: '/'
  }
end
initialize_customvalues() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 377
def initialize_customvalues
  if attributes[:customvalues] && attributes[:customvalues].is_a?(Array)
    attributes[:customvalues].map { |cfield| cfield.is_a?(Hash) ? service.customvalue.new(cfield) : cfield }
  end
end
initialize_interfaces() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 353
def initialize_interfaces
  if attributes[:interfaces] && attributes[:interfaces].is_a?(Array)
    attributes[:interfaces].map! { |nic| nic.is_a?(Hash) ? service.interfaces.new(nic) : nic }
  end
end
initialize_nvme_controllers() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 396
def initialize_nvme_controllers
  if attributes[:nvme_controllers].is_a?(Array)
    attributes[:nvme_controllers].map! do |controller|
      controller.is_a?(Hash) ? Fog::Vsphere::Compute::NVMEController.new(controller) : controller
    end
  end
end
initialize_scsi_controllers() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 383
def initialize_scsi_controllers
  if attributes[:scsi_controllers].is_a?(Array) && !attributes[:scsi_controllers].empty?
    attributes[:scsi_controllers].map! do |controller|
      controller.is_a?(Hash) ? Fog::Vsphere::Compute::SCSIController.new(controller) : controller
    end
  elsif attributes[:scsi_controller].is_a?(Hash) && !attributes[:scsi_controller].empty?
    attributes[:scsi_controllers] = [
      Fog::Vsphere::Compute::SCSIController.new(attributes[:scsi_controller])
    ]
  end
  attributes[:scsi_controllers] = [Fog::Vsphere::Compute::SCSIController.new] if !attributes[:scsi_controllers]&.any? && unassigned_volumes?
end
initialize_volumes() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 367
def initialize_volumes
  return unless attributes[:volumes].is_a?(Array)
  attributes[:volumes].map! do |vol|
    vol = service.volumes.new({ server: self }.merge(vol)) if vol.is_a?(Hash)
    vol.server = self
    update_controller_key(vol)
    vol
  end
end
unassigned_volumes?() click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 359
def unassigned_volumes?
  attributes[:volumes]&.any? { |vol| !vol.key?(:controller_key) } || false
end
update_controller_key(vol) click to toggle source
# File lib/fog/vsphere/models/compute/server.rb, line 363
def update_controller_key(vol)
  vol.controller_key ||= attributes[:scsi_controllers].first&.key || 1000
end