Merge pull request #7 from jaypipes/working
Grab superuser/pass from the underlying db-specific cookbook
This commit is contained in:
@@ -82,31 +82,41 @@ module Openstack
|
||||
|
||||
# Library routine that uses the database cookbook to create the
|
||||
# service's database and grant read/write access to the
|
||||
# given user and password. A privileged "super user" and password
|
||||
# is supplied, which is needed to create the database and user
|
||||
# records. Returns the db info from the db() library call.
|
||||
def db_create_with_user(service, super_user, super_pass, user, pass)
|
||||
# given user and password.
|
||||
#
|
||||
# A privileged "super user" and password is determined from the
|
||||
# underlying database cookbooks. For instance, if a MySQL database
|
||||
# is used, the node["mysql"]["server_root_password"] is used along
|
||||
# with the "root" (super)user.
|
||||
def db_create_with_user(service, user, pass)
|
||||
info = db(service)
|
||||
if info
|
||||
host = info['host']
|
||||
port = info['port'].to_s
|
||||
type = info['db_type']
|
||||
db_name = info['db_name']
|
||||
connection_info = {
|
||||
'host' => host,
|
||||
'port' => port,
|
||||
'username' => super_user,
|
||||
'password' => super_pass
|
||||
}
|
||||
prov = case type
|
||||
case type
|
||||
when 'postgresql', 'pgsql'
|
||||
Chef::Provider::Database::Postgresql
|
||||
prov = Chef::Provider::Database::Postgresql
|
||||
# See https://github.com/opscode-cookbooks/postgresql/blob/master/recipes/server.rb#L41
|
||||
super_user = 'postgres'
|
||||
super_password = node['postgresql']['password']['postgres']
|
||||
when 'mysql'
|
||||
Chef::Provider::Database::Mysql
|
||||
prov = Chef::Provider::Database::Mysql
|
||||
# See https://github.com/opscode-cookbooks/mysql/blob/master/recipes/server.rb#L40
|
||||
super_user = 'root'
|
||||
super_password = node['mysql']['server_root_password']
|
||||
else
|
||||
Chef::Log.error("Unsupported database type #{type}")
|
||||
prov = Chef::Log.error("Unsupported database type #{type}")
|
||||
end
|
||||
|
||||
connection_info = {
|
||||
:host => host,
|
||||
:port => port,
|
||||
:username => super_user,
|
||||
:password => super_password
|
||||
}
|
||||
|
||||
# create database
|
||||
database "create #{db_name} database" do
|
||||
provider prov
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#
|
||||
# Cookbook Name:: openstack-utils
|
||||
# Cookbook Name:: openstack-common
|
||||
# library:: default
|
||||
#
|
||||
# Copyright 2012, Jay Pipes
|
||||
|
||||
@@ -133,11 +133,11 @@ describe ::Openstack do
|
||||
describe "#db_create_with_user" do
|
||||
it "returns nil when no openstack.db not in node attrs" do
|
||||
@subject.instance_variable_set(:@node, {})
|
||||
@subject.db_create_with_user("nonexisting", "superuser", "superpass", "user", "pass").should be_nil
|
||||
@subject.db_create_with_user("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_create_with_user("nonexisting", "superuser", "superpass", "user", "pass").should be_nil
|
||||
@subject.db_create_with_user("nonexisting", "user", "pass").should be_nil
|
||||
end
|
||||
it "returns db info and creates database with user when service found" do
|
||||
stub_const("Chef::Provider::Database::Mysql", nil)
|
||||
@@ -147,7 +147,7 @@ describe ::Openstack do
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 3306
|
||||
}
|
||||
result = @subject.db_create_with_user("compute", "superuser", "superpass", "user", "pass")
|
||||
result = @subject.db_create_with_user("compute", "user", "pass")
|
||||
result.should eq expect
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user