Last active
December 14, 2015 03:59
-
-
Save lmammino/5024980 to your computer and use it in GitHub Desktop.
Symfony2 command to test rackspace authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Sbaam\Bundle\UtilsBundle\Command; | |
use Symfony\Component\Console\Command\Command, | |
Symfony\Component\Console\Input\InputArgument, | |
Symfony\Component\Console\Input\InputInterface, | |
Symfony\Component\Console\Input\InputOption, | |
Symfony\Component\Console\Output\OutputInterface; | |
class RackspaceAuthenticationCommand extends Command | |
{ | |
protected function configure() | |
{ | |
$this | |
->setName('rackspace:authentication:test') | |
->setDescription('Performs authentication on rackspace multiple times and outputs the percentage of successful authentication') | |
->addOption( 'times', 't', InputOption::VALUE_OPTIONAL, 'The number of times authentication will be tried', 1 ) | |
->addOption( 'delay', 'D', InputOption::VALUE_OPTIONAL, 'The number of seconds between each attempt') | |
->addOption( 'full', 'F', InputOption::VALUE_NONE, 'If true will enable the rackspace debug mode and prints out the whole requests headers') | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
/** | |
* @var \Symfony\Component\DependencyInjection\ContainerInterface $container | |
*/ | |
$container = $this->getApplication()->getKernel()->getContainer(); | |
$username = $container->getParameter('rackspace_username'); | |
$apiKey = $container->getParameter('rackspace_apikey'); | |
$times = $input->getOption('times'); | |
$delay = $input->getOption('delay'); | |
$full = $input->getOption('full'); | |
$errors = array(); | |
$failures = 0; | |
for($i=0; $i<$times; $i++) | |
{ | |
$authentication = new \CF_Authentication($username, $apiKey); | |
if($full) | |
$authentication->setDebug(true); | |
try | |
{ | |
if($full) | |
$output->writeln(sprintf("\n<info>- Attempt #%d:</info>", $i)); | |
$authentication->authenticate(); | |
if(!$full) | |
$output->write('<info>.</info>'); | |
} | |
catch (\Exception $e) | |
{ | |
$errors[] = sprintf('[%s] %s ("%s" on line %d)'."\n%s", get_class($e), $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString()); | |
$failures++; | |
if($full) | |
$output->writeln(sprintf('<error>%s</error>', $e->getTraceAsString())); | |
else | |
$output->write('<error>.</error>'); | |
} | |
if($delay && $delay > 0) | |
sleep($delay); | |
} | |
$successRate = ($times - $failures) * 100 / $times; | |
$output->write("\n\n<info>Success Rate: </info>"); | |
if($failures > 0) | |
$output->writeln(sprintf('<error>%s%%</error>', $successRate)); | |
else | |
$output->writeln(sprintf('<info>%s%%</info>', $successRate)); | |
if(!empty($errors)) | |
$output->writeln(sprintf("\n<error>%d</error> errors:", $failures)); | |
foreach($errors as $error) | |
{ | |
$output->writeln(sprintf('<error>%s</error>', $error)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment