class OpenStack::Nova::Compute::Server

An OpenStack Server

Attributes

Constants

SERVER_STATUSES

Public Class Methods

all_by_tenant(tenant) click to toggle source

Return the list of server for a given tenant

Attributes

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

active?() click to toggle source

true if the status is ACTIVE

# File lib/open_stack/nova/compute/server.rb, line 383
def active?
  status == "ACTIVE"
end
add_floating_ip(floating_ip) click to toggle source

Assign a floating IP to the server

Attributes

  • floating_ip - a FloatingIP to be attached to the server.

# File lib/open_stack/nova/compute/server.rb, line 311
def add_floating_ip(floating_ip)
  post(:action, {}, {:addFloatingIp => {:address => floating_ip.ip}}.to_json)
end
attach_volume!(volume, device_name) click to toggle source

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
attached_volumes() click to toggle source

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
console_output(length=50) click to toggle source

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
create_new_image(name, metadata={}) click to toggle source

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
deleted?() click to toggle source

true if the status is DELETED

# File lib/open_stack/nova/compute/server.rb, line 398
def deleted?
  status == "DELETED"
end
flavor() click to toggle source

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
flavor=(flavor) click to toggle source

Set the flavor for this server (if the server is not persisted)

Attributes

# 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
image() click to toggle source

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
image=(image) click to toggle source

Set the image for this server (if the server is not persisted)

Attributes

# 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
key_pair() click to toggle source

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
key_pair=(key_pair) click to toggle source

Set the keypair for this server (if the server is not persisted)

Attributes

# 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() click to toggle source

PAUSE a server.

# File lib/open_stack/nova/compute/server.rb, line 363
def pause
  post(:action, {}, {:'pause' => nil}.to_json)
end
paused?() click to toggle source

true if the status is PAUSED

# File lib/open_stack/nova/compute/server.rb, line 388
def paused?
  status == "PAUSED"
end
reboot(type=:hard) click to toggle source

Reboot the server

Attributes

  • type - type of reboot. Should be ‘hard’ or ‘soft’ (defaults to ‘hard’, may be nil)

# File lib/open_stack/nova/compute/server.rb, line 319
def reboot(type=:hard)
  post(:action, {}, {:reboot => {:type => type}}.to_json)
end
refresh_status!() click to toggle source

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() click to toggle source

Resume a SUSPENDED server.

# File lib/open_stack/nova/compute/server.rb, line 378
def resume
  post(:action, {}, {:'resume' => nil}.to_json)
end
security_groups() click to toggle source

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
security_groups=(security_groups) click to toggle source

Set security groups for this server

Attributes

# 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
shutoff?() click to toggle source

true if the status is SHUTOFF

# File lib/open_stack/nova/compute/server.rb, line 393
def shutoff?
  status == "SHUTOFF"
end
start() click to toggle source

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
status_description() click to toggle source

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
stop() click to toggle source

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() click to toggle source

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
task_description() click to toggle source

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
unpause() click to toggle source

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
vnc_console(type='novnc') click to toggle source

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
volume_attachments(scope = :all) click to toggle source

The OpenStack::Nova::Compute::VolumeAttachment(s) for this server

Attributes

# 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

initialize(attributes = {}, persisted = false) click to toggle source
Calls superclass method
# 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