schema.rb 1.59 KB
#
# Author:: Earth U (<sysadmin @ chromedia.com>)
# Cookbook Name:: cfe-mariadb
# Recipe:: schema
#
# Copyright 2016, Chromedia Far East, 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.
#

mysql2_chef_gem 'default' do
  provider    Chef::Provider::Mysql2ChefGem::Mariadb
  gem_version '0.4.4'
  action      :install
end

# Prepare the needed databases and users.
con = {
  :host     => node['mariadb']['mysqld']['bind_address'],
  :port     => node['mariadb']['mysqld']['port'],
  :username => 'root',
  :password => node['mariadb']['server_root_password']
}
node['cfe-mariadb']['db_map'].each do |dbx|

  if dbx.is_a?(Array)
    dbx_name = dbx[0]
    dbx = dbx[1]
  else
    dbx_name = dbx[:db_name]
  end

  dbencoding = dbx[:char_set] || 'utf8'
  dbcollate  = dbx[:collate] || 'utf8_general_ci'
  mysql_database dbx_name do
    connection con
    action    :create
    encoding  dbencoding
    collation dbcollate
  end

  mysql_database_user dbx[:db_user] do
    connection    con
    password      dbx[:db_pass]
    database_name dbx_name
    host          '%'
    privileges    [:all]
    action        :grant
  end
end