99 lines
2.6 KiB
Puppet
99 lines
2.6 KiB
Puppet
# Copyright 2015 Mirantis, 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.
|
|
|
|
# Define: mysql::db
|
|
#
|
|
# This module creates database instances, a user, and grants that user
|
|
# privileges to the database. It can also import SQL from a file in order to,
|
|
# for example, initialize a database schema.
|
|
#
|
|
# Since it requires class mysql::server, we assume to run all commands as the
|
|
# root mysql user against the local mysql server.
|
|
#
|
|
# Parameters:
|
|
# [*title*] - mysql database name.
|
|
# [*user*] - username to create and grant access.
|
|
# [*password*] - user's password.
|
|
# [*charset*] - database charset.
|
|
# [*host*] - host for assigning privileges to user.
|
|
# [*grant*] - array of privileges to grant user.
|
|
# [*enforce_sql*] - whether to enforce or conditionally run sql on creation.
|
|
# [*sql*] - sql statement to run.
|
|
#
|
|
# Actions:
|
|
#
|
|
# Requires:
|
|
#
|
|
# class mysql::server
|
|
#
|
|
# Sample Usage:
|
|
#
|
|
# mysql::db { 'mydb':
|
|
# user => 'my_user',
|
|
# password => 'password',
|
|
# host => $::hostname,
|
|
# grant => ['all']
|
|
# }
|
|
#
|
|
define plugin_midonet::db (
|
|
$user,
|
|
$password,
|
|
$allowed_hosts,
|
|
$charset = 'utf8',
|
|
$host = 'localhost',
|
|
$grant = 'all',
|
|
$sql = '',
|
|
$enforce_sql = false,
|
|
) {
|
|
|
|
database { $name:
|
|
ensure => present,
|
|
charset => $charset,
|
|
provider => 'mysql',
|
|
}
|
|
|
|
database_user { "${user}@${host}":
|
|
ensure => present,
|
|
password_hash => mysql_password($password),
|
|
provider => 'mysql',
|
|
require => Database[$name],
|
|
}
|
|
|
|
database_grant { "${user}@${host}/${name}":
|
|
privileges => $grant,
|
|
provider => 'mysql',
|
|
require => Database_user["${user}@${host}"],
|
|
}
|
|
|
|
neutron::db::mysql::host_access { $allowed_hosts:
|
|
user => $user,
|
|
password => $password,
|
|
database => $name,
|
|
}
|
|
|
|
|
|
$refresh = ! $enforce_sql
|
|
|
|
if $sql {
|
|
exec{ "${name}-import":
|
|
command => "/usr/bin/mysql ${name} < ${sql}",
|
|
logoutput => true,
|
|
refreshonly => $refresh,
|
|
require => Database_grant["${user}@${host}/${name}"],
|
|
subscribe => Database[$name],
|
|
}
|
|
}
|
|
|
|
}
|