Skip to content

Commit

Permalink
Merge pull request #16 from City-of-Helsinki/UHF-9130
Browse files Browse the repository at this point in the history
UHF-9130: Make fresh fixes
  • Loading branch information
tuutti authored Oct 27, 2023
2 parents 3f74602 + 35ce1f1 commit f063599
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions src/Drush/Commands/OpenShiftDrushCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DrupalTools\Drush\Commands;

use Drush\Attributes\Command;
use Drush\Commands\DrushCommands;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -153,44 +154,54 @@ 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.'));
}

/**
* 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);
$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 --hex-blob',
'--result-file=/tmp/dump.sql',
]);
$this->invokeOc([
Expand All @@ -205,11 +216,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',
Expand All @@ -228,11 +238,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);

Expand All @@ -242,16 +251,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;
Expand Down

0 comments on commit f063599

Please sign in to comment.