class OpenStack::Nova::Compute::Server
Attributes¶ ↑
-
name
- The name of the server -
status
- Status of the server (see docs.openstack.org/api/openstack-compute/2/content/List_Servers-d1e2078.html) -
vm_state
- Extended Instance Status -
task
- If notnil
, contains the taskOpenStack
is preforming on this server -
power_state
-Server
power state (0|1) -
tenant_id
- Identifier of the tenant this server belongs to -
user_id
- Identifier of the user that created this server -
image_id
- Identifier of the image used to create this server -
flavor_id
- Identifier of the flavor used to create this server -
key_pair_id
- Identifier of the keypair used by this server -
updated_at
- Last modification timestamp -
created_at
- Creation timestamp
Constants
- SERVER_STATUSES
Public Class Methods
Return the list of server for a given tenant
Attributes¶ ↑
-
tenant
- anOpenStack::Keystone::Admin::Tenant
instance (or a tenant id)
Notes¶ ↑
This method require an admin access
# File lib/open_stack/nova/compute/server.rb, line 72 def self.all_by_tenant(tenant) tenant_id = tenant.is_a?(OpenStack::Keystone::Admin::Tenant) ? tenant.id : tenant find(:all, :params => {:tenant_id => tenant_id}) end
Public Instance Methods
true if the status is ACTIVE
# File lib/open_stack/nova/compute/server.rb, line 383 def active? status == "ACTIVE" end
Attach a volume
Attributes¶ ↑
-
volume
- An OpenStack::Nova::Compute::Volume instance -
device_name
- Name the device (from server perspective) (e.g. “/dev/vdc”)
# File lib/open_stack/nova/compute/server.rb, line 252 def attach_volume!(volume, device_name) VolumeAttachment.create(:volume => volume, :device => device_name, :server => self) end
Array of OpenStack::Nova::Compute::Volume attached to this server
# File lib/open_stack/nova/compute/server.rb, line 243 def attached_volumes volume_attachments.present? ? volume_attachments.map { |va| va.volume } : [] end
Gets the output from the console log for a server
Attributes¶ ↑
-
length
- numbers of lines to get (defaults to 50, may be nil)
# File lib/open_stack/nova/compute/server.rb, line 336 def console_output(length=50) response = post(:action, {}, {:'os-getConsoleOutput' => {:length => length}}.to_json) ActiveSupport::JSON.decode(response.body)['output'] end
Creates a new snapshot of server
Attributes¶ ↑
-
name
- name of the new snapshot image -
metadata
- hash of metadata (may be nil)
# File lib/open_stack/nova/compute/server.rb, line 328 def create_new_image(name, metadata={}) post(:action, {}, {:createImage => {:name => name, :metadata => metadata}}.to_json) end
true if the status is DELETED
# File lib/open_stack/nova/compute/server.rb, line 398 def deleted? status == "DELETED" end
The instance of OpenStack::Nova::Compute::Flavor
used for this server
# File lib/open_stack/nova/compute/server.rb, line 174 def flavor if flavor_id.present? @flavor ||= Flavor.find(flavor_id) end end
Set the flavor for this server (if the server is not persisted)
Attributes¶ ↑
-
flavor
- An instance ofOpenStack::Nova::Compute::Flavor
or a flavor id
# File lib/open_stack/nova/compute/server.rb, line 184 def flavor=(flavor) unless persisted? @flavor = nil # nullify @flavor because the flavor id is changed self.flavor_id = flavor.is_a?(OpenStack::Nova::Compute::Flavor) ? flavor.id : flavor end end
The instance of OpenStack::Nova::Compute::Image
used for this server
# File lib/open_stack/nova/compute/server.rb, line 156 def image if image_id.present? @image ||= Image.find(image_id) end end
Set the image for this server (if the server is not persisted)
Attributes¶ ↑
-
image
- An instance ofOpenStack::Nova::Compute::Image
or an image id
# File lib/open_stack/nova/compute/server.rb, line 166 def image=(image) unless persisted? @image = nil # nullify @@image because the image id is changed self.image_id = image.is_a?(OpenStack::Nova::Compute::Image) ? image.id : image end end
The instance of OpenStack::Nova::Compute::KeyPair
used for this server (if any)
# File lib/open_stack/nova/compute/server.rb, line 192 def key_pair if key_pair_id.present? @keypair ||= KeyPair.find(key_pair_id) end end
Set the keypair for this server (if the server is not persisted)
Attributes¶ ↑
-
key_pair
- An instance ofOpenStack::Nova::Compute::KeyPair
or a key-pair id
# File lib/open_stack/nova/compute/server.rb, line 202 def key_pair=(key_pair) unless persisted? @keypair = nil # nullify @@keypair because the keypair id is changed self.key_pair_id = key_pair.id end end
PAUSE a server.
# File lib/open_stack/nova/compute/server.rb, line 363 def pause post(:action, {}, {:'pause' => nil}.to_json) end
true if the status is PAUSED
# File lib/open_stack/nova/compute/server.rb, line 388 def paused? status == "PAUSED" end
Refresh server status This method updates the following attributes:
* progress * status * task * power_state * vm_state * ip addresses
# File lib/open_stack/nova/compute/server.rb, line 264 def refresh_status! if persisted? updated = Server.find(self.id) self.progress = updated.progress self.status = updated.status self.task = updated.task self.power_state = updated.power_state self.vm_state = updated.vm_state self.nets = updated.nets end self end
Resume a SUSPENDED server.
# File lib/open_stack/nova/compute/server.rb, line 378 def resume post(:action, {}, {:'resume' => nil}.to_json) end
The array of OpenStack::Nova::Compute::SecurityGroup
instances associated with this server
# File lib/open_stack/nova/compute/server.rb, line 210 def security_groups if persisted? get('os-security-groups').map { |sg| OpenStack::Nova::Compute::SecurityGroup.new(sg, true) } else security_group_ids.map { |sg_id| OpenStack::Nova::Compute::SecurityGroup.find sg_id } end end
Set security groups for this server
Attributes¶ ↑
-
security_groups
- Array ofOpenStack::Nova::Compute::SecurityGroup
instances
# File lib/open_stack/nova/compute/server.rb, line 222 def security_groups=(security_groups) return if persisted? # Do Nothing (it's a read-only attribute for OpenStack) self.security_group_ids = security_groups.map { |sg| sg.id } security_groups end
true if the status is SHUTOFF
# File lib/open_stack/nova/compute/server.rb, line 393 def shutoff? status == "SHUTOFF" end
Returns a STOPPED server to ACTIVE status.
# File lib/open_stack/nova/compute/server.rb, line 358 def start post(:action, {}, {:'os-start' => nil}.to_json) end
Returns an extended (and localized) description for the server status
# File lib/open_stack/nova/compute/server.rb, line 298 def status_description I18n.t(SERVER_STATUSES[status], :scope => [:openstack, :status]) end
Halts a running server. Changes status to STOPPED.
# File lib/open_stack/nova/compute/server.rb, line 353 def stop post(:action, {}, {:'os-stop' => nil}.to_json) end
Suspend a running server. Changes status to SUSPENDED.
# File lib/open_stack/nova/compute/server.rb, line 373 def suspend post(:action, {}, {:'suspend' => nil}.to_json) end
Returns a localized description for the server task (if any)
# File lib/open_stack/nova/compute/server.rb, line 303 def task_description I18n.t(task, :scope => [:openstack, :tasks]) if task.present? end
Returns a PAUSED server to ACTIVE status.
# File lib/open_stack/nova/compute/server.rb, line 368 def unpause post(:action, {}, {:'unpause' => nil}.to_json) end
Accesses a VNC console for a specific server
Attributes¶ ↑
-
length
- numbers of lines to get (defaults to 50, may be nil)
# File lib/open_stack/nova/compute/server.rb, line 346 def vnc_console(type='novnc') response = post(:action, {}, {:'os-getVNCConsole' => {:type => type}}.to_json) ActiveSupport::JSON.decode(response.body)['console']['url'] end
The OpenStack::Nova::Compute::VolumeAttachment(s) for this server
Attributes¶ ↑
-
scope
- AnActiveResource
find scope (default: :all)
# File lib/open_stack/nova/compute/server.rb, line 238 def volume_attachments(scope = :all) VolumeAttachment.find(scope, :params => {:server_id => self.id}) end
Protected Instance Methods
# File lib/open_stack/nova/compute/server.rb, line 78 def initialize(attributes = {}, persisted = false) # :notnew: attributes = attributes.with_indifferent_access new_attributes = { :id => attributes[:id], :name => attributes[:name], :status => attributes[:status], :updated_at => attributes[:updated].present? ? DateTime.strptime(attributes[:updated], OpenStack::DATETIME_FORMAT) : nil, :created_at => attributes[:created].present? ? DateTime.strptime(attributes[:created], OpenStack::DATETIME_FORMAT) : nil, :vm_state => attributes[:'OS-EXT-STS:vm_state'], :task => attributes[:'OS-EXT-STS:task_state'], :power_state => attributes[:'OS-EXT-STS:power_state'], :progress => attributes[:progress], :host_id => attributes[:hostId], :user_data => attributes[:user_data], :tenant_id => attributes[:tenant_id] } if attributes[:key_pair].present? new_attributes[:key_pair_id] = attributes[:key_pair].name else new_attributes[:key_pair_id] = attributes[:key_name] end if attributes[:image].present? new_attributes[:image_id] = attributes[:image].is_a?(Image) ? attributes[:image].id : attributes[:image][:id] elsif attributes[:image_id].present? new_attributes[:image_id] = attributes[:image_id] end if attributes[:flavor].present? new_attributes[:flavor_id] = attributes[:flavor].is_a?(Flavor) ? attributes[:flavor].id : attributes[:flavor][:id] elsif attributes[:flavor_id].present? new_attributes[:flavor_id] = attributes[:flavor_id] end if persisted # We ignore the list of security group names provided in attributes[:security_group] # Security group ids will be retrieved when needed new_attributes[:security_group_ids] = [] new_attributes[:nets] = [] attributes[:addresses].each do |net_name, addresses| new_attributes[:nets] << {:name => net_name, :addresses => addresses} end else if attributes[:security_group_ids].nil? new_attributes[:security_group_ids] = attributes[:security_groups].nil? ? [] : attributes[:security_groups].map { |sg| sg.id } else new_attributes[:security_group_ids] = attributes[:security_group_ids] end end super(new_attributes, persisted) self end