class Rackspace::Vm

Attributes

flavor_id[R]
image_name[R]
name[R]
size[R]
volume[R]

Public Class Methods

new(params) click to toggle source
Calls superclass method Rackspace::Base::new
# File lib/rackspace/vm.rb, line 5
def initialize(params)
  super params
  @name              = params.fetch(:name)
  @image_name        = params.fetch(:image_name,  DEFAULT_IMAGE_NAME)
  @size              = params.fetch(:size,        DEFAULT_SIZE)
  @flavor_id         = params.fetch(:flavor_id,   "#{DEFAULT_FLAVOUR}-#{size}")
  @volume_definition = params[:volume]
  @state             = params[:state]  # Allow this to be passed in, if we know it already
end

Public Instance Methods

age_in_seconds() click to toggle source
# File lib/rackspace/vm.rb, line 35
def age_in_seconds
  server.created && (Time.now - Time.parse(server.created)).to_i
end
attach_volume(vol) click to toggle source
# File lib/rackspace/vm.rb, line 51
def attach_volume(vol)
  server.attach_volume(vol.volume)
end
create() click to toggle source
# File lib/rackspace/vm.rb, line 15
def create
  log "Creating server #{name} in account #{account}"
  @server = server_api.create(
    name:        name,
    flavor_id:   flavor_id,
    image_id:    image_id,
    personality: personality
  )
  build_and_attach_volume if @volume_definition
  self
end
destroy(options = {}) click to toggle source
# File lib/rackspace/vm.rb, line 27
def destroy(options = {})
  log "Destroying server #{name} in account #{account}"
  server.attachments.map(&:destroy)
  volume.destroy if options[:destroy_volume]
  server.destroy
  self
end
private_ip() click to toggle source
# File lib/rackspace/vm.rb, line 43
def private_ip
  server.private_ip_address
end
public_ip() click to toggle source
# File lib/rackspace/vm.rb, line 39
def public_ip
  server.ipv4_address
end
state() click to toggle source
# File lib/rackspace/vm.rb, line 47
def state
  @state ||= server.state
end

Private Instance Methods

attachments() click to toggle source
# File lib/rackspace/vm.rb, line 70
def attachments
  server.attachments
end
build_and_attach_volume() click to toggle source
# File lib/rackspace/vm.rb, line 74
def build_and_attach_volume
  vol = Rackspace::Volume.new(@volume_definition.merge(
      account:      account,
      region:       region,
      credentials:  credentials,
      name:         "#{name}-vol",
  )).create

  log "Attaching block storage volume #{vol.name}"
  server.attach_volume(vol.volume)
  @volume = nil # force an API query next time volume is called
end
find_by_name() click to toggle source
# File lib/rackspace/vm.rb, line 102
def find_by_name
  server_api.find_by_name(name)
end
image_id() click to toggle source
# File lib/rackspace/vm.rb, line 98
def image_id
  server_api.image_by_name(image_name).id
end
personality() click to toggle source
# File lib/rackspace/vm.rb, line 91
def personality
  [{
    'path'     => '/root/.ssh/authorized_keys',
    'contents' => Base64.encode64(credentials.fetch(:public_ssh_key)),
  }]
end
server() click to toggle source
# File lib/rackspace/vm.rb, line 87
def server
  @server ||= server_api.find_by_name(name)
end