Files
cookbook-openstack-common/libraries/endpoints.rb
Jan Klare 1858c025b2 library cleanup and refactoring
* version bump to 13.0.0 for mitaka release
* removed suse support
* removed general endpoint method, since we should be able to always specify
  which endpoint we need
* removed fallbacks in specific_endpoint method, since this behaviour is not a
  very obvious one to the user and it should rather return an error than an
  unexpected result
* dry public, internal and admin endpoint methods
* removed obsolete private methods
* adapted method calls for admin_endpoint in libraries/cli.rb
* refactored set_endpoints_by_interface recipe to directly call address_for
  instead of address, since the recipe already checks for an existing attribute
  ..['bind_interface'] and therefore address would redirect to address_for
  anyways
* moved the nested hash order for the public, internal and admin attributes to
  to be more clear and to break all existing calls to fix them during the
  refactoring process of all cookbooks
  e.g: node['openstack']['endpoints']['internal']['identity'] is now
  node['openstack']['endpoints']['identity']['internal'] and can be moved into
  the identity cookbook. This also streamlines these endpoint attributes with
  the bind_interface and host attributes
* removed dependency on openstack-identity cookbooks by moving openrc recipe to
  opentack-identity (same for corrensponding specs and template)
* removed address method and use the address (or hostname) defined in the
  endpoints hash directly (logic to set this attribute should rather be
  done in a wrapper (with a fitting method) instead of a static and predefined
  one)
* removed set_endpoints_by_interface recipe since logic for defining the
  endpoints will be moved to wrapper cookbooks
* added helper method merge_config_options for generation of config hashes used
  in service config templates
* added template for openstack-service.conf.erb which can be used by all service
  cookbooks
* deleted all endpoints attibutes, since these are moved to the service
  cookbooks for easier dependency handling

Implements: blueprint cookbook-refactoring
Change-Id: I0547182085eed91d05384fdd7734408a839a9a2c
2016-02-05 08:38:07 +01:00

79 lines
2.4 KiB
Ruby

# encoding: UTF-8
#
# Cookbook Name:: openstack-common
# library:: endpoints
#
# Copyright 2012-2013, AT&T Services, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Endpoint methods
module ::Openstack
# Instead of specifying the verbose node['openstack']['db'][service],
# this shortcut allows the simpler and shorter db(service), where
# service can be: 'compute', 'image', 'identity', 'network', etc.
def db(service)
node['openstack']['db'][service]
rescue
nil
end
# Shortcut to get the SQLAlchemy DB URI for a named service
def db_uri(service, user, pass, is_slave = false) # rubocop:disable MethodLength, CyclomaticComplexity
info = db(service)
return unless info
if is_slave
host = info['slave_host']
port = info['slave_port'].to_s
else
host = info['host']
port = info['port'].to_s
end
type = info['service_type']
name = info['db_name']
options = info['options'][type]
# Normalize to the SQLAlchemy standard db type identifier
case type
when 'pgsql'
type = 'postgresql'
when 'mariadb', 'galera', 'percona-cluster'
type = 'mysql'
end
# Build uri
case type
when 'mysql', 'postgresql'
"#{type}://#{user}:#{pass}@#{host}:#{port}/#{name}#{options}"
when 'sqlite'
# SQLite uses filepaths not db name
# README(galstrom): 3 slashes is a relative path, 4 slashes is an absolute path
# example: info['path'] = 'path/to/foo.db' -- will return sqlite:///foo.db
# example: info['path'] = '/path/to/foo.db' -- will return sqlite:////foo.db
path = info['path']
"#{type}:///#{path}#{options}"
end
end
# Find the specific endpoint type ('internal', 'admin' or
# 'public') for the given service.
%w(public internal admin).each do |ep_type|
define_method "#{ep_type}_endpoint" do |service|
uri_from_hash(node['openstack']['endpoints'][service][ep_type])
end
end
end