2010-03-19 18:29:42 -07:00
|
|
|
require 'fog/model'
|
|
|
|
|
2009-10-14 22:03:16 -07:00
|
|
|
module Fog
|
|
|
|
module Rackspace
|
2010-09-08 13:50:23 -07:00
|
|
|
class Compute
|
2009-10-14 22:03:16 -07:00
|
|
|
|
|
|
|
class Server < Fog::Model
|
|
|
|
|
2009-10-25 18:24:20 -07:00
|
|
|
identity :id
|
|
|
|
|
2010-01-12 22:00:49 -08:00
|
|
|
attribute :addresses
|
2010-09-07 11:30:02 -07:00
|
|
|
attribute :flavor_id, :aliases => 'flavorId'
|
|
|
|
attribute :host_id, :aliases => 'hostId'
|
|
|
|
attribute :image_id, :aliases => 'imageId'
|
2010-01-12 22:00:49 -08:00
|
|
|
attribute :metadata
|
|
|
|
attribute :name
|
2009-10-14 22:03:16 -07:00
|
|
|
attribute :personality
|
|
|
|
attribute :progress
|
2010-01-12 22:00:49 -08:00
|
|
|
attribute :status
|
2009-10-14 22:03:16 -07:00
|
|
|
|
2010-09-15 17:31:45 -07:00
|
|
|
attr_accessor :password, :private_key_path, :public_key_path, :username
|
2010-06-05 23:05:06 -07:00
|
|
|
|
2010-06-19 17:46:21 -07:00
|
|
|
def initialize(attributes={})
|
|
|
|
@flavor_id ||= 1
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2009-10-14 22:03:16 -07:00
|
|
|
def destroy
|
2009-11-21 13:56:39 -08:00
|
|
|
requires :id
|
2009-10-14 22:03:16 -07:00
|
|
|
connection.delete_server(@id)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2010-01-12 22:00:49 -08:00
|
|
|
def flavor
|
|
|
|
requires :flavor_id
|
|
|
|
connection.flavors.get(@flavor_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def image
|
|
|
|
requires :image_id
|
|
|
|
connection.images.get(@image_id)
|
|
|
|
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
|
|
|
|
|
2010-01-12 22:00:49 -08:00
|
|
|
def ready?
|
|
|
|
@status == 'ACTIVE'
|
|
|
|
end
|
|
|
|
|
2009-11-21 10:42:20 -08:00
|
|
|
def reboot(type = 'SOFT')
|
2009-11-21 13:56:39 -08:00
|
|
|
requires :id
|
2009-11-21 10:42:20 -08:00
|
|
|
connection.reboot_server(@id, type)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2010-06-05 23:05:06 -07:00
|
|
|
def private_key_path
|
2010-09-15 17:31:45 -07:00
|
|
|
@private_key_path ||= Fog.credentials[:private_key_path]
|
2010-06-05 23:05:06 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def public_key_path
|
2010-09-15 17:31:45 -07:00
|
|
|
@public_key_path ||= Fog.credentials[:public_key_path]
|
2010-06-05 23:05:06 -07:00
|
|
|
end
|
|
|
|
|
2009-10-14 22:03:16 -07:00
|
|
|
def save
|
2010-07-17 17:01:18 -05:00
|
|
|
requires :flavor_id, :image_id, :name
|
2010-05-02 19:45:40 -07:00
|
|
|
options = {
|
|
|
|
'metadata' => @metadata,
|
|
|
|
'name' => @name,
|
|
|
|
'personality' => @personality
|
|
|
|
}
|
2009-10-15 15:06:50 -07:00
|
|
|
options = options.reject {|key, value| value.nil?}
|
2010-05-02 19:45:40 -07: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 = {})
|
|
|
|
requires :addresses, :identity, :public_key_path, :username
|
|
|
|
Fog::SSH.new(addresses['public'].first, username, credentials).run([
|
2010-06-05 23:05:06 -07:00
|
|
|
%{mkdir .ssh},
|
|
|
|
%{echo "#{File.read(File.expand_path(public_key_path))}" >> ~/.ssh/authorized_keys},
|
|
|
|
%{passwd -l root},
|
|
|
|
%{echo "#{attributes.to_json}" >> ~/attributes.json},
|
|
|
|
%{echo "#{metadata.to_json}" >> ~/metadata.json}
|
|
|
|
])
|
|
|
|
rescue Errno::ECONNREFUSED
|
|
|
|
sleep(1)
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
2010-09-15 17:31:45 -07:00
|
|
|
def ssh(commands)
|
|
|
|
requires :addresses, :identity, :private_key_path, :username
|
|
|
|
@ssh ||= Fog::SSH.new(addresses['public'].first, username, :keys => [private_key_path])
|
|
|
|
@ssh.run(commands)
|
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
@username ||= root
|
|
|
|
end
|
|
|
|
|
2010-06-05 23:05:06 -07:00
|
|
|
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
|