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
135
# 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

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)


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

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.



311
312
313
# 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

#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_volumesObject

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



336
337
338
339
340
# 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 = {}) ⇒ Object

Creates a new snapshot of server

Attributes

  • name - name of the new snapshot image

  • metadata - hash of metadata (may be nil)



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

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

#deleted?Boolean

true if the status is DELETED

Returns:

  • (Boolean)


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

def deleted?
  status == "DELETED"
end

#encode(options = {}) ⇒ Object

Overloads ActiveRecord::encode method



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

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



174
175
176
177
178
# 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) ⇒ 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



184
185
186
187
188
189
# 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

#imageObject

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



156
157
158
159
160
# 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) ⇒ 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



166
167
168
169
170
171
# 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_pairObject

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



192
193
194
195
196
# 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) ⇒ 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



202
203
204
205
206
207
# 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

#pauseObject

PAUSE a server.



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

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

#paused?Boolean

true if the status is PAUSED

Returns:

  • (Boolean)


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

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)



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

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


264
265
266
267
268
269
270
271
272
273
274
275
276
# 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

#resumeObject

Resume a SUSPENDED server.



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

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

#security_groupsObject

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



210
211
212
213
214
215
216
# 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) ⇒ Object

Set security groups for this server

Attributes

  • security_groups - Array of OpenStack::Nova::Compute::SecurityGroup instances



222
223
224
225
226
227
228
# 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?Boolean

true if the status is SHUTOFF

Returns:

  • (Boolean)


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

def shutoff?
  status == "SHUTOFF"
end

#startObject

Returns a STOPPED server to ACTIVE status.



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

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

#status_descriptionObject

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



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

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

#stopObject

Halts a running server. Changes status to STOPPED.



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

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

#suspendObject

Suspend a running server. Changes status to SUSPENDED.



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

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

#task_descriptionObject

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



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

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

#unpauseObject

Returns a PAUSED server to ACTIVE status.



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

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)



346
347
348
349
350
# 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) ⇒ 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 volume_attachments(scope = :all)
  VolumeAttachment.find(scope, :params => {:server_id => self.id})
end