Skip to content

Commit

Permalink
Merge pull request #337 from pjcdawkins/rename-backup-snapshot
Browse files Browse the repository at this point in the history
Rename 'backup' to 'snapshot'
  • Loading branch information
pjcdawkins committed Sep 5, 2015
2 parents 8454db3 + 039dac0 commit 5c566cf
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 149 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ domain
domain:list (domains) Get a list of all domains
environment
environment:activate Activate an environment
environment:backup Make a backup (snapshot) of an environment
environment:branch (branch) Branch an environment
environment:checkout (checkout) Check out an environment
environment:delete Delete an environment
Expand All @@ -112,7 +111,6 @@ environment
environment:merge (merge) Merge an environment
environment:metadata Read or set metadata for an environment
environment:relationships (relationships) List an environment's relationships
environment:restore Restore an environment backup
environment:routes (routes) List an environment's routes
environment:set-remote Set the remote environment to track for a branch
environment:sql (sql) Run SQL on the remote database
Expand All @@ -134,6 +132,10 @@ project
project:get (get) Clone and build a project locally
project:list (projects) Get a list of all active projects
project:metadata Read or set metadata for a project
snapshot
snapshot:create Make a snapshot of an environment
snapshot:list (snapshots) List available snapshots of an environment
snapshot:restore Restore an environment snapshot
ssh-key
ssh-key:add Add a new SSH key
ssh-key:delete Delete an SSH key
Expand Down
5 changes: 3 additions & 2 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ protected function getCommands()
$commands[] = new Command\Domain\DomainDeleteCommand();
$commands[] = new Command\Domain\DomainListCommand();
$commands[] = new Command\Environment\EnvironmentActivateCommand();
$commands[] = new Command\Environment\EnvironmentBackupCommand();
$commands[] = new Command\Environment\EnvironmentBranchCommand();
$commands[] = new Command\Environment\EnvironmentCheckoutCommand();
$commands[] = new Command\Environment\EnvironmentDeleteCommand();
Expand All @@ -116,7 +115,6 @@ protected function getCommands()
$commands[] = new Command\Environment\EnvironmentMetadataCommand();
$commands[] = new Command\Environment\EnvironmentMergeCommand();
$commands[] = new Command\Environment\EnvironmentRelationshipsCommand();
$commands[] = new Command\Environment\EnvironmentRestoreCommand();
$commands[] = new Command\Environment\EnvironmentRoutesCommand();
$commands[] = new Command\Environment\EnvironmentSshCommand();
$commands[] = new Command\Environment\EnvironmentSqlCommand();
Expand All @@ -138,6 +136,9 @@ protected function getCommands()
$commands[] = new Command\Project\ProjectMetadataCommand();
$commands[] = new Command\Self\SelfBuildCommand();
$commands[] = new Command\Self\SelfUpdateCommand();
$commands[] = new Command\Snapshot\SnapshotCreateCommand();
$commands[] = new Command\Snapshot\SnapshotListCommand();
$commands[] = new Command\Snapshot\SnapshotRestoreCommand();
$commands[] = new Command\SshKey\SshKeyAddCommand();
$commands[] = new Command\SshKey\SshKeyDeleteCommand();
$commands[] = new Command\SshKey\SshKeyListCommand();
Expand Down
111 changes: 0 additions & 111 deletions src/Command/Environment/EnvironmentBackupCommand.php

This file was deleted.

28 changes: 28 additions & 0 deletions src/Command/PlatformCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ abstract class PlatformCommand extends Command
protected $environmentsTtl;

private $hiddenInList = false;
private $hiddenAliases = array();

/**
* The project, selected either by an option or the CWD.
Expand Down Expand Up @@ -865,4 +866,31 @@ protected function runOtherCommand($name, array $arguments = array(), InputInter

return $command->run($cmdInput, $this->output);
}

/**
* Add aliases that should be hidden from help.
*
* @see parent::setAliases()
*
* @param array $hiddenAliases
*
* @return self
*/
protected function setHiddenAliases(array $hiddenAliases)
{
$this->hiddenAliases = $hiddenAliases;
$this->setAliases(array_merge($this->getAliases(), $hiddenAliases));

return $this;
}

/**
* Get aliases that should be visible in help.
*
* @return array
*/
public function getVisibleAliases()
{
return array_diff($this->getAliases(), $this->hiddenAliases);
}
}
65 changes: 65 additions & 0 deletions src/Command/Snapshot/SnapshotCreateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
namespace Platformsh\Cli\Command\Snapshot;

use Platformsh\Cli\Command\PlatformCommand;
use Platformsh\Cli\Util\ActivityUtil;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class SnapshotCreateCommand extends PlatformCommand
{

protected function configure()
{
$this
->setName('snapshot:create')
->setHiddenAliases(array('backup', 'environment:backup'))
->setDescription('Make a snapshot of an environment')
->addArgument('environment', InputArgument::OPTIONAL, 'The environment')
->addOption('no-wait', null, InputOption::VALUE_NONE, 'Do not wait for the snapshot to complete');
$this->addProjectOption()
->addEnvironmentOption();
$this->setHelp('See https://docs.platform.sh/use-platform/backup-and-restore.html');
$this->addExample('Make a snapshot of the current environment');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->validateInput($input);

$selectedEnvironment = $this->getSelectedEnvironment();
$environmentId = $selectedEnvironment['id'];
if (!$selectedEnvironment->operationAvailable('backup')) {
$this->stdErr->writeln(
"Operation not available: cannot create a snapshot of <error>$environmentId</error>"
);

return 1;
}

$activity = $selectedEnvironment->backup();

$this->stdErr->writeln("Creating a snapshot of <info>$environmentId</info>");

if (!$input->getOption('no-wait')) {
$success = ActivityUtil::waitAndLog(
$activity,
$this->stdErr,
"A snapshot of environment <info>$environmentId</info> has been created",
"The snapshot failed"
);
if (!$success) {
return 1;
}
}

if (!empty($activity['payload']['backup_name'])) {
$name = $activity['payload']['backup_name'];
$output->writeln("Snapshot name: <info>$name</info>");
}

return 0;
}
}
58 changes: 58 additions & 0 deletions src/Command/Snapshot/SnapshotListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace Platformsh\Cli\Command\Snapshot;

use Platformsh\Cli\Command\PlatformCommand;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SnapshotListCommand extends PlatformCommand
{

protected function configure()
{
$this
->setName('snapshot:list')
->setAliases(array('snapshots'))
->setDescription('List available snapshots of an environment')
->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'Limit the number of snapshots to list', 10);
$this->addProjectOption()
->addEnvironmentOption();
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->validateInput($input);

$environment = $this->getSelectedEnvironment();

$stdErr = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;

$stdErr->writeln("Finding snapshots for the environment <info>{$environment['id']}</info>");
$results = $environment->getActivities($input->getOption('limit'), 'environment.backup');
if (!$results) {
$stdErr->writeln('No snapshots found');
return 1;
}

$headers = array("Created", "% Complete", "Snapshot name");
$rows = array();
foreach ($results as $result) {
$payload = $result->getProperty('payload');
$snapshot_name = !empty($payload['backup_name']) ? $payload['backup_name'] : 'N/A';
$rows[] = array(
date('Y-m-d H:i:s', strtotime($result->getProperty('created_at'))),
$result->getCompletionPercent(),
$snapshot_name,
);
}

$table = new Table($output);
$table->setHeaders($headers);
$table->setRows($rows);
$table->render();
return 0;
}
}
Loading

0 comments on commit 5c566cf

Please sign in to comment.