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 23:48:22 +08: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 15:52:43 -07:00
|
|
|
attribute :created, :type => :time
|
|
|
|
attribute :updated, :type => :time
|
2012-02-22 00:09:26 +08:00
|
|
|
|
2012-02-17 08:35:24 +08:00
|
|
|
attribute :tenant_id
|
|
|
|
attribute :user_id
|
|
|
|
attribute :key_name
|
2012-06-01 10:01:18 +02: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
|
2013-03-15 11:37:24 +01:00
|
|
|
attr_writer :image_ref, :flavor_ref, :nics, :os_scheduler_hints
|
2013-06-26 09:58:51 +02:00
|
|
|
attr_accessor :block_device_mapping
|
2012-02-22 00:09:26 +08:00
|
|
|
|
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
def initialize(attributes={})
|
2012-12-19 22:28:04 +00:00
|
|
|
# Old 'connection' is renamed as service and should be used instead
|
|
|
|
prepare_service_value(attributes)
|
2012-02-22 00:09:26 +08:00
|
|
|
|
2012-02-17 08:35:24 +08:00
|
|
|
self.security_groups = attributes.delete(:security_groups)
|
|
|
|
self.min_count = attributes.delete(:min_count)
|
|
|
|
self.max_count = attributes.delete(:max_count)
|
2013-03-14 23:40:26 +01:00
|
|
|
self.nics = attributes.delete(:nics)
|
2012-08-16 17:30:50 +02:00
|
|
|
self.os_scheduler_hints = attributes.delete(:os_scheduler_hints)
|
2013-06-26 09:58:51 +02:00
|
|
|
self.block_device_mapping = attributes.delete(:block_device_mapping)
|
2012-02-22 00:09:26 +08:00
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def metadata
|
|
|
|
@metadata ||= begin
|
|
|
|
Fog::Compute::OpenStack::Metadata.new({
|
2012-12-22 23:24:36 +00:00
|
|
|
:service => service,
|
2011-09-26 02:41:54 -04:00
|
|
|
:parent => self
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def metadata=(new_metadata={})
|
2013-02-25 13:49:23 -05:00
|
|
|
return unless new_metadata
|
2011-09-26 02:41:54 -04:00
|
|
|
metas = []
|
|
|
|
new_metadata.each_pair {|k,v| metas << {"key" => k, "value" => v} }
|
2013-02-25 13:49:23 -05:00
|
|
|
@metadata = metadata.load(metas)
|
2011-09-26 02:41:54 -04:00
|
|
|
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
|
2012-12-22 23:24:36 +00:00
|
|
|
service.delete_server(id)
|
2011-09-26 02:41:54 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def images
|
|
|
|
requires :id
|
2012-12-22 23:24:36 +00:00
|
|
|
service.images(:server => self)
|
2011-09-26 02:41:54 -04:00
|
|
|
end
|
|
|
|
|
2013-02-25 14:51:09 +02:00
|
|
|
def all_addresses
|
|
|
|
# currently openstack API does not tell us what is a floating ip vs a fixed ip for the vm listing,
|
|
|
|
# we fall back to get all addresses and filter sadly.
|
|
|
|
@all_addresses ||= service.list_all_addresses.body["floating_ips"].select{|data| data['instance_id'] == id}
|
2011-09-26 02:41:54 -04:00
|
|
|
end
|
2012-02-22 00:09:26 +08:00
|
|
|
|
2013-02-27 11:25:52 +02:00
|
|
|
def reload
|
|
|
|
@all_addresses = nil
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2013-02-25 14:51:09 +02:00
|
|
|
# returns all ip_addresses for a given instance
|
|
|
|
# this includes both the fixed ip(s) and the floating ip(s)
|
|
|
|
def ip_addresses
|
|
|
|
addresses.values.flatten.map{|x| x['addr']}
|
|
|
|
end
|
|
|
|
|
|
|
|
def floating_ip_addresses
|
|
|
|
all_addresses.map{|addr| addr["ip"]}
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :public_ip_addresses, :floating_ip_addresses
|
|
|
|
|
|
|
|
def floating_ip_address
|
|
|
|
floating_ip_addresses.first
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :public_ip_address, :floating_ip_address
|
|
|
|
|
|
|
|
def private_ip_addresses
|
|
|
|
ip_addresses - floating_ip_addresses
|
|
|
|
end
|
|
|
|
|
|
|
|
def private_ip_address
|
|
|
|
private_ip_addresses.first
|
2011-09-26 02:41:54 -04:00
|
|
|
end
|
2012-02-22 00:09:26 +08: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-12-22 23:24:36 +00:00
|
|
|
service.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
|
2012-12-22 23:24:36 +00:00
|
|
|
service.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
|
2012-12-22 23:24:36 +00:00
|
|
|
service.resize_server(id, flavor_ref)
|
2011-09-26 02:41:54 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def revert_resize
|
|
|
|
requires :id
|
2012-12-22 23:24:36 +00:00
|
|
|
service.revert_resize_server(id)
|
2011-09-26 02:41:54 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirm_resize
|
|
|
|
requires :id
|
2012-12-22 23:24:36 +00:00
|
|
|
service.confirm_resize_server(id)
|
2011-09-26 02:41:54 -04:00
|
|
|
true
|
|
|
|
end
|
2012-02-22 00:09:26 +08:00
|
|
|
|
2012-12-05 19:29:19 -08:00
|
|
|
def security_groups
|
|
|
|
requires :id
|
|
|
|
|
2012-12-22 23:24:36 +00:00
|
|
|
groups = service.list_security_groups(id).body['security_groups']
|
2012-12-05 19:29:19 -08:00
|
|
|
|
|
|
|
groups.map do |group|
|
|
|
|
sg = Fog::Compute::OpenStack::SecurityGroup.new group
|
2012-12-22 23:24:36 +00:00
|
|
|
sg.connection = service
|
2012-12-05 19:29:19 -08:00
|
|
|
sg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-17 08:35:24 +08:00
|
|
|
def security_groups=(new_security_groups)
|
|
|
|
@security_groups = new_security_groups
|
|
|
|
end
|
2012-02-22 00:09:26 +08:00
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
def reboot(type = 'SOFT')
|
|
|
|
requires :id
|
2012-12-22 23:24:36 +00:00
|
|
|
service.reboot_server(id, type)
|
2011-09-26 02:41:54 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2011-09-26 11:38:05 -04:00
|
|
|
def create_image(name, metadata={})
|
|
|
|
requires :id
|
2012-12-22 23:24:36 +00:00
|
|
|
service.create_image(id, name, metadata)
|
2011-09-26 11:38:05 -04:00
|
|
|
end
|
|
|
|
|
2012-02-23 16:47:58 +08:00
|
|
|
def console(log_length = nil)
|
|
|
|
requires :id
|
2012-12-22 23:24:36 +00:00
|
|
|
service.get_console_output(id, log_length)
|
2012-02-23 16:47:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def migrate
|
|
|
|
requires :id
|
2012-12-22 23:24:36 +00:00
|
|
|
service.migrate_server(id)
|
2012-02-23 16:47:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def live_migrate(host, block_migration, disk_over_commit)
|
|
|
|
requires :id
|
2012-12-22 23:24:36 +00:00
|
|
|
service.live_migrate_server(id, host, block_migration, disk_over_commit)
|
2012-02-28 00:27:00 +08:00
|
|
|
end
|
2012-02-23 16:47:58 +08:00
|
|
|
|
2012-02-24 11:37:35 +08:00
|
|
|
def associate_address(floating_ip)
|
|
|
|
requires :id
|
2012-12-22 23:24:36 +00:00
|
|
|
service.associate_address id, floating_ip
|
2012-02-24 11:37:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def disassociate_address(floating_ip)
|
|
|
|
requires :id
|
2012-12-22 23:24:36 +00:00
|
|
|
service.disassociate_address id, floating_ip
|
2012-11-26 17:30:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_vm_state(vm_state)
|
|
|
|
requires :id
|
2012-12-22 23:24:36 +00:00
|
|
|
service.reset_server_state id, vm_state
|
2012-02-24 11:37:35 +08:00
|
|
|
end
|
|
|
|
|
2012-02-17 08:35:24 +08: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-28 00:27:00 +08:00
|
|
|
def networks
|
2012-12-22 23:24:36 +00:00
|
|
|
service.networks(:server => self)
|
2012-02-28 00:27:00 +08:00
|
|
|
end
|
|
|
|
|
2013-04-04 00:14:54 +02:00
|
|
|
def volumes
|
|
|
|
requires :id
|
|
|
|
service.volumes.find_all do |vol|
|
|
|
|
vol.attachments.find { |attachment| attachment["serverId"] == id }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-27 15:08:40 +01:00
|
|
|
def volume_attachments
|
|
|
|
requires :id
|
|
|
|
service.get_server_volumes(id).body['volumeAttachments']
|
|
|
|
end
|
|
|
|
|
|
|
|
def attach_volume(volume_id, device_name)
|
|
|
|
requires :id
|
|
|
|
service.attach_volume(volume_id, id, device_name)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def detach_volume(volume_id)
|
|
|
|
requires :id
|
|
|
|
service.detach_volume(id, volume_id)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
def save
|
2012-12-23 02:45:05 +00:00
|
|
|
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
|
2013-06-26 09:58:51 +02:00
|
|
|
requires :flavor_ref, :name
|
|
|
|
requires_one :image_ref, :block_device_mapping
|
2011-09-26 02:41:54 -04:00
|
|
|
options = {
|
|
|
|
'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 15:02:45 -07:00
|
|
|
'user_data' => user_data_encoded,
|
2012-02-17 08:35:24 +08:00
|
|
|
'key_name' => key_name,
|
|
|
|
'security_groups' => @security_groups,
|
|
|
|
'min_count' => @min_count,
|
|
|
|
'max_count' => @max_count,
|
2013-03-14 23:40:26 +01:00
|
|
|
'nics' => @nics,
|
|
|
|
'os:scheduler_hints' => @os_scheduler_hints,
|
2013-06-26 09:58:51 +02:00
|
|
|
'block_device_mapping' => @block_device_mapping
|
2011-09-26 02:41:54 -04:00
|
|
|
}
|
2013-02-25 13:49:23 -05:00
|
|
|
options['metadata'] = metadata.to_hash unless @metadata.nil?
|
2011-09-26 02:41:54 -04:00
|
|
|
options = options.reject {|key, value| value.nil?}
|
2012-12-22 23:24:36 +00:00
|
|
|
data = service.create_server(name, image_ref, flavor_ref, options)
|
2011-09-26 02:41:54 -04:00
|
|
|
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
|