lib/fog/openstack/models/compute/server.rb in fog-openstack-0.1.5 vs lib/fog/openstack/models/compute/server.rb in fog-openstack-0.1.6
- old
+ new
@@ -8,19 +8,20 @@
identity :id
attribute :instance_name, :aliases => 'OS-EXT-SRV-ATTR:instance_name'
attribute :addresses
attribute :flavor
- attribute :host_id, :aliases => 'hostId'
+ attribute :host_id, :aliases => 'hostId'
attribute :image
attribute :metadata
attribute :links
attribute :name
# @!attribute [rw] personality
# @note This attribute is only used for server creation. This field will be nil on subsequent retrievals.
- # @return [Hash] Hash containing data to inject into the file system of the cloud server instance during server creation.
+ # @return [Hash] Hash containing data to inject into the file system of the cloud server instance during
+ # server creation.
# @example To inject fog.txt into file system
# :personality => [{ :path => '/root/fog.txt',
# :contents => Base64.encode64('Fog was here!')
# }]
# @see #create
@@ -50,15 +51,15 @@
attr_reader :password
attr_writer :image_ref, :flavor_ref, :nics, :os_scheduler_hints
attr_accessor :block_device_mapping, :block_device_mapping_v2
- # In some cases it's handy to be able to store the project for the record, e.g. swift doesn't contain project info
- # in the result, so we can track it in this attribute based on what project was used in the request
+ # In some cases it's handy to be able to store the project for the record, e.g. swift doesn't contain project
+ # info in the result, so we can track it in this attribute based on what project was used in the request
attr_accessor :project
- def initialize(attributes={})
+ def initialize(attributes = {})
# Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
self.security_groups = attributes.delete(:security_groups)
self.min_count = attributes.delete(:min_count)
@@ -71,21 +72,19 @@
super
end
def metadata
@metadata ||= begin
- Fog::Compute::OpenStack::Metadata.new({
- :service => service,
- :parent => self
- })
+ Fog::Compute::OpenStack::Metadata.new(:service => service,
+ :parent => self)
end
end
- def metadata=(new_metadata={})
+ def metadata=(new_metadata = {})
return unless new_metadata
metas = []
- new_metadata.each_pair {|k,v| metas << {"key" => k, "value" => v} }
+ new_metadata.each_pair { |k, v| metas << {"key" => k, "value" => v} }
@metadata = metadata.load(metas)
end
def user_data=(ascii_userdata)
self.user_data_encoded = [ascii_userdata].pack('m') if ascii_userdata
@@ -104,46 +103,53 @@
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.
# Only includes manually-assigned addresses, not auto-assigned
- @all_addresses ||= service.list_all_addresses.body["floating_ips"].select{|data| data['instance_id'] == id}
+ @all_addresses ||= service.list_all_addresses.body["floating_ips"].select { |data| data['instance_id'] == id }
end
def reload
@all_addresses = nil
super
end
# 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']}
+ addresses ? addresses.values.flatten.collect { |x| x['addr'] } : []
end
def floating_ip_addresses
- all_floating=addresses.values.flatten.select{ |data| data["OS-EXT-IPS:type"]=="floating" }.map{|addr| addr["addr"] }
+ all_floating = if addresses
+ flattened_values = addresses.values.flatten
+ flattened_values.select { |d| d["OS-EXT-IPS:type"] == "floating" }.collect { |a| a["addr"] }
+ else
+ []
+ end
# Return them all, leading with manually assigned addresses
- manual = all_addresses.map{|addr| addr["ip"]}
+ manual = all_addresses.collect { |addr| addr["ip"] }
- all_floating.sort{ |a,b|
+ all_floating.sort do |a, b|
a_manual = manual.include? a
b_manual = manual.include? b
- if a_manual and !b_manual
+ if a_manual && !b_manual
-1
- elsif !a_manual and b_manual
+ elsif !a_manual && b_manual
1
- else 0 end
- }
+ else
+ 0
+ end
+ end
all_floating.empty? ? manual : all_floating
end
def public_ip_addresses
if floating_ip_addresses.empty?
- addresses.find_all{|s| s[0] =~ /public/i}.map{|a| a[1][0]['addr']}
+ addresses.select { |s| s[0] =~ /public/i }.collect { |a| a[1][0]['addr'] }
else
floating_ip_addresses
end
end
@@ -156,48 +162,40 @@
end
def private_ip_addresses
rfc1918_regexp = /(^10\.|^172\.1[6-9]\.|^172\.2[0-9]\.|^172.3[0-1]\.|^192\.168\.)/
almost_private = ip_addresses - public_ip_addresses - floating_ip_addresses
- almost_private.find_all{ |ip| rfc1918_regexp.match ip }
+ almost_private.select { |ip| rfc1918_regexp.match ip }
end
def private_ip_address
private_ip_addresses.first
end
- def image_ref
- @image_ref
- end
+ attr_reader :image_ref
- def image_ref=(new_image_ref)
- @image_ref = new_image_ref
- end
+ attr_writer :image_ref
- def flavor_ref
- @flavor_ref
- end
+ attr_reader :flavor_ref
- def flavor_ref=(new_flavor_ref)
- @flavor_ref = new_flavor_ref
- end
+ attr_writer :flavor_ref
def ready?
- self.state == 'ACTIVE'
+ state == 'ACTIVE'
end
def failed?
- self.state == 'ERROR'
+ state == 'ERROR'
end
def change_password(admin_password)
requires :id
service.change_server_password(id, admin_password)
true
end
- def rebuild(image_ref, name, admin_pass=nil, metadata=nil, personality=nil)
+ def rebuild(image_ref, name, admin_pass = nil, metadata = nil, personality = nil)
requires :id
service.rebuild_server(id, image_ref, name, admin_pass, metadata, personality)
true
end
@@ -223,17 +221,15 @@
requires :id
groups = service.list_security_groups(:server_id => id).body['security_groups']
groups.map do |group|
- Fog::Compute::OpenStack::SecurityGroup.new group.merge({:service => service})
+ Fog::Compute::OpenStack::SecurityGroup.new group.merge(:service => service)
end
end
- def security_groups=(new_security_groups)
- @security_groups = new_security_groups
- end
+ attr_writer :security_groups
def reboot(type = 'SOFT')
requires :id
service.reboot_server(id, type)
true
@@ -280,11 +276,11 @@
def shelve_offload
requires :id
service.shelve_offload_server(id)
end
- def create_image(name, metadata={})
+ def create_image(name, metadata = {})
requires :id
service.create_image(id, name, metadata)
end
def console(log_length = nil)
@@ -300,11 +296,11 @@
def live_migrate(host, block_migration, disk_over_commit)
requires :id
service.live_migrate_server(id, host, block_migration, disk_over_commit)
end
- def evacuate(host = nil, on_shared_storage = true, admin_password = nil)
+ def evacuate(host = nil, on_shared_storage = nil, admin_password = nil)
requires :id
service.evacuate_server(id, host, on_shared_storage, admin_password)
end
def associate_address(floating_ip)
@@ -320,17 +316,13 @@
def reset_vm_state(vm_state)
requires :id
service.reset_server_state id, vm_state
end
- def min_count=(new_min_count)
- @min_count = new_min_count
- end
+ attr_writer :min_count
- def max_count=(new_max_count)
- @max_count = new_max_count
- end
+ attr_writer :max_count
def networks
service.networks(:server => self)
end
@@ -357,53 +349,48 @@
service.detach_volume(id, volume_id)
true
end
def save
- raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
+ raise Fog::Errors::Error, 'Resaving an existing object may create a duplicate' if persisted?
requires :flavor_ref, :name
requires_one :image_ref, :block_device_mapping, :block_device_mapping_v2
options = {
- 'personality' => personality,
- 'accessIPv4' => accessIPv4,
- 'accessIPv6' => accessIPv6,
- 'availability_zone' => availability_zone,
- 'user_data' => user_data_encoded,
- 'key_name' => key_name,
- 'config_drive' => config_drive,
- 'security_groups' => @security_groups,
- 'min_count' => @min_count,
- 'max_count' => @max_count,
- 'nics' => @nics,
- 'os:scheduler_hints' => @os_scheduler_hints,
- 'block_device_mapping' => @block_device_mapping,
+ 'personality' => personality,
+ 'accessIPv4' => accessIPv4,
+ 'accessIPv6' => accessIPv6,
+ 'availability_zone' => availability_zone,
+ 'user_data' => user_data_encoded,
+ 'key_name' => key_name,
+ 'config_drive' => config_drive,
+ 'security_groups' => @security_groups,
+ 'min_count' => @min_count,
+ 'max_count' => @max_count,
+ 'nics' => @nics,
+ 'os:scheduler_hints' => @os_scheduler_hints,
+ 'block_device_mapping' => @block_device_mapping,
'block_device_mapping_v2' => @block_device_mapping_v2,
}
options['metadata'] = metadata.to_hash unless @metadata.nil?
- options = options.reject {|key, value| value.nil?}
+ options = options.reject { |_key, value| value.nil? }
data = service.create_server(name, image_ref, flavor_ref, options)
merge_attributes(data.body['server'])
true
end
def setup(credentials = {})
requires :ssh_ip_address, :identity, :public_key, :username
- Fog::SSH.new(ssh_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}
- ])
+ ssh = Fog::SSH.new(ssh_ip_address, username, credentials)
+ ssh.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
-
- private
-
- def adminPass=(new_admin_pass)
- @password = new_admin_pass
end
end
end
end
end