Class: OpenStack::Nova::Compute::Server
- Inherits:
-
BaseDetail
- Object
- ActiveResource::Base
- Base
- Common
- Base
- BaseDetail
- OpenStack::Nova::Compute::Server
- Defined in:
- lib/open_stack/nova/compute/server.rb
Overview
An OpenStack 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 task OpenStack 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
Constant Summary collapse
- SERVER_STATUSES =
{ :ACTIVE => I18n.t(:active, :scope => [:openstack, :status]), :BUILD => I18n.t(:building, :scope => [:openstack, :status]), :DELETED => I18n.t(:deleted, :scope => [:openstack, :status]), :ERROR => I18n.t(:in_error, :scope => [:openstack, :status]), :HARD_REBOOT => I18n.t(:hard_rebooting, :scope => [:openstack, :status]), :PASSWORD => I18n.t(:resetting_password, :scope => [:openstack, :status]), :REBOOT => I18n.t(:soft_rebooting, :scope => [:openstack, :status]), :REBUILD => I18n.t(:rebuilding_from_image, :scope => [:openstack, :status]), :RESCUE => I18n.t(:in_rescue_mode, :scope => [:openstack, :status]), :RESIZE => I18n.t(:resizing, :scope => [:openstack, :status]), :REVERT_RESIZE => I18n.t(:revert_resizing, :scope => [:openstack, :status]), :SHUTOFF => I18n.t(:user_powered_down, :scope => [:openstack, :status]), :SUSPENDED => I18n.t(:suspended, :scope => [:openstack, :status]), :PAUSED => I18n.t(:paused, :scope => [:openstack, :status]), :UNKNOWN => I18n.t(:unknown, :scope => [:openstack, :status]), :VERIFY_RESIZE => I18n.t(:awaiting_verification, :scope => [:openstack, :status]) }.with_indifferent_access
Class Method Summary collapse
-
.all_by_tenant(tenant) ⇒ Object
Return the list of server for a given tenant.
Instance Method Summary collapse
-
#active? ⇒ Boolean
true if the status is ACTIVE.
-
#add_floating_ip(floating_ip) ⇒ Object
Assign a floating IP to the server.
-
#addresses ⇒ Object
Addresses hash associated to this server.
-
#addresses=(something) ⇒ Object
:nodoc: do Nothing (it’s a read-only attribute for OpenStack).
-
#attach_volume!(volume, device_name) ⇒ Object
Attach a volume.
-
#attached_volumes ⇒ Object
Array of OpenStack::Nova::Compute::Volume attached to this server.
-
#console_output(length = 50) ⇒ Object
Gets the output from the console log for a server.
-
#create_new_image(name, metadata = {}) ⇒ Object
Creates a new snapshot of server.
-
#deleted? ⇒ Boolean
true if the status is DELETED.
-
#encode(options = {}) ⇒ Object
Overloads ActiveRecord::encode method.
-
#flavor ⇒ Object
The instance of OpenStack::Nova::Compute::Flavor used for this server.
-
#flavor=(flavor) ⇒ Object
Set the flavor for this server (if the server is not persisted).
-
#image ⇒ Object
The instance of OpenStack::Nova::Compute::Image used for this server.
-
#image=(image) ⇒ Object
Set the image for this server (if the server is not persisted).
-
#initialize(attributes = {}, persisted = false) ⇒ Server
constructor
:notnew:.
-
#key_pair ⇒ Object
The instance of OpenStack::Nova::Compute::KeyPair used for this server (if any).
-
#key_pair=(key_pair) ⇒ Object
Set the keypair for this server (if the server is not persisted).
-
#pause ⇒ Object
PAUSE a server.
-
#paused? ⇒ Boolean
true if the status is PAUSED.
-
#reboot(type = :hard) ⇒ Object
Reboot the server.
-
#refresh_status! ⇒ Object
Refresh server status This method updates the following attributes: * progress * status * task * power_state * vm_state.
-
#resume ⇒ Object
Resume a SUSPENDED server.
-
#security_groups ⇒ Object
The array of OpenStack::Nova::Compute::SecurityGroup instances associated with this server.
-
#security_groups=(security_groups) ⇒ Object
Set security groups for this server.
-
#shutoff? ⇒ Boolean
true if the status is SHUTOFF.
-
#start ⇒ Object
Returns a STOPPED server to ACTIVE status.
-
#status_description ⇒ Object
Returns an extended (and localized) description for the server status.
-
#stop ⇒ Object
Halts a running server.
-
#suspend ⇒ Object
Suspend a running server.
-
#task_description ⇒ Object
Returns a localized description for the server task (if any).
-
#unpause ⇒ Object
Returns a PAUSED server to ACTIVE status.
-
#vnc_console(type = 'novnc') ⇒ Object
Accesses a VNC console for a specific server.
-
#volume_attachments(scope = :all) ⇒ Object
The OpenStack::Nova::Compute::VolumeAttachment(s) for this server.
Methods inherited from BaseDetail
collection_path, collection_path_create, #collection_path_create
Methods inherited from Base
Methods inherited from Common
collection_path, custom_method_collection_url, element_path
Methods inherited from Base
Methods inherited from ActiveResource::Base
Constructor Details
#initialize(attributes = {}, persisted = false) ⇒ Server
:notnew:
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/open_stack/nova/compute/server.rb', line 72 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'], :host_id => attributes[:hostId], :user_data => attributes[:user_data], } 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] = [] 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 |
Class Method Details
.all_by_tenant(tenant) ⇒ Object
Return the list of server for a given tenant
Attributes
-
tenant- an OpenStack::Keystone::Admin::Tenant instance (or a tenant id)
Notes
This method require an admin access
66 67 68 69 70 |
# File 'lib/open_stack/nova/compute/server.rb', line 66 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 |
Instance Method Details
#active? ⇒ Boolean
true if the status is ACTIVE
381 382 383 |
# File 'lib/open_stack/nova/compute/server.rb', line 381 def active? status == "ACTIVE" end |
#add_floating_ip(floating_ip) ⇒ Object
Assign a floating IP to the server
Attributes
-
floating_ip- a FloatingIP to be attached to the server.
309 310 311 |
# File 'lib/open_stack/nova/compute/server.rb', line 309 def add_floating_ip(floating_ip) post(:action, {}, {:addFloatingIp => {:address => floating_ip.ip}}.to_json) end |
#addresses ⇒ Object
Addresses hash associated to this server
219 220 221 222 223 224 225 226 227 228 |
# File 'lib/open_stack/nova/compute/server.rb', line 219 def addresses addresses = {} if persisted? response = get('ips') response.each do |net, address| addresses[net] = address end end addresses end |
#addresses=(something) ⇒ Object
:nodoc: do Nothing (it’s a read-only attribute for OpenStack)
230 231 232 |
# File 'lib/open_stack/nova/compute/server.rb', line 230 def addresses=(something) # :nodoc: do Nothing (it's a read-only attribute for OpenStack) end |
#attach_volume!(volume, device_name) ⇒ Object
Attach a volume
Attributes
-
volume- An OpenStack::Nova::Compute::Volume instance -
device_name- Name the device (from server perspective) (e.g. “/dev/vdc”)
252 253 254 |
# 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 |
#attached_volumes ⇒ Object
Array of OpenStack::Nova::Compute::Volume attached to this server
243 244 245 |
# File 'lib/open_stack/nova/compute/server.rb', line 243 def attached_volumes .present? ? .map { |va| va.volume } : [] end |
#console_output(length = 50) ⇒ Object
Gets the output from the console log for a server
Attributes
-
length- numbers of lines to get (defaults to 50, may be nil)
334 335 336 337 338 |
# File 'lib/open_stack/nova/compute/server.rb', line 334 def console_output(length=50) response = post(:action, {}, {:'os-getConsoleOutput' => {:length => length}}.to_json) ActiveSupport::JSON.decode(response.body)['output'] end |
#create_new_image(name, metadata = {}) ⇒ Object
Creates a new snapshot of server
Attributes
-
name- name of the new snapshot image -
metadata- hash of metadata (may be nil)
326 327 328 |
# File 'lib/open_stack/nova/compute/server.rb', line 326 def create_new_image(name, ={}) post(:action, {}, {:createImage => {:name => name, :metadata => }}.to_json) end |
#deleted? ⇒ Boolean
true if the status is DELETED
396 397 398 |
# File 'lib/open_stack/nova/compute/server.rb', line 396 def deleted? status == "DELETED" end |
#encode(options = {}) ⇒ Object
Overloads ActiveRecord::encode method
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/open_stack/nova/compute/server.rb', line 125 def encode(={}) #:nodoc: # Custom encoding to deal with openstack API to_encode = { :server => { :name => name, :imageRef => image_id, :flavorRef => flavor_id, } } # Optional attributes (openstack will not accept empty attribute for update/create) to_encode[:server][:user_data] = Base64.strict_encode64(user_data) if user_data.present? to_encode[:server][:key_name] = key_pair_id if key_pair_id.present? to_encode[:server][:security_groups] = security_groups.map { |sg| {:name => sg.name} } to_encode.send("to_#{self.class.format.extension}", ) end |
#flavor ⇒ Object
The instance of OpenStack::Nova::Compute::Flavor used for this server
162 163 164 165 166 |
# File 'lib/open_stack/nova/compute/server.rb', line 162 def flavor if flavor_id.present? @flavor ||= Flavor.find(flavor_id) end end |
#flavor=(flavor) ⇒ Object
Set the flavor for this server (if the server is not persisted)
Attributes
-
flavor- An instance of OpenStack::Nova::Compute::Flavor or a flavor id
172 173 174 175 176 177 |
# File 'lib/open_stack/nova/compute/server.rb', line 172 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 |
#image ⇒ Object
The instance of OpenStack::Nova::Compute::Image used for this server
144 145 146 147 148 |
# File 'lib/open_stack/nova/compute/server.rb', line 144 def image if image_id.present? @image ||= Image.find(image_id) end end |
#image=(image) ⇒ Object
Set the image for this server (if the server is not persisted)
Attributes
-
image- An instance of OpenStack::Nova::Compute::Image or an image id
154 155 156 157 158 159 |
# File 'lib/open_stack/nova/compute/server.rb', line 154 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 |
#key_pair ⇒ Object
The instance of OpenStack::Nova::Compute::KeyPair used for this server (if any)
180 181 182 183 184 |
# File 'lib/open_stack/nova/compute/server.rb', line 180 def key_pair if key_pair_id.present? @keypair ||= KeyPair.find(key_pair_id) end end |
#key_pair=(key_pair) ⇒ Object
Set the keypair for this server (if the server is not persisted)
Attributes
-
key_pair- An instance of OpenStack::Nova::Compute::KeyPair or a key-pair id
190 191 192 193 194 195 |
# File 'lib/open_stack/nova/compute/server.rb', line 190 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 ⇒ Object
PAUSE a server.
361 362 363 |
# File 'lib/open_stack/nova/compute/server.rb', line 361 def pause post(:action, {}, {:'pause' => nil}.to_json) end |
#paused? ⇒ Boolean
true if the status is PAUSED
386 387 388 |
# File 'lib/open_stack/nova/compute/server.rb', line 386 def paused? status == "PAUSED" end |
#reboot(type = :hard) ⇒ Object
Reboot the server
Attributes
-
type- type of reboot. Should be ‘hard’ or ‘soft’ (defaults to ‘hard’, may be nil)
317 318 319 |
# File 'lib/open_stack/nova/compute/server.rb', line 317 def reboot(type=:hard) post(:action, {}, {:reboot => {:type => type}}.to_json) end |
#refresh_status! ⇒ Object
Refresh server status This method updates the following attributes:
* progress
* status
* task
* power_state
* vm_state
263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/open_stack/nova/compute/server.rb', line 263 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 end end |
#resume ⇒ Object
Resume a SUSPENDED server.
376 377 378 |
# File 'lib/open_stack/nova/compute/server.rb', line 376 def resume post(:action, {}, {:'resume' => nil}.to_json) end |
#security_groups ⇒ Object
The array of OpenStack::Nova::Compute::SecurityGroup instances associated with this server
198 199 200 201 202 203 204 |
# File 'lib/open_stack/nova/compute/server.rb', line 198 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 |
#security_groups=(security_groups) ⇒ Object
Set security groups for this server
Attributes
-
security_groups- Array of OpenStack::Nova::Compute::SecurityGroup instances
210 211 212 213 214 215 216 |
# File 'lib/open_stack/nova/compute/server.rb', line 210 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 |
#shutoff? ⇒ Boolean
true if the status is SHUTOFF
391 392 393 |
# File 'lib/open_stack/nova/compute/server.rb', line 391 def shutoff? status == "SHUTOFF" end |
#start ⇒ Object
Returns a STOPPED server to ACTIVE status.
356 357 358 |
# File 'lib/open_stack/nova/compute/server.rb', line 356 def start post(:action, {}, {:'os-start' => nil}.to_json) end |
#status_description ⇒ Object
Returns an extended (and localized) description for the server status
296 297 298 |
# File 'lib/open_stack/nova/compute/server.rb', line 296 def status_description SERVER_STATUSES[status] end |
#stop ⇒ Object
Halts a running server. Changes status to STOPPED.
351 352 353 |
# File 'lib/open_stack/nova/compute/server.rb', line 351 def stop post(:action, {}, {:'os-stop' => nil}.to_json) end |
#suspend ⇒ Object
Suspend a running server. Changes status to SUSPENDED.
371 372 373 |
# File 'lib/open_stack/nova/compute/server.rb', line 371 def suspend post(:action, {}, {:'suspend' => nil}.to_json) end |
#task_description ⇒ Object
Returns a localized description for the server task (if any)
301 302 303 |
# File 'lib/open_stack/nova/compute/server.rb', line 301 def task_description I18n.t(task, :scope => [:openstack, :tasks]) if task.present? end |
#unpause ⇒ Object
Returns a PAUSED server to ACTIVE status.
366 367 368 |
# File 'lib/open_stack/nova/compute/server.rb', line 366 def unpause post(:action, {}, {:'unpause' => nil}.to_json) end |
#vnc_console(type = 'novnc') ⇒ Object
Accesses a VNC console for a specific server
Attributes
-
length- numbers of lines to get (defaults to 50, may be nil)
344 345 346 347 348 |
# File 'lib/open_stack/nova/compute/server.rb', line 344 def vnc_console(type='novnc') response = post(:action, {}, {:'os-getVNCConsole' => {:type => type}}.to_json) ActiveSupport::JSON.decode(response.body)['console']['url'] end |
#volume_attachments(scope = :all) ⇒ Object
The OpenStack::Nova::Compute::VolumeAttachment(s) for this server
Attributes
-
scope- An ActiveResource find scope (default: :all)
238 239 240 |
# File 'lib/open_stack/nova/compute/server.rb', line 238 def (scope = :all) VolumeAttachment.find(scope, :params => {:server_id => self.id}) end |