summaryrefslogtreecommitdiffstats
path: root/provision_mysql.module
diff options
context:
space:
mode:
Diffstat (limited to 'provision_mysql.module')
-rw-r--r--provision_mysql.module101
1 files changed, 101 insertions, 0 deletions
diff --git a/provision_mysql.module b/provision_mysql.module
new file mode 100644
index 0000000..538050c
--- /dev/null
+++ b/provision_mysql.module
@@ -0,0 +1,101 @@
+<?php
+/**
+ * @file
+ * Mysql provisioning module.
+ *
+ * The goal of this module is to create mysql databases and user accounts, for sites that are about to be created.
+ * It uses the provision API to tie into the right places in the site creation work flow.
+ */
+
+/**
+ * @ingroup provisionui
+ * @{
+ */
+
+/**
+ * Implementation of provision_service()
+ */
+function provision_mysql_provision_service() {
+ return t("Mysql database server");
+}
+
+/**
+ * Implementation of provision_configure
+ */
+function provision_mysql_provision_configure() {
+ $form['provision_mysql_user'] = array(
+ '#type' => 'textfield',
+ '#required' => TRUE,
+ '#title' => t('Mysql user account'),
+ '#description' => t('The user that will be used to create users and databases for new sites.'),
+ '#size' => 40,
+ '#default_value' => variable_get('provision_mysql_user', 'root'),
+ '#maxlength' => 255,
+ );
+ $form['provision_mysql_password'] = array(
+ '#type' => 'password',
+ '#required' => TRUE,
+ '#title' => t('Mysql user password'),
+ '#description' => t('The user account that will be used to create new mysql users and databases for new sites'),
+ '#size' => 30,
+ '#maxlength' => 64,
+ );
+
+ $form['provision_mysql_host'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Mysql server hostname'),
+ '#description' => t('The mysql server to connect to.'),
+ '#size' => 30,
+ '#default_value' => variable_get('provision_mysql_host', 'localhost'),
+ '#maxlength' => 64,
+ );
+ return $form;
+}
+/**
+ * @} end "ingroup provisionui"
+ */
+
+
+function provision_mysql_provision_pre_install($url, &$data) {
+ $data['site-db-type'] = 'mysql'; # only support innodb. for now.
+ $data['site-db-host'] = ($data['site-db-host']) ? $data['site-db-host'] : variable_get('provision_mysql_host', 'localhost');
+ $data['site-db-passwd'] = user_password(); # generate a random password for use
+ if ($data['site_id']) {
+ $data['site-db-name'] = 'site_' . $data['site_id'];
+ $data['site-db-username'] = $data['site-db-name']; // mysql has some really really stupid rules about who db / usernames, so site id is the safest.
+ }
+ else {
+ $data['site-db-name'] = substr(ereg_replace("^www\.", "", str_replace(".", "", $url)), 0, 10);
+ $data['site-db-username'] = $data['site-db-name'];
+ // TODO : A reasonable fallback if the site id isn't available. This is going to make it a bit harder to test at first, but that's ok.
+ }
+
+ # For this to work, the user account the provisioning site has been set up with, requires CREATE database permissions.
+ # TODO : Add additional configuration for a database account to use for these , but this is the quickest way to get the code up and running.
+ $db_url = sprintf("mysqli://%s:%s@%s/mysql", variable_get('provision_mysql_user', 'root'), variable_get('provision_mysql_password', 'root'), $data['site-db-host'] );
+
+ if ( db_result(db_query("SHOW DATABASES LIKE '%s'", $data['site-db-name'])) ) {
+ db_query("DROP DATABASE %s", $data['site-db-name']);
+ }
+
+ db_query("CREATE DATABASE %s", $data['site-db-name']);
+
+ if ( !db_result(db_query("SHOW DATABASES LIKE '%s'", $data['site-db-name'])) ) {
+ provision_set_error(PROVISION_DB_ERROR);
+ provision_log("error", "Database could not be created.");
+
+ return FALSE;
+ }
+
+ db_query("GRANT ALL PRIVILEGES ON %s.* TO %s@`%%` IDENTIFIED BY '%s'", $data['site-db-name'], $data['site-db-username'], $data['mysql_passwd']);
+ db_query("GRANT ALL PRIVILEGES ON %s.* TO %s@%s IDENTIFIED BY '%s'",$data['site-db-name'], $data['site-db-username'], $data['site-db-host'], $data['mysql_passwd']);
+
+
+ if ($data['site-mysql-old-passwords']) {
+ db_query("SET PASSWORD FOR '%s'@'%%' = OLD_PASSWORD('%s')", $data['site-db-username'], $data['site-db-passwd']);
+ db_query("SET PASSWORD FOR %s@%s = OLD_PASSWORD('%s')", $data['site-db-username'], $data['site-db-host'], $data['site-db-passwd']);
+ }
+ db_query("FLUSH PRIVILEGES");
+ #TODO : Test to confirm that the database is actually writeable. Taking this on faith for now.
+}
+