2010-10-04 17:02:08 -04:00
|
|
|
require 'fog/core/model'
|
2010-03-19 21:29:42 -04:00
|
|
|
|
2009-10-15 01:03:16 -04:00
|
|
|
module Fog
|
|
|
|
module Rackspace
|
2010-09-08 16:50:23 -04:00
|
|
|
class Compute
|
2009-10-15 01:03:16 -04:00
|
|
|
|
|
|
|
class Server < Fog::Model
|
|
|
|
|
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
|
2010-01-13 01:00:49 -05:00
|
|
|
attribute :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={})
|
2010-11-19 16:45:45 -05:00
|
|
|
self.flavor_id ||= 1
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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?
|
2010-11-19 16:45:45 -05:00
|
|
|
status == '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 = {})
|
2010-09-23 20:39:25 -04:00
|
|
|
requires :addresses, :identity, :public_key, :username
|
2010-09-15 20:31:45 -04:00
|
|
|
Fog::SSH.new(addresses['public'].first, 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}},
|
2010-06-06 02:05:06 -04:00
|
|
|
%{echo "#{attributes.to_json}" >> ~/attributes.json},
|
|
|
|
%{echo "#{metadata.to_json}" >> ~/metadata.json}
|
|
|
|
])
|
|
|
|
rescue Errno::ECONNREFUSED
|
|
|
|
sleep(1)
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
2010-09-15 20:31:45 -04:00
|
|
|
def ssh(commands)
|
2011-01-22 04:11:19 -05:00
|
|
|
requires :addresses, :identity, :username
|
|
|
|
|
|
|
|
options = {}
|
|
|
|
options[:key_data] = [private_key] if private_key
|
|
|
|
Fog::SSH.new(addresses['public'].first, username, options).run(commands)
|
2010-09-15 20:31:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|