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

Inherits:
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.



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

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



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

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

#add_interface(attrs) ⇒ Object



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

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

  interfaces.create(attrs)
end

#cdrom(key) ⇒ Object



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

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

#cdroms(opts = {}) ⇒ Object



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

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


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

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



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

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

#customvaluesObject



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

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

#destroy(options = {}) ⇒ Object



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

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



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

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



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

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

#folderObject



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

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

#guest_processes(opts = {}) ⇒ Object



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

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)


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

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

#interfacesObject



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

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

#macObject



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

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

#memoryObject



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

def memory
  memory_mb * 1024 * 1024
end

#migrate(options = {}) ⇒ Object



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

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)


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

def new?
  id.nil?
end

#ready?Boolean

Returns:

  • (Boolean)


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

def ready?
  power_state == 'poweredOn'
end

#reboot(options = {}) ⇒ Object



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

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

#relative_pathObject



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

def relative_path
  requires :path, :datacenter

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

#reloadObject



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

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



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

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



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

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

#scsi_controllerObject



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

def scsi_controller
  scsi_controllers.first
end

#scsi_controllersObject



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

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

#snapshots(opts = {}) ⇒ Object



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

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

#socketsObject



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

def sockets
  cpus / corespersocket
end

#start(_options = {}) ⇒ Object



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

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

#stop(options = {}) ⇒ Object



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

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



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

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



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

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

#tools_installed?Boolean

Returns:

  • (Boolean)


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

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

#tools_running?Boolean

Returns:

  • (Boolean)


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

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

#update_interface(attrs) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#volumesObject



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

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