Class: Blimpy::Boxes::OpenStack

Inherits:
Blimpy::Box show all
Defined in:
lib/blimpy/boxes/openstack.rb

Defined Under Namespace

Classes: FloatingIp

Instance Attribute Summary collapse

Attributes inherited from Blimpy::Box

#allowed_regions, #flavor, #fleet_id, #group, #image_id, #livery, #name, #ports, #provision_on_start, #region, #ssh_port, #tags, #username

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Blimpy::Box

#bootstrap, #bootstrap_livery, #destroy, from_instance_id, #immutable_attributes, #online!, #poststart, #provision, #resume, #run_command, #scp_file, #scp_files, #ssh_commands, #ssh_into, #start, #state_file, #stop, #type, #wait_for_sshd, #with_data, #write_state_file

Methods included from Helpers::State

#ensure_state_folder, #state_file, #state_folder

Constructor Details

#initialize(server = nil) ⇒ OpenStack

Returns a new instance of OpenStack.



28
29
30
31
32
33
34
35
# 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

Instance Attribute Details

#floating_ipObject

Returns the value of attribute floating_ip.



26
27
28
# File 'lib/blimpy/boxes/openstack.rb', line 26

def floating_ip
  @floating_ip
end

#key_nameObject

Returns the value of attribute key_name.



26
27
28
# File 'lib/blimpy/boxes/openstack.rb', line 26

def key_name
  @key_name
end

Class Method Details

.fog_server_for_instance(id, blimpdata) ⇒ Object



7
8
9
10
11
# 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

Instance Method Details

#allocate_ipObject



99
100
101
102
103
104
105
106
107
# 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_ipObject



109
110
111
112
113
114
115
116
117
118
# 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_ipObject



132
133
134
# File 'lib/blimpy/boxes/openstack.rb', line 132

def deallocate_ip
  fog.release_address(floating_ip.id)
end

#disassociate_ipObject



128
129
130
# File 'lib/blimpy/boxes/openstack.rb', line 128

def disassociate_ip
  fog.disassociate_address(@server.id, floating_ip.address)
end

#dnsObject



56
57
58
59
60
61
62
# File 'lib/blimpy/boxes/openstack.rb', line 56

def dns
  if floating_ip.nil?
    'unavailable'
  else
    floating_ip.address
  end
end

#flavor_id(name) ⇒ Object



88
89
90
91
92
93
# 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

#flavorsObject



84
85
86
# File 'lib/blimpy/boxes/openstack.rb', line 84

def flavors
  @flavors ||= fog.flavors
end

#fogObject



78
79
80
81
82
# File 'lib/blimpy/boxes/openstack.rb', line 78

def fog
  @fog ||= begin
    Fog::Compute.new(:provider => 'openstack', :openstack_tenant => @region)
  end
end

#internal_dnsObject



64
65
66
# File 'lib/blimpy/boxes/openstack.rb', line 64

def internal_dns
  'unavailable'
end

#ports=(new_pors) ⇒ Object



37
38
39
# 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

#postdestroyObject



124
125
126
# File 'lib/blimpy/boxes/openstack.rb', line 124

def postdestroy
  deallocate_ip unless floating_ip.nil?
end

#predestroyObject



120
121
122
# File 'lib/blimpy/boxes/openstack.rb', line 120

def predestroy
  disassociate_ip unless floating_ip.nil?
end

#prestartObject



95
96
97
# File 'lib/blimpy/boxes/openstack.rb', line 95

def prestart
  allocate_ip
end

#serializable_attributesObject



52
53
54
# File 'lib/blimpy/boxes/openstack.rb', line 52

def serializable_attributes
  super + [:floating_ip]
end

#validate!Object



68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# 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