class Blimpy::Boxes::OpenStack

Attributes

floating_ip[RW]
key_name[RW]

Public Class Methods

fog_server_for_instance(id, blimpdata) click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 7
def self.fog_server_for_instance(id, blimpdata)
  region = blimpdata[:region]
  fog = Fog::Compute.new(:provider => 'OpenStack', :openstack_tenant => region)
  fog.servers.get(id)
end
new(server=nil) click to toggle source
Calls superclass method Blimpy::Box.new
# File lib/blimpy/boxes/openstack.rb, line 28
def initialize(server=nil)
  super(server)
  @username = 'ubuntu'
  @flavor = 'm1.tiny'
  @group = 'default'
  @key_name = nil
  @floating_ip = nil
end

Public Instance Methods

allocate_ip() click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 99
def allocate_ip
  response = fog.allocate_address
  unless response.status == 200
    raise Blimpy::UnknownError, "Blimpy was unable to allocate a floating IP address; #{response.inspect}"
  end

  details = response.body['floating_ip']
  @floating_ip = FloatingIp.new(details['ip'], details['id'])
end
associate_ip() click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 109
def associate_ip
  if floating_ip.nil?
    raise Blimpy::UnknownError, "Blimpy cannot associate a floating IP until it's been allocated properly!"
  end
  response = fog.associate_address(@server.id, floating_ip.address)

  unless response.status == 202
    raise Blimpy::UnknownError, "Blimpy failed to associate the IP somehow #{response.inspect}"
  end
end
deallocate_ip() click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 132
def deallocate_ip
  fog.release_address(floating_ip.id)
end
disassociate_ip() click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 128
def disassociate_ip
  fog.disassociate_address(@server.id, floating_ip.address)
end
dns() click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 56
def dns
  if floating_ip.nil?
    'unavailable'
  else
    floating_ip.address
  end
end
flavor_id(name) click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 88
def flavor_id(name)
  flavors.each do |flavor|
    return flavor.id if flavor.name == name
  end
  nil
end
flavors() click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 84
def flavors
  @flavors ||= fog.flavors
end
fog() click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 78
def fog
  @fog ||= begin
    Fog::Compute.new(:provider => 'openstack', :openstack_tenant => @region)
  end
end
internal_dns() click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 64
def internal_dns
  'unavailable'
end
ports=(new_pors) click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 37
def ports=(new_pors)
  raise Blimpy::UnsupportedFeatureException, 'Opening arbitrary ports in OpenStack is currently not supported'
end
postdestroy() click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 124
def postdestroy
  deallocate_ip unless floating_ip.nil?
end
predestroy() click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 120
def predestroy
  disassociate_ip unless floating_ip.nil?
end
prestart() click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 95
def prestart
  allocate_ip
end
serializable_attributes() click to toggle source
Calls superclass method Blimpy::Box#serializable_attributes
# File lib/blimpy/boxes/openstack.rb, line 52
def serializable_attributes
  super + [:floating_ip]
end
validate!() click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 68
def validate!
  if @region.nil?
    raise Blimpy::BoxValidationError, "Cannot spin up machine without a set region"
  end

  if flavor_id(@flavor).nil?
    raise Blimpy::BoxValidationError, "'#{@flavor}' is not a valid OpenStack tenant name"
  end
end
wait_for_state(until_state, &block) click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 41
def wait_for_state(until_state, &block)
  until @server.ready?
    sleep 1
    @server.reload
  end
  # OpenStack doesn't seem to like it if you try to associate the IP
  # address too early, but will properly associate it after the machine is
  # ready
  associate_ip
end

Private Instance Methods

create_host() click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 146
def create_host
  tags = @tags.merge({:Name => @name, :CreatedBy => 'Blimpy', :BlimpyFleetId => @fleet_id})

  groups = [@group]
  import_key
  fog.servers.create(:image_ref => image_id,
                     :flavor_ref => flavor_id(@flavor),
                     :key_name => Blimpy::Keys.key_name,
                     :groups => groups,
                     :name => @name,
                     :tags => tags)
end
import_key() click to toggle source
# File lib/blimpy/boxes/openstack.rb, line 138
def import_key
  material = Blimpy::Keys.public_key
  begin
    fog.create_key_pair(Blimpy::Keys.key_name, material)
  rescue Excon::Errors::Conflict => e
  end
end