Adds db information lookup to defaults and library
Add node['openstack']['db'] array of db info hashes Add ::Openstack::db() and ::Openstack::db_uri() methods to the library.
This commit is contained in:
@@ -136,3 +136,75 @@ default['openstack']['endpoints']['image-registry']['path'] = "/v2"
|
||||
# default['openstack']['endpoints']['compute-volume']['uri'] = "https://volume.example.com:8776/"v1/%(tenant_id)s"
|
||||
# default['openstack']['endpoints']['image-api']['uri'] = "https://image.example.com:9292/v2"
|
||||
# default['openstack']['endpoints']['image-registry']['uri'] = "https://image.example.com:9191/v2"
|
||||
|
||||
# ======================== OpenStack DB Support ================================
|
||||
#
|
||||
# This section of node attributes stores information about the database hosts
|
||||
# used in an OpenStack deployment.
|
||||
#
|
||||
# There is no 'scheme' key. Instead, there is a 'db_type' key that should
|
||||
# contain one of 'sqlite', 'mysql', or 'postgresql'
|
||||
#
|
||||
# The ::Openstack::db(<SERVICE_NAME>) library routine allows a lookup from any recipe
|
||||
# to this array, returning the host information for the server that contains
|
||||
# the database for <SERVICE_NAME>, where <SERVICE_NAME> is one of 'compute' (Nova),
|
||||
# 'image' (Glance), 'identity' (Keystone), 'network' (Quantum), or 'volume' (Cinder)
|
||||
#
|
||||
# The ::Openstack::db_connection(<SERVICE_NAME>, <USER>, <PASSWORD>) library routine
|
||||
# returns the SQLAlchemy DB URI for <SERVICE_NAME>, with the supplied user and password
|
||||
# that a calling service might be using when connecting to the database.
|
||||
#
|
||||
# For example, let's assume that the database that is used by the OpenStack Identity
|
||||
# service (Keystone) is configured as follows:
|
||||
#
|
||||
# host: 192.168.0.3
|
||||
# port: 3306
|
||||
# db_type: mysql
|
||||
# db_name: keystone
|
||||
#
|
||||
# Further suppose that a node running the OpenStack Identity API service needs to
|
||||
# connect to the above identity database server. It has the following in it's node
|
||||
# attributes:
|
||||
#
|
||||
# node['db']['user'] = 'keystone'
|
||||
# node['db']['password'] = 'password'
|
||||
#
|
||||
# In a "keystone" recipe, you might find the following code:
|
||||
#
|
||||
# user = node['db']['user']
|
||||
# pass = node['db']['password']
|
||||
#
|
||||
# sql_connection = ::Openstack::db_uri('identity', user, pass)
|
||||
#
|
||||
# The sql_connection variable would then be set to "mysql://keystone:password@192.168.0.3:keystone"
|
||||
# and could then be written to the keystone.conf file in a template.
|
||||
|
||||
# Database used by the OpenStack Compute (Nova) service
|
||||
default['openstack']['db']['compute']['db_type'] = "mysql"
|
||||
default['openstack']['db']['compute']['host'] = "127.0.0.1"
|
||||
default['openstack']['db']['compute']['port'] = 3306
|
||||
default['openstack']['db']['compute']['db_name'] = "nova"
|
||||
|
||||
# Database used by the OpenStack Identity (Keystone) service
|
||||
default['openstack']['db']['identity']['db_type'] = "mysql"
|
||||
default['openstack']['db']['identity']['host'] = "127.0.0.1"
|
||||
default['openstack']['db']['identity']['port'] = 3306
|
||||
default['openstack']['db']['identity']['db_name'] = "keystone"
|
||||
|
||||
# Database used by the OpenStack Image (Glance) service
|
||||
default['openstack']['db']['image']['db_type'] = "mysql"
|
||||
default['openstack']['db']['image']['host'] = "127.0.0.1"
|
||||
default['openstack']['db']['image']['port'] = 3306
|
||||
default['openstack']['db']['image']['db_name'] = "glance"
|
||||
|
||||
# Database used by the OpenStack Network (Quantum) service
|
||||
default['openstack']['db']['network']['db_type'] = "mysql"
|
||||
default['openstack']['db']['network']['host'] = "127.0.0.1"
|
||||
default['openstack']['db']['network']['port'] = 3306
|
||||
default['openstack']['db']['network']['db_name'] = "quantum"
|
||||
|
||||
# Database used by the OpenStack Volume (Cinder) service
|
||||
default['openstack']['db']['volume']['db_type'] = "mysql"
|
||||
default['openstack']['db']['volume']['host'] = "127.0.0.1"
|
||||
default['openstack']['db']['volume']['port'] = 3306
|
||||
default['openstack']['db']['volume']['db_name'] = "cinder"
|
||||
|
||||
@@ -47,4 +47,23 @@ module Openstack
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
|
||||
# Instead of specifying the verbose node["openstack"]["db"][service],
|
||||
# this shortcut allows the simpler and shorter db(service), where
|
||||
# service is one of 'compute', 'image', 'identity', 'network',
|
||||
# and 'volume'
|
||||
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)
|
||||
info = db(service)
|
||||
if info
|
||||
result = info['db_type'] + '://' + user + ':' + pass
|
||||
result = result + '@' + info['host'] + ':' + info['port'].to_s + '/' + info['db_name']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -74,6 +74,7 @@ describe ::Openstack do
|
||||
@subject.endpoint_uri("compute-api").should eq "http://localhost"
|
||||
end
|
||||
end
|
||||
|
||||
describe "#endpoints" do
|
||||
it "does nothing when no endpoints" do
|
||||
@subject.instance_variable_set(:@node, {})
|
||||
@@ -96,4 +97,36 @@ describe ::Openstack do
|
||||
@count.should >= 1
|
||||
end
|
||||
end
|
||||
|
||||
describe "#db" do
|
||||
it "returns nil when no openstack.db not in node attrs" do
|
||||
@subject.instance_variable_set(:@node, {})
|
||||
@subject.db("nonexisting").should be_nil
|
||||
end
|
||||
it "returns nil when no such service was found" do
|
||||
@subject.instance_variable_set(:@node, @chef_run.node)
|
||||
@subject.db("nonexisting").should be_nil
|
||||
end
|
||||
it "returns db info hash when service found" do
|
||||
@subject.instance_variable_set(:@node, @chef_run.node)
|
||||
@subject.db("compute")['host'].should == "127.0.0.1"
|
||||
@subject.db("compute").has_key?("uri").should be_false
|
||||
end
|
||||
end
|
||||
|
||||
describe "#db_uri" do
|
||||
it "returns nil when no openstack.db not in node attrs" do
|
||||
@subject.instance_variable_set(:@node, {})
|
||||
@subject.db_uri("nonexisting", "user", "pass").should be_nil
|
||||
end
|
||||
it "returns nil when no such service was found" do
|
||||
@subject.instance_variable_set(:@node, @chef_run.node)
|
||||
@subject.db_uri("nonexisting", "user", "pass").should be_nil
|
||||
end
|
||||
it "returns db info hash when service found" do
|
||||
@subject.instance_variable_set(:@node, @chef_run.node)
|
||||
expect = "mysql://user:pass@127.0.0.1:3306/nova"
|
||||
@subject.db_uri("compute", "user", "pass").should eq expect
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user