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

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

Instance Attribute Summary

Attributes inherited from Server

#private_key, #private_key_path, #public_key, #public_key_path, #ssh_options, #ssh_port, #username

Attributes inherited from Model

#collection, #service

Instance Method Summary collapse

Methods included from Deprecation

deprecate, self_deprecate

Methods inherited from Server

#scp, #scp_download, #ssh, #ssh_ip_address, #ssh_ip_address=, #sshable?

Methods inherited from Model

#inspect, #symbolize_keys, #to_json, #wait_for

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Fog::Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



51
52
53
54
55
56
57
58
# File 'lib/fog/vsphere/models/compute/server.rb', line 51

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_controller
end

Instance Method Details

#add_interface(attrs) ⇒ Object



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

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

  interfaces.create(attrs)
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


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/fog/vsphere/models/compute/server.rb', line 125

def clone(options = {})
  requires :name, :datacenter, :relative_path

  # Convert symbols to strings
  req_options = options.inject({}) { |hsh, (k,v)| hsh[k.to_s] = v; hsh }

  # Give our path to the request
  req_options['template_path'] ="#{relative_path}/#{name}"
  req_options['datacenter'] = "#{datacenter}"

  # 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 => self.service))

  # We need to assign the collection otherwise we
  # cannot reload the model.
  new_vm.collection = self.collection

  # Return the new VM model.
  new_vm
end

#config_vnc(options = {}) ⇒ Object

defines VNC attributes on the hypervisor



163
164
165
166
# File 'lib/fog/vsphere/models/compute/server.rb', line 163

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

#customvaluesObject



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

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

#destroy(options = {}) ⇒ Object



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

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



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

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

#folderObject



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

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

#interface_ready?(attrs) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#interfacesObject



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

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

#macObject



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

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

#memoryObject



174
175
176
# File 'lib/fog/vsphere/models/compute/server.rb', line 174

def memory
  memory_mb * 1024 * 1024
end

#migrate(options = {}) ⇒ Object



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

def migrate(options = {})
  options = { :priority => 'defaultPriority' }.merge(options)
  requires :instance_uuid
  service.vm_migrate('instance_uuid' => instance_uuid, 'priority' => options[:priority])
end

#new?Boolean

Returns:

  • (Boolean)


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

def new?
  id.nil?
end

#ready?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/fog/vsphere/models/compute/server.rb', line 150

def ready?
  power_state == "poweredOn"
end

#reboot(options = {}) ⇒ Object



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

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

#reloadObject



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

def reload
  # reload does not re-read assoiciated attributes, so we clear it manually
  [:interfaces, :volumes].each do |attr|
    self.attributes.delete(attr)
  end
  super
end

#saveObject



228
229
230
231
232
233
234
235
236
237
# File 'lib/fog/vsphere/models/compute/server.rb', line 228

def save
  requires :name, :cluster, :datacenter
  if persisted?
    raise "update is not supported yet"
   # service.update_vm(attributes)
  else
    self.id = service.create_vm(attributes)
  end
  reload
end

#scsi_controllerObject



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

def scsi_controller
  self.attributes[:scsi_controller] ||= service.get_vm_first_scsi_controller(id)
end

#socketsObject



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

def sockets
  cpus / corespersocket
end

#start(options = {}) ⇒ Object



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

def start(options = {})
  requires :instance_uuid
  service.vm_power_on('instance_uuid' => instance_uuid)
end

#stop(options = {}) ⇒ Object



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

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])
end

#tools_installed?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/fog/vsphere/models/compute/server.rb', line 154

def tools_installed?
  tools_state != "toolsNotInstalled"
end

#tools_running?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/fog/vsphere/models/compute/server.rb', line 158

def tools_running?
  tools_state == "toolsOk"
end

#update_interface(attrs) ⇒ Object



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

def update_interface attrs
  wait_for { not ready? } if interface_ready? attrs
  service.update_vm_interface(id, attrs)
end

#vm_reconfig_cpus(options = {}) ⇒ Object



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

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



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

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



70
71
72
73
# File 'lib/fog/vsphere/models/compute/server.rb', line 70

def vm_reconfig_memory(options = {})
  requires :instance_uuid, :memory
  service.vm_reconfig_memory('instance_uuid' => instance_uuid, 'memory' => memory_mb)
end

#vncObject

returns a hash of VNC attributes required for service



169
170
171
172
# File 'lib/fog/vsphere/models/compute/server.rb', line 169

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

#volumesObject



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

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