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

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

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



57
58
59
60
61
62
63
64
# File 'lib/fog/vsphere/models/compute/server.rb', line 57

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_volumes
  initialize_customvalues
  initialize_scsi_controllers
end

Instance Method Details

#acquire_ticket(_type = nil) ⇒ Object



327
328
329
# File 'lib/fog/vsphere/models/compute/server.rb', line 327

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

#add_interface(attrs) ⇒ Object



228
229
230
231
232
# File 'lib/fog/vsphere/models/compute/server.rb', line 228

def add_interface(attrs)
  Fog::Logger.deprecation('<server>.add_interface is deprecated. Call <server>.interfaces.create instead.')

  interfaces.create(attrs)
end

#cdrom(key) ⇒ Object



273
274
275
# File 'lib/fog/vsphere/models/compute/server.rb', line 273

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

#cdroms(opts = {}) ⇒ Object



269
270
271
# File 'lib/fog/vsphere/models/compute/server.rb', line 269

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


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/fog/vsphere/models/compute/server.rb', line 153

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



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

def config_vnc(options = {})
  requires :instance_uuid
  service.vm_config_vnc(options.merge('instance_uuid' => instance_uuid))
end

#customvaluesObject



282
283
284
# File 'lib/fog/vsphere/models/compute/server.rb', line 282

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

#destroy(options = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/fog/vsphere/models/compute/server.rb', line 123

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



240
241
242
243
244
# File 'lib/fog/vsphere/models/compute/server.rb', line 240

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



254
255
256
# File 'lib/fog/vsphere/models/compute/server.rb', line 254

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

#folderObject



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

def folder
  return nil unless datacenter && path
  attributes[:folder] ||= service.folders(datacenter: datacenter, type: :vm).get(path)
end

#guest_processes(opts = {}) ⇒ Object



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

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)


224
225
226
# File 'lib/fog/vsphere/models/compute/server.rb', line 224

def interface_ready?(attrs)
  (attrs.is_a?(Hash) && attrs[:blocking]) || attrs.is_a?(Fog::Vsphere::Compute::Interface)
end

#interfacesObject



220
221
222
# File 'lib/fog/vsphere/models/compute/server.rb', line 220

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

#macObject



216
217
218
# File 'lib/fog/vsphere/models/compute/server.rb', line 216

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

#memoryObject



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

def memory
  memory_mb * 1024 * 1024
end

#migrate(options = {}) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/fog/vsphere/models/compute/server.rb', line 133

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)


309
310
311
# File 'lib/fog/vsphere/models/compute/server.rb', line 309

def new?
  id.nil?
end

#ready?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/fog/vsphere/models/compute/server.rb', line 184

def ready?
  power_state == 'poweredOn'
end

#reboot(options = {}) ⇒ Object



117
118
119
120
121
# File 'lib/fog/vsphere/models/compute/server.rb', line 117

def reboot(options = {})
  options = { force: false }.merge(options)
  requires :instance_uuid
  service.vm_reboot('instance_uuid' => instance_uuid, 'force' => options[:force])
end

#relative_pathObject



321
322
323
324
325
# File 'lib/fog/vsphere/models/compute/server.rb', line 321

def relative_path
  requires :path, :datacenter

  (path.split('/').reject(&:empty?) - ['Datacenters', datacenter, 'vm']).join('/')
end

#reloadObject



313
314
315
316
317
318
319
# File 'lib/fog/vsphere/models/compute/server.rb', line 313

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



258
259
260
261
262
263
264
265
266
267
# File 'lib/fog/vsphere/models/compute/server.rb', line 258

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



299
300
301
302
303
304
305
306
307
# File 'lib/fog/vsphere/models/compute/server.rb', line 299

def save
  requires :name, :cluster, :datacenter
  if persisted?
    service.update_vm(self)
  else
    self.id = service.create_vm(attributes)
  end
  reload
end

#scsi_controllerObject



290
291
292
# File 'lib/fog/vsphere/models/compute/server.rb', line 290

def scsi_controller
  scsi_controllers.first
end

#scsi_controllersObject



286
287
288
# File 'lib/fog/vsphere/models/compute/server.rb', line 286

def scsi_controllers
  attributes[:scsi_controllers] ||= service.list_vm_scsi_controllers(id)
end

#snapshots(opts = {}) ⇒ Object



250
251
252
# File 'lib/fog/vsphere/models/compute/server.rb', line 250

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

#socketsObject



212
213
214
# File 'lib/fog/vsphere/models/compute/server.rb', line 212

def sockets
  cpus / corespersocket
end

#start(_options = {}) ⇒ Object



100
101
102
103
# File 'lib/fog/vsphere/models/compute/server.rb', line 100

def start(_options = {})
  requires :instance_uuid
  service.vm_power_on('instance_uuid' => instance_uuid) unless ready?
end

#stop(options = {}) ⇒ Object



105
106
107
108
109
# File 'lib/fog/vsphere/models/compute/server.rb', line 105

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



111
112
113
114
115
# File 'lib/fog/vsphere/models/compute/server.rb', line 111

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



179
180
181
182
# File 'lib/fog/vsphere/models/compute/server.rb', line 179

def take_snapshot(options = {})
  requires :instance_uuid
  service.vm_take_snapshot(options.merge('instance_uuid' => instance_uuid))
end

#tools_installed?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/fog/vsphere/models/compute/server.rb', line 188

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

#tools_running?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/fog/vsphere/models/compute/server.rb', line 192

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

#update_interface(attrs) ⇒ Object



234
235
236
237
238
# File 'lib/fog/vsphere/models/compute/server.rb', line 234

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



85
86
87
88
# File 'lib/fog/vsphere/models/compute/server.rb', line 85

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



95
96
97
98
# File 'lib/fog/vsphere/models/compute/server.rb', line 95

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



80
81
82
83
# File 'lib/fog/vsphere/models/compute/server.rb', line 80

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



90
91
92
93
# File 'lib/fog/vsphere/models/compute/server.rb', line 90

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



75
76
77
78
# File 'lib/fog/vsphere/models/compute/server.rb', line 75

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



203
204
205
206
# File 'lib/fog/vsphere/models/compute/server.rb', line 203

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

#volumesObject



246
247
248
# File 'lib/fog/vsphere/models/compute/server.rb', line 246

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