From e6c25eb43300b4dd023752dbecdabebf5f60e6d4 Mon Sep 17 00:00:00 2001 From: tuutti Date: Wed, 25 Oct 2023 09:20:24 +0300 Subject: [PATCH 1/3] Use cron pod for database dumps --- src/Drush/Commands/OpenShiftDrushCommands.php | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/Drush/Commands/OpenShiftDrushCommands.php b/src/Drush/Commands/OpenShiftDrushCommands.php index 962ec3e..e68d694 100644 --- a/src/Drush/Commands/OpenShiftDrushCommands.php +++ b/src/Drush/Commands/OpenShiftDrushCommands.php @@ -4,6 +4,7 @@ namespace DrupalTools\Drush\Commands; +use Drush\Attributes\Command; use Drush\Commands\DrushCommands; use Symfony\Component\Process\Process; @@ -153,21 +154,24 @@ private function invokeOc( * The pod name. */ private function getDrupalPodName(array $items) : string { - $deploymentConfigName = getenv('OC_DEPLOYMENT_CONFIG_NAME') ?: 'drupal'; - - foreach ($items as $item) { - $labels = $item->metadata->labels ?? NULL; - - if ( - (!isset($labels->deploymentconfig)) || - $labels->deploymentconfig !== $deploymentConfigName - ) { - continue; - } - if ((!isset($item->status->phase)) || $item->status->phase !== 'Running') { - continue; + $deploymentConfigNames = [ + getenv('OC_DEPLOYMENT_CONFIG_NAME') ?: '', + 'drupal-cron', + 'drupal', + ]; + + foreach ($deploymentConfigNames as $name) { + foreach ($items as $item) { + if (!isset($item->metadata->labels->deploymentconfig)) { + continue; + } + if ((!isset($item->status->phase)) || $item->status->phase !== 'Running') { + continue; + } + if ($item->metadata->labels->deploymentconfig === $name) { + return $item->metadata->name; + } } - return $item->metadata->name; } throw new \InvalidArgumentException(dt('No running pod found.')); } @@ -175,11 +179,10 @@ private function getDrupalPodName(array $items) : string { /** * Gets the database dump. * - * @command helfi:oc:get-dump - * * @return int * The exit code. */ + #[Command(name: 'helfi:oc:get-dump')] public function getDatabaseDump() : int { $this->invokeOc(['get', 'pods', '-o', 'json'], callback: function ($output) { $data = json_decode($output); @@ -191,6 +194,7 @@ public function getDatabaseDump() : int { 'drush', 'sql:dump', '--structure-tables-key=common', + '--no-tablespaces', '--result-file=/tmp/dump.sql', ]); $this->invokeOc([ @@ -205,11 +209,10 @@ public function getDatabaseDump() : int { /** * Sanitizes the current database. * - * @command helfi:oc:sanitize-database - * * @return int * The exit code. */ + #[Command(name: 'helfi:oc:sanitize-database')] public function sanitizeDatabase() : int { $process = $this->processManager()->process([ 'drush', @@ -228,11 +231,10 @@ public function sanitizeDatabase() : int { * @param string $token * The token. * - * @command helfi:oc:login - * * @return int * The exit code. */ + #[Command(name: 'helfi:oc:login')] public function login(string $token) : int { $this->ensureLoginDetails($token); @@ -242,16 +244,15 @@ public function login(string $token) : int { /** * Checks whether user is logged in or not. * - * @command helfi:oc:whoami - * * @return int * The exit code. */ + #[Command(name: 'helfi:oc:whoami')] public function whoami() : int { try { $this->invokeOc(['whoami']); } - catch (\Exception $e) { + catch (\Exception) { return self::EXIT_FAILURE; } return self::EXIT_SUCCESS; From 96d30bcd6d25a3efd321fe632f5c4286a6b23e18 Mon Sep 17 00:00:00 2001 From: tuutti Date: Wed, 25 Oct 2023 13:37:44 +0300 Subject: [PATCH 2/3] Fixed tablespaces --- src/Drush/Commands/OpenShiftDrushCommands.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Drush/Commands/OpenShiftDrushCommands.php b/src/Drush/Commands/OpenShiftDrushCommands.php index e68d694..9815b93 100644 --- a/src/Drush/Commands/OpenShiftDrushCommands.php +++ b/src/Drush/Commands/OpenShiftDrushCommands.php @@ -194,7 +194,7 @@ public function getDatabaseDump() : int { 'drush', 'sql:dump', '--structure-tables-key=common', - '--no-tablespaces', + '--extra-dump="--no-tablespaces"', '--result-file=/tmp/dump.sql', ]); $this->invokeOc([ From 35ce1f186fa157c14e33b8e85b1e37223374fe0f Mon Sep 17 00:00:00 2001 From: tuutti Date: Thu, 26 Oct 2023 10:14:35 +0300 Subject: [PATCH 3/3] Delete old dump, --hex-blob --- src/Drush/Commands/OpenShiftDrushCommands.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Drush/Commands/OpenShiftDrushCommands.php b/src/Drush/Commands/OpenShiftDrushCommands.php index 9815b93..c1c2bd1 100644 --- a/src/Drush/Commands/OpenShiftDrushCommands.php +++ b/src/Drush/Commands/OpenShiftDrushCommands.php @@ -188,13 +188,20 @@ public function getDatabaseDump() : int { $data = json_decode($output); $pod = $this->getDrupalPodName($data->items); + $this->invokeOc([ + 'rsh', + $pod, + 'rm', + '-f', + '/tmp/dump.sql', + ]); $this->invokeOc([ 'rsh', $pod, 'drush', 'sql:dump', '--structure-tables-key=common', - '--extra-dump="--no-tablespaces"', + '--extra-dump=--no-tablespaces --hex-blob', '--result-file=/tmp/dump.sql', ]); $this->invokeOc([