2010-10-04 17:02:08 -04:00
|
|
|
require 'fog/core/model'
|
2010-04-16 03:20:02 -04:00
|
|
|
|
|
|
|
module Fog
|
2010-05-17 23:57:13 -04:00
|
|
|
module Terremark
|
2010-04-16 03:20:02 -04:00
|
|
|
module Shared
|
|
|
|
|
|
|
|
class Server < Fog::Model
|
|
|
|
|
|
|
|
identity :id
|
|
|
|
|
|
|
|
attribute :name
|
|
|
|
attribute :status
|
2010-05-18 14:38:50 -04:00
|
|
|
attribute :OperatingSystem
|
|
|
|
attribute :VirtualHardware
|
2010-04-16 03:20:02 -04:00
|
|
|
|
|
|
|
def destroy
|
|
|
|
requires :id
|
2010-11-19 16:45:45 -05:00
|
|
|
data = connection.power_off(id).body
|
2010-04-16 03:20:02 -04:00
|
|
|
task = connection.tasks.new(data)
|
|
|
|
task.wait_for { ready? }
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.delete_vapp(id)
|
2010-04-16 03:20:02 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# { '0' => 'Being created', '2' => 'Powered Off', '4' => 'Powered On'}
|
|
|
|
def ready?
|
2010-11-19 16:45:45 -05:00
|
|
|
status == '2'
|
2010-04-16 03:20:02 -04:00
|
|
|
end
|
|
|
|
|
2010-04-30 18:04:34 -04:00
|
|
|
def on?
|
2010-11-19 16:45:45 -05:00
|
|
|
status == '4'
|
2010-04-30 18:04:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def off?
|
2010-11-19 16:45:45 -05:00
|
|
|
status == '2'
|
2010-04-30 18:04:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def power_on(options = {})
|
|
|
|
requires :id
|
|
|
|
begin
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.power_on(id)
|
2010-04-30 18:04:34 -04:00
|
|
|
rescue Excon::Errors::InternalServerError => e
|
|
|
|
#Frankly we shouldn't get here ...
|
|
|
|
raise e unless e.to_s =~ /because it is already powered on/
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def power_off
|
|
|
|
requires :id
|
|
|
|
begin
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.power_off(id)
|
2010-04-30 18:04:34 -04:00
|
|
|
rescue Excon::Errors::InternalServerError => e
|
|
|
|
#Frankly we shouldn't get here ...
|
|
|
|
raise e unless e.to_s =~ /because it is already powered off/
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def shutdown
|
|
|
|
requires :id
|
|
|
|
begin
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.power_shutdown(id)
|
2010-04-30 18:04:34 -04:00
|
|
|
rescue Excon::Errors::InternalServerError => e
|
|
|
|
#Frankly we shouldn't get here ...
|
|
|
|
raise e unless e.to_s =~ /because it is already powered off/
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def power_reset
|
2010-04-16 03:20:02 -04:00
|
|
|
requires :id
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.power_reset(id)
|
2010-04-16 03:20:02 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2010-04-30 18:04:34 -04:00
|
|
|
def graceful_restart
|
|
|
|
requires :id
|
|
|
|
shutdown
|
|
|
|
wait_for { off? }
|
|
|
|
power_on
|
|
|
|
end
|
|
|
|
|
2010-04-16 03:20:02 -04:00
|
|
|
def save
|
|
|
|
requires :name
|
2010-11-19 16:45:45 -05:00
|
|
|
data = connection.instantiate_vapp(name)
|
2010-04-16 03:20:02 -04:00
|
|
|
merge_attributes(data.body)
|
2010-11-19 16:45:45 -05:00
|
|
|
task = connection.deploy_vapp(id)
|
2010-04-16 03:20:02 -04:00
|
|
|
task.wait_for { ready? }
|
2010-11-19 16:45:45 -05:00
|
|
|
task = connection.power_on(id)
|
2010-04-16 03:20:02 -04:00
|
|
|
task.wait_for { ready? }
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def href=(new_href)
|
2010-11-19 16:45:45 -05:00
|
|
|
self.id = new_href.split('/').last.to_i
|
2010-04-16 03:20:02 -04:00
|
|
|
end
|
|
|
|
|
2010-04-30 18:04:34 -04:00
|
|
|
def type=(new_type); @type = new_type; end
|
|
|
|
def size=(new_size); @size = new_size; end
|
|
|
|
def IpAddress=(new_ipaddress); @IpAddress = new_ipaddress; end
|
|
|
|
def Links=(new_links); @Links = new_links; end
|
2010-04-16 03:20:02 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|