Skip to content

Commit

Permalink
Adjusts log command to match db backups.
Browse files Browse the repository at this point in the history
  • Loading branch information
typhonius committed Mar 21, 2020
1 parent 7ac8f81 commit 7b0db5d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 29 deletions.
12 changes: 4 additions & 8 deletions src/Cli/AcquiaCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

use Robo\Robo;
use Robo\Common\ConfigAwareTrait;
use Symfony\Component\Lock\Factory;
use Symfony\Component\Lock\Store\SemaphoreStore;
use Symfony\Component\Console\Command\LockableTrait;
use Robo\Runner as RoboRunner;
use Robo\Application;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -26,6 +25,7 @@ class AcquiaCli
{

use ConfigAwareTrait;
use LockableTrait;

private $runner;

Expand Down Expand Up @@ -168,11 +168,7 @@ public function injectParameters($container)
public function run(InputInterface $input, OutputInterface $output)
{
// Obtain a lock and exit if the command is already running.
$store = new SemaphoreStore();
$factory = new Factory($store);
$lock = $factory->createLock('acquia-cli-command');

if (!$lock->acquire()) {
if (!$this->lock('acquia-cli-command')) {
$output->writeln('The command is already running in another process.');

return 0;
Expand All @@ -181,7 +177,7 @@ public function run(InputInterface $input, OutputInterface $output)
$statusCode = $this->runner->run($input, $output);

// Specifically release the lock after successful command invocation.
$lock->release();
$this->release();

return $statusCode;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Commands/AcquiaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
*/
abstract class AcquiaCommand extends Tasks
{
// @TODO https://github.com/boedah/robo-drush/issues/18
//use \Boedah\Robo\Task\Drush\loadTasks;

/**
* @var \AcquiaCli\Cli\CloudApi $cloudapiService
Expand Down Expand Up @@ -103,8 +101,9 @@ public function getApplications()
protected function confirm($question, $default = false)
{
if ($this->input()->getOption('yes')) {
// @TODO add this back in later.
// $this->say('Ignoring confirmation question as --yes option passed.');
if ($this->output->isVerbose()) {
$this->say('Ignoring confirmation question as --yes option passed.');
}

return true;
}
Expand Down Expand Up @@ -172,8 +171,9 @@ public function initUuidHook(InputInterface $input, AnnotationData $annotationDa
protected function waitForNotification($response)
{
if ($this->input()->getOption('no-wait')) {
// @TODO put this back in later.
// $this->say('Skipping wait for notification.');
if ($this->output->isVerbose()) {
$this->say('Skipping wait for notification.');
}
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/DbBackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function dbBackupLink($uuid, $environment, $dbName, $backupId)
* @aliases db:backup:download
* @option $backup Select which backup to download by backup ID. If omitted, the latest will be downloaded.
* @option $path Select a path to download the backup to. If omitted, the system temp directory will be used.
* @option $filename Choose a filename to call the backup. If omitted, the name will be automatically generated.
* @option $filename Choose a filename for the backup. If omitted, the name will be automatically generated.
*/
public function dbBackupDownload(
Client $client,
Expand Down
39 changes: 26 additions & 13 deletions src/Commands/LogsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,40 @@ public function logSnapshot(CloudApi $cloudapi, Logs $logsAdapter, $uuid, $envir
* @param string $uuid
* @param string $environment
* @param string $logType
* @param string $path
*
* @command log:download
* @option $path Select a path to download the log to. If omitted, the system temp directory will be used.
* @option $filename Choose a filename for the dowloaded log. If omitted, the name will be automatically generated.
*/
public function logDownload(CloudApi $cloudapi, Logs $logsAdapter, $uuid, $environment, $logType, $path = null)
{
public function logDownload(
CloudApi $cloudapi,
Logs $logsAdapter,
$uuid,
$environment,
$logType,
$opts = ['path' => null, 'filename' => null]
) {
$environment = $cloudapi->getEnvironment($uuid, $environment);

$label = $environment->label;
$envName = $environment->name;
$backupName = "${envName}-${logType}";

$log = $logsAdapter->download($environment->uuid, $logType);

if (null === $path) {
$location = tempnam(sys_get_temp_dir(), sprintf('%s-', $backupName)) . '.tar.gz';
if (null === $opts['filename']) {
$backupName = sprintf('%s-%s', $environment->name, $logType);
} else {
$backupName = $opts['filename'];
}

if (null === $opts['path']) {
$tmpLocation = tempnam(sys_get_temp_dir(), $backupName);
$location = sprintf('%s.tar.gz', $tmpLocation);
if (is_string($tmpLocation)) {
rename($tmpLocation, $location);
} else {
throw new \Exception('Unable to make temporary file.');
}
} else {
// @TODO do we want to put in a tempnam here or allow
// for full definition of the path?
$location = $path . $backupName . ".tar.gz";
$location = sprintf("%s/%s.tar.gz", $opts['path'], $backupName);
}

if (file_put_contents($location, $log, LOCK_EX)) {
$this->say(sprintf('Log downloaded to %s', $location));
} else {
Expand Down
28 changes: 27 additions & 1 deletion tests/Commands/LogsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function testDownloadLogsCommands()
$actualResponse = $this->execute($command);

$this->assertEquals(
preg_match('@> Log downloaded to ((\S+)dev-apache-access-(\w+).tar.gz)@', $actualResponse, $matches),
preg_match('@> Log downloaded to ((\S+)dev-apache-access(\w+).tar.gz)@', $actualResponse, $matches),
1
);

Expand All @@ -32,6 +32,32 @@ public function testDownloadLogsCommands()
}
}

public function testDownloadLogsCommandsWithOptions()
{
$command = [
'log:download',
'devcloud:devcloud2',
'dev',
'apache-access',
'--filename=bar',
'--path=/tmp'
];

$actualResponse = $this->execute($command);

$this->assertEquals(
preg_match(
'@> Log downloaded to ((/tmp/)bar.tar.gz)@',
$actualResponse,
$matches
),
1
);

$this->assertStringStartsWith('> Log downloaded to ', $actualResponse);
$this->assertStringContainsString('/tmp/', $matches[2]);
}

public function testLogstream()
{

Expand Down

0 comments on commit 7b0db5d

Please sign in to comment.