2013-09-13 19:12:33 +02:00
|
|
|
# See README.md for details.
|
2011-05-27 03:11:24 +02:00
|
|
|
define mysql::db (
|
|
|
|
$user,
|
|
|
|
$password,
|
2014-09-21 00:43:01 +02:00
|
|
|
$dbname = $name,
|
|
|
|
$charset = 'utf8',
|
|
|
|
$collate = 'utf8_general_ci',
|
|
|
|
$host = 'localhost',
|
|
|
|
$grant = 'ALL',
|
|
|
|
$sql = undef,
|
|
|
|
$enforce_sql = false,
|
|
|
|
$ensure = 'present',
|
2013-10-31 14:31:37 +01:00
|
|
|
$import_timeout = 300,
|
2011-05-27 03:11:24 +02:00
|
|
|
) {
|
2013-07-13 01:06:53 +02:00
|
|
|
#input validation
|
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'.")
|
2014-03-07 08:08:09 +01:00
|
|
|
$table = "${dbname}.*"
|
2014-11-14 22:16:01 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2013-09-09 17:34:39 +02:00
|
|
|
include '::mysql::client'
|
|
|
|
|
2014-03-07 22:23:36 +01:00
|
|
|
$db_resource = {
|
2012-08-22 00:40:42 +02:00
|
|
|
ensure => $ensure,
|
2011-12-19 16:25:35 +01:00
|
|
|
charset => $charset,
|
2013-08-18 02:54:02 +02:00
|
|
|
collate => $collate,
|
2011-05-25 08:22:43 +02:00
|
|
|
provider => 'mysql',
|
2014-10-24 17:54:33 +02:00
|
|
|
require => [ Class['mysql::client'] ],
|
2011-05-25 08:22:43 +02:00
|
|
|
}
|
2014-03-07 22:23:36 +01:00
|
|
|
ensure_resource('mysql_database', $dbname, $db_resource)
|
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),
|
2013-09-09 17:34:39 +02:00
|
|
|
provider => 'mysql',
|
2013-04-11 19:02:41 +02:00
|
|
|
}
|
2013-08-18 02:54:02 +02:00
|
|
|
ensure_resource('mysql_user', "${user}@${host}", $user_resource)
|
2011-12-19 16:25:35 +01:00
|
|
|
|
2012-08-22 00:40:42 +02:00
|
|
|
if $ensure == 'present' {
|
2013-08-29 00:03:51 +02:00
|
|
|
mysql_grant { "${user}@${host}/${table}":
|
2012-08-22 00:40:42 +02:00
|
|
|
privileges => $grant,
|
|
|
|
provider => 'mysql',
|
2013-08-29 00:03:51 +02:00
|
|
|
user => "${user}@${host}",
|
|
|
|
table => $table,
|
2014-10-24 17:54:33 +02:00
|
|
|
require => [
|
|
|
|
Mysql_database[$dbname],
|
|
|
|
Mysql_user["${user}@${host}"],
|
|
|
|
],
|
2012-08-22 00:40:42 +02:00
|
|
|
}
|
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 {
|
2014-03-07 08:08:09 +01:00
|
|
|
exec{ "${dbname}-import":
|
2014-09-21 00:43:01 +02:00
|
|
|
command => "cat ${sql_inputs} | mysql ${dbname}",
|
2012-08-22 00:40:42 +02:00
|
|
|
logoutput => true,
|
2013-08-29 00:03:51 +02:00
|
|
|
environment => "HOME=${::root_home}",
|
2012-08-22 00:40:42 +02:00
|
|
|
refreshonly => $refresh,
|
2014-09-21 00:43:01 +02:00
|
|
|
path => '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin',
|
2013-08-29 00:03:51 +02:00
|
|
|
require => Mysql_grant["${user}@${host}/${table}"],
|
2014-03-07 08:08:09 +01:00
|
|
|
subscribe => Mysql_database[$dbname],
|
2013-10-31 14:31:37 +01:00
|
|
|
timeout => $import_timeout,
|
2012-08-22 00:40:42 +02:00
|
|
|
}
|
2011-05-25 08:22:43 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-19 16:25:35 +01:00
|
|
|
}
|