Class: Fog::Compute::RackspaceV2::Server

Inherits:
Server
  • Object
show all
Defined in:
lib/fog/rackspace/models/compute_v2/server.rb

Constant Summary collapse

ACTIVE =

States

'ACTIVE'
BUILD =
'BUILD'
DELETED =
'DELETED'
ERROR =
'ERROR'
HARD_REBOOT =
'HARD_REBOOT'
MIGRATING =
'MIGRATING'
PASSWORD =
'PASSWORD'
REBOOT =
'REBOOT'
REBUILD =
'REBUILD'
RESCUE =
'RESCUE'
RESIZE =
'RESIZE'
REVERT_RESIZE =
'REVERT_RESIZE'
SUSPENDED =
'SUSPENDED'
UNKNOWN =
'UNKNOWN'
VERIFY_RESIZE =
'VERIFY_RESIZE'

Instance Attribute Summary collapse

Attributes inherited from Server

#private_key, #private_key_path, #public_key, #public_key_path, #ssh_options, #ssh_port, #username

Attributes inherited from Model

#collection, #service

Instance Method Summary collapse

Methods inherited from Server

#scp, #scp_download, #ssh, #sshable?

Methods inherited from Model

#inspect, #reload, #symbolize_keys, #to_json, #wait_for

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Fog::Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



145
146
147
148
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 145

def initialize(attributes={})
  @service = attributes[:service]
  super
end

Instance Attribute Details

#addressHash<String, Array[Hash]> (readonly)

Returns IP addresses allocated for server by network.

Examples:

{ 
   "public" => [
        {"version"=>4, "addr"=>"166.78.105.63"}, 
        {"version"=>6, "addr"=>"2001:4801:7817:0072:0fe1:75e8:ff10:61a9"}
               ], 
  "private"=> [{"version"=>4, "addr"=>"10.177.18.209"}]
}

Returns:

  • (Hash<String, Array[Hash]>)

    IP addresses allocated for server by network



128
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 128

attribute :addresses

#bandwidthArray (readonly)

Returns The amount of bandwidth used for the specified audit period.

Returns:

  • (Array)

    The amount of bandwidth used for the specified audit period.

See Also:



116
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 116

attribute :bandwidth, :aliases => 'rax-bandwidth:bandwidth'

#createdString (readonly)

Returns The time stamp for the creation date.

Returns:

  • (String)

    The time stamp for the creation date.



35
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 35

attribute :created

#disk_configString<AUTO, MANUAL>

The disk configuration value.

* AUTO -   The server is built with a single partition the size of the target flavor disk. The file system is automatically adjusted to fit the entire partition. 
           This keeps things simple and automated. AUTO is valid only for images and servers with a single partition that use the EXT3 file system. 
           This is the default setting for applicable Rackspace base images.

* MANUAL - The server is built using whatever partition scheme and file system is in the source image. If the target flavor disk is larger, 
           the remaining disk space is left unpartitioned. This enables images to have non-EXT3 file systems, multiple partitions, 
           and so on, and enables you to manage the disk configuration.

Returns:

  • (String<AUTO, MANUAL>)

    The disk configuration value.

See Also:



111
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 111

attribute :disk_config, :aliases => 'OS-DCF:diskConfig'

#flavor_idString (readonly)

Returns The flavor Id.



133
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 133

attribute :flavor_id, :aliases => 'flavor', :squash => 'id'

#host Id(Id) ⇒ Object (readonly)

The host Id. The compute provisioning algorithm has an anti-affinity property that attempts to spread customer VMs across hosts. Under certain situations, VMs from the same customer might be placed on the same host. hostId represents the host your server runs on and can be used to determine this scenario if it is relevant to your application. HostId is unique per account and is not globally unique.



48
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 48

attribute :host_id, :aliases => 'hostId'

#idString (readonly)

Returns The server id.

Returns:



27
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 27

identity :id

#image_idString (readonly)

Returns The image Id.



138
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 138

attribute :image_id, :aliases => 'image', :squash => 'id'

#ipv4_addressString Also known as: access_ipv4_address

Note:

This field will populate once the server is ready to use.

Returns The public IP version 4 access address.



91
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 91

attribute :ipv4_address, :aliases => 'accessIPv4'

#ipv6_addressString Also known as: access_ipv6_address

Note:

This field will populate once the server is ready to use.

Returns The public IP version 6 access address.



97
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 97

attribute :ipv6_address, :aliases => 'accessIPv6'

Returns Server links.

Returns:

  • (Array)

    Server links.



74
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 74

attribute :links

#nameString

Returns The server name.

Returns:

  • (String)

    The server name.



31
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 31

attribute :name

#passwordString (readonly)

Note:

This value is ONLY populated on server creation.

Returns Password for system adminstrator account.

Returns:

  • (String)

    Password for system adminstrator account.



142
143
144
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 142

def password
  @password
end

#personalityHash

Note:

This attribute is only used for server creation. This field will be nil on subsequent retrievals.

Returns Hash containing data to inject into the file system of the cloud server instance during server creation.

Examples:

To inject fog.txt into file system

:personality => [{ :path => '/root/fog.txt',
                   :contents => Base64.encode64('Fog was here!')
                }]

Returns:

  • (Hash)

    Hash containing data to inject into the file system of the cloud server instance during server creation.

See Also:



85
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 85

attribute :personality

#progressFixnum (readonly)

Returns The build completion progress, as a percentage. Value is from 0 to 100.

Returns:

  • (Fixnum)

    The build completion progress, as a percentage. Value is from 0 to 100.



62
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 62

attribute :progress

#stateString (readonly)

Returns server status.



53
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 53

attribute :state, :aliases => 'status'

#state_extString (readonly)

Returns server (extended) status.



58
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 58

attribute :state_ext, :aliases => 'OS-EXT-STS:task_state'

#tenant_idString (readonly)

Returns The tenant Id.

Returns:



70
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 70

attribute :tenant_id

#updatedString (readonly)

Returns The time stamp for the last update.

Returns:

  • (String)

    The time stamp for the last update.



39
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 39

attribute :updated

#user_idString (readonly)

Returns The user Id.

Returns:



66
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 66

attribute :user_id

Instance Method Details

#attach_volume(volume, device = nil) ⇒ Fog::Compute::RackspaceV2::Attachment

Attaches Cloud Block Volume

Parameters:

Returns:

Raises:

See Also:



330
331
332
333
334
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 330

def attach_volume(volume, device=nil)
  requires :identity
  volume_id = volume.is_a?(String) ? volume : volume.id
  attachments.create(:server_id => identity, :volume_id => volume_id, :device => device)
end

#attachmentsFog::Compute::RackspaceV2::Attachments

Attached Cloud Block Volumes



312
313
314
315
316
317
318
319
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 312

def attachments
  @attachments ||= begin
    Fog::Compute::RackspaceV2::Attachments.new({
      :service => service,
      :server => self
    })
  end
end

#change_admin_password(password) ⇒ Boolean

Note:

Though Rackspace does not enforce complexity requirements for the password, the operating system might. If the password is not complex enough, the server might enter an ERROR state.

Change admin password

  • Status Transition:

    • ACTIVE -> PASSWORD -> ACTIVE

    • ACTIVE -> PASSWORD -> ERROR (on error)

Parameters:

  • password (String)

    The administrator password.

Returns:

  • (Boolean)

    returns true if operation was scheduled

Raises:

See Also:



511
512
513
514
515
516
517
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 511

def change_admin_password(password)
  requires :identity
  service.change_server_password(identity, password)
  self.state = PASSWORD
  @password = password
  true
end

#confirm_resizeObject

  • VERIFY_RESIZE -> ERROR (on error)å



435
436
437
438
439
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 435

def confirm_resize
  requires :identity
  service.confirm_resize_server(identity)
  true
end

#create(options) ⇒ Boolean

Note:

You should use servers.create to create servers instead calling this method directly

Creates server

  • requires attributes: service:, :name, :image_id, and :flavor_id

  • optional attributes :disk_config, :metadata, :personality

  • State Transitions

    • BUILD -> ACTIVE

    • BUILD -> ERROR (on error)



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 199

def create(options)
  requires :name, :image_id, :flavor_id

  options[:networks] ||= attributes[:networks]
  options[:disk_config] = disk_config unless disk_config.nil?
  options[:metadata] = .to_hash unless @metadata.nil?
  options[:personality] = personality unless personality.nil?
  options[:keypair] ||= attributes[:keypair]

  if options[:networks]
    options[:networks].map! { |id| { :uuid => id } }
  end
  data = service.create_server(name, image_id, flavor_id, 1, 1, options)
  merge_attributes(data.body['server'])
  true
end

#create_image(name, options = {}) ⇒ Fog::ComputeRackspaceV2::Image

Creates Image from server. Once complete, a new image is available that you can use to rebuild or create servers.

  • State Transition:

    • SAVING -> ACTIVE

    • SAVING -> ERROR (on error)

Parameters:

  • name (String)

    name of image to create

  • options (Hash) (defaults to: {})

    :

Options Hash (options):

  • metadata (Hash<String, String>)

    hash of containing metadata key value pairs.

Returns:

  • (Fog::ComputeRackspaceV2::Image)

    image being created

Raises:

See Also:



294
295
296
297
298
299
300
301
302
303
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 294

def create_image(name, options = {})
  requires :identity
  response = service.create_image(identity, name, options)
  begin
    image_id = response.headers["Location"].match(/\/([^\/]+$)/)[1]
    Fog::Compute::RackspaceV2::Image.new(:collection => service.images, :service => service, :id => image_id)
  rescue
    nil
  end
end

#destroyBoolean

Destroy the server

  • Status Transition:

    • ACTIVE -> DELETED

    • ERROR -> DELETED



252
253
254
255
256
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 252

def destroy
  requires :identity
  service.delete_server(identity)
  true
end

#flavorFog::Compute::RackspaceV2::Flavor

Server flavor



264
265
266
267
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 264

def flavor
  requires :flavor_id
  @flavor ||= service.flavors.get(flavor_id)
end

#imageFog::Compute::RackspaceV2::Image

Server image



275
276
277
278
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 275

def image
  requires :image_id
  @image ||= service.images.get(image_id)
end

#metadataFog::Compute::RackspaceV2::Metadata

Server metadata

Returns:



157
158
159
160
161
162
163
164
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 157

def 
  @metadata ||= begin
    Fog::Compute::RackspaceV2::Metadata.new({
      :service => service,
      :parent => self
    })
  end
end

#metadata=(hash = {}) ⇒ Object

Set server metadata

Parameters:

  • hash (Hash) (defaults to: {})

    contains key value pairs



168
169
170
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 168

def metadata=(hash={})
  .from_hash(hash)
end

#private_ip_addressString

Server’s private IPv4 address

Returns:

  • (String)

    private IPv4 address



338
339
340
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 338

def private_ip_address
  addresses['private'].select{|a| a["version"] == 4}[0]["addr"] rescue ''
end

#public_ip_addressString

Server’s public IPv4 address

Returns:

  • (String)

    public IPv4 address



344
345
346
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 344

def public_ip_address
  ipv4_address
end

#ready?(ready_state = ACTIVE, error_states = [ERROR]) ⇒ Boolean

Is server is in ready state

Parameters:

  • ready_state (String) (defaults to: ACTIVE)

    By default state is ACTIVE

  • error_states (Array, String) (defaults to: [ERROR])

    By default state is ERROR

Returns:

  • (Boolean)

    returns true if server is in a ready state

Raises:



353
354
355
356
357
358
359
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 353

def ready?(ready_state = ACTIVE, error_states=[ERROR])
  if error_states
    error_states = Array(error_states)
    raise InvalidServerStateException.new(ready_state, state) if error_states.include?(state)
  end
  state == ready_state
end

#reboot(type = 'SOFT') ⇒ Boolean

Reboot server

  • State transition:

    • ACTIVE -> REBOOT -> ACTIVE (soft reboot)

    • ACTIVE -> HARD_REBOOT -> ACTIVE (hard reboot)

Parameters:

  • type (String<SOFT, HARD>) (defaults to: 'SOFT')

    ‘SOFT’ will do a soft reboot. ‘HARD’ will do a hard reboot.

Returns:

  • (Boolean)

    returns true if server is being rebooted

Raises:

See Also:



373
374
375
376
377
378
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 373

def reboot(type = 'SOFT')
  requires :identity
  service.reboot_server(identity, type)
  self.state = type == 'SOFT' ? REBOOT : HARD_REBOOT
  true
end

#rebuild(image_id, options = {}) ⇒ Boolean

Rebuild removes all data on the server and replaces it with the specified image. The id and all IP addresses remain the same.

  • Status Transition:

    • ACTIVE -> REBUILD -> ACTIVE

    • ACTIVE -> REBUILD -> ERROR (on error)

Parameters:

  • image_id (String)

    image to use for rebuild

Returns:

  • (Boolean)

    returns true if rebuild is in process

Raises:

See Also:



393
394
395
396
397
398
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 393

def rebuild(image_id, options={})
  requires :identity
  service.rebuild_server(identity, image_id, options)
  self.state = REBUILD
  true
end

#rescueBoolean

Note:

Rescue mode is only guaranteed to be active for 90 minutes.

Place existing server into rescue mode, allowing for offline editing of configuration. The original server’s disk is attached to a new instance of the same base image for a period of time to facilitate working within rescue mode. The original server will be automatically restored after 90 minutes.

  • Status Transition:

    • ACTIVE -> PREP_RESCUE -> RESCUE

Returns:

  • (Boolean)

    returns true if call to put server in rescue mode reports success

Raises:

See Also:



472
473
474
475
476
477
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 472

def rescue
  requires :identity
  data = service.rescue_server(identity)
  merge_attributes(data.body)
  true
end

#resize(flavor_id) ⇒ Boolean

Note:

All resizes are automatically confirmed after 24 hours if you do not explicitly confirm or revert the resize.

Resize existing server to a different flavor, in essence, scaling the server up or down. The original server is saved for a period of time to allow rollback if there is a problem. All resizes should be tested and explicitly confirmed, at which time the original server is removed. All resizes are automatically confirmed after 24 hours if they are not confirmed or reverted.

  • Status Transition:

    • ACTIVE -> QUEUE_RESIZE -> PREP_RESIZE -> VERIFY_RESIZE

    • ACTIVE -> QUEUE_RESIZE -> ACTIVE (on error)



415
416
417
418
419
420
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 415

def resize(flavor_id)
  requires :identity
  service.resize_server(identity, flavor_id)
  self.state = RESIZE
  true
end

#revert_resizeBoolean

Note:

All resizes are automatically confirmed after 24 hours if you do not explicitly confirm or revert the resize.

Reverts server resize operation

  • Status Transition:

    • VERIFY_RESIZE -> ACTIVE

    • VERIFY_RESIZE -> ERROR (on error)



454
455
456
457
458
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 454

def revert_resize
  requires :identity
  service.revert_resize_server(identity)
  true
end

#save(options = {}) ⇒ Boolean

Saves the server. Creates server if it is new, otherwise it will update server attributes name, accessIPv4, and accessIPv6.

Returns:

  • (Boolean)

    true if server has started saving



175
176
177
178
179
180
181
182
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 175

def save(options = {})
  if persisted?
    update
  else
    create(options)
  end
  true
end

#setup(credentials = {}) ⇒ Object

Setup server for SSH access



521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 521

def setup(credentials = {})
  requires :public_ip_address, :identity, :public_key, :username
  Fog::SSH.new(public_ip_address, username, credentials).run([
    %{mkdir .ssh},
    %{echo "#{public_key}" >> ~/.ssh/authorized_keys},
    %{passwd -l #{username}},
    %{echo "#{Fog::JSON.encode(attributes)}" >> ~/attributes.json},
    %{echo "#{Fog::JSON.encode()}" >> ~/metadata.json}
  ])
rescue Errno::ECONNREFUSED
  sleep(1)
  retry
end

#unrescueBoolean

Note:

Rescue mode is only guaranteed to be active for 90 minutes.

Remove existing server from rescue mode.

  • Status Transition:

    • PREP_UNRESCUE -> ACTIVE

Returns:

  • (Boolean)

    returns true if call to remove server from rescue mode reports success

Raises:

See Also:



491
492
493
494
495
496
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 491

def unrescue
  requires :identity
  service.unrescue_server(identity)
  self.state = ACTIVE
  true
end

#updateObject

Note:

If you edit the server name, the server host name does not change. Also, server names are not guaranteed to be unique.

Updates server This will update :name, :accessIPv4, and :accessIPv6 attributes.

  • State Transition

    • ACTIVE -> ACTIVE



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 228

def update
  requires :identity
  options = {
    'name' => name,
    'accessIPv4' => ipv4_address,
    'accessIPv6' => ipv6_address
  }

  data = service.update_server(identity, options)
  merge_attributes(data.body['server'])
  true
end