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
|
2012-04-29 22:48:06 -04:00
|
|
|
attribute :image
|
|
|
|
attribute :sshkeyFingerPrint
|
2010-04-16 03:20:02 -04:00
|
|
|
attribute :status
|
2010-05-18 14:38:50 -04:00
|
|
|
attribute :OperatingSystem
|
|
|
|
attribute :VirtualHardware
|
2012-04-29 22:48:06 -04:00
|
|
|
attribute :IpAddress
|
|
|
|
attribute :PublicIpAddress
|
2010-04-16 03:20:02 -04:00
|
|
|
|
|
|
|
def destroy
|
2012-04-29 22:48:06 -04:00
|
|
|
case self.status
|
|
|
|
when "0"
|
|
|
|
return false
|
|
|
|
when "4"
|
|
|
|
data = connection.power_off(self.id).body
|
|
|
|
wait_for { off? }
|
|
|
|
end
|
|
|
|
connection.delete_vapp(self.id)
|
2010-04-16 03:20:02 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2012-04-29 22:48:06 -04:00
|
|
|
def delete_internet_services
|
|
|
|
|
|
|
|
#Find the internet service
|
|
|
|
services = connection.get_internet_services(connection.default_vdc_id)
|
|
|
|
internet_info = services.body["InternetServices"].find {|item| item["Name"] == self.name}
|
|
|
|
|
|
|
|
#Delete all the associated nodes
|
|
|
|
if internet_info
|
|
|
|
nodes = connection.get_node_services(internet_info["Id"]).body["NodeServices"]
|
|
|
|
nodes.select { |item| connection.delete_node_service(item["Id"]) }
|
|
|
|
|
|
|
|
#Clear out the services
|
|
|
|
connection.delete_internet_service(internet_info["Id"])
|
|
|
|
|
|
|
|
#Release IP Address
|
|
|
|
connection.delete_public_ip(internet_info["PublicIpAddress"]["Id"])
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2010-04-16 03:20:02 -04:00
|
|
|
# { '0' => 'Being created', '2' => 'Powered Off', '4' => 'Powered On'}
|
|
|
|
def ready?
|
2012-04-29 22:48:06 -04:00
|
|
|
state = connection.get_vapp(id).body["status"]
|
|
|
|
puts " id : #{id}, state : #{state}"
|
|
|
|
state == '2'
|
2010-04-16 03:20:02 -04:00
|
|
|
end
|
|
|
|
|
2010-04-30 18:04:34 -04:00
|
|
|
def on?
|
2012-04-29 22:48:06 -04:00
|
|
|
state = connection.get_vapp(id).body["status"]
|
|
|
|
puts " id : #{id}, state : #{state}"
|
|
|
|
state == '4'
|
2010-04-30 18:04:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def off?
|
2012-04-29 22:48:06 -04:00
|
|
|
state = connection.get_vapp(id).body["status"]
|
|
|
|
state == '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
|
|
|
|
|
2012-04-29 22:48:06 -04:00
|
|
|
def create_internet_service(protocol="TCP", port="22")
|
|
|
|
data = connection.create_internet_service(
|
|
|
|
vdc = connection.default_vdc_id,
|
|
|
|
name = self.name,
|
|
|
|
protocol = protocol,
|
|
|
|
port = port,
|
|
|
|
options = {'Enabled' => 'true',
|
|
|
|
"Description" => :name
|
|
|
|
}
|
|
|
|
)
|
|
|
|
merge_attributes(data.body)
|
|
|
|
ssh_service = data.body["Id"]
|
|
|
|
data = connection.add_node_service(
|
|
|
|
service_id = ssh_service,
|
|
|
|
ip = self.IpAddress,
|
|
|
|
name = self.name,
|
|
|
|
port = port,
|
|
|
|
options = {'Enabled' => 'true',
|
|
|
|
"Description" => :name
|
|
|
|
}
|
|
|
|
)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2010-04-16 03:20:02 -04:00
|
|
|
def save
|
|
|
|
requires :name
|
2012-04-29 22:48:06 -04:00
|
|
|
data = connection.instantiate_vapp_template(server_name=name, vapp_template=image, options={'ssh_key_fingerprint'=>sshkeyFingerPrint})
|
2010-04-16 03:20:02 -04:00
|
|
|
merge_attributes(data.body)
|
|
|
|
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 Links=(new_links); @Links = new_links; end
|
2010-04-16 03:20:02 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|