diff --git a/db/pgsql/pgsql_service.inc b/db/pgsql/pgsql_service.inc new file mode 100644 index 0000000..3885c51 --- /dev/null +++ b/db/pgsql/pgsql_service.inc @@ -0,0 +1,126 @@ +query("SELECT datname FROM pg_database WHERE datname = '%s'", $name); + if ($result) { + return $result->fetchColumn(0); + } + } + + + function drop_database($name) { + return $this->query("DROP DATABASE %s", $name); + } + + + function create_database($name) { + return $this->query("CREATE DATABASE %s OWNER DEFAULT", $name); + } + + function can_create_database() { + $test = drush_get_option('aegir_db_prefix', 'site_') .'test'; + $this->create_database($test); + + if ($this->database_exists($test)) { + if (!$this->drop_database($test)) { + drush_log(dt("Failed to drop database @dbname", array('@dbname' => $test)), 'warning'); + } + return TRUE; + } + return FALSE; + } + + + function grant($name, $username, $password, $host = '') { + //Does the username exist? + if (!$this->query("SELECT * FROM pg_user WHERE username = '%s'", $username)) { + $this->query("CREATE USER %s WITH PASSWORD '%s' CREATEDB", $username, $password); + } + //TODO: Do we need to change the database owner to the new user too? + return $this->query("GRANT ALL PRIVILEGES ON %s TO %s", $name, $username); + } + + function revoke($name, $username, $host = '') { + $success = $this->query("REVOKE ALL PRIVILEGES ON %s FROM %s", $name, $username); + $grant_found = FALSE; + + //TODO: Do we need to check to see if user is owner of databases or not before removing them? + $users = $this->query("SELECT * FROM pg_user WHERE username = '%s'", $username); + $user = $users->fetch(); + + if ($grant_found) { + $success = $this->query("DROP USER %s", $username) && $success; + } + return $success; + } + + + function import_dump($dump_file, $creds) { + extract($creds); + + $cmd = sprintf("pg_restore -U %s -p %s -h %s -d %s -f %s", escapeshellcmd(drush_get_option('db_user')), escapeshellcmd(drush_get_option('db_passwd')), escapeshellcmd(drush_get_option('db_host')), escapeshellcmd($db_name), escapeshellcmd($dump_file)); + + $success = $this->shell_exec($cmd); + + drush_log(sprintf("Importing database using command: %s", $cmd)); + + if (!$success) { + drush_set_error('PROVISION_DB_IMPORT_FAILED', dt("Database import failed: %output", array('%output' => $this->shell_exec_output))); + } + } + + function grant_host(provisionServer $server) { + $command = sprintf('psql -u intntnllyInvalid -h %s -P %s', + escapeshellarg($this->server->remote_host), + escapeshellarg($this->server->db_port)); + + + $server->shell_exec($command); + if (preg_match("/Access denied for user 'intntnllyInvalid'@'([^']*)'/", implode('', drush_shell_exec_output()), $match)) { + return $match[1]; + } + elseif (preg_match("/Host '([^']*)' is not allowed to connect to/", implode('', drush_shell_exec_output()), $match)) { + return $match[1]; + } + else { + return drush_set_error('PROVISION_DB_CONNECT_FAIL', dt('Dummy connection failed to fail: %msg', array('%msg' => join("\n", drush_shell_exec_output())))); + } + } + + function generate_dump() { + // Aet the umask to 077 so that the dump itself is generated so it's + // non-readable by the webserver. + umask(0077); + // Mixed copy-paste of drush_shell_exec and provision_shell_exec. + $cmd = sprintf('pg_dump -U %s -p %s -h %s -f %s/database.sql %s', escapeshellcmd(drush_get_option('db_user')), escapeshellcmd(drush_get_option('db_passwd')), escapeshellcmd(drush_get_option('db_host')), escapeshellcmd(d()->site_path), escapeshellcmd(drush_get_option('db_name'))); + $success = $this->shell_exec($cmd); + + if (!$success && !drush_get_option('force', false)) { + drush_set_error('PROVISION_BACKUP_FAILED', dt('Could not generate database backup from pg_dump. (error: %msg)', array('%msg' => $this->safe_shell_exec_output))); + } + // Reset the umask to normal permissions. + umask(0022); + } + + /** + * 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) + */ + function safe_shell_exec($cmd, $db_host, $db_user, $db_passwd, $dump_file = null) { + //TODO: Need to research this a bit more... Currently using shell_exec() in dump and restore + } +}