2011-08-11 12:53:52 -04:00
|
|
|
require 'fog/compute/models/server'
|
2011-03-01 16:37:27 -05:00
|
|
|
|
|
|
|
module Fog
|
2011-06-16 19:28:54 -04:00
|
|
|
module Compute
|
|
|
|
class Linode
|
2011-08-11 12:53:52 -04:00
|
|
|
class Server < Fog::Compute::Server
|
2011-03-08 16:33:02 -05:00
|
|
|
attr_reader :stack_script
|
2011-10-05 17:59:32 -04:00
|
|
|
attr_accessor :private_key, :username
|
2011-03-01 16:37:27 -05:00
|
|
|
identity :id
|
|
|
|
attribute :name
|
|
|
|
attribute :status
|
|
|
|
|
2011-10-05 17:59:32 -04:00
|
|
|
def initialize(attributes={})
|
|
|
|
super
|
|
|
|
self.username = 'root'
|
|
|
|
end
|
|
|
|
|
2011-03-01 16:37:27 -05:00
|
|
|
def ips
|
2011-06-16 19:28:54 -04:00
|
|
|
Fog::Compute::Linode::Ips.new :server => self, :connection => connection
|
2011-03-01 16:37:27 -05:00
|
|
|
end
|
|
|
|
|
2011-10-05 17:33:49 -04:00
|
|
|
def public_ip_address
|
2011-10-05 18:13:24 -04:00
|
|
|
ips.find{|ip| ip.ip !~ /^192/}.ip
|
2011-10-05 17:33:49 -04:00
|
|
|
end
|
|
|
|
|
2011-03-04 15:03:51 -05:00
|
|
|
def disks
|
2011-06-16 19:28:54 -04:00
|
|
|
Fog::Compute::Linode::Disks.new :server => self, :connection => connection
|
2011-03-04 15:03:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def disks?
|
|
|
|
not disks.empty?
|
|
|
|
end
|
|
|
|
|
2011-03-17 14:49:58 -04:00
|
|
|
def reboot
|
|
|
|
connection.linode_reboot id
|
|
|
|
end
|
|
|
|
|
|
|
|
def shutdown
|
|
|
|
connection.linode_shutdown id
|
|
|
|
end
|
|
|
|
|
|
|
|
def boot
|
|
|
|
connection.linode_boot id, config
|
2011-08-11 12:53:52 -04:00
|
|
|
end
|
2011-03-17 14:49:58 -04:00
|
|
|
|
2011-03-01 16:37:27 -05:00
|
|
|
def save
|
|
|
|
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
|
2011-03-17 15:06:27 -04:00
|
|
|
@data_center, @flavor, @image, @kernel, @type, @payment_terms, @stack_script, @name, @password, @callback =
|
|
|
|
attributes.values_at :data_center, :flavor, :image, :kernel, :type, :payment_terms, :stack_script, :name, :password, :callback
|
2011-03-07 16:05:20 -05:00
|
|
|
|
|
|
|
create_linode
|
|
|
|
@callback.call self if @callback
|
|
|
|
create_disks
|
|
|
|
create_config
|
|
|
|
boot_linode
|
2011-03-18 12:53:57 -04:00
|
|
|
self
|
2011-03-07 16:05:20 -05:00
|
|
|
rescue Exception => ex
|
|
|
|
destroy if id
|
|
|
|
raise ex
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
requires :identity
|
2011-03-04 15:03:51 -05:00
|
|
|
connection.linode_shutdown id
|
2011-03-07 16:05:20 -05:00
|
|
|
disks.each { |disk| disk.destroy }
|
2011-03-04 15:03:51 -05:00
|
|
|
wait_for { not disks? }
|
|
|
|
connection.linode_delete id
|
|
|
|
end
|
2011-03-07 16:05:20 -05:00
|
|
|
|
|
|
|
private
|
2011-03-17 15:06:27 -04:00
|
|
|
def config
|
|
|
|
connection.linode_config_list(id).body['DATA'].first['ConfigID']
|
|
|
|
end
|
2011-08-11 12:53:52 -04:00
|
|
|
|
2011-03-07 16:05:20 -05:00
|
|
|
def create_linode
|
|
|
|
self.id = connection.linode_create(@data_center.id, @flavor.id, @payment_terms).body['DATA']['LinodeID']
|
|
|
|
connection.linode_update id, :label => @name
|
|
|
|
ips.create
|
2011-03-08 16:33:02 -05:00
|
|
|
reload
|
2011-03-07 16:05:20 -05:00
|
|
|
end
|
2011-08-11 12:53:52 -04:00
|
|
|
|
2011-03-07 16:05:20 -05:00
|
|
|
def create_disks
|
|
|
|
@swap = disks.create :type => :swap, :name => @name, :size => @flavor.ram
|
2011-03-08 16:33:02 -05:00
|
|
|
@disk = disks.create(:type => @type, :image => @image, :stack_script => @stack_script,
|
|
|
|
:password => @password, :name => @name, :size => (@flavor.disk*1024)-@flavor.ram)
|
2011-03-07 16:05:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_config
|
2011-03-17 15:06:27 -04:00
|
|
|
@config = connection.linode_config_create(id, @kernel.id, @name, "#{@disk.id},#{@swap.id},,,,,,,").body['DATA']['ConfigID']
|
2011-03-07 16:05:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def boot_linode
|
|
|
|
connection.linode_boot id, @config
|
|
|
|
end
|
2011-03-01 16:37:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|