2011-06-01 05:47:19 +02:00
|
|
|
# Define: mysql::db
|
2011-05-25 08:22:43 +02:00
|
|
|
#
|
2012-02-09 20:26:00 +01:00
|
|
|
# This module creates database instances, a user, and grants that user
|
2012-05-04 08:35:51 +02:00
|
|
|
# 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.
|
2011-06-01 05:47:19 +02:00
|
|
|
#
|
|
|
|
# Parameters:
|
2012-02-09 20:26:00 +01:00
|
|
|
# [*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.
|
2012-08-22 00:40:42 +02:00
|
|
|
# [*ensure*] - specifies if a database is present or absent.
|
2011-06-01 05:47:19 +02:00
|
|
|
#
|
|
|
|
# Actions:
|
|
|
|
#
|
|
|
|
# Requires:
|
|
|
|
#
|
2012-02-09 20:26:00 +01:00
|
|
|
# class mysql::server
|
|
|
|
#
|
2011-06-01 05:47:19 +02:00
|
|
|
# Sample Usage:
|
2012-02-09 20:26:00 +01:00
|
|
|
#
|
2011-06-01 05:47:19 +02:00
|
|
|
# mysql::db { 'mydb':
|
|
|
|
# user => 'my_user',
|
|
|
|
# password => 'password',
|
|
|
|
# host => $::hostname,
|
|
|
|
# grant => ['all']
|
|
|
|
# }
|
2011-05-25 08:22:43 +02:00
|
|
|
#
|
2011-05-27 03:11:24 +02:00
|
|
|
define mysql::db (
|
|
|
|
$user,
|
|
|
|
$password,
|
2012-02-09 20:26:00 +01:00
|
|
|
$charset = 'utf8',
|
|
|
|
$host = 'localhost',
|
|
|
|
$grant = 'all',
|
|
|
|
$sql = '',
|
2012-08-22 00:40:42 +02:00
|
|
|
$enforce_sql = false,
|
|
|
|
$ensure = 'present'
|
2011-05-27 03:11:24 +02:00
|
|
|
) {
|
2011-05-30 03:15:55 +02:00
|
|
|
|
2012-08-23 10:55:43 +02:00
|
|
|
validate_re($ensure, '^(present|absent)$',
|
2012-08-22 00:40:42 +02:00
|
|
|
"${ensure} is not supported for ensure. Allowed values are 'present' and 'absent'.")
|
|
|
|
|
2011-05-25 08:22:43 +02:00
|
|
|
database { $name:
|
2012-08-22 00:40:42 +02:00
|
|
|
ensure => $ensure,
|
2011-12-19 16:25:35 +01:00
|
|
|
charset => $charset,
|
2011-05-25 08:22:43 +02:00
|
|
|
provider => 'mysql',
|
2011-12-19 16:25:35 +01:00
|
|
|
require => Class['mysql::server'],
|
2013-02-18 12:24:57 +01:00
|
|
|
before => Database_user["${user}@${host}"],
|
2011-05-25 08:22:43 +02:00
|
|
|
}
|
2011-12-19 16:25:35 +01:00
|
|
|
|
2013-04-11 19:02:41 +02:00
|
|
|
$user_resource = {
|
|
|
|
ensure => $ensure,
|
|
|
|
password_hash => mysql_password($password),
|
|
|
|
provider => 'mysql'
|
|
|
|
}
|
2013-03-22 00:59:27 +01:00
|
|
|
ensure_resource('database_user', "${user}@${host}", $user_resource)
|
2011-12-19 16:25:35 +01:00
|
|
|
|
2012-08-22 00:40:42 +02:00
|
|
|
if $ensure == 'present' {
|
|
|
|
database_grant { "${user}@${host}/${name}":
|
|
|
|
privileges => $grant,
|
|
|
|
provider => 'mysql',
|
|
|
|
require => Database_user["${user}@${host}"],
|
|
|
|
}
|
2011-12-19 16:25:35 +01:00
|
|
|
|
2012-08-22 00:40:42 +02:00
|
|
|
$refresh = ! $enforce_sql
|
2012-03-18 07:56:13 +01:00
|
|
|
|
2012-08-22 00:40:42 +02:00
|
|
|
if $sql {
|
|
|
|
exec{ "${name}-import":
|
|
|
|
command => "/usr/bin/mysql ${name} < ${sql}",
|
|
|
|
logoutput => true,
|
|
|
|
refreshonly => $refresh,
|
|
|
|
require => Database_grant["${user}@${host}/${name}"],
|
|
|
|
subscribe => Database[$name],
|
|
|
|
}
|
2011-05-25 08:22:43 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-19 16:25:35 +01:00
|
|
|
}
|