module-puppetlabs-mysql/manifests/db.pp

80 lines
2 KiB
ObjectPascal
Raw Normal View History

# Define: mysql::db
2011-05-25 08:22:43 +02:00
#
# This module creates database instances, a user,
# and grants that user privileges to the DB.
#
# Parameters:
# [*title*] - database name
# [*user*] - user to create
# [*password*] - user's password
# [*charset*] - charset for db
# [*host*] - host for assigning privileges to user
# [*grant*] - array of privileges to grant to user
# [*sql*] - sql to inject in db (always runs)
#
# Actions:
#
# Requires:
#
# Sample Usage:
#
# mysql::db { 'mydb':
# user => 'my_user',
# password => 'password',
# host => $::hostname,
# grant => ['all']
# }
2011-05-25 08:22:43 +02:00
#
define mysql::db (
$user,
$password,
$charset = 'utf8',
$host = 'localhost',
$grant='all',
$enforce_sql = false,
$sql=''
) {
if $grant == 'all' {
$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']
} else {
$safe_grant = $grant
}
2011-05-25 08:22:43 +02:00
database { $name:
ensure => present,
charset => $charset,
2011-05-25 08:22:43 +02:00
provider => 'mysql',
require => Class['mysql::server'],
notify => $sql ? {
'' => undef,
default => Exec["${name}-import-import"],
}
2011-05-25 08:22:43 +02:00
}
database_user{"${user}@${host}":
ensure => present,
password_hash => mysql_password($password),
provider => 'mysql',
require => Database[$name],
2011-05-25 08:22:43 +02:00
}
database_grant{"${user}@${host}/${name}":
2011-05-25 08:22:43 +02:00
# privileges => [ 'alter_priv', 'insert_priv', 'select_priv', 'update_priv' ],
privileges => $safe_grant,
provider => 'mysql',
require => Database_user["${user}@${host}"],
2011-05-25 08:22:43 +02:00
}
2011-05-25 08:22:43 +02:00
if($sql) {
exec{"${name}-import-import":
command => "/usr/bin/mysql -u ${user} -p${password} -h ${host} ${name} < ${sql}",
logoutput => true,
refreshonly => $enforce_sql ? {
true => false,
false => true,
},
2011-05-25 08:22:43 +02:00
}
}
}