Skip to content

Commit

Permalink
CLI-1330: TypeError for SSH commands on Node environments
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Apr 25, 2024
1 parent 6ced4f9 commit 92daf9a
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 42 deletions.
16 changes: 14 additions & 2 deletions src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ protected function isAcsfEnv(mixed $cloudEnvironment): bool {
protected function getAcsfSites(EnvironmentResponse $cloudEnvironment): array {

Check warning on line 1333 in src/Command/CommandBase.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "ProtectedVisibility": --- Original +++ New @@ @@ /** * @return array<mixed> */ - protected function getAcsfSites(EnvironmentResponse $cloudEnvironment) : array + private function getAcsfSites(EnvironmentResponse $cloudEnvironment) : array { $envAlias = self::getEnvironmentAlias($cloudEnvironment); $command = ['cat', "/var/www/site-php/{$envAlias}/multisite-config.json"];
$envAlias = self::getEnvironmentAlias($cloudEnvironment);
$command = ['cat', "/var/www/site-php/$envAlias/multisite-config.json"];
$process = $this->sshHelper->executeCommand($cloudEnvironment, $command, FALSE);
$process = $this->sshHelper->executeCommand($cloudEnvironment->sshUrl, $command, FALSE);
if ($process->isSuccessful()) {
return json_decode($process->getOutput(), TRUE, 512, JSON_THROW_ON_ERROR);
}
Expand All @@ -1346,7 +1346,7 @@ protected function getAcsfSites(EnvironmentResponse $cloudEnvironment): array {
private function getCloudSites(EnvironmentResponse $cloudEnvironment): array {
$sitegroup = self::getSitegroup($cloudEnvironment);
$command = ['ls', $this->getCloudSitesPath($cloudEnvironment, $sitegroup)];
$process = $this->sshHelper->executeCommand($cloudEnvironment, $command, FALSE);
$process = $this->sshHelper->executeCommand($cloudEnvironment->sshUrl, $command, FALSE);
$sites = array_filter(explode("\n", trim($process->getOutput())));
if ($process->isSuccessful() && $sites) {
return $sites;
Expand Down Expand Up @@ -1843,4 +1843,16 @@ protected function validatePhpVersion(string $version): string {
return $version;
}

protected function promptChooseDrupalSite(EnvironmentResponse $environment): string {
if ($this->isAcsfEnv($environment)) {
return $this->promptChooseAcsfSite($environment);
}

if ($environment->type === 'drupal') {
return $this->promptChooseCloudSite($environment);
}

throw new AcquiaCliException('Environment type {type} is not supported', ['type' => $environment->type]);
}

}
12 changes: 3 additions & 9 deletions src/Command/Pull/PullCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ private function importDatabaseDump(string $localDumpFilepath, string $dbHost, s
}
}

private function determineSite(string|EnvironmentResponse|array $environment, InputInterface $input): mixed {
private function determineSite(string|EnvironmentResponse|array $environment, InputInterface $input): string {
if (isset($this->site)) {
return $this->site;
}
Expand All @@ -423,15 +423,9 @@ private function determineSite(string|EnvironmentResponse|array $environment, In
return $input->getArgument('site');
}

if ($this->isAcsfEnv($environment)) {
$site = $this->promptChooseAcsfSite($environment);
}
else {
$site = $this->promptChooseCloudSite($environment);
}
$this->site = $site;
$this->site = $this->promptChooseDrupalSite($environment);

return $site;
return $this->site;
}

private function rsyncFilesFromCloud(EnvironmentResponse $chosenEnvironment, Closure $outputCallback, string $site): void {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Push/PushDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private function uploadDatabaseDump(
private function importDatabaseDumpOnRemote(EnvironmentResponse $environment, string $remoteDumpFilepath, DatabaseResponse $database): void {
$this->logger->debug("Importing $remoteDumpFilepath to MySQL on remote machine");
$command = "pv $remoteDumpFilepath --bytes --rate | gunzip | MYSQL_PWD={$database->password} mysql --host={$this->getHostFromDatabaseResponse($environment, $database)} --user={$database->user_name} {$this->getNameFromDatabaseResponse($database)}";
$process = $this->sshHelper->executeCommand($environment, [$command], ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL));
$process = $this->sshHelper->executeCommand($environment->sshUrl, [$command], ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL));

Check warning on line 86 in src/Command/Push/PushDatabaseCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "GreaterThan": --- Original +++ New @@ @@ { $this->logger->debug("Importing {$remoteDumpFilepath} to MySQL on remote machine"); $command = "pv {$remoteDumpFilepath} --bytes --rate | gunzip | MYSQL_PWD={$database->password} mysql --host={$this->getHostFromDatabaseResponse($environment, $database)} --user={$database->user_name} {$this->getNameFromDatabaseResponse($database)}"; - $process = $this->sshHelper->executeCommand($environment->sshUrl, [$command], $this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL); + $process = $this->sshHelper->executeCommand($environment->sshUrl, [$command], $this->output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL); if (!$process->isSuccessful()) { throw new AcquiaCliException('Unable to import database on remote machine. {message}', ['message' => $process->getErrorOutput()]); }
if (!$process->isSuccessful()) {
throw new AcquiaCliException('Unable to import database on remote machine. {message}', ['message' => $process->getErrorOutput()]);
}
Expand Down
7 changes: 1 addition & 6 deletions src/Command/Push/PushFilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$destinationEnvironment = $this->determineEnvironment($input, $output);
$chosenSite = $input->getArgument('site');
if (!$chosenSite) {
if ($this->isAcsfEnv($destinationEnvironment)) {
$chosenSite = $this->promptChooseAcsfSite($destinationEnvironment);
}
else {
$chosenSite = $this->promptChooseCloudSite($destinationEnvironment);
}
$chosenSite = $this->promptChooseDrupalSite($destinationEnvironment);
}
$answer = $this->io->confirm("Overwrite the public files directory on <bg=cyan;options=bold>$destinationEnvironment->name</> with a copy of the files from the current machine?");
if (!$answer) {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Remote/DrushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
implode(' ', $drushArguments),
];

return $this->sshHelper->executeCommand($environment, $drushCommandArguments)->getExitCode();
return $this->sshHelper->executeCommand($environment->sshUrl, $drushCommandArguments)->getExitCode();
}

}
2 changes: 1 addition & 1 deletion src/Command/Remote/SshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$sshCommand[] = implode(' ', $arguments['ssh_command']);
}
$sshCommand = (array) implode('; ', $sshCommand);
return $this->sshHelper->executeCommand($environment, $sshCommand)->getExitCode();
return $this->sshHelper->executeCommand($environment->sshUrl, $sshCommand)->getExitCode();
}

}
4 changes: 2 additions & 2 deletions src/Command/Ssh/SshKeyCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ private function checkPermissions(array $userPerms, string $cloudAppUuid, Output
break;
case 'add ssh key to non-prod':
if ($nonProdEnv = $this->getAnyNonProdAhEnvironment($cloudAppUuid)) {
$mappings['nonprod']['ssh_target'] = $nonProdEnv;
$mappings['nonprod']['ssh_target'] = $nonProdEnv->sshUrl;
}
break;
case 'add ssh key to prod':
if ($prodEnv = $this->getAnyProdAhEnvironment($cloudAppUuid)) {
$mappings['prod']['ssh_target'] = $prodEnv;
$mappings['prod']['ssh_target'] = $prodEnv->sshUrl;
}
break;
}
Expand Down
13 changes: 4 additions & 9 deletions src/Helpers/SshHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Acquia\Cli\Helpers;

use Acquia\Cli\Exception\AcquiaCliException;
use AcquiaCloudApi\Response\EnvironmentResponse;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
Expand All @@ -32,20 +31,16 @@ public function __construct(
*
* @param int|null $timeout
*/
public function executeCommand(EnvironmentResponse|string $target, array $commandArgs, bool $printOutput = TRUE, int $timeout = NULL): Process {
public function executeCommand(string $sshUrl, array $commandArgs, bool $printOutput = TRUE, int $timeout = NULL): Process {
$commandSummary = $this->getCommandSummary($commandArgs);

if (is_a($target, EnvironmentResponse::class)) {
$target = $target->sshUrl;
}

// Remove site_env arg.
unset($commandArgs['alias']);
$process = $this->sendCommand($target, $commandArgs, $printOutput, $timeout);
$process = $this->sendCommand($sshUrl, $commandArgs, $printOutput, $timeout);

$this->logger->debug('Command: {command} [Exit: {exit}]', [
'command' => $commandSummary,
'env' => $target,
'env' => $sshUrl,
'exit' => $process->getExitCode(),
]);

Expand All @@ -56,7 +51,7 @@ public function executeCommand(EnvironmentResponse|string $target, array $comman
return $process;
}

private function sendCommand(?string $url, array $command, bool $printOutput, ?int $timeout = NULL): Process {
private function sendCommand(string $url, array $command, bool $printOutput, ?int $timeout = NULL): Process {
$command = array_values($this->getSshCommand($url, $command));
$this->localMachineHelper->checkRequiredBinariesExist(['ssh']);

Expand Down
9 changes: 4 additions & 5 deletions tests/phpunit/src/CommandTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Acquia\Cli\Helpers\LocalMachineHelper;
use Acquia\Cli\Helpers\SshHelper;
use AcquiaCloudApi\Response\DatabaseResponse;
use AcquiaCloudApi\Response\EnvironmentResponse;
use Exception;
use Gitlab\Api\Projects;
use Gitlab\Api\Users;
Expand Down Expand Up @@ -264,7 +263,7 @@ protected function mockGetAcsfSites(mixed $sshHelper): array {
$multisiteConfig = file_get_contents(Path::join($this->realFixtureDir, '/multisite-config.json'));
$acsfMultisiteFetchProcess->getOutput()->willReturn($multisiteConfig)->shouldBeCalled();
$sshHelper->executeCommand(
Argument::type('object'),
Argument::type('string'),
['cat', '/var/www/site-php/profserv2.01dev/multisite-config.json'],
FALSE
)->willReturn($acsfMultisiteFetchProcess->reveal())->shouldBeCalled();
Expand All @@ -277,7 +276,7 @@ protected function mockGetCloudSites(mixed $sshHelper, mixed $environment): void
$parts = explode('.', $environment->ssh_url);
$sitegroup = reset($parts);
$sshHelper->executeCommand(
Argument::type('object'),
Argument::type('string'),
['ls', "/mnt/files/$sitegroup.{$environment->name}/sites"],
FALSE
)->willReturn($cloudMultisiteFetchProcess->reveal())->shouldBeCalled();
Expand Down Expand Up @@ -434,11 +433,11 @@ protected function mockPollCloudViaSsh(object $environmentsResponse): ObjectProp
->willReturn($gitProcess->reveal())
->shouldBeCalled();
// Mock non-prod.
$sshHelper->executeCommand(new EnvironmentResponse($environmentsResponse->_embedded->items[0]), ['ls'], FALSE)
$sshHelper->executeCommand($environmentsResponse->_embedded->items[0]->ssh_url, ['ls'], FALSE)
->willReturn($process->reveal())
->shouldBeCalled();
// Mock prod.
$sshHelper->executeCommand(new EnvironmentResponse($environmentsResponse->_embedded->items[1]), ['ls'], FALSE)
$sshHelper->executeCommand($environmentsResponse->_embedded->items[1]->ssh_url, ['ls'], FALSE)
->willReturn($process->reveal())
->shouldBeCalled();
return $sshHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ protected function createCommand(): CommandBase {
}

public function testCreate(): void {
parent::runTestCreate();
$this->runTestCreate();
}

/**
* @group brokenProphecy
*/
public function testSshKeyAlreadyUploaded(): void {
parent::runTestSshKeyAlreadyUploaded();
$this->runTestSshKeyAlreadyUploaded();
}

}
2 changes: 1 addition & 1 deletion tests/phpunit/src/Commands/Pull/PullCommandTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ protected function mockSettingsFiles(ObjectProphecy $fs): void {
protected function mockListSites(SshHelper|ObjectProphecy $sshHelper): void {
$process = $this->mockProcess();
$process->getOutput()->willReturn('default')->shouldBeCalled();
$sshHelper->executeCommand(Argument::type('object'), ['ls', '/mnt/files/site.dev/sites'], FALSE)
$sshHelper->executeCommand(Argument::type('string'), ['ls', '/mnt/files/site.dev/sites'], FALSE)
->willReturn($process->reveal())->shouldBeCalled();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testPullProdDatabase(): void {
$sshHelper = $this->mockSshHelper();
$process = $this->mockProcess();
$process->getOutput()->willReturn('default')->shouldBeCalled();
$sshHelper->executeCommand(Argument::type('object'), ['ls', '/mnt/files/site.prod/sites'], FALSE)
$sshHelper->executeCommand(Argument::type('string'), ['ls', '/mnt/files/site.prod/sites'], FALSE)
->willReturn($process->reveal())->shouldBeCalled();
$this->mockGetBackup($environment);
$this->mockExecuteMySqlListTables($localMachineHelper, 'drupal');
Expand Down
3 changes: 1 addition & 2 deletions tests/phpunit/src/Commands/Push/PushDatabaseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Command\Push\PushDatabaseCommand;
use Acquia\Cli\Tests\CommandTestBase;
use AcquiaCloudApi\Response\EnvironmentResponse;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;

Expand Down Expand Up @@ -98,7 +97,7 @@ protected function mockImportDatabaseDumpOnRemote(
mixed $process
): void {
$sshHelper->executeCommand(
new EnvironmentResponse($environmentsResponse),
$environmentsResponse->ssh_url,
['pv /mnt/tmp/profserv2.01dev/acli-mysql-dump-drupal.sql.gz --bytes --rate | gunzip | MYSQL_PWD=password mysql --host=fsdb-74.enterprise-g1.hosting.acquia.com.enterprise-g1.hosting.acquia.com --user=s164 profserv2db14390'],
TRUE,
NULL
Expand Down

0 comments on commit 92daf9a

Please sign in to comment.