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