1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/linode/models/compute/server.rb

99 lines
2.7 KiB
Ruby
Raw Normal View History

require 'fog/compute/models/server'
2011-03-01 16:37:27 -05:00
module Fog
module Compute
class Linode
class Server < Fog::Compute::Server
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
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
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
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
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-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
reload
2011-03-07 16:05:20 -05:00
end
2011-03-07 16:05:20 -05:00
def create_disks
@swap = disks.create :type => :swap, :name => @name, :size => @flavor.ram
@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