1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

generalize code

This commit is contained in:
nightshade427 2011-03-07 16:05:20 -05:00 committed by Nicholas Ricketts
parent a810092449
commit ad5ec1da27
5 changed files with 127 additions and 34 deletions

View file

@ -7,10 +7,54 @@ module Fog
identity :id
attribute :name
attribute :type
attribute :server_id
def delete
connection.linode_disk_delete server_id, id
def save
requires :server
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
@type, @image, @stack_script, @script_options, @name, @password, @size =
attributes.values_at :type, :image, :stack_script, :script_options, :name, :password, :size
create_disk
end
def destroy
requires :identity, :server
connection.linode_disk_delete server.id, id
end
def server
@server
end
private
def server=(server)
@server = server
end
def create_disk
case
when @image && @stack_script then create_disk_from_stack_script
when @image then create_disk_from_image
when @type then create_disk_type
else raise 'disk cannot be created'
end
end
def create_disk_type
self.id = connection.linode_disk_create(server.id, "#{@name}_#{@type}", @type, @size).body['DATA']['DiskID']
reload
end
def create_disk_from_image
disk = connection.linode_disk_createfromdistribution server.id, @image.id, "#{@name}_main", @size, @password
self.id = disk.body['DATA']['DiskID']
reload
end
def create_disk_from_stack_script
disk = connection.linode_disk_createfromstackscript(server.id, @script.id, @image.id,
"#{@name}_main", @size, @password, @script_options)
self.id = disk.body['DATA']['DiskID']
reload
end
end
end

View file

@ -6,17 +6,25 @@ module Fog
class Compute
class Disks < Fog::Collection
model Fog::Linode::Compute::Disk
attribute :server
def all(linode_id)
load disks(linode_id)
def all
requires :server
load disks(server.id)
end
def get(linode_id, id)
new disks(linode_id, id).first
def get(id)
requires :server
new disks(server.id, id).first
rescue Fog::Linode::Compute::NotFound
nil
end
def new(attributes = {})
requires :server
super({ :server => server }.merge!(attributes))
end
private
def disks(linode_id, id=nil)
connection.linode_disk_list(linode_id, id).body['DATA'].map { |disk| map_disk disk }

View file

@ -7,6 +7,23 @@ module Fog
identity :id
attribute :ip
attribute :public
def save
requires :server
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
connection.linode_ip_addprivate server.id
server.ips.all.find { |ip| !ip.public }
end
def server
@server
end
private
def server=(server)
@server = server
end
end
end
end

View file

@ -6,16 +6,24 @@ module Fog
class Compute
class Ips < Fog::Collection
model Fog::Linode::Compute::Ip
attribute :server
def all(linode_id)
load ips(linode_id)
def all
requires :server
load ips(server.id)
end
def get(linode_id, id)
new ips(linode_id, id).first
def get(id)
requires :server
new ips(server.id, id).first
rescue Fog::Linode::Compute::NotFound
nil
end
end
def new(attributes = {})
requires :server
super({ :server => server }.merge!(attributes))
end
private
def ips(linode_id, id=nil)

View file

@ -4,16 +4,17 @@ module Fog
module Linode
class Compute
class Server < Fog::Model
attr_accessor :stack_script
identity :id
attribute :name
attribute :status
def ips
connection.ips.all(self.id)
@ips = Fog::Linode::Compute::Ips.new :server => self, :connection => connection
end
def disks
connection.disks.all(self.id)
@disks = Fog::Linode::Compute::Disks.new :server => self, :connection => connection
end
def disks?
@ -22,33 +23,48 @@ module Fog
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
data_center, flavor, image, name, password = attributes.values_at :data_center, :flavor, :image, :name, :password
@data_center, @flavor, @image, @type, @payment_terms, @stack_script, @name, @password, @callback =
attributes.values_at :data_center, :flavor, :image, :type, :payment_terms, :stack_script, :name, :password, :callback
id = connection.linode_create(data_center.id, flavor.id, 1).body['DATA']['LinodeID']
connection.linode_ip_addprivate id
ip = connection.ips.all(id).find { |ip| !ip.public }
script = connection.stack_scripts.get 2161
script_options = { :hostname => name, :privip => ip.ip }
swap = connection.linode_disk_create(id, "#{name}_swap", 'swap', flavor.ram).body['DATA']['DiskID']
disk = connection.linode_disk_createfromstackscript(id, script.id, image.id, "#{name}_main",
(flavor.disk*1024)-flavor.ram, password, script_options).body['DATA']['DiskID']
config = connection.linode_config_create(id, 110, name, "#{disk},#{swap},,,,,,,").body['DATA']['ConfigID']
connection.linode_update id, :label => name
connection.linode_boot id, config
new_server = connection.servers.get id
# merge_attributes(new_server)
true
create_linode
@callback.call self if @callback
create_disks
create_config
boot_linode
rescue Exception => ex
destroy if id
raise ex
end
def delete
def destroy
requires :identity
connection.linode_shutdown id
disks.each { |disk| disk.delete }
disks.each { |disk| disk.destroy }
wait_for { not disks? }
connection.linode_delete id
end
private
def create_linode
self.id = connection.linode_create(@data_center.id, @flavor.id, @payment_terms).body['DATA']['LinodeID']
reload
connection.linode_update id, :label => @name
ips.create
end
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,
:script_options => @script_options, :name => @name, :size => (@flavor.disk*1024)-@flavor.ram)
end
def create_config
@config = connection.linode_config_create(id, 110, @name, "#{@disk.id},#{@swap.id},,,,,,,").body['DATA']['ConfigID']
end
def boot_linode
connection.linode_boot id, @config
end
end
end
end