2011-05-23 21:17:16 -04:00
|
|
|
module Fog
|
|
|
|
module Vcloud
|
|
|
|
class Compute
|
|
|
|
class Server < Fog::Vcloud::Model
|
|
|
|
|
|
|
|
identity :href, :aliases => :Href
|
|
|
|
|
|
|
|
ignore_attributes :xmlns, :xmlns_i, :xmlns_xsi, :xmlns_xsd
|
|
|
|
|
|
|
|
attribute :type
|
|
|
|
attribute :name
|
|
|
|
attribute :status
|
|
|
|
attribute :network_connections, :aliases => :NetworkConnectionSection, :squash => :NetworkConnection
|
|
|
|
attribute :os, :aliases => :OperatingSystemSection
|
|
|
|
attribute :virtual_hardware, :aliases => :VirtualHardwareSection
|
2011-05-25 21:36:13 -04:00
|
|
|
attribute :description, :aliases => :Description
|
2011-05-23 21:17:16 -04:00
|
|
|
attribute :storage_size, :aliases => :size
|
|
|
|
attribute :links, :aliases => :Link, :type => :array
|
|
|
|
|
2011-05-26 23:47:24 -04:00
|
|
|
attribute :vm_data, :aliases => :Children, :squash => :Vm
|
|
|
|
|
|
|
|
def ip_address
|
|
|
|
load_unless_loaded!
|
|
|
|
vm[0][:NetworkConnectionSection][:NetworkConnection][:IpAddress]
|
|
|
|
end
|
|
|
|
|
2011-05-23 21:17:16 -04:00
|
|
|
def friendly_status
|
|
|
|
load_unless_loaded!
|
|
|
|
case status
|
|
|
|
when '0'
|
|
|
|
'creating'
|
2011-05-26 23:46:32 -04:00
|
|
|
when '8'
|
2011-05-23 21:17:16 -04:00
|
|
|
'off'
|
|
|
|
when '4'
|
|
|
|
'on'
|
|
|
|
else
|
|
|
|
'unkown'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ready?
|
2011-05-25 02:16:13 -04:00
|
|
|
reload # always ensure we have the correct status
|
|
|
|
status != '0' # ready unless creating.
|
2011-05-23 21:17:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def on?
|
2011-05-25 02:16:13 -04:00
|
|
|
reload # always ensure we have the correct status
|
2011-05-23 21:17:16 -04:00
|
|
|
status == '4'
|
|
|
|
end
|
|
|
|
|
|
|
|
def off?
|
2011-05-25 02:16:13 -04:00
|
|
|
reload # always ensure we have the correct status
|
2011-05-25 20:13:05 -04:00
|
|
|
status == '8'
|
2011-05-23 21:17:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def power_on
|
|
|
|
power_operation( :power_on => :powerOn )
|
|
|
|
end
|
|
|
|
|
|
|
|
def power_off
|
|
|
|
power_operation( :power_off => :powerOff )
|
|
|
|
end
|
|
|
|
|
|
|
|
def shutdown
|
|
|
|
power_operation( :power_shutdown => :shutdown )
|
|
|
|
end
|
|
|
|
|
|
|
|
def power_reset
|
|
|
|
power_operation( :power_reset => :reset )
|
|
|
|
end
|
|
|
|
|
2011-05-30 01:16:31 -04:00
|
|
|
# This is the real power-off operation
|
|
|
|
def undeploy
|
|
|
|
connection.undeploy href
|
|
|
|
end
|
|
|
|
|
2011-05-23 21:17:16 -04:00
|
|
|
def graceful_restart
|
|
|
|
requires :href
|
|
|
|
shutdown
|
|
|
|
wait_for { off? }
|
|
|
|
power_on
|
|
|
|
end
|
|
|
|
|
2011-05-26 23:47:24 -04:00
|
|
|
def vm
|
|
|
|
load_unless_loaded!
|
|
|
|
self.vm_data
|
|
|
|
end
|
|
|
|
|
2011-05-23 21:17:16 -04:00
|
|
|
def name=(new_name)
|
|
|
|
attributes[:name] = new_name
|
|
|
|
@changed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def cpus
|
|
|
|
if cpu_mess
|
2011-05-26 23:47:24 -04:00
|
|
|
{ :count => cpu_mess[:"rasd:VirtualQuantity"].to_i,
|
|
|
|
:units => cpu_mess[:"rasd:AllocationUnits"] }
|
2011-05-23 21:17:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cpus=(qty)
|
|
|
|
@changed = true
|
2011-05-26 23:47:24 -04:00
|
|
|
cpu_mess[:"rasd:VirtualQuantity"] = qty.to_s
|
2011-05-23 21:17:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def memory
|
|
|
|
if memory_mess
|
2011-05-26 23:47:24 -04:00
|
|
|
{ :amount => memory_mess[:"rasd:VirtualQuantity"].to_i,
|
|
|
|
:units => memory_mess[:"rasd:AllocationUnits"] }
|
2011-05-23 21:17:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def memory=(amount)
|
|
|
|
@changed = true
|
2011-05-30 01:20:13 -04:00
|
|
|
@update_memory_value = amount
|
|
|
|
amount
|
2011-05-23 21:17:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def disks
|
2011-05-30 01:20:13 -04:00
|
|
|
pp disk_mess
|
2011-05-23 21:17:16 -04:00
|
|
|
disk_mess.map do |dm|
|
2011-05-26 23:47:24 -04:00
|
|
|
{ :number => dm[:"rasd:AddressOnParent"], :size => dm[:"rasd:VirtualQuantity"].to_i, :resource => dm[:"rasd:HostResource"] }
|
2011-05-23 21:17:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_disk(size)
|
|
|
|
if @disk_change == :deleted
|
|
|
|
raise RuntimeError, "Can't add a disk w/o saving changes or reloading"
|
|
|
|
else
|
|
|
|
@disk_change = :added
|
|
|
|
load_unless_loaded!
|
|
|
|
virtual_hardware[:Item] << { :ResourceType => '17',
|
|
|
|
:AddressOnParent => (disk_mess.map { |dm| dm[:AddressOnParent] }.sort.last.to_i + 1).to_s,
|
|
|
|
:VirtualQuantity => size.to_s }
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_disk(number)
|
|
|
|
if @disk_change == :added
|
|
|
|
raise RuntimeError, "Can't delete a disk w/o saving changes or reloading"
|
|
|
|
else
|
|
|
|
@disk_change = :deleted
|
|
|
|
load_unless_loaded!
|
|
|
|
unless number == 0
|
|
|
|
virtual_hardware[:Item].delete_if { |vh| vh[:ResourceType] == '17' && vh[:AddressOnParent].to_i == number }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
reset_tracking
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
if new_record?
|
|
|
|
#Lame ...
|
|
|
|
raise RuntimeError, "Should not be here"
|
|
|
|
else
|
|
|
|
if on?
|
|
|
|
if @changed
|
|
|
|
raise RuntimeError, "Can't save cpu, name or memory changes while the VM is on."
|
|
|
|
end
|
|
|
|
end
|
2011-05-30 01:20:13 -04:00
|
|
|
if @update_memory_value
|
|
|
|
memory_mess[:"rasd:VirtualQuantity"] = @update_memory_value.to_s
|
|
|
|
connection.configure_vm_memory(memory_mess)
|
|
|
|
end
|
|
|
|
#connection.configure_vapp( href, _compose_vapp_data )
|
2011-05-23 21:17:16 -04:00
|
|
|
end
|
|
|
|
reset_tracking
|
2011-05-30 01:20:13 -04:00
|
|
|
true
|
2011-05-23 21:17:16 -04:00
|
|
|
end
|
|
|
|
|
2011-05-30 01:16:31 -04:00
|
|
|
def destroy
|
|
|
|
if on?
|
|
|
|
undeploy
|
|
|
|
wait_for { off? }
|
|
|
|
end
|
|
|
|
wait_for { off? } # be sure..
|
|
|
|
sleep 2 # API lies. need to give it some time to be happy.
|
|
|
|
connection.delete_vapp(href).body[:status] == "running"
|
|
|
|
end
|
|
|
|
alias :delete :destroy
|
|
|
|
|
2011-05-23 21:17:16 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def reset_tracking
|
|
|
|
@disk_change = false
|
|
|
|
@changed = false
|
2011-05-30 01:20:13 -04:00
|
|
|
@update_memory_value = nil
|
2011-05-23 21:17:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def _compose_vapp_data
|
|
|
|
{ :name => name,
|
|
|
|
:cpus => cpus[:count],
|
|
|
|
:memory => memory[:amount],
|
|
|
|
:disks => disks
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-05-26 23:47:24 -04:00
|
|
|
def virtual_hardware_section
|
|
|
|
load_unless_loaded!
|
|
|
|
vm[0][:"ovf:VirtualHardwareSection"][:"ovf:Item"]
|
|
|
|
end
|
|
|
|
|
2011-05-23 21:17:16 -04:00
|
|
|
def memory_mess
|
|
|
|
load_unless_loaded!
|
2011-05-26 23:47:24 -04:00
|
|
|
if virtual_hardware_section
|
|
|
|
virtual_hardware_section.detect { |item| item[:"rasd:ResourceType"] == "4" }
|
2011-05-23 21:17:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cpu_mess
|
|
|
|
load_unless_loaded!
|
2011-05-26 23:47:24 -04:00
|
|
|
if virtual_hardware_section
|
|
|
|
virtual_hardware_section.detect { |item| item[:"rasd:ResourceType"] == "3" }
|
2011-05-23 21:17:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def disk_mess
|
|
|
|
load_unless_loaded!
|
2011-05-26 23:47:24 -04:00
|
|
|
if virtual_hardware_section
|
|
|
|
virtual_hardware_section.select { |item| item[:"rasd:ResourceType"] == "17" }
|
2011-05-23 21:17:16 -04:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def power_operation(op)
|
|
|
|
requires :href
|
|
|
|
begin
|
|
|
|
connection.send(op.keys.first, href + "/power/action/#{op.values.first}" )
|
|
|
|
rescue Excon::Errors::InternalServerError => e
|
|
|
|
#Frankly we shouldn't get here ...
|
|
|
|
raise e unless e.to_s =~ /because it is already powered o(n|ff)/
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|