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.



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

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



329
330
331
# File 'lib/fog/vsphere/models/compute/server.rb', line 329

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

#add_interface(attrs) ⇒ Object



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

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

  interfaces.create(attrs)
end

#cdrom(key) ⇒ Object



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

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

#cdroms(opts = {}) ⇒ Object



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

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


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

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



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

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

#customvaluesObject



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

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

#destroy(options = {}) ⇒ Object



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

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



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

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



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

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

#folderObject



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

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

#guest_processes(opts = {}) ⇒ Object



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

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)


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

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

#interfacesObject



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

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

#macObject



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

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

#memoryObject



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

def memory
  memory_mb * 1024 * 1024
end

#migrate(options = {}) ⇒ Object



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

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)


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

def new?
  id.nil?
end

#ready?Boolean

Returns:

  • (Boolean)


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

def ready?
  power_state == 'poweredOn'
end

#reboot(options = {}) ⇒ Object



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

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

#relative_pathObject



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

def relative_path
  requires :path, :datacenter

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

#reloadObject



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

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



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

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



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

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

#scsi_controllerObject



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

def scsi_controller
  scsi_controllers.first
end

#scsi_controllersObject



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

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

#snapshots(opts = {}) ⇒ Object



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

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

#socketsObject



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

def sockets
  cpus / corespersocket
end

#start(_options = {}) ⇒ Object



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

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

#stop(options = {}) ⇒ Object



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

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



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

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



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

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

#tools_installed?Boolean

Returns:

  • (Boolean)


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

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

#tools_running?Boolean

Returns:

  • (Boolean)


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

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

#update_interface(attrs) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#volumesObject



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

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