Commit ed589ed7 authored by Adrian Rossouw's avatar Adrian Rossouw

all the files moved from db_server

parent ceaf53d6
<?php
function drush_provision_mysql_pre_provision_backup($url = NULL) {
# set the umask to 077 so that the dump itself is generated so it's non-readable by the webserver
umask(0077);
drush_log("Generating mysql dump for $url.", 'backup');
# mixed copy-paste of drush_shell_exec and provision_shell_exec
$cmd = sprintf("mysqldump --defaults-file=/dev/fd/3 -rsites/%s/database.sql %s", escapeshellcmd($url), escapeshellcmd(drush_get_option('db_name')));
drush_log($cmd);
if (drush_get_context('DRUSH_VERBOSE') || drush_get_context('DRUSH_SIMULATE')) {
drush_print('Executing: ' . $cmd, $indent);
}
function drush_db_pre_provision_backup($url = NULL) {
provision_service('db')->dump_site_database($url);
if (drush_get_context('DRUSH_SIMULATE')) {
return true;
}
# pipe handling code
# we go through all this trouble to hide the password from the commandline, it's the most secure way (apart from writing a temporary file, which would create conflicts in parallel runs)
$mycnf = sprintf('[client]
host=%s
user=%s
password=%s
', drush_get_option('db_host'), urldecode(drush_get_option('db_user')), urldecode(drush_get_option('db_passwd')));
$descriptorspec = array(
// 0 => array("pipe", "r"), // this would be stdin, but we don't need to input into mysqldump
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w"), // stderr is a file to write to
3 => array("pipe", "r"), // fd3 is our special file descriptor where we pass credentials
);
$process = proc_open($cmd, $descriptorspec, $pipes);
$output = array();
if (is_resource($process)) {
fwrite($pipes[3], $mycnf);
fclose($pipes[3]);
$output = array_filter(array_merge(explode("\n", stream_get_contents($pipes[1])), explode("\n", stream_get_contents($pipes[2]))));
// "It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock"
fclose($pipes[1]);
fclose($pipes[2]);
$return_value = proc_close($process);
} else {
// XXX: failed to execute? unsure when this happens
$return_value = -1;
}
# resuming drush_exec copy/paste
$indent = 0;
_drush_shell_exec_output_set($output);
if (drush_get_context('DRUSH_VERBOSE')) {
foreach ($output as $line) {
drush_print($line, $indent + 2);
}
}
$result = ($return_value == 0);
if (!$result && !drush_get_option('force', false)) {
drush_set_error('PROVISION_BACKUP_FAILED', dt("Could not generate database backup from mysqldump"));
}
# reset the umask to normal permissions
umask(0022);
}
function drush_provision_mysql_pre_provision_backup_rollback($url = NULL) {
function drush_db_pre_provision_backup_rollback($url = NULL) {
provision_path("unlink", drush_get_option('sites_path') . "/$url/database.sql", TRUE, dt("Deleted mysql dump from sites directory"),
dt("Could not delete mysql dump from sites directory"));
}
function drush_provision_mysql_post_provision_backup($url = NULL) {
drush_provision_mysql_pre_provision_backup_rollback($url);
function drush_db_post_provision_backup($url = NULL) {
drush_db_pre_provision_backup_rollback($url);
}
<?php
// $Id$
function drush_db_provision_clone_validate($url = null) {
provision_service('db')->connect($url);
}
......@@ -11,6 +11,35 @@
function db_drush_init() {
include_once('mysql/mysql_service.inc');
$command = drush_get_command();
$command = explode(" ", $command['command']);
if (preg_match("/^provision-/", $command[0])) {
// TODO - remove this shit
$master_db = drush_get_option('master_db');
$db = parse_url($master_db);
drush_set_default('master_db_user', urldecode($db['user']));
drush_set_default('master_db_passwd', urldecode($db['pass']));
drush_set_default('master_db_host', urldecode($db['host']));
drush_set_default('db_host', urldecode($db['host']));
drush_set_default('master_db_type', $db['scheme']);
drush_set_default('db_type', $db['scheme']);
$creds = array();
$options = array(
'master_db_user' => 'user',
'master_db_passwd' => 'pass',
'master_db_host' => 'host',
'master_db_type' => 'type');
foreach ($options as $option => $key) {
$creds[$key] = drush_get_option($option, '');
}
provision_service('db', new provisionService_db_mysql($creds));
}
// this is where we generate the db service object.
}
......@@ -262,7 +291,8 @@ class provisionService_db_pdo extends provisionService_db {
function __construct($creds) {
parent::__construct($creds);
$this->dsn = sprintf("%s:dbname=%s;host=%s", $this->db_type, $this->creds['name'], $this->creds['host']);
$this->dsn = sprintf("%s:dbname=%s;host=%s", $this->PDO_type, $this->creds['name'], $this->creds['host']);
}
......@@ -292,7 +322,7 @@ class provisionService_db_pdo extends provisionService_db {
try {
$result = $this->conn->query($query);
}
catch (PDOException e) {
catch (PDOException $e) {
drush_log($e->getMessage(), 'warning');
return FALSE;
}
......
<?php
function drush_provision_mysql_provision_delete_validate() {
function drush_db_provision_delete_validate($url = null) {
if (PROVISION_CONTEXT_SITE) {
provision_db_connect();
provision_service('db')->connect();
}
}
......@@ -12,8 +12,8 @@ function drush_provision_mysql_provision_delete_validate() {
*
* This will drop the database, revoke the privileges and flush the privileges.
*/
function drush_provision_mysql_provision_delete($url = NULL) {
function drush_db_provision_delete($url = NULL) {
if (PROVISION_CONTEXT_SITE) {
return _provision_mysql_destroy_site_db(drush_get_option('db_name'), drush_get_option('db_user'), drush_get_option('db_passwd'));
provision_service('db')->destroy_site_db($url);
}
}
......@@ -4,8 +4,7 @@
// extends the pdo implementation
class provisionService_db_mysql extends provisionService_db_pdo {
static public $PDO_type = 'mysql';
function database_exists($name) {
......
<?php
function drush_provision_mysql_provision_restore_validate() {
provision_db_connect();
function drush_db_provision_restore_validate($url) {
provision_service('db')->connect($url);
}
function drush_provision_mysql_pre_provision_restore($url = NULL) {
$db_type = drush_set_option('db_type', drush_get_option('db_type'));
$db_host = drush_set_option('db_host', drush_get_option('db_host'));
$db_passwd = drush_set_option('db_passwd', provision_password());
$db_name = drush_set_option('db_name', _provision_mysql_suggest_db_name($url));
$db_user = drush_set_option('db_user', $db_name);
_provision_mysql_new_site_db($db_name, $db_user, $db_passwd);
function drush_db_pre_provision_restore($url = NULL) {
provision_service('db')->create_site_database($url);
}
function drush_provision_mysql_provision_restore($url) {
_provision_mysql_import_dump(
drush_get_option('sites_path') .'/'. $url .'/database.sql',
drush_get_option('db_name'), drush_get_option('db_user'),
drush_get_option('db_passwd'), drush_get_option('db_host')
);
function drush_db_provision_restore($url) {
provision_service('db')->import_site_database($url);
}
function drush_provision_mysql_pre_provision_restore_rollback($url = NULL) {
_provision_mysql_destroy_site_db(drush_get_option('db_name'), drush_get_option('db_user'), drush_get_option('db_passwd'));
function drush_db_pre_provision_restore_rollback($url = NULL) {
provision_service('db')->destroy_site_db($url);
$keys = array('db_name', 'db_passwd', 'db_user', 'db_host');
......@@ -39,21 +29,14 @@ function drush_provision_mysql_pre_provision_restore_rollback($url = NULL) {
// Rollback doesn't apply here yet. Unless we trigger a restore of the first dump
// made. Which could go on infinitely if something is really long.
function drush_provision_mysql_post_provision_restore($url = NULL) {
function drush_db_post_provision_restore($url = NULL) {
provision_path('unlink', drush_get_option('sites_path') . '/' . $url .'/database.sql', TRUE,
dt("Removed dump file @path after restoring from it"),
dt("Could not remove dump file @path"), 'DRUSH_PERM_ERROR');
$db_grant_host = _provision_mysql_grant_host(
drush_get_option('db_host', '', 'site'),
drush_get_option('web_ip', null, 'site'),
drush_get_option('web_host', null, 'site'));
// We have now completed successfully, remove the old database.
_provision_mysql_destroy_site_db(
drush_get_option('db_name', null, 'site'),
drush_get_option('db_user', null, 'site'),
drush_get_option('db_passwd', null, 'site'),
$db_grant_host);
provision_service('db')->destroy_site_database($url);
// TODO - test this .. it's probably horribly broken .. deleting the newly restored db instead of the old one.
// The new database credentials will be saved against the site now.
drush_set_option('db_name', drush_get_option('db_name'), 'site');
......
<?php
function drush_provision_mysql_provision_verify_validate() {
provision_db_connect();
provision_service('db')->connect();
}
/**
......@@ -11,7 +11,7 @@ function drush_provision_mysql_provision_verify_validate() {
*/
function drush_provision_mysql_provision_verify() {
if (PROVISION_CONTEXT_PLATFORM) {
if (!_provision_mysql_can_create_database()) {
if (!provision_service('db')->can_create_database()) {
drush_set_error('PROVISION_CREATE_DB_FAILED');
}
else {
......
<?php
// $Id$
function drush_provision_mysql_provision_clone_validate() {
provision_db_connect();
}
name = Provision: Mysql
description = Provides provisioning requirements for the Mysql database
package = Provision
dependencies[] = provision
core = 6.x
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment