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.



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

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



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

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


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

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

  # Convert symbols to strings
  req_options = options.reduce({}) { |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



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

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

#customvaluesObject



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

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

#destroy(options = {}) ⇒ Object



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

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



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

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



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

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)


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

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

#interfacesObject



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

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

#macObject



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

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

#memoryObject



172
173
174
# File 'lib/fog/vsphere/models/compute/server.rb', line 172

def memory
  memory_mb * 1024 * 1024
end

#migrate(options = {}) ⇒ Object



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

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)


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

def new?
  id.nil?
end

#ready?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/fog/vsphere/models/compute/server.rb', line 148

def ready?
  power_state == "poweredOn"
end

#reboot(options = {}) ⇒ Object



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

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

#relative_pathObject



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

def relative_path
  requires :path, :datacenter

  (path.split('/').reject {|e| e.empty?} - ["Datacenters", datacenter, "vm"]).join("/")
end

#reloadObject



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

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



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

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



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

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

#socketsObject



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

def sockets
  cpus / corespersocket
end

#start(options = {}) ⇒ Object



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

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

#stop(options = {}) ⇒ Object



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

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)


152
153
154
# File 'lib/fog/vsphere/models/compute/server.rb', line 152

def tools_installed?
  tools_state != "toolsNotInstalled"
end

#tools_running?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/fog/vsphere/models/compute/server.rb', line 156

def tools_running?
  tools_state == "toolsOk"
end

#update_interface(attrs) ⇒ Object



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

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

#vm_reconfig_cpus(options = {}) ⇒ Object



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

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



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

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

End Lazy Loaded Attributes



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

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



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

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

#volumesObject



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

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