2011-08-11 12:53:52 -04:00
|
|
|
require 'fog/compute/models/server'
|
2010-11-08 07:21:31 -05:00
|
|
|
|
|
|
|
module Fog
|
2011-06-16 19:28:54 -04:00
|
|
|
module Compute
|
|
|
|
class Brightbox
|
2010-11-08 07:21:31 -05:00
|
|
|
|
2011-08-11 12:53:52 -04:00
|
|
|
class Server < Fog::Compute::Server
|
2010-11-08 07:21:31 -05:00
|
|
|
|
|
|
|
identity :id
|
2011-08-22 12:41:18 -04:00
|
|
|
attribute :resource_type
|
2010-11-08 07:21:31 -05:00
|
|
|
attribute :url
|
2011-08-22 12:41:18 -04:00
|
|
|
|
2010-11-08 07:21:31 -05:00
|
|
|
attribute :name
|
2011-05-24 17:59:48 -04:00
|
|
|
attribute :state, :aliases => 'status'
|
2011-08-22 12:41:18 -04:00
|
|
|
|
2010-11-08 07:21:31 -05:00
|
|
|
attribute :hostname
|
2012-05-09 07:01:17 -04:00
|
|
|
attribute :fqdn
|
2010-11-08 07:21:31 -05:00
|
|
|
attribute :user_data
|
2011-08-22 12:41:18 -04:00
|
|
|
attribute :console_url
|
2012-02-08 08:30:35 -05:00
|
|
|
attribute :fqdn
|
2011-08-22 12:41:18 -04:00
|
|
|
attribute :console_token
|
2010-11-08 07:21:31 -05:00
|
|
|
|
2011-08-22 12:41:18 -04:00
|
|
|
# Times
|
2011-08-26 11:00:24 -04:00
|
|
|
attribute :created_at, :type => :time
|
|
|
|
attribute :started_at, :type => :time
|
|
|
|
attribute :console_token_expires, :type => :time
|
|
|
|
attribute :deleted_at, :type => :time
|
2010-11-08 07:21:31 -05:00
|
|
|
|
2011-08-22 12:41:18 -04:00
|
|
|
# Links - to be replaced
|
2011-05-24 17:59:48 -04:00
|
|
|
attribute :account_id, :aliases => "account", :squash => "id"
|
|
|
|
attribute :image_id, :aliases => "image", :squash => "id"
|
2011-10-14 06:31:23 -04:00
|
|
|
|
2010-11-08 07:21:31 -05:00
|
|
|
attribute :snapshots
|
|
|
|
attribute :cloud_ips
|
|
|
|
attribute :interfaces
|
2011-09-22 06:14:55 -04:00
|
|
|
attribute :server_groups
|
2011-10-14 06:32:01 -04:00
|
|
|
attribute :zone
|
|
|
|
attribute :server_type
|
2010-11-08 07:21:31 -05:00
|
|
|
|
2011-04-14 19:31:58 -04:00
|
|
|
def initialize(attributes={})
|
2012-10-19 04:26:21 -04:00
|
|
|
# Call super first to initialize the 'connection' object for our default image
|
2011-04-14 19:31:58 -04:00
|
|
|
super
|
2012-10-19 04:26:21 -04:00
|
|
|
# FIXME connection is actually <Fog::Compute::Brightbox::Real> not <Fog::Connection>
|
|
|
|
self.image_id ||= connection.default_image
|
2011-04-14 19:31:58 -04:00
|
|
|
end
|
|
|
|
|
2011-10-14 06:31:23 -04:00
|
|
|
def zone_id
|
2011-10-24 06:41:33 -04:00
|
|
|
if t_zone_id = attributes[:zone_id]
|
|
|
|
t_zone_id
|
|
|
|
elsif zone
|
|
|
|
zone[:id] || zone['id']
|
|
|
|
end
|
2011-10-14 06:31:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def flavor_id
|
2011-10-24 06:41:33 -04:00
|
|
|
if t_flavour_id = attributes[:flavor_id]
|
|
|
|
t_flavour_id
|
|
|
|
elsif server_type
|
|
|
|
server_type[:id] || server_type['id']
|
|
|
|
end
|
2011-10-14 06:31:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def zone_id=(incoming_zone_id)
|
|
|
|
attributes[:zone_id] = incoming_zone_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def flavor_id=(incoming_flavour_id)
|
|
|
|
attributes[:flavor_id] = incoming_flavour_id
|
|
|
|
end
|
|
|
|
|
2010-11-08 07:21:31 -05:00
|
|
|
def snapshot
|
|
|
|
requires :identity
|
|
|
|
connection.snapshot_server(identity)
|
|
|
|
end
|
|
|
|
|
2012-08-14 12:08:39 -04:00
|
|
|
# Directly requesting a server reboot is not supported in the API
|
|
|
|
# so needs to attempt a shutdown/stop, wait and start again.
|
|
|
|
#
|
|
|
|
# Default behaviour is a hard reboot because it is more reliable
|
|
|
|
# because the state of the server's OS is irrelevant.
|
|
|
|
#
|
|
|
|
# @param [Boolean] use_hard_reboot
|
|
|
|
# @return [Boolean]
|
|
|
|
def reboot(use_hard_reboot = true)
|
|
|
|
requires :identity
|
|
|
|
if ready?
|
|
|
|
unless use_hard_reboot
|
|
|
|
soft_reboot
|
|
|
|
else
|
|
|
|
hard_reboot
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# Not able to reboot if not ready in the first place
|
|
|
|
false
|
|
|
|
end
|
2010-11-08 07:21:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
|
|
|
requires :identity
|
|
|
|
connection.start_server(identity)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
requires :identity
|
|
|
|
connection.stop_server(identity)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def shutdown
|
|
|
|
requires :identity
|
|
|
|
connection.shutdown_server(identity)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
requires :identity
|
|
|
|
connection.destroy_server(identity)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def flavor
|
|
|
|
requires :flavor_id
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.flavors.get(flavor_id)
|
2010-11-08 07:21:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def image
|
|
|
|
requires :image_id
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.images.get(image_id)
|
2010-11-08 07:21:31 -05:00
|
|
|
end
|
|
|
|
|
2011-02-22 19:36:15 -05:00
|
|
|
def private_ip_address
|
2011-09-30 12:50:31 -04:00
|
|
|
unless interfaces.empty?
|
|
|
|
interfaces.first["ipv4_address"]
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2011-02-22 19:36:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def public_ip_address
|
2011-09-30 12:50:31 -04:00
|
|
|
unless cloud_ips.empty?
|
|
|
|
cloud_ips.first["public_ip"]
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2011-02-22 19:36:15 -05:00
|
|
|
end
|
|
|
|
|
2010-11-08 07:21:31 -05:00
|
|
|
def ready?
|
2011-05-24 17:59:48 -04:00
|
|
|
self.state == 'active'
|
2010-11-08 07:21:31 -05:00
|
|
|
end
|
|
|
|
|
2011-03-01 12:10:41 -05:00
|
|
|
def activate_console
|
|
|
|
requires :identity
|
|
|
|
response = connection.activate_console_server(identity)
|
|
|
|
[response["console_url"], response["console_token"], response["console_token_expires"]]
|
|
|
|
end
|
|
|
|
|
2010-11-08 07:21:31 -05:00
|
|
|
def save
|
2011-08-23 12:22:07 -04:00
|
|
|
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
|
2010-11-08 07:21:31 -05:00
|
|
|
requires :image_id
|
|
|
|
options = {
|
2010-11-19 16:45:45 -05:00
|
|
|
:image => image_id,
|
|
|
|
:name => name,
|
|
|
|
:zone => zone_id,
|
2011-09-22 06:14:55 -04:00
|
|
|
:user_data => user_data,
|
|
|
|
:server_groups => server_groups
|
2010-11-08 07:21:31 -05:00
|
|
|
}.delete_if {|k,v| v.nil? || v == "" }
|
2011-08-19 08:55:07 -04:00
|
|
|
unless flavor_id.nil? || flavor_id == ""
|
|
|
|
options.merge!(:server_type => flavor_id)
|
|
|
|
end
|
2010-11-09 12:40:04 -05:00
|
|
|
data = connection.create_server(options)
|
2010-11-08 07:21:31 -05:00
|
|
|
merge_attributes(data)
|
|
|
|
true
|
|
|
|
end
|
2012-08-14 12:08:39 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
# Hard reboots are fast, avoiding the OS by doing a "power off"
|
|
|
|
def hard_reboot
|
|
|
|
stop
|
|
|
|
wait_for { ! ready? }
|
|
|
|
start
|
|
|
|
end
|
|
|
|
|
|
|
|
# Soft reboots often timeout if the OS missed the request so we do more
|
|
|
|
# error checking trying to detect the timeout
|
|
|
|
#
|
|
|
|
# @fixme - Using side effect of wait_for's (evaluated block) to detect timeouts
|
|
|
|
def soft_reboot
|
|
|
|
shutdown
|
|
|
|
if wait_for(20) { ! ready? }
|
|
|
|
# Server is now down, start it up again
|
|
|
|
start
|
|
|
|
else
|
|
|
|
# We timed out
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2010-11-08 07:21:31 -05:00
|
|
|
end
|
2012-08-14 12:08:39 -04:00
|
|
|
|
2010-11-08 07:21:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|