2011-09-26 02:41:54 -04:00
|
|
|
require 'fog/compute/models/server'
|
|
|
|
require 'fog/openstack/models/compute/metadata'
|
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
|
|
|
class OpenStack
|
|
|
|
|
|
|
|
class Server < Fog::Compute::Server
|
|
|
|
|
|
|
|
identity :id
|
|
|
|
|
|
|
|
attribute :addresses
|
|
|
|
attribute :flavor
|
|
|
|
attribute :host_id, :aliases => 'hostId'
|
|
|
|
attribute :image
|
|
|
|
attribute :metadata
|
|
|
|
attribute :links
|
|
|
|
attribute :name
|
|
|
|
attribute :personality
|
|
|
|
attribute :progress
|
|
|
|
attribute :accessIPv4
|
|
|
|
attribute :accessIPv6
|
2011-10-11 17:08:34 -04:00
|
|
|
attribute :availability_zone
|
2011-10-13 23:14:34 -04:00
|
|
|
attribute :user_data_encoded
|
2011-09-26 02:41:54 -04:00
|
|
|
attribute :state, :aliases => 'status'
|
2012-02-16 19:35:24 -05:00
|
|
|
|
|
|
|
attribute :tenant_id
|
|
|
|
attribute :user_id
|
|
|
|
attribute :key_name
|
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
|
|
|
|
attr_reader :password
|
|
|
|
attr_writer :private_key, :private_key_path, :public_key, :public_key_path, :username, :image_ref, :flavor_ref
|
2012-02-16 19:35:24 -05:00
|
|
|
|
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
def initialize(attributes={})
|
|
|
|
@connection = attributes[:connection]
|
2011-10-11 17:04:58 -04:00
|
|
|
attributes[:metadata] = {}
|
2012-02-16 19:35:24 -05:00
|
|
|
|
|
|
|
self.security_groups = attributes.delete(:security_groups)
|
|
|
|
self.min_count = attributes.delete(:min_count)
|
|
|
|
self.max_count = attributes.delete(:max_count)
|
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def metadata
|
|
|
|
@metadata ||= begin
|
|
|
|
Fog::Compute::OpenStack::Metadata.new({
|
|
|
|
:connection => connection,
|
|
|
|
:parent => self
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def metadata=(new_metadata={})
|
|
|
|
metas = []
|
|
|
|
new_metadata.each_pair {|k,v| metas << {"key" => k, "value" => v} }
|
|
|
|
metadata.load(metas)
|
|
|
|
end
|
|
|
|
|
2011-10-13 23:14:34 -04:00
|
|
|
def user_data=(ascii_userdata)
|
|
|
|
self.user_data_encoded = [ascii_userdata].pack('m')
|
|
|
|
end
|
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
def destroy
|
|
|
|
requires :id
|
|
|
|
connection.delete_server(id)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def images
|
|
|
|
requires :id
|
|
|
|
connection.images(:server => self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def private_ip_address
|
|
|
|
nil
|
|
|
|
end
|
2012-02-16 19:35:24 -05:00
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
def private_key_path
|
|
|
|
@private_key_path ||= Fog.credentials[:private_key_path]
|
|
|
|
@private_key_path &&= File.expand_path(@private_key_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def private_key
|
|
|
|
@private_key ||= private_key_path && File.read(private_key_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_ip_address
|
|
|
|
addresses['public'].first
|
|
|
|
end
|
2012-02-16 19:35:24 -05:00
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
def public_key_path
|
|
|
|
@public_key_path ||= Fog.credentials[:public_key_path]
|
|
|
|
@public_key_path &&= File.expand_path(@public_key_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_key
|
|
|
|
@public_key ||= public_key_path && File.read(public_key_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def image_ref
|
|
|
|
@image_ref
|
|
|
|
end
|
|
|
|
|
|
|
|
def image_ref=(new_image_ref)
|
|
|
|
@image_ref = new_image_ref
|
|
|
|
end
|
|
|
|
|
|
|
|
def flavor_ref
|
|
|
|
@flavor_ref
|
|
|
|
end
|
|
|
|
|
|
|
|
def flavor_ref=(new_flavor_ref)
|
|
|
|
@flavor_ref = new_flavor_ref
|
|
|
|
end
|
|
|
|
|
|
|
|
def ready?
|
|
|
|
self.state == 'ACTIVE'
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_password(admin_password)
|
|
|
|
requires :id
|
|
|
|
connection.change_password_server(id, admin_password)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2011-09-29 14:10:21 -04:00
|
|
|
def rebuild(image_ref, name, admin_pass=nil, metadata=nil, personality=nil)
|
2011-09-26 02:41:54 -04:00
|
|
|
requires :id
|
2011-09-29 14:10:21 -04:00
|
|
|
connection.rebuild_server(id, image_ref, name, admin_pass, metadata, personality)
|
2011-09-26 02:41:54 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def resize(flavor_ref)
|
|
|
|
requires :id
|
|
|
|
connection.resize_server(id, flavor_ref)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def revert_resize
|
|
|
|
requires :id
|
|
|
|
connection.revert_resize_server(id)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirm_resize
|
|
|
|
requires :id
|
|
|
|
connection.confirm_resize_server(id)
|
|
|
|
true
|
|
|
|
end
|
2012-02-16 19:35:24 -05:00
|
|
|
|
|
|
|
def security_groups=(new_security_groups)
|
|
|
|
@security_groups = new_security_groups
|
|
|
|
end
|
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
def reboot(type = 'SOFT')
|
|
|
|
requires :id
|
|
|
|
connection.reboot_server(id, type)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2011-09-26 11:38:05 -04:00
|
|
|
def create_image(name, metadata={})
|
|
|
|
requires :id
|
|
|
|
connection.create_image(id, name, metadata)
|
|
|
|
end
|
|
|
|
|
2012-02-16 19:35:24 -05:00
|
|
|
def min_count=(new_min_count)
|
|
|
|
@min_count = new_min_count
|
|
|
|
end
|
|
|
|
|
|
|
|
def max_count=(new_max_count)
|
|
|
|
@max_count = new_max_count
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Implement /os-volumes-boot support with 'block_device_mapping'
|
2011-09-26 02:41:54 -04:00
|
|
|
def save
|
|
|
|
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
|
|
|
|
requires :flavor_ref, :image_ref, :name
|
|
|
|
meta_hash = {}
|
|
|
|
metadata.each { |meta| meta_hash.store(meta.key, meta.value) }
|
|
|
|
options = {
|
|
|
|
'metadata' => meta_hash,
|
|
|
|
'personality' => personality,
|
|
|
|
'accessIPv4' => accessIPv4,
|
2011-10-11 17:08:34 -04:00
|
|
|
'accessIPv6' => accessIPv6,
|
2011-10-13 23:14:34 -04:00
|
|
|
'availability_zone' => availability_zone,
|
2012-02-16 19:35:24 -05:00
|
|
|
'userdata' => user_data_encoded,
|
|
|
|
'key_name' => key_name,
|
|
|
|
'security_groups' => @security_groups,
|
|
|
|
'min_count' => @min_count,
|
|
|
|
'max_count' => @max_count,
|
2011-09-26 02:41:54 -04:00
|
|
|
}
|
|
|
|
options = options.reject {|key, value| value.nil?}
|
|
|
|
data = connection.create_server(name, image_ref, flavor_ref, options)
|
|
|
|
merge_attributes(data.body['server'])
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
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}},
|
2012-04-25 10:31:28 -04:00
|
|
|
%{echo "#{Fog::JSON.encode(attributes)}" >> ~/attributes.json},
|
|
|
|
%{echo "#{Fog::JSON.encode(metadata)}" >> ~/metadata.json}
|
2011-09-26 02:41:54 -04:00
|
|
|
])
|
|
|
|
rescue Errno::ECONNREFUSED
|
|
|
|
sleep(1)
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
@username ||= 'root'
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def adminPass=(new_admin_pass)
|
|
|
|
@password = new_admin_pass
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|