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
|
|
|
|
# privileges to the database.
|
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.
|
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 = '',
|
|
|
|
$enforce_sql = false
|
2011-05-27 03:11:24 +02:00
|
|
|
) {
|
2011-05-30 03:15:55 +02:00
|
|
|
|
2011-12-19 16:25:35 +01:00
|
|
|
if $grant == 'all' {
|
2012-02-09 20:26:00 +01:00
|
|
|
$safe_grant = [ 'alter_priv', 'alter_routine_priv', 'create_priv', 'create_routine_priv', 'create_tmp_table_priv', 'create_view_priv', 'delete_priv', 'drop_priv', 'event_priv', 'execute_priv', 'grant_priv', 'index_priv', 'insert_priv', 'lock_tables_priv', 'references_priv', 'select_priv', 'show_view_priv', 'trigger_priv', 'update_priv']
|
2011-12-19 16:25:35 +01:00
|
|
|
} else {
|
|
|
|
$safe_grant = $grant
|
|
|
|
}
|
|
|
|
|
2011-05-25 08:22:43 +02:00
|
|
|
database { $name:
|
2011-12-19 16:25:35 +01:00
|
|
|
ensure => present,
|
|
|
|
charset => $charset,
|
2011-05-25 08:22:43 +02:00
|
|
|
provider => 'mysql',
|
2011-12-19 16:25:35 +01:00
|
|
|
require => Class['mysql::server'],
|
2011-05-25 08:22:43 +02:00
|
|
|
}
|
2011-12-19 16:25:35 +01:00
|
|
|
|
2012-02-09 20:26:00 +01:00
|
|
|
database_user { "${user}@${host}":
|
2011-12-19 16:25:35 +01:00
|
|
|
ensure => present,
|
2011-05-27 03:11:24 +02:00
|
|
|
password_hash => mysql_password($password),
|
2011-12-19 16:25:35 +01:00
|
|
|
provider => 'mysql',
|
|
|
|
require => Database[$name],
|
2011-05-25 08:22:43 +02:00
|
|
|
}
|
2011-12-19 16:25:35 +01:00
|
|
|
|
2012-02-09 20:26:00 +01:00
|
|
|
database_grant { "${user}@${host}/${name}":
|
2011-12-19 16:25:35 +01:00
|
|
|
privileges => $safe_grant,
|
|
|
|
provider => 'mysql',
|
|
|
|
require => Database_user["${user}@${host}"],
|
2011-05-25 08:22:43 +02:00
|
|
|
}
|
2011-12-19 16:25:35 +01:00
|
|
|
|
2012-02-09 20:26:00 +01:00
|
|
|
if $sql {
|
|
|
|
exec{ "${name}-import":
|
2011-12-19 16:25:35 +01:00
|
|
|
command => "/usr/bin/mysql -u ${user} -p${password} -h ${host} ${name} < ${sql}",
|
|
|
|
logoutput => true,
|
2012-02-09 20:26:00 +01:00
|
|
|
refreshonly => ! $enforce_sql,
|
|
|
|
require => Database_grant["${user}@${host}/${name}"],
|
|
|
|
subscribe => Database[$name],
|
2011-05-25 08:22:43 +02:00
|
|
|
}
|
|
|
|
}
|
2012-02-09 20:26:00 +01:00
|
|
|
|
2011-12-19 16:25:35 +01:00
|
|
|
}
|