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.



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

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



325
326
327
# File 'lib/fog/vsphere/models/compute/server.rb', line 325

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

#add_interface(attrs) ⇒ Object



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

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

  interfaces.create(attrs)
end

#cdrom(key) ⇒ Object



271
272
273
# File 'lib/fog/vsphere/models/compute/server.rb', line 271

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

#cdroms(opts = {}) ⇒ Object



267
268
269
# File 'lib/fog/vsphere/models/compute/server.rb', line 267

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


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

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['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



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

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

#customvaluesObject



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

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

#destroy(options = {}) ⇒ Object



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

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



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

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



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

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

#folderObject



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

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

#guest_processes(opts = {}) ⇒ Object



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

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)


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

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

#interfacesObject



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

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

#macObject



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

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

#memoryObject



206
207
208
# File 'lib/fog/vsphere/models/compute/server.rb', line 206

def memory
  memory_mb * 1024 * 1024
end

#migrate(options = {}) ⇒ Object



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

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)


307
308
309
# File 'lib/fog/vsphere/models/compute/server.rb', line 307

def new?
  id.nil?
end

#ready?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/fog/vsphere/models/compute/server.rb', line 182

def ready?
  power_state == 'poweredOn'
end

#reboot(options = {}) ⇒ Object



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

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

#relative_pathObject



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

def relative_path
  requires :path, :datacenter

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

#reloadObject



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

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



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

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



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

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

#scsi_controllerObject



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

def scsi_controller
  scsi_controllers.first
end

#scsi_controllersObject



284
285
286
# File 'lib/fog/vsphere/models/compute/server.rb', line 284

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

#snapshots(opts = {}) ⇒ Object



248
249
250
# File 'lib/fog/vsphere/models/compute/server.rb', line 248

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

#socketsObject



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

def sockets
  cpus / corespersocket
end

#start(_options = {}) ⇒ Object



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

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

#stop(options = {}) ⇒ Object



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

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



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

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



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

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

#tools_installed?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/fog/vsphere/models/compute/server.rb', line 186

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

#tools_running?Boolean

Returns:

  • (Boolean)


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

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

#update_interface(attrs) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#volumesObject



244
245
246
# File 'lib/fog/vsphere/models/compute/server.rb', line 244

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