diff --git a/libraries/default.rb b/libraries/default.rb index a73797a4..00110cdb 100644 --- a/libraries/default.rb +++ b/libraries/default.rb @@ -79,4 +79,61 @@ module Openstack end end end + + # 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) + 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 + when 'postgresql', 'pgsql' + Chef::Provider::Database::Postgresql + when 'mysql' + Chef::Provider::Database::Mysql + else + Chef::Log.error("Unsupported database type #{type}") + end + + # create database + database "create #{db_name} database" do + provider prov + connection connection_info + database_name db_name + action :create + end + + # create user + database_user username do + provider prov + connection connection_info + password pass + action :create + end + + # grant privs to user + database_user username do + provider prov + connection connection_info + password pass + database_name db_name + host '%' + privileges [:all] + action :grant + end + end + info + end end diff --git a/metadata.rb b/metadata.rb index d1c2c0ab..6c48d878 100644 --- a/metadata.rb +++ b/metadata.rb @@ -1,7 +1,9 @@ +name "openstack-common" maintainer "Jay Pipes" maintainer_email "jaypipes@gmail.com" license "Apache 2.0" description "Common OpenStack attributes, libraries and recipes." long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version "0.0.1" +depends "database" depends "openstack-utils" diff --git a/spec/default_spec.rb b/spec/default_spec.rb index 833c4bfc..db515c8c 100644 --- a/spec/default_spec.rb +++ b/spec/default_spec.rb @@ -129,4 +129,26 @@ describe ::Openstack do @subject.db_uri("compute", "user", "pass").should eq expect end end + + 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 + 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 + end + it "returns db info and creates database with user when service found" do + stub_const("Chef::Provider::Database::Mysql", nil) + Chef::Recipe.any_instance.stub(:database).and_return(Hash.new) + @subject.instance_variable_set(:@node, @chef_run.node) + expect = { + 'host' => '127.0.0.1', + 'port' => 3306 + } + result = @subject.db_create_with_user("compute", "superuser", "superpass", "user", "pass") + result.should eq expect + end + end end