Skip to content

Commit

Permalink
CLI-520: [pull:files] not enough free disk space (#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Nov 4, 2021
1 parent 01f1ae1 commit d2733cc
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/Command/Pull/PullCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,13 +631,10 @@ protected function rsyncFilesFromCloud($chosen_environment, Closure $output_call
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
private function checkDiskSpace($environment, $destination_directory, $source_directory): void {
$process = $this->localMachineHelper->execute(['du', '-s', $destination_directory], NULL, NULL, FALSE);
$local_size = explode("\t", $process->getOutput())[0];
$process = $this->sshHelper->executeCommand($environment, ['du', '-s', $source_directory], FALSE);
$remote_size = explode("\t", $process->getOutput())[0];
$local_size = $this->localMachineHelper->getDirectoryUsage($destination_directory);
$remote_size = $this->sshHelper->getDirectoryUsage($environment, $source_directory);
$delta = $remote_size - $local_size;
$process = $this->localMachineHelper->execute(['df', '--output=avail', '-k', $destination_directory], NULL, NULL, FALSE);
$local_free_space = explode("\n", $process->getOutput())[1];
$local_free_space = $this->localMachineHelper->getFreeSpace($destination_directory);
// Apply a 10% safety margin.
if ($delta * 1.1 > $local_free_space) {
throw new AcquiaCliException('Not enough free space to pull files from the {environment} environment.
Expand Down
39 changes: 39 additions & 0 deletions src/Helpers/LocalMachineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,43 @@ public function startBrowser($uri = NULL, $browser = NULL): bool {
return FALSE;
}

/**
* @param $directory
*
* @return int
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
public function getFreeSpace($directory): int {
$this->getFilesystem()->mkdir($directory);
$process = $this->execute([
'df',
'--output=avail',
'-k',
$directory
], NULL, NULL, FALSE);
if (!$process->isSuccessful()) {
throw new AcquiaCliException('Failed to get local free space: {error}', ['error' => $process->getErrorOutput()]);
}
return explode("\n", $process->getOutput())[1];
}

/**
* @param $directory
*
* @return int
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
public function getDirectoryUsage($directory): int {
$this->getFilesystem()->mkdir($directory);
$process = $this->execute([
'du',
'-s',
$directory
], NULL, NULL, FALSE);
if (!$process->isSuccessful()) {
throw new AcquiaCliException('Failed to get usage of local files directory: {error}', ['error' => $process->getErrorOutput()]);
}
return explode("\t", $process->getOutput())[0];
}

}
19 changes: 19 additions & 0 deletions src/Helpers/SshHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ public function executeCommand($environment, array $command_args, $print_output
return $process;
}

/**
* @param $environment
* @param $directory
*
* @return int
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
public function getDirectoryUsage($environment, $directory): int {
$process = $this->executeCommand($environment, [
'du',
'-s',
$directory
], FALSE);
if (!$process->isSuccessful()) {
throw new AcquiaCliException('Failed to get usage of remote files directory: {error}', ['error' => $process->getErrorOutput()]);
}
return explode("\t", $process->getOutput())[0];
}

/**
* Sends a command to an environment via SSH.
*
Expand Down
3 changes: 3 additions & 0 deletions tests/phpunit/src/CommandTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ protected function mockLocalMachineHelper(): ObjectProphecy {
$local_machine_helper = $this->prophet->prophesize(LocalMachineHelper::class);
$local_machine_helper->useTty()->willReturn(FALSE);
$local_machine_helper->getLocalFilepath(Path::join($this->dataDir, 'acquia-cli.json'))->willReturn(Path::join($this->dataDir, 'acquia-cli.json'));
$local_machine_helper->getFreeSpace(Argument::any())->willReturn(12345);
$local_machine_helper->getDirectoryUsage(Argument::any())->willReturn(123);

return $local_machine_helper;
}
Expand All @@ -214,6 +216,7 @@ protected function mockLocalMachineHelper(): ObjectProphecy {
*/
protected function mockSshHelper(): ObjectProphecy {
$ssh_helper = $this->prophet->prophesize(SshHelper::class);
$ssh_helper->getDirectoryUsage(Argument::any(), Argument::any())->willReturn(123);
return $ssh_helper;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ protected function mockExecuteRsync(
string $source_dir,
string $destination_dir
): void {
// @todo restore these methods
// @see https://github.com/acquia/cli/issues/714
/**
$process = $this->mockProcess();
$process->getOutput()->willReturn("123\tfiles")->shouldBeCalled();
$local_machine_helper->execute(['du', '-s', $destination_dir . 'files'], NULL, NULL, FALSE)
Expand All @@ -145,8 +148,10 @@ protected function mockExecuteRsync(
$process->getOutput()->willReturn("\tAvail\n12345")->shouldBeCalled();
$local_machine_helper->execute(['df', '--output=avail', '-k', $destination_dir . 'files'], NULL, NULL, FALSE)
->willReturn($process->reveal())->shouldBeCalled();
**/

$local_machine_helper->checkRequiredBinariesExist(['rsync'])->shouldBeCalled();
$process = $this->mockProcess();
$command = [
'rsync',
'-rltDvPhe',
Expand Down

0 comments on commit d2733cc

Please sign in to comment.