Class: OpenStack::Nova::Compute::Server

Inherits:
BaseDetail show all
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 not nil, 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 => :active,
    :BUILD => :building,
    :DELETED => :deleted,
    :ERROR => :in_error,
    :HARD_REBOOT => :hard_rebooting,
    :PASSWORD => :resetting_password,
    :REBOOT => :soft_rebooting,
    :REBUILD => :rebuilding_from_image,
    :RESCUE => :in_rescue_mode,
    :RESIZE => :resizing,
    :REVERT_RESIZE => :revert_resizing,
    :SHUTOFF => :user_powered_down,
    :SUSPENDED => :suspended,
    :PAUSED => :paused,
    :UNKNOWN => :unknown,
    :VERIFY_RESIZE => :awaiting_verification
}.with_indifferent_access

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseDetail

collection_path, collection_path_create, #collection_path_create

Methods inherited from Base

site, site=

Methods inherited from Common

collection_path, custom_method_collection_url, element_path

Methods inherited from Base

headers

Methods inherited from ActiveResource::Base

#load

Constructor Details

#initialize(attributes = {}, persisted = false) ⇒ Server

:notnew:



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
123
124
125
126
127
128
129
130
131
132
133
134
# 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],
  }

  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

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



72
73
74
75
76
# 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

Instance Method Details

#active?Boolean

true if the status is ACTIVE

Returns:

  • (Boolean)


382
383
384
# File 'lib/open_stack/nova/compute/server.rb', line 382

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.



310
311
312
# File 'lib/open_stack/nova/compute/server.rb', line 310

def add_floating_ip(floating_ip)
  post(:action, {}, {:addFloatingIp => {:address => floating_ip.ip}}.to_json)
end

#addresses=(something) ⇒ Object

:nodoc: do Nothing (it’s a read-only attribute for OpenStack)



229
230
231
# File 'lib/open_stack/nova/compute/server.rb', line 229

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”)



251
252
253
# File 'lib/open_stack/nova/compute/server.rb', line 251

def attach_volume!(volume, device_name)
  VolumeAttachment.create(:volume => volume, :device => device_name, :server => self)
end

#attached_volumesObject

Array of OpenStack::Nova::Compute::Volume attached to this server



242
243
244
# File 'lib/open_stack/nova/compute/server.rb', line 242

def attached_volumes
  volume_attachments.present? ? volume_attachments.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)



335
336
337
338
339
# File 'lib/open_stack/nova/compute/server.rb', line 335

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)



327
328
329
# File 'lib/open_stack/nova/compute/server.rb', line 327

def create_new_image(name, ={})
  post(:action, {}, {:createImage => {:name => name, :metadata => }}.to_json)
end

#deleted?Boolean

true if the status is DELETED

Returns:

  • (Boolean)


397
398
399
# File 'lib/open_stack/nova/compute/server.rb', line 397

def deleted?
  status == "DELETED"
end

#encode(options = {}) ⇒ Object

Overloads ActiveRecord::encode method



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/open_stack/nova/compute/server.rb', line 137

def encode(options={}) # :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}", options)
end

#flavorObject

The instance of OpenStack::Nova::Compute::Flavor used for this server



173
174
175
176
177
# File 'lib/open_stack/nova/compute/server.rb', line 173

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



183
184
185
186
187
188
# File 'lib/open_stack/nova/compute/server.rb', line 183

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

#imageObject

The instance of OpenStack::Nova::Compute::Image used for this server



155
156
157
158
159
# File 'lib/open_stack/nova/compute/server.rb', line 155

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



165
166
167
168
169
170
# File 'lib/open_stack/nova/compute/server.rb', line 165

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_pairObject

The instance of OpenStack::Nova::Compute::KeyPair used for this server (if any)



191
192
193
194
195
# File 'lib/open_stack/nova/compute/server.rb', line 191

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



201
202
203
204
205
206
# File 'lib/open_stack/nova/compute/server.rb', line 201

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

#pauseObject

PAUSE a server.



362
363
364
# File 'lib/open_stack/nova/compute/server.rb', line 362

def pause
  post(:action, {}, {:'pause' => nil}.to_json)
end

#paused?Boolean

true if the status is PAUSED

Returns:

  • (Boolean)


387
388
389
# File 'lib/open_stack/nova/compute/server.rb', line 387

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)



318
319
320
# File 'lib/open_stack/nova/compute/server.rb', line 318

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
* ip addresses


263
264
265
266
267
268
269
270
271
272
273
274
275
# 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.nets = updated.nets
  end

  self
end

#resumeObject

Resume a SUSPENDED server.



377
378
379
# File 'lib/open_stack/nova/compute/server.rb', line 377

def resume
  post(:action, {}, {:'resume' => nil}.to_json)
end

#security_groupsObject

The array of OpenStack::Nova::Compute::SecurityGroup instances associated with this server



209
210
211
212
213
214
215
# File 'lib/open_stack/nova/compute/server.rb', line 209

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



221
222
223
224
225
226
227
# File 'lib/open_stack/nova/compute/server.rb', line 221

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

Returns:

  • (Boolean)


392
393
394
# File 'lib/open_stack/nova/compute/server.rb', line 392

def shutoff?
  status == "SHUTOFF"
end

#startObject

Returns a STOPPED server to ACTIVE status.



357
358
359
# File 'lib/open_stack/nova/compute/server.rb', line 357

def start
  post(:action, {}, {:'os-start' => nil}.to_json)
end

#status_descriptionObject

Returns an extended (and localized) description for the server status



297
298
299
# File 'lib/open_stack/nova/compute/server.rb', line 297

def status_description
  I18n.t(SERVER_STATUSES[status], :scope => [:openstack, :status])
end

#stopObject

Halts a running server. Changes status to STOPPED.



352
353
354
# File 'lib/open_stack/nova/compute/server.rb', line 352

def stop
  post(:action, {}, {:'os-stop' => nil}.to_json)
end

#suspendObject

Suspend a running server. Changes status to SUSPENDED.



372
373
374
# File 'lib/open_stack/nova/compute/server.rb', line 372

def suspend
  post(:action, {}, {:'suspend' => nil}.to_json)
end

#task_descriptionObject

Returns a localized description for the server task (if any)



302
303
304
# File 'lib/open_stack/nova/compute/server.rb', line 302

def task_description
  I18n.t(task, :scope => [:openstack, :tasks]) if task.present?
end

#unpauseObject

Returns a PAUSED server to ACTIVE status.



367
368
369
# File 'lib/open_stack/nova/compute/server.rb', line 367

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)



345
346
347
348
349
# File 'lib/open_stack/nova/compute/server.rb', line 345

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)



237
238
239
# File 'lib/open_stack/nova/compute/server.rb', line 237

def volume_attachments(scope = :all)
  VolumeAttachment.find(scope, :params => {:server_id => self.id})
end