Class: OpenstackController

Inherits:
Object show all
Defined in:
lib/providers/openstack/openstack.rb,
lib/providers/openstack/openstack_get.rb,
lib/providers/openstack/openstack_query.rb,
lib/providers/openstack/openstack_create.rb,
lib/providers/openstack/openstack_delete.rb,
lib/providers/openstack/openstack_update.rb

Overview

Define Openstack update controller

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.base_method(crud_type) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/providers/openstack/openstack.rb', line 226

def self.base_method(crud_type)
  define_method(crud_type) do |sObjectType, hParams|
    method_name = "#{crud_type}_#{sObjectType}"
    if self.class.method_defined? method_name
      send(method_name, hParams)
    else
      controller_error "'%s' is not a valid object for '%s'",
                       sObjectType, crud_type
    end
  end
end

.def_basic_query(connection, name, property_name = nil) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/providers/openstack/openstack_query.rb', line 19

def self.def_basic_query(connection, name, property_name = nil)
  property_name = property_name.nil? ? name.to_s + 's' : property_name.to_s

  define_method("query_#{name}") do |hParams, _query|
    required?(hParams, connection)
    hParams[connection].send(property_name).all
  end
end

.def_complex_query(connection, name, property_name = nil) ⇒ Object

Implementation of API NOT supporting query Hash The function will filter itself.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/providers/openstack/openstack_query.rb', line 40

def self.def_complex_query(connection, name, property_name = nil)
  property_name = property_name.nil? ? name.to_s + 's' : property_name.to_s

  define_method("query_#{name}") do |hParams, query|
    required?(hParams, connection)

    key_pairs = hParams[connection].send(property_name).all
    results = []
    key_pairs.each do |sElem|
      selected = true
      attributes = sElem.instance_variable_get(:@attributes)
      query.each do |key, value|
        if attributes[key] != value
          selected = false
          break
        end
      end
      results.push sElem if selected
    end
    results
  end
end

.def_cruds(*crud_types) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/providers/openstack/openstack.rb', line 189

def self.def_cruds(*crud_types)
  crud_types.each do |crud_type|
    case crud_type
    when :create, :delete
      base_method(crud_type)
    when :query, :get
      query_method(crud_type)
    when :update
      update_method(crud_type)
    end
  end
end

.def_get(connection, name, property_name = nil) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/providers/openstack/openstack_get.rb', line 19

def self.def_get(connection, name, property_name = nil)
  property_name = property_name.nil? ? "#{name}s" : property_name.to_s
  define_method("get_#{name}") do |hParams, sUniqId|
    required?(hParams, connection)
    hParams[connection].send(property_name).get(sUniqId)
  end
end

.def_simple_query(connection, name, property_name = nil) ⇒ Object

Implementation of API supporting query Hash



29
30
31
32
33
34
35
36
# File 'lib/providers/openstack/openstack_query.rb', line 29

def self.def_simple_query(connection, name, property_name = nil)
  property_name = property_name.nil? ? name.to_s + 's' : property_name.to_s

  define_method("query_#{name}") do |hParams, query|
    required?(hParams, connection)
    hParams[connection].send(property_name).all query
  end
end

.query_method(crud_type) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
# File 'lib/providers/openstack/openstack.rb', line 214

def self.query_method(crud_type)
  define_method(crud_type) do |sObjectType, sCondition, hParams|
    method_name = "#{crud_type}_#{sObjectType}"
    if self.class.method_defined? method_name
      send(method_name, hParams, sCondition)
    else
      controller_error "'%s' is not a valid object for '%s'",
                       sObjectType, crud_type
    end
  end
end

.update_method(crud_type) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
# File 'lib/providers/openstack/openstack.rb', line 202

def self.update_method(crud_type)
  define_method(crud_type) do |sObjectType, obj, hParams|
    method_name = "#{crud_type}_#{sObjectType}"
    if self.class.method_defined? method_name
      send(method_name, obj, hParams)
    else
      controller_error "'%s' is not a valid object for '%s'",
                       sObjectType, crud_type
    end
  end
end

Instance Method Details

#allocate_new_ip(compute_connect) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/providers/openstack/openstack_create.rb', line 153

def allocate_new_ip(compute_connect)
  # Create a new public IP to add in the pool.
  pools = compute_connect.addresses.get_address_pools
  controller_error('No IP Pool found') if pools.length == 0
  # TODO: Be able to support choice of pool at setup time.
  if pools.length > 1
    Lorj.warning('Several pools found. Selecting the first one.')
  end
  compute_connect.addresses.create 'pool' => pools[0]['name']
end

#connect(sObjectType, hParams) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/providers/openstack/openstack.rb', line 241

def connect(sObjectType, hParams)
  case sObjectType
  when :services
    # Fog use URI type for auth uri: URI.parse(:auth_uri)
    # Convert openstack_auth_uri to type URI
    hParams[:hdata][:openstack_auth_uri] =
        URI.parse(hParams[:hdata][:openstack_auth_uri])
    retrieve_result =
        Fog::OpenStack.retrieve_tokens_v2(hParams[:hdata],
                                          hParams[:excon_opts])
    creds = format_retrieve_result(retrieve_result)
    return creds
  when :compute_connection
    Fog::Compute.new(
      hParams[:hdata].merge(:provider => :openstack)
    )
  when :network_connection
    Fog::Network::OpenStack.new(hParams[:hdata])
  else
    controller_error "'%s' is not a valid object for 'connect'", sObjectType
  end
end

#create_keypairs(hParams) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/providers/openstack/openstack_create.rb', line 79

def create_keypairs(hParams)
  required?(hParams, :compute_connection)
  required?(hParams, :keypair_name)
  required?(hParams, :public_key)

  # API:
  # https://github.com/fog/fog/blob/master/lib/fog/openstack/docs/compute.md
  service = hParams[:compute_connection]
  service.key_pairs.create(:name => hParams[:keypair_name],
                           :public_key => hParams[:public_key])
end

#create_network(hParams) ⇒ Object



30
31
32
33
34
35
# File 'lib/providers/openstack/openstack_create.rb', line 30

def create_network(hParams)
  required?(hParams, :network_connection)
  required?(hParams, :network_name)

  hParams[:network_connection].networks.create(hParams[:hdata])
end

#create_public_ip(hParams) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/providers/openstack/openstack_create.rb', line 119

def create_public_ip(hParams)
  required?(hParams, :compute_connection)
  required?(hParams, :server)

  compute_connect = hParams[:compute_connection]
  server = hParams[:server]

  while server.state != 'ACTIVE'
    sleep(5)
    server = compute_connect.servers.get(server.id)
  end

  addresses = compute_connect.addresses.all
  address = nil
  # Search for an available IP
  addresses.each do |elem|
    if elem.fixed_ip.nil?
      address = elem
      break
    end
  end

  address = allocate_new_ip(compute_connect) if address.nil?
  if address.nil?
    controller_error("No Public IP to assign to server '%s'", server.name)
  end

  address.server = server # associate the server
  address.reload
  # This function needs to returns a list of object.
  # This list must support the each function.
  address
end

#create_router(hParams) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/providers/openstack/openstack_create.rb', line 56

def create_router(hParams)
  required?(hParams, :network_connection)
  required?(hParams, :router_name)

  # Forcelly used admin_status_up to true. Coming from HPCloud.
  # But not sure if we need it or not.
  #  hParams[:hdata] = hParams[:hdata].merge(:admin_state_up => true)

  hParams[:network_connection].routers.create(hParams[:hdata])
end

#create_router_interface(hParams) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/providers/openstack/openstack_create.rb', line 67

def create_router_interface(hParams)
  required?(hParams, :network_connection)
  required?(hParams, :router)
  required?(hParams, :subnetwork)

  service = hParams[:network_connection]
  router = hParams[:router]
  result = service.add_router_interface(router.id, hParams[:subnetwork].id)
  fail if result.status != 200
  result
end

#create_rule(hParams) ⇒ Object



50
51
52
53
54
# File 'lib/providers/openstack/openstack_create.rb', line 50

def create_rule(hParams)
  required?(hParams, :network_connection)
  required?(hParams, :security_groups)
  hParams[:network_connection].security_group_rules.create(hParams[:hdata])
end

#create_security_groups(hParams) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/providers/openstack/openstack_create.rb', line 19

def create_security_groups(hParams)
  required?(hParams, :network_connection)
  required?(hParams, :tenants)
  required?(hParams, :security_group)

  service = hParams[:network_connection]

  service.security_groups.create(:name => hParams[:security_group],
                                 :tenant_id => hParams[:tenants].id)
end

#create_server(hParams) ⇒ Object



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
# File 'lib/providers/openstack/openstack_create.rb', line 91

def create_server(hParams)
  [:compute_connection, :image,
   :network, :flavor, :keypairs,
   :security_groups, :server_name].each do |required_param|
    required?(hParams, required_param)
  end

  options = {
    :name             => hParams[:server_name],
    :flavor_ref       => hParams[:flavor].id,
    :image_ref        => hParams[:image].id,
    :key_name         => hParams[:keypairs].name,
    :security_groups  => [hParams[:security_groups].name],
    :nics             => [{ :net_id => hParams[:network].id }]
  }

  if hParams[:user_data]
    options[:user_data_encoded] =
      Base64.strict_encode64(hParams[:user_data])
  end
  options[:metadata] = hParams[:meta_data] if hParams[:meta_data]

  compute_connect = hParams[:compute_connection]

  server = compute_connect.servers.create(options)
  compute_connect.servers.get(server.id) if server
end

#create_subnetwork(hParams) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/providers/openstack/openstack_create.rb', line 37

def create_subnetwork(hParams)
  required?(hParams, :network_connection)
  required?(hParams, :network)

  netconn = hParams[:network_connection]
  netconn.subnets.create(
    :network_id => hParams[:network].id,
    :name => hParams[:subnetwork_name],
    :cidr => get_next_subnet(netconn),
    :ip_version => '4'
  )
end

#delete_server(hParams) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/providers/openstack/openstack_delete.rb', line 19

def delete_server(hParams)
  required?(hParams, :compute_connection)
  required?(hParams, :server)

  compute_connect = hParams[:compute_connection]
  server = hParams[:server]

  compute_connect.servers.get(server.id).destroy
end

#get_attr(oControlerObject, key) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/providers/openstack/openstack.rb', line 283

def get_attr(oControlerObject, key)
  if oControlerObject.is_a?(Excon::Response)
    oControlerObject.data.rh_get(:body, key)
  else
    attributes = oControlerObject.attributes
    controller_error "attribute '%s' is unknown in '%s'."\
                     " Valid one are : '%s'",
                     key[0],
                     oControlerObject.class,
                     oControlerObject.class.attributes unless
                     oControlerObject.class.attributes.include?(key[0])

    return attributes.rh_get(key) if attributes.rh_exist?(key)
    return oControlerObject.send(key[0]) if key.length == 1
    oControlerObject.send(key[0]).rh_get(key[1..-1])
  end
rescue => e
  controller_error "==>Unable to map '%s'. %s", key, e.message
end

#get_next_subnet(oNetworkConnect) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/providers/openstack/openstack_create.rb', line 164

def get_next_subnet(oNetworkConnect)
  subnet_values = []
  subnets = oNetworkConnect.subnets.all

  subnets.each do|s|
    subnet_values.push(s.cidr)
  end

  gap = false
  count = 0
  range_used = []
  new_subnet = 0
  new_cidr = ''

  subnet_values = subnet_values.sort!

  subnet_values.each do|value|
    range_used.push(value[5])
  end

  range_used.each do |n|
    if count.to_i == n.to_i
    else
    new_subnet = count
    gap = true
    break
    end
    count += 1
  end

  if gap
    new_cidr = format('10.0.%s.0/24', count)
  else
    max_value = range_used.max
    new_subnet = max_value.to_i + 1
    new_cidr  = format('10.0.%s.0/24', new_subnet)
  end
  new_cidr
rescue => e
  Logging.error("%s\n%s", e.message, e.backtrace.join("\n"))
end

#get_server_log(hParams, sUniqId) ⇒ Object



35
36
37
38
# File 'lib/providers/openstack/openstack_get.rb', line 35

def get_server_log(hParams, sUniqId)
  required?(hParams, :server)
  hParams[:server].console(sUniqId)
end

#query_security_groups(hParams, query) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/providers/openstack/openstack_query.rb', line 89

def query_security_groups(hParams, query)
  required?(hParams, :network_connection)
  required?(hParams, :tenants)

  query[:tenant_id] = hParams[:tenants].id
  hParams[:network_connection].send(:security_groups).all query
end

#set_attr(oControlerObject, key, value) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/providers/openstack/openstack.rb', line 264

def set_attr(oControlerObject, key, value)
  if oControlerObject.is_a?(Excon::Response)
    controller_error "No set feature for '%s'", oControlerObject.class
  end

  attributes = oControlerObject.attributes

  controller_error "attribute '%s' is unknown in '%s'. Valid one are : '%s'",
                   key[0],
                   oControlerObject.class,
                   oControlerObject.class.attributes unless
                   oControlerObject.class.attributes.include?(key[0])

  attributes.rh_set(value, key)
rescue => e
  controller_error "Unable to map '%s' on '%s'. %s",
                   key, oControlerObject, e.message
end

#update_router(obj_to_save, _hParams) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/providers/openstack/openstack_update.rb', line 19

def update_router(obj_to_save, _hParams)
  router = obj_to_save[:object]
  # The optional external_gateway_info must be a The network_id for the
  # external gateway. It cannot be a Hash
  # See API : http://developer.openstack.org/api-ref-networking-v2.html
  router.external_gateway_info = router.external_gateway_info['network_id']
  # Save will restore the Hash.
  begin
    router.save
    true
  rescue => e
    Lorj.error "OpenStack: Router save error.\n%s\n%s",
               e.message, e.backtrace.join("\n")
    false
  end
end