# mysql.pp # Copyright (C) 2007 David Schmitt # See LICENSE for the full license granted to you. import "variables.pp" import "passwords" class mysql::server { include passwords include variables package { "mysql-server": name => "MySQL-server-community", ensure => installed, } package { "mysql-client": name => "MySQL-client-community", ensure => installed, } service { "mysql": ensure => running, hasstatus => true, require => Package["mysql-server"], } # It is more convenient to set the root password with # and exec than using the plugin $mysql_root_user = $variables::mysql_root_user $mysql_root_database = $variables::mysql_root_database $mysql_root_password = $passwords::mysql_root_password $mysql_root_local_host = $variables::mysql_root_local_host $mysql_global_host = $variables::mysql_global_host $mysql_replication_user = $variables::mysql_replication_user $mysql_replication_password = $passwords::mysql_replication_password $mysql_cmd_root_without_pwd = "/usr/bin/mysql --user=$mysql_root_user --database=$mysql_root_database --host=$mysql_root_local_host" $mysql_cmd_root_with_pwd = "/usr/bin/mysql --user=$mysql_root_user --database=$mysql_root_database --host=$mysql_root_local_host --password=$mysql_root_password" $mysql_cmd_repl_with_pwd = "/usr/bin/mysql --user=$mysql_replication_user --database=$mysql_root_database --host=$mysql_root_local_host --password=$mysql_replication_password" exec { "mysql root password init": command => "$mysql_cmd_root_without_pwd --execute=\"Update user set password=password('$mysql_root_password') where user='$mysql_root_user';\"", unless => "$mysql_cmd_root_with_pwd --execute '\s'", require => Service["mysql"], } exec { "mysql root flush password": command => "$mysql_cmd_root_without_pwd --execute=\"flush privileges;\"", unless => "$mysql_cmd_root_with_pwd --execute '\s'", require => Exec["mysql root password init"], } exec {"mysql create replication user": command => "$mysql_cmd_root_with_pwd --execute=\"Create user '$mysql_replication_user'@'$mysql_global_host' identified by '$mysql_replication_password';\"", unless => "$mysql_cmd_repl_with_pwd --execute '\s'", require => Exec["mysql root flush password"], } exec { "grants all to replication user": command => "$mysql_cmd_root_with_pwd --execute=\"GRANT All PRIVILEGES ON *.* TO '$mysql_replication_user'@'$mysql_global_host' IDENTIFIED BY '$mysql_replication_password';\"", unless => "$mysql_cmd_repl_with_pwd --execute '\s'", require => Exec["mysql create replication user"], } exec { "mysql root flush grants": command => "$mysql_cmd_root_without_pwd --execute=\"flush privileges;\"", unless => "$mysql_cmd_root_with_pwd --execute '\s'", require => Exec["grants all to replication user"], } } class mysql::standalone inherits mysql::server { mysql::mysql_config {"set binary logging": binary_logging => false, } } class mysql::m2s inherits mysql::server { mysql::mysql_config {"set binary logging": binary_logging => true, } exec { "restart mysql server": command => "service mysql restart", unless => "$mysql_cmd_repl_with_pwd --execute=\"show master status;\" | grep mysqllog", require => Service ["mysql"], } } class mysql::slave inherits mysql::server { mysql::mysql_config {"set binary logging": binary_logging => false, } mysql::mysql_replication {"Configure Slave Server for Replication":} } class mysql::m2m inherits mysql::server { mysql::mysql_config {"set binary logging": binary_logging => true, } mysql::mysql_replication {"Configure Master Server for Master to Master Replication": } } define mysql::mysql_config($binary_logging){ file { "/etc/my.cnf": ensure => present, owner => "mysql", group => "mysql", mode => 0644, content => template("mysql/my.cnf.erb"), before => Service["mysql"], } } define mysql::mysql_replication{ file { "/var/lib/mysql/gather_master_data.bash": owner => "mysql", group => "mysql", ensure => present, mode => 0755, content => template("mysql/gather_master_data.bash.erb"), require => Exec["grants all to replication user"], } exec { "remove set master datafile": command => "rm /var/lib/mysql/set_master_repl_data.sql", unless => "$mysql_cmd_repl_with_pwd --execute=\"show slave status;\" | grep Wait", require => File["/var/lib/mysql/gather_master_data.bash"], } exec { "set master data for slave": command => "/var/lib/mysql/gather_master_data.bash", creates => "/var/lib/mysql/set_master_repl_data.sql", unless => "$mysql_cmd_repl_with_pwd --execute=\"show slave status;\" | grep Wait", require => File["/var/lib/mysql/gather_master_data.bash"], } exec { "restart mysql server": command => "service mysql restart", unless => "$mysql_cmd_repl_with_pwd --execute=\"show slave status;\" | grep Wait", require => Exec["set master data for slave"], } exec { "start slave server": command => "$mysql_cmd_repl_with_pwd --execute=\"start slave;\"", unless => "$mysql_cmd_repl_with_pwd --execute=\"show slave status;\" | grep Wait", require => Exec["set master data for slave"], } } define mysql::datasource($rootpw, $ds_name, $ds_owner, $ds_owner_pwd, $ds_user, $ds_user_pwd, $ds_schema) { # TODO: This will soon be replaced. # The execs will selectively run based on the value of $mysql_type include mysql::standalone $mysql_root_cmd = "/usr/bin/mysql -u root -p$rootpw" exec { "create datasource $ds_name": command => "/usr/bin/mysqladmin -u root -p$rootpw create $ds_name", unless => "$mysql_root_cmd $ds_name -e '\s'", require => [Service["mysql"], Exec["mysql root password init"]], } exec { "create grants $ds_name": command => "$mysql_root_cmd -e \"GRANT ALL PRIVILEGES ON *.* TO '$ds_owner'@'%' IDENTIFIED BY '$ds_owner_pwd' WITH GRANT OPTION;\"", unless => "/usr/bin/mysql --host $ipaddress --user=$ds_owner -password=$ds_owner_pwd -e '\s'", require => [Service["mysql"], Exec["mysql root password init"]], } exec { "create grants $ds_owner": command => "$mysql_root_cmd -e \"GRANT SELECT,INSERT,UPDATE,DELETE ON $ds_name.* TO '$ds_user'@'%' IDENTIFIED BY '$ds_user_pwd';\"", unless => "/usr/bin/mysql --host $ipaddress --user=$ds_user -password=$ds_user_pwd -e '\s'", require => [Service["mysql"], Exec["mysql root password init"]], } # Only create the schema is a template directory was specified if $ds_schema { exec { "create db $ds_name": command => "$mysql_root_cmd $ds_name < $ds_schema > /var/lib/mysql/${ds_name}-create-db.log", creates => "/var/lib/mysql/${ds_name}-create-db.log", require => [Service["mysql"], Exec["create datasource $ds_name"]], } } }