Skip to content

Commit

Permalink
Merge branch 'master' into remove/ssh_framework_check
Browse files Browse the repository at this point in the history
  • Loading branch information
TeslaDethray authored Jan 11, 2017
2 parents 88b59e1 + 44f2b73 commit a7cae0d
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 81 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#Change Log
# Change Log
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org)

## MASTER
### Added
- Added `--to=` option to `backup:get` to allow specifying of a local download location. (#1520)

## 1.0.0-beta.2 - 2017-01-10
### Fixed
- Fixed fatal error by adding back the use statement for ProcessUtils in SSHBaseCommand. (#1494)
Expand Down
26 changes: 19 additions & 7 deletions src/Commands/Backup/GetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Pantheon\Terminus\Commands\Backup;

use Pantheon\Terminus\Commands\TerminusCommand;
use Pantheon\Terminus\Request\RequestAwareInterface;
use Pantheon\Terminus\Request\RequestAwareTrait;
use Pantheon\Terminus\Site\SiteAwareInterface;
use Pantheon\Terminus\Site\SiteAwareTrait;
use Pantheon\Terminus\Exceptions\TerminusNotFoundException;
Expand All @@ -11,8 +13,9 @@
* Class GetCommand
* @package Pantheon\Terminus\Commands\Backup
*/
class GetCommand extends TerminusCommand implements SiteAwareInterface
class GetCommand extends TerminusCommand implements SiteAwareInterface, RequestAwareInterface
{
use RequestAwareTrait;
use SiteAwareTrait;

/**
Expand All @@ -25,16 +28,21 @@ class GetCommand extends TerminusCommand implements SiteAwareInterface
* @param string $site_env Site & environment in the format `site-name.env`
* @option string $file [filename.tgz] Name of backup file
* @option string $element [code|files|database|db] Backup element to retrieve
* @option string $to Local path to save to
* @throws TerminusNotFoundException
*
* @usage terminus backup:get <site>.<env>
* Displays the URL for the most recent backup of any type in <site>'s <env> environment.
* @usage terminus backup:get awesome-site.dev --file=2016-08-18T23-16-20_UTC_code.tar.gz
* Displays the URL for the backup with the specified file name in <site>'s <env> environment.
* @usage terminus backup:get awesome-site.dev --element=code
* Displays the URL for the most recent code backup in <site>'s <env> environment.
* @usage terminus backup:get <site>.<env> --file=<file_name>
* Displays the URL for the backup with the file name <file_name> in <site>'s <env> environment.
* @usage terminus backup:get <site>.<env> --element=<element>
* Displays the URL for the most recent <element> backup in <site>'s <env> environment.
* @usage terminus backup:get <site>.<env> --to=<path>
* Saves the most recent backup of any type in <site>'s <env> environment to <path>.
* @usage terminus backup:get <site>.<env> --element=<element> --to=<path>
* Saves the most recent <element> backup in <site>'s <env> environment to <path>.
*/
public function getBackup($site_env, array $options = ['file' => null, 'element' => null,])
public function getBackup($site_env, array $options = ['file' => null, 'element' => null, 'to' => null,])
{
list($site, $env) = $this->getSiteEnv($site_env);

Expand All @@ -52,6 +60,10 @@ public function getBackup($site_env, array $options = ['file' => null, 'element'
$backup = array_shift($backups);
}

return $backup->getUrl();
$backup_url = $backup->getUrl();
if (!isset($options['to']) || is_null($save_path = $options['to'])) {
return $backup_url;
}
$this->request()->download($backup_url, $save_path);
}
}
34 changes: 17 additions & 17 deletions src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
use GuzzleHttp\Psr7\Request as HttpRequest;
use League\Container\ContainerAwareInterface;
use League\Container\ContainerAwareTrait;
use Pantheon\Terminus\Exceptions\TerminusException;
use Pantheon\Terminus\Helpers\LocalMachineHelper;
use Pantheon\Terminus\Session\SessionAwareInterface;
use Pantheon\Terminus\Session\SessionAwareTrait;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Robo\Common\ConfigAwareTrait;
use Robo\Contract\ConfigAwareInterface;
use Pantheon\Terminus\Exceptions\TerminusException;

/**
* Class Request
Expand All @@ -27,33 +28,34 @@
*
* @package Pantheon\Terminus\Request
*/
class Request implements ConfigAwareInterface, SessionAwareInterface, LoggerAwareInterface, ContainerAwareInterface
class Request implements ConfigAwareInterface, ContainerAwareInterface, LoggerAwareInterface, SessionAwareInterface
{
use LoggerAwareTrait;
use ConfigAwareTrait;
use SessionAwareTrait;
use ContainerAwareTrait;
use LoggerAwareTrait;
use SessionAwareTrait;

const PAGED_REQUEST_ENTRY_LIMIT = 100;

/**
* Download file from target URL
*
* @param string $url URL to download from
* @param string $target Target file's name
* @return bool True if download succeeded
* @throws TerminusException
*/
public static function download($url, $target)
public function download($url, $target)
{
if (file_exists($target)) {
throw new TerminusException(
'Target file {target} already exists.',
compact('target')
);
if ($this->getContainer()->get(LocalMachineHelper::class)->getFilesystem()->exists($target)) {
throw new TerminusException('Target file {target} already exists.', compact('target'));
}

$client = new Client();
$parsed_url = parse_url($url);
$client = $this->getContainer()->get(Client::class, [[
'base_uri' => $parsed_url['host'],
RequestOptions::VERIFY => (boolean)$this->getConfig()->get('verify_host_cert', true),
],]);
$client->request('GET', $url, ['sink' => $target,]);
return true;
}

/**
Expand All @@ -63,14 +65,12 @@ public static function download($url, $target)
* @param array $options Options for the request
* string method GET is default
* array form_params Fed into the body of the request
* integer limit Max number of entries to return
* @return array
*/
public function pagedRequest($path, array $options = [])
{
$limit = 100;
if (isset($options['limit'])) {
$limit = $options['limit'];
}
$limit = isset($options['limit']) ? $options['limit'] : self::PAGED_REQUEST_ENTRY_LIMIT;

//$results is an associative array so we don't refetch
$results = [];
Expand Down
32 changes: 32 additions & 0 deletions tests/unit_tests/Commands/Backup/GetCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Pantheon\Terminus\Commands\Backup\GetCommand;
use Pantheon\Terminus\Exceptions\TerminusNotFoundException;
use Pantheon\Terminus\Request\Request;

/**
* Class GetCommandTest
Expand Down Expand Up @@ -109,4 +110,35 @@ public function testGetBackupNoBackups()
$out = $this->command->getBackup("$site.{$this->environment->id}", compact('element'));
$this->assertNull($out);
}

/**
* Tests the backup:get command when saving the backup to a file
*/
public function testGetBackupToFile()
{
$test_filename = 'test.tar.gz';
$test_download_url = 'http://download';
$test_save_path = '/tmp/file.tar.gz';
$request = $this->getMockBuilder(Request::class)
->disableOriginalConstructor()
->getMock();

$this->backups->expects($this->once())
->method('getBackupByFileName')
->with($test_filename)
->willReturn($this->backup);
$this->backup->expects($this->once())
->method('getUrl')
->willReturn($test_download_url);
$request->expects($this->once())
->method('download')
->with(
$this->equalTo($test_download_url),
$this->equalTo($test_save_path)
);

$this->command->setRequest($request);
$out = $this->command->getBackup('mysite.dev', ['file' => $test_filename, 'to' => $test_save_path,]);
$this->assertNull($out);
}
}
Loading

0 comments on commit a7cae0d

Please sign in to comment.