2011-08-11 11:53:52 -05:00
|
|
|
require 'fog/compute/models/server'
|
2010-03-19 18:29:42 -07:00
|
|
|
|
2009-10-14 22:03:16 -07:00
|
|
|
module Fog
|
2011-06-16 16:28:54 -07:00
|
|
|
module Compute
|
|
|
|
class Rackspace
|
2009-10-14 22:03:16 -07:00
|
|
|
|
2011-08-11 11:53:52 -05:00
|
|
|
class Server < Fog::Compute::Server
|
2009-10-14 22:03:16 -07:00
|
|
|
|
2009-10-25 18:24:20 -07:00
|
|
|
identity :id
|
|
|
|
|
2010-01-12 22:00:49 -08:00
|
|
|
attribute :addresses
|
2011-12-14 18:05:07 +00:00
|
|
|
attribute :flavor_id, :aliases => 'flavorId', :type => :integer
|
2010-09-07 11:30:02 -07:00
|
|
|
attribute :host_id, :aliases => 'hostId'
|
2011-12-14 18:05:07 +00:00
|
|
|
attribute :image_id, :aliases => 'imageId', :type => :integer
|
2010-01-12 22:00:49 -08:00
|
|
|
attribute :metadata
|
|
|
|
attribute :name
|
2009-10-14 22:03:16 -07:00
|
|
|
attribute :personality
|
|
|
|
attribute :progress
|
2011-05-24 14:59:48 -07:00
|
|
|
attribute :state, :aliases => 'status'
|
2009-10-14 22:03:16 -07:00
|
|
|
|
2010-09-27 20:38:17 -07:00
|
|
|
attr_reader :password
|
2010-06-05 23:05:06 -07:00
|
|
|
|
2010-06-19 17:46:21 -07:00
|
|
|
def initialize(attributes={})
|
2011-04-14 16:31:58 -07:00
|
|
|
self.flavor_id ||= 1 # 256 server
|
|
|
|
self.image_id ||= 49 # Ubuntu 10.04 LTS 64bit
|
2010-06-19 17:46:21 -07:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2009-10-14 22:03:16 -07:00
|
|
|
def destroy
|
2009-11-21 13:56:39 -08:00
|
|
|
requires :id
|
2010-11-19 13:45:45 -08:00
|
|
|
connection.delete_server(id)
|
2009-10-14 22:03:16 -07:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2010-01-12 22:00:49 -08:00
|
|
|
def flavor
|
|
|
|
requires :flavor_id
|
2010-11-19 13:45:45 -08:00
|
|
|
connection.flavors.get(flavor_id)
|
2010-01-12 22:00:49 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def image
|
|
|
|
requires :image_id
|
2010-11-19 13:45:45 -08:00
|
|
|
connection.images.get(image_id)
|
2010-01-12 22:00:49 -08:00
|
|
|
end
|
|
|
|
|
2009-11-19 08:19:46 -08:00
|
|
|
def images
|
2009-11-21 13:56:39 -08:00
|
|
|
requires :id
|
2009-11-19 08:19:46 -08:00
|
|
|
connection.images(:server => self)
|
|
|
|
end
|
|
|
|
|
2011-02-22 16:36:15 -08:00
|
|
|
def private_ip_address
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_ip_address
|
2011-03-22 16:41:46 -07:00
|
|
|
addresses['public'].first
|
2011-02-22 16:36:15 -08:00
|
|
|
end
|
|
|
|
|
2010-10-04 16:08:34 -07:00
|
|
|
def ready?
|
2011-05-24 14:59:48 -07:00
|
|
|
self.state == 'ACTIVE'
|
2010-10-04 16:08:34 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def reboot(type = 'SOFT')
|
|
|
|
requires :id
|
2010-11-19 13:45:45 -08:00
|
|
|
connection.reboot_server(id, type)
|
2010-10-04 16:08:34 -07:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2009-10-14 22:03:16 -07:00
|
|
|
def save
|
2010-10-04 16:08:34 -07:00
|
|
|
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
|
2010-11-29 19:28:14 -08:00
|
|
|
requires :flavor_id, :image_id
|
2010-05-02 19:45:40 -07:00
|
|
|
options = {
|
2010-11-19 13:45:45 -08:00
|
|
|
'metadata' => metadata,
|
|
|
|
'name' => name,
|
|
|
|
'personality' => personality
|
2010-05-02 19:45:40 -07:00
|
|
|
}
|
2009-10-15 15:06:50 -07:00
|
|
|
options = options.reject {|key, value| value.nil?}
|
2010-11-19 13:45:45 -08:00
|
|
|
data = connection.create_server(flavor_id, image_id, options)
|
2009-10-14 22:03:16 -07:00
|
|
|
merge_attributes(data.body['server'])
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2010-09-15 17:31:45 -07:00
|
|
|
def setup(credentials = {})
|
2011-03-22 16:41:46 -07:00
|
|
|
requires :public_ip_address, :identity, :public_key, :username
|
|
|
|
Fog::SSH.new(public_ip_address, username, credentials).run([
|
2010-06-05 23:05:06 -07:00
|
|
|
%{mkdir .ssh},
|
2010-09-23 17:39:25 -07:00
|
|
|
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
|
2011-01-31 17:42:53 -08:00
|
|
|
%{passwd -l #{username}},
|
2012-04-25 10:31:28 -04:00
|
|
|
%{echo "#{Fog::JSON.encode(attributes)}" >> ~/attributes.json},
|
|
|
|
%{echo "#{Fog::JSON.encode(metadata)}" >> ~/metadata.json}
|
2010-06-05 23:05:06 -07:00
|
|
|
])
|
|
|
|
rescue Errno::ECONNREFUSED
|
|
|
|
sleep(1)
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def adminPass=(new_admin_pass)
|
|
|
|
@password = new_admin_pass
|
|
|
|
end
|
|
|
|
|
2009-10-14 22:03:16 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2010-06-05 23:05:06 -07:00
|
|
|
|
2009-10-14 22:03:16 -07:00
|
|
|
end
|