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
|
2012-04-08 11:48:22 -04:00
|
|
|
attribute :instance_name, :aliases => 'OS-EXT-SRV-ATTR:instance_name'
|
2011-09-26 02:41:54 -04:00
|
|
|
|
|
|
|
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-10-18 18:52:43 -04:00
|
|
|
attribute :created, :type => :time
|
|
|
|
attribute :updated, :type => :time
|
2012-02-21 11:09:26 -05:00
|
|
|
|
2012-02-16 19:35:24 -05:00
|
|
|
attribute :tenant_id
|
|
|
|
attribute :user_id
|
|
|
|
attribute :key_name
|
2012-06-01 04:01:18 -04:00
|
|
|
attribute :fault
|
|
|
|
attribute :os_dcf_disk_config, :aliases => 'OS-DCF:diskConfig'
|
|
|
|
attribute :os_ext_srv_attr_host, :aliases => 'OS-EXT-SRV-ATTR:host'
|
|
|
|
attribute :os_ext_srv_attr_hypervisor_hostname, :aliases => 'OS-EXT-SRV-ATTR:hypervisor_hostname'
|
|
|
|
attribute :os_ext_srv_attr_instance_name, :aliases => 'OS-EXT-SRV-ATTR:instance_name'
|
|
|
|
attribute :os_ext_sts_power_state, :aliases => 'OS-EXT-STS:power_state'
|
|
|
|
attribute :os_ext_sts_task_state, :aliases => 'OS-EXT-STS:task_state'
|
|
|
|
attribute :os_ext_sts_vm_state, :aliases => 'OS-EXT-STS:vm_state'
|
2011-09-26 02:41:54 -04:00
|
|
|
|
|
|
|
attr_reader :password
|
2012-10-03 06:49:55 -04:00
|
|
|
attr_writer :image_ref, :flavor_ref, :os_scheduler_hints
|
2012-02-21 11:09:26 -05:00
|
|
|
|
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
def initialize(attributes={})
|
2012-12-19 17:28:04 -05:00
|
|
|
# Old 'connection' is renamed as service and should be used instead
|
|
|
|
prepare_service_value(attributes)
|
2011-10-11 17:04:58 -04:00
|
|
|
attributes[:metadata] = {}
|
2012-02-21 11:09:26 -05:00
|
|
|
|
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)
|
2012-08-16 11:30:50 -04:00
|
|
|
self.os_scheduler_hints = attributes.delete(:os_scheduler_hints)
|
2012-02-21 11:09:26 -05:00
|
|
|
|
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
|
2012-05-26 02:02:03 -04:00
|
|
|
if addresses['private']
|
|
|
|
#assume only a single private
|
|
|
|
return addresses['private'].first
|
2012-05-30 23:44:38 -04:00
|
|
|
elsif addresses['internet']
|
2012-05-26 02:02:03 -04:00
|
|
|
#assume no private IP means private cloud
|
|
|
|
return addresses['internet'].first
|
|
|
|
end
|
2011-09-26 02:41:54 -04:00
|
|
|
end
|
2012-02-21 11:09:26 -05:00
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
def public_ip_address
|
2012-05-26 02:02:03 -04:00
|
|
|
if addresses['public']
|
|
|
|
#assume last is either original or assigned from floating IPs
|
|
|
|
return addresses['public'].last
|
2012-05-30 23:44:38 -04:00
|
|
|
elsif addresses['internet']
|
2012-05-26 02:02:03 -04:00
|
|
|
#assume no public IP means private cloud
|
|
|
|
return addresses['internet'].first
|
|
|
|
end
|
2011-09-26 02:41:54 -04:00
|
|
|
end
|
2012-02-21 11:09:26 -05:00
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
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
|
2012-09-05 08:16:42 -04:00
|
|
|
connection.change_server_password(id, admin_password)
|
2011-09-26 02:41:54 -04:00
|
|
|
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-21 11:09:26 -05:00
|
|
|
|
2012-12-05 22:29:19 -05:00
|
|
|
def security_groups
|
|
|
|
requires :id
|
|
|
|
|
|
|
|
groups = connection.list_security_groups(id).body['security_groups']
|
|
|
|
|
|
|
|
groups.map do |group|
|
|
|
|
sg = Fog::Compute::OpenStack::SecurityGroup.new group
|
|
|
|
sg.connection = connection
|
|
|
|
sg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-16 19:35:24 -05:00
|
|
|
def security_groups=(new_security_groups)
|
|
|
|
@security_groups = new_security_groups
|
|
|
|
end
|
2012-02-21 11:09:26 -05:00
|
|
|
|
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-23 03:47:58 -05:00
|
|
|
def console(log_length = nil)
|
|
|
|
requires :id
|
|
|
|
connection.get_console_output(id, log_length)
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate
|
|
|
|
requires :id
|
|
|
|
connection.migrate_server(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def live_migrate(host, block_migration, disk_over_commit)
|
|
|
|
requires :id
|
|
|
|
connection.live_migrate_server(id, host, block_migration, disk_over_commit)
|
2012-02-27 11:27:00 -05:00
|
|
|
end
|
2012-02-23 03:47:58 -05:00
|
|
|
|
2012-02-23 22:37:35 -05:00
|
|
|
def associate_address(floating_ip)
|
|
|
|
requires :id
|
|
|
|
connection.associate_address id, floating_ip
|
|
|
|
end
|
|
|
|
|
|
|
|
def disassociate_address(floating_ip)
|
|
|
|
requires :id
|
|
|
|
connection.disassociate_address id, floating_ip
|
2012-11-26 04:30:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_vm_state(vm_state)
|
|
|
|
requires :id
|
|
|
|
connection.reset_server_state id, vm_state
|
2012-02-23 22:37:35 -05:00
|
|
|
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
|
|
|
|
|
2012-02-27 11:27:00 -05:00
|
|
|
def networks
|
|
|
|
connection.networks(:server => self)
|
|
|
|
end
|
|
|
|
|
2012-02-16 19:35:24 -05:00
|
|
|
# TODO: Implement /os-volumes-boot support with 'block_device_mapping'
|
2011-09-26 02:41:54 -04:00
|
|
|
def save
|
2012-12-22 21:45:05 -05:00
|
|
|
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
|
2011-09-26 02:41:54 -04:00
|
|
|
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-05-29 18:02:45 -04:00
|
|
|
'user_data' => user_data_encoded,
|
2012-02-16 19:35:24 -05:00
|
|
|
'key_name' => key_name,
|
|
|
|
'security_groups' => @security_groups,
|
|
|
|
'min_count' => @min_count,
|
|
|
|
'max_count' => @max_count,
|
2012-08-16 11:30:50 -04:00
|
|
|
'os:scheduler_hints' => @os_scheduler_hints
|
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
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def adminPass=(new_admin_pass)
|
|
|
|
@password = new_admin_pass
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|