module-puppetlabs-mysql/manifests/db.pp

72 lines
1.9 KiB
ObjectPascal
Raw Permalink Normal View History

# See README.md for details.
define mysql::db (
$user,
$password,
$dbname = $name,
$charset = 'utf8',
$collate = 'utf8_general_ci',
$host = 'localhost',
$grant = 'ALL',
$sql = undef,
$enforce_sql = false,
$ensure = 'present',
$import_timeout = 300,
) {
#input validation
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'.")
$table = "${dbname}.*"
if !(is_array($sql) or is_string($sql)) {
fail('$sql must be either a string or an array.')
}
$sql_inputs = join([$sql], ' ')
2012-08-22 00:40:42 +02:00
include '::mysql::client'
$db_resource = {
2012-08-22 00:40:42 +02:00
ensure => $ensure,
charset => $charset,
collate => $collate,
2011-05-25 08:22:43 +02:00
provider => 'mysql',
require => [ Class['mysql::client'] ],
2011-05-25 08:22:43 +02:00
}
ensure_resource('mysql_database', $dbname, $db_resource)
$user_resource = {
ensure => $ensure,
password_hash => mysql_password($password),
provider => 'mysql',
}
ensure_resource('mysql_user', "${user}@${host}", $user_resource)
2012-08-22 00:40:42 +02:00
if $ensure == 'present' {
mysql_grant { "${user}@${host}/${table}":
2012-08-22 00:40:42 +02:00
privileges => $grant,
provider => 'mysql',
user => "${user}@${host}",
table => $table,
require => [
Mysql_database[$dbname],
Mysql_user["${user}@${host}"],
],
2012-08-22 00:40:42 +02:00
}
2012-08-22 00:40:42 +02:00
$refresh = ! $enforce_sql
2012-08-22 00:40:42 +02:00
if $sql {
exec{ "${dbname}-import":
command => "cat ${sql_inputs} | mysql ${dbname}",
2012-08-22 00:40:42 +02:00
logoutput => true,
environment => "HOME=${::root_home}",
2012-08-22 00:40:42 +02:00
refreshonly => $refresh,
path => '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin',
require => Mysql_grant["${user}@${host}/${table}"],
subscribe => Mysql_database[$dbname],
timeout => $import_timeout,
2012-08-22 00:40:42 +02:00
}
2011-05-25 08:22:43 +02:00
}
}
}