Class: Fog::Vsphere::Compute::Server

Inherits:
Compute::Server
  • Object
show all
Extended by:
Deprecation
Defined in:
lib/fog/vsphere/models/compute/server.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



62
63
64
65
66
67
68
69
70
# 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

Instance Method Details

#acquire_ticket(_type = nil) ⇒ Object



337
338
339
# File 'lib/fog/vsphere/models/compute/server.rb', line 337

def acquire_ticket(_type = nil)
  service.tickets(server: self).create
end

#add_interface(attrs) ⇒ Object



234
235
236
237
238
# 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) ⇒ Object



279
280
281
# File 'lib/fog/vsphere/models/compute/server.rb', line 279

def cdrom(key)
  cdroms.get(key)
end

#cdroms(opts = {}) ⇒ Object



275
276
277
# File 'lib/fog/vsphere/models/compute/server.rb', line 275

def cdroms(opts = {})
  service.cdroms(instance_uuid: id).all(opts)
end

#clone(options = {}) ⇒ Object

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


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 '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 = {}) ⇒ Object

defines VNC attributes on the hypervisor



203
204
205
206
# 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

#customvaluesObject



288
289
290
# File 'lib/fog/vsphere/models/compute/server.rb', line 288

def customvalues
  attributes[:customvalues] ||= id.nil? ? [] : service.customvalues(vm: self)
end

#destroy(options = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
# 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) ⇒ Object



246
247
248
249
250
# 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) ⇒ Object



260
261
262
# File 'lib/fog/vsphere/models/compute/server.rb', line 260

def find_snapshot(snapshot_ref)
  snapshots.get(snapshot_ref)
end

#folderObject



304
305
306
307
# 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 = {}) ⇒ Object



283
284
285
286
# 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) ⇒ Boolean

Returns:

  • (Boolean)


230
231
232
# 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

#interfacesObject



226
227
228
# File 'lib/fog/vsphere/models/compute/server.rb', line 226

def interfaces
  attributes[:interfaces] ||= id.nil? ? [] : service.interfaces(server_id: id)
end

#macObject



222
223
224
# File 'lib/fog/vsphere/models/compute/server.rb', line 222

def mac
  interfaces.first.mac unless interfaces.empty?
end

#memoryObject



214
215
216
# File 'lib/fog/vsphere/models/compute/server.rb', line 214

def memory
  memory_mb * 1024 * 1024
end

#migrate(options = {}) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# 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?Boolean

Returns:

  • (Boolean)


319
320
321
# File 'lib/fog/vsphere/models/compute/server.rb', line 319

def new?
  id.nil?
end

#nvme_controllersObject



296
297
298
# File 'lib/fog/vsphere/models/compute/server.rb', line 296

def nvme_controllers
  attributes[:nvme_controllers] ||= service.list_vm_nvme_controllers(id)
end

#ready?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/fog/vsphere/models/compute/server.rb', line 190

def ready?
  power_state == 'poweredOn'
end

#reboot(options = {}) ⇒ Object



123
124
125
126
127
# 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_pathObject



331
332
333
334
335
# 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

#reloadObject



323
324
325
326
327
328
329
# 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) ⇒ Object



264
265
266
267
268
269
270
271
272
273
# 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

#saveObject



309
310
311
312
313
314
315
316
317
# 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_controllerObject



300
301
302
# File 'lib/fog/vsphere/models/compute/server.rb', line 300

def scsi_controller
  scsi_controllers.first
end

#scsi_controllersObject



292
293
294
# 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 = {}) ⇒ Object



256
257
258
# File 'lib/fog/vsphere/models/compute/server.rb', line 256

def snapshots(opts = {})
  service.snapshots(server_id: id).all(opts)
end

#socketsObject



218
219
220
# File 'lib/fog/vsphere/models/compute/server.rb', line 218

def sockets
  cpus / corespersocket
end

#start(_options = {}) ⇒ Object



106
107
108
109
# 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 = {}) ⇒ Object



111
112
113
114
115
# 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 = {}) ⇒ Object



117
118
119
120
121
# 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 = {}) ⇒ Object



185
186
187
188
# 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?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/fog/vsphere/models/compute/server.rb', line 194

def tools_installed?
  !(tools_state == 'toolsNotInstalled' || tools_version == 'guestToolsNotInstalled')
end

#tools_running?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/fog/vsphere/models/compute/server.rb', line 198

def tools_running?
  %w[toolsOk toolsOld].include? tools_state
end

#update_interface(attrs) ⇒ Object



240
241
242
243
244
# 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 = {}) ⇒ Object



91
92
93
94
# 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 = {}) ⇒ Object



101
102
103
104
# 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 = {}) ⇒ Object



86
87
88
89
# 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 = {}) ⇒ Object



96
97
98
99
# 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_renameObject

End Lazy Loaded Attributes



81
82
83
84
# 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

#vncObject

returns a hash of VNC attributes required for service



209
210
211
212
# File 'lib/fog/vsphere/models/compute/server.rb', line 209

def vnc
  requires :instance_uuid
  service.vm_get_vnc(instance_uuid)
end

#volumesObject



252
253
254
# File 'lib/fog/vsphere/models/compute/server.rb', line 252

def volumes
  attributes[:volumes] ||= id.nil? ? [] : service.volumes(server_id: id)
end