2012-07-30 18:22:16 -04:00
|
|
|
require 'fog/compute/models/server'
|
2013-01-07 12:10:17 -05:00
|
|
|
require 'fog/rackspace/models/compute_v2/metadata'
|
2012-07-30 18:22:16 -04:00
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
|
|
|
class RackspaceV2
|
|
|
|
class Server < Fog::Compute::Server
|
|
|
|
# States
|
|
|
|
ACTIVE = 'ACTIVE'
|
|
|
|
BUILD = 'BUILD'
|
|
|
|
DELETED = 'DELETED'
|
|
|
|
ERROR = 'ERROR'
|
|
|
|
HARD_REBOOT = 'HARD_REBOOT'
|
|
|
|
MIGRATING = 'MIGRATING'
|
|
|
|
PASSWORD = 'PASSWORD'
|
|
|
|
REBOOT = 'REBOOT'
|
|
|
|
REBUILD = 'REBUILD'
|
|
|
|
RESCUE = 'RESCUE'
|
|
|
|
RESIZE = 'RESIZE'
|
|
|
|
REVERT_RESIZE = 'REVERT_RESIZE'
|
|
|
|
SUSPENDED = 'SUSPENDED'
|
|
|
|
UNKNOWN = 'UNKNOWN'
|
|
|
|
VERIFY_RESIZE = 'VERIFY_RESIZE'
|
|
|
|
|
|
|
|
identity :id
|
|
|
|
|
|
|
|
attribute :name
|
|
|
|
attribute :created
|
|
|
|
attribute :updated
|
|
|
|
attribute :host_id, :aliases => 'hostId'
|
|
|
|
attribute :state, :aliases => 'status'
|
|
|
|
attribute :progress
|
|
|
|
attribute :user_id
|
|
|
|
attribute :tenant_id
|
|
|
|
attribute :links
|
2012-10-17 14:16:15 -04:00
|
|
|
attribute :personality
|
2012-07-30 18:22:16 -04:00
|
|
|
attribute :ipv4_address, :aliases => 'accessIPv4'
|
|
|
|
attribute :ipv6_address, :aliases => 'accessIPv6'
|
|
|
|
attribute :disk_config, :aliases => 'OS-DCF:diskConfig'
|
|
|
|
attribute :bandwidth, :aliases => 'rax-bandwidth:bandwidth'
|
|
|
|
attribute :addresses
|
|
|
|
attribute :flavor_id, :aliases => 'flavor', :squash => 'id'
|
|
|
|
attribute :image_id, :aliases => 'image', :squash => 'id'
|
2013-01-22 16:34:52 -05:00
|
|
|
|
2013-01-10 16:48:30 -05:00
|
|
|
attr_reader :password
|
|
|
|
|
2013-01-07 12:10:17 -05:00
|
|
|
def initialize(attributes={})
|
2013-01-08 10:09:43 -05:00
|
|
|
@service = attributes[:service]
|
2013-01-07 12:10:17 -05:00
|
|
|
super
|
2013-01-10 16:48:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
alias :access_ipv4_address :ipv4_address
|
|
|
|
alias :access_ipv4_address= :ipv4_address=
|
|
|
|
alias :access_ipv6_address :ipv6_address
|
|
|
|
alias :access_ipv6_address= :ipv6_address=
|
2013-01-07 12:10:17 -05:00
|
|
|
|
|
|
|
def metadata
|
|
|
|
@metadata ||= begin
|
|
|
|
Fog::Compute::RackspaceV2::Metadata.new({
|
2013-01-08 10:09:43 -05:00
|
|
|
:service => service,
|
2013-01-07 12:10:17 -05:00
|
|
|
:parent => self
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def metadata=(hash={})
|
|
|
|
metadata.from_hash(hash)
|
|
|
|
end
|
2012-08-08 21:57:37 -04:00
|
|
|
|
2012-07-30 18:22:16 -04:00
|
|
|
def save
|
2012-12-22 21:45:05 -05:00
|
|
|
if persisted?
|
2012-07-30 18:22:16 -04:00
|
|
|
update
|
|
|
|
else
|
|
|
|
create
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
requires :name, :image_id, :flavor_id
|
2012-10-17 14:16:15 -04:00
|
|
|
|
2012-10-09 12:24:08 -04:00
|
|
|
options = {}
|
|
|
|
options[:disk_config] = disk_config unless disk_config.nil?
|
2013-01-22 16:34:52 -05:00
|
|
|
options[:metadata] = metadata.to_hash unless @metadata.nil?
|
2012-10-17 14:16:15 -04:00
|
|
|
options[:personality] = personality unless personality.nil?
|
2012-11-09 20:37:22 -05:00
|
|
|
|
2012-12-22 18:24:03 -05:00
|
|
|
data = service.create_server(name, image_id, flavor_id, 1, 1, options)
|
2012-07-30 18:22:16 -04:00
|
|
|
merge_attributes(data.body['server'])
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2013-01-10 16:48:30 -05:00
|
|
|
requires :identity
|
|
|
|
options = {
|
|
|
|
'name' => name,
|
|
|
|
'accessIPv4' => ipv4_address,
|
|
|
|
'accessIPv6' => ipv6_address
|
|
|
|
}
|
|
|
|
|
|
|
|
data = service.update_server(identity, options)
|
2012-07-30 18:22:16 -04:00
|
|
|
merge_attributes(data.body['server'])
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
requires :identity
|
2012-12-22 18:24:03 -05:00
|
|
|
service.delete_server(identity)
|
2012-07-30 18:22:16 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def flavor
|
|
|
|
requires :flavor_id
|
2012-12-22 18:24:03 -05:00
|
|
|
@flavor ||= service.flavors.get(flavor_id)
|
2012-07-30 18:22:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def image
|
|
|
|
requires :image_id
|
2012-12-22 18:24:03 -05:00
|
|
|
@image ||= service.images.get(image_id)
|
2012-07-30 18:22:16 -04:00
|
|
|
end
|
2012-12-22 18:24:03 -05:00
|
|
|
|
2012-12-11 09:35:06 -05:00
|
|
|
def create_image(name, options = {})
|
|
|
|
requires :identity
|
2012-12-22 18:24:03 -05:00
|
|
|
response = service.create_image(identity, name, options)
|
2013-01-14 17:50:50 -05:00
|
|
|
begin
|
|
|
|
image_id = response.headers["Location"].match(/\/([^\/]+$)/)[1]
|
|
|
|
Fog::Compute::RackspaceV2::Image.new(:collection => service.images, :service => service, :id => image_id)
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
2012-12-11 09:35:06 -05:00
|
|
|
end
|
2012-07-30 18:22:16 -04:00
|
|
|
|
2012-08-30 11:02:14 -04:00
|
|
|
def attachments
|
|
|
|
@attachments ||= begin
|
|
|
|
Fog::Compute::RackspaceV2::Attachments.new({
|
2012-12-22 18:24:03 -05:00
|
|
|
:service => service,
|
2012-08-30 11:02:14 -04:00
|
|
|
:server => self
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2013-01-09 15:54:02 -05:00
|
|
|
|
2013-01-11 09:29:53 -05:00
|
|
|
def attach_volume(volume, device=nil)
|
2013-01-09 15:54:02 -05:00
|
|
|
requires :identity
|
|
|
|
volume_id = volume.is_a?(String) ? volume : volume.id
|
|
|
|
attachments.create(:server_id => identity, :volume_id => volume_id, :device => device)
|
|
|
|
end
|
2012-12-22 18:24:03 -05:00
|
|
|
|
2012-11-01 15:33:54 -04:00
|
|
|
def private_ip_address
|
|
|
|
addresses['private'].select{|a| a["version"] == 4}[0]["addr"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_ip_address
|
|
|
|
ipv4_address
|
|
|
|
end
|
2012-08-30 11:02:14 -04:00
|
|
|
|
2013-01-11 17:36:07 -05:00
|
|
|
def ready?(ready_state = ACTIVE, error_states=[ERROR])
|
|
|
|
if error_states
|
|
|
|
error_states = Array(error_states)
|
2013-01-14 09:36:21 -05:00
|
|
|
raise "Server should have transitioned to '#{ready_state}' not '#{state}'" if error_states.include?(state)
|
2013-01-11 17:36:07 -05:00
|
|
|
end
|
|
|
|
state == ready_state
|
2012-07-30 18:22:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reboot(type = 'SOFT')
|
|
|
|
requires :identity
|
2012-12-22 18:24:03 -05:00
|
|
|
service.reboot_server(identity, type)
|
2012-07-30 18:22:16 -04:00
|
|
|
self.state = type == 'SOFT' ? REBOOT : HARD_REBOOT
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def resize(flavor_id)
|
|
|
|
requires :identity
|
2012-12-22 18:24:03 -05:00
|
|
|
service.resize_server(identity, flavor_id)
|
2012-07-30 18:22:16 -04:00
|
|
|
self.state = RESIZE
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-01-08 11:53:43 -05:00
|
|
|
def rebuild(image_id, options={})
|
2012-07-30 18:22:16 -04:00
|
|
|
requires :identity
|
2013-01-08 11:53:43 -05:00
|
|
|
service.rebuild_server(identity, image_id, options)
|
2012-07-30 18:22:16 -04:00
|
|
|
self.state = REBUILD
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirm_resize
|
|
|
|
requires :identity
|
2012-12-22 18:24:03 -05:00
|
|
|
service.confirm_resize_server(identity)
|
2012-07-30 18:22:16 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def revert_resize
|
|
|
|
requires :identity
|
2012-12-22 18:24:03 -05:00
|
|
|
service.revert_resize_server(identity)
|
2012-07-30 18:22:16 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_admin_password(password)
|
|
|
|
requires :identity
|
2012-12-22 18:24:03 -05:00
|
|
|
service.change_server_password(identity, password)
|
2012-07-30 18:22:16 -04:00
|
|
|
self.state = PASSWORD
|
2012-08-08 21:57:37 -04:00
|
|
|
@password = password
|
2012-07-30 18:22:16 -04:00
|
|
|
true
|
|
|
|
end
|
2012-12-22 18:24:03 -05:00
|
|
|
|
2012-11-01 16:24:43 -04:00
|
|
|
def setup(credentials = {})
|
|
|
|
requires :public_ip_address, :identity, :public_key, :username
|
|
|
|
Fog::SSH.new(public_ip_address, username, credentials).run([
|
|
|
|
%{mkdir .ssh},
|
|
|
|
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
|
|
|
|
%{passwd -l #{username}},
|
|
|
|
%{echo "#{Fog::JSON.encode(attributes)}" >> ~/attributes.json},
|
|
|
|
%{echo "#{Fog::JSON.encode(metadata)}" >> ~/metadata.json}
|
|
|
|
])
|
|
|
|
rescue Errno::ECONNREFUSED
|
|
|
|
sleep(1)
|
|
|
|
retry
|
|
|
|
end
|
2012-08-08 21:57:37 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def adminPass=(new_admin_pass)
|
|
|
|
@password = new_admin_pass
|
|
|
|
end
|
2012-07-30 18:22:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|