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

Inherits:
Server show all
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, #username

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Server

#scp, #scp_download, #ssh, #ssh_port, #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 Attributes::InstanceMethods

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

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



45
46
47
48
49
50
# File 'lib/fog/vsphere/models/compute/server.rb', line 45

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
end

Instance Method Details

#clone(options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fog/vsphere/models/compute/server.rb', line 96

def clone(options = {})
  requires :name, :datacenter
  # 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['path'] ="#{path}/#{name}"
  # Perform the actual clone
  clone_results = connection.vm_clone(req_options)
  # Create the new VM model.
  new_vm = self.class.new(clone_results['vm_attributes'])
  # We need to assign the collection and the connection otherwise we
  # cannot reload the model.
  new_vm.collection = self.collection
  new_vm.connection = self.connection
  # Return the new VM model.
  new_vm
end

#config_vnc(options = {}) ⇒ Object

defines VNC attributes on the hypervisor



123
124
125
126
# File 'lib/fog/vsphere/models/compute/server.rb', line 123

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

#destroy(options = {}) ⇒ Object



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

def destroy(options = {})
  requires :instance_uuid
  stop if ready? # need to turn it off before destroying
  connection.vm_destroy('instance_uuid' => instance_uuid)
end

#folderObject



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

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

#interfacesObject



142
143
144
# File 'lib/fog/vsphere/models/compute/server.rb', line 142

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

#macObject



138
139
140
# File 'lib/fog/vsphere/models/compute/server.rb', line 138

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

#memoryObject



134
135
136
# File 'lib/fog/vsphere/models/compute/server.rb', line 134

def memory
  memory_mb * 1024 * 1024
end

#migrate(options = {}) ⇒ Object



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

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

#new?Boolean

Returns:

  • (Boolean)


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

def new?
  id.nil?
end

#ready?Boolean

Returns:

  • (Boolean)


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

def ready?
  power_state == "poweredOn"
end

#reboot(options = {}) ⇒ Object



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

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

#reloadObject



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

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



155
156
157
158
159
160
161
162
163
164
# File 'lib/fog/vsphere/models/compute/server.rb', line 155

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

#start(options = {}) ⇒ Object



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

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

#stop(options = {}) ⇒ Object



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

def stop(options = {})
  options = { :force => !tools_installed? }.merge(options)
  requires :instance_uuid
  connection.vm_power_off('instance_uuid' => instance_uuid, 'force' => options[:force])
end

#tools_installed?Boolean

Returns:

  • (Boolean)


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

def tools_installed?
  tools_state != "toolsNotInstalled"
end

#vm_reconfig_cpus(options = {}) ⇒ Object



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

def vm_reconfig_cpus(options = {})
  requires :instance_uuid, :cpus
  connection.vm_reconfig_cpus('instance_uuid' => instance_uuid, 'cpus' => cpus)
end

#vm_reconfig_hardware(options = {}) ⇒ Object



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

def vm_reconfig_hardware(options = {})
  requires :instance_uuid, :hardware_spec
  connection.vm_reconfig_hardware('instance_uuid' => instance_uuid, 'hardware_spec' => hardware_spec)
end

#vm_reconfig_memory(options = {}) ⇒ Object



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

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

#vncObject

returns a hash of VNC attributes required for connection



129
130
131
132
# File 'lib/fog/vsphere/models/compute/server.rb', line 129

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

#volumesObject



146
147
148
# File 'lib/fog/vsphere/models/compute/server.rb', line 146

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