Commit 925ed937 authored by Adrian Rossouw's avatar Adrian Rossouw
Browse files

tried to unfuck the crazy /dev/fd3 code .. probably broke a bunch of things

parent ed589ed7
......@@ -8,13 +8,13 @@ class provisionService_db_mysql extends provisionService_db_pdo {
function database_exists($name) {
$result = $this->query("SHOW DATABASES LIKE '%s'", $name);
$result = $this->query("SHOW DATABASES LIKE %s", $name);
return $result->fetchColumn(0);
}
function drop_database($name) {
return $this->query("DROP DATABASE `%s`", $name);
return $this->query("DROP DATABASE %s", $name);
}
......@@ -63,62 +63,21 @@ class provisionService_db_mysql extends provisionService_db_pdo {
}
function import_dump($dump_file, $creds) {
extract($creds);
$cmd = sprintf("mysql --defaults-file=/dev/fd/3 %s", escapeshellcmd($db_name));
drush_log(sprintf("Importing database using command: %s", $cmd));
# pipe handling code, this is inspired by drush_provision_mysql_pre_provision_backup()
# 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
', $db_host, $db_user, $db_passwd);
$descriptorspec = array(
0 => array("file", $dump_file, "r"),
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 = "";
if (is_resource($process)) {
fwrite($pipes[3], $mycnf);
fclose($pipes[3]);
$output = stream_get_contents($pipes[1]) . 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;
}
$this->safe_shell_exec($cmd, $db_host, $db_user, $db_passwd);
drush_log(sprintf("Importing database using command: %s", $cmd));
if ($return_value != 0) {
drush_set_error('PROVISION_DB_IMPORT_FAILED', dt("Database import failed: %output", array('%output' => $output)));
}
}
/**
* Properly guess the host part of the MySQL username based on a given
* database server host , web server IP and web server host.
*
* If the database and web server are the same machine, return a localhost
* grant, which is a special and very common case which does not involve any
* dns lookups and is thus faster and more secure.
*
* If the web server and database server are different machines, we prefer to
* use the IP address, which is faster and more secure because fewer lookups
* are done during connections.
*
*/
function grant_host($db_host, $web_ip, $web_host) {
$result = provision_db_result(provision_db_query("select current_user()"));
preg_match('/^.*@(.*)$/', $result, $matches);
......@@ -131,41 +90,42 @@ password=%s
# 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);
}
if (drush_get_context('DRUSH_SIMULATE')) {
return true;
$this->safe_shell_exec($cmd, drush_get_option('db_host'), urldecode(drush_get_option('db_user')), urldecode(drush_get_option('db_passwd')));
$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);
# pipe handling code
}
function safe_shell_exec($cmd, $db_host, $db_user, $db_passwd) {
# 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')));
$mycnf = sprintf('[client]
host=%s
user=%s
password=%s
', $db_host, $db_user, $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
0 => array("file", $dump_file, "r"),
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();
$output = "";
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]))));
$output = stream_get_contents($pipes[1]) . 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]);
......@@ -176,24 +136,7 @@ password=%s
$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);
return ($return_value == 0);
}
}
<?php
function drush_provision_mysql_provision_verify_validate() {
function drush_db_provision_verify_validate() {
provision_service('db')->connect();
}
......@@ -9,7 +9,7 @@ function drush_provision_mysql_provision_verify_validate() {
*
* Can't be rolled back.
*/
function drush_provision_mysql_provision_verify() {
function drush_db_provision_verify() {
if (PROVISION_CONTEXT_PLATFORM) {
if (!provision_service('db')->can_create_database()) {
drush_set_error('PROVISION_CREATE_DB_FAILED');
......@@ -20,7 +20,7 @@ function drush_provision_mysql_provision_verify() {
}
}
function drush_provision_mysql_post_provision_verify() {
function drush_db_post_provision_verify() {
if (PROVISION_CONTEXT_PLATFORM) {
drush_set_option('master_db', drush_get_option('master_db'), 'drupal');
}
......
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