Skip to content

Commit 3a2624b

Browse files
author
Mauricio Walters
committed
Merge pull request #5 from cordoval/5-cleanup
cleanup
2 parents 62f4fb3 + 370d214 commit 3a2624b

9 files changed

+64
-64
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/composer.lock

Aws/AbstractAwsCommand.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626

2727
/**
2828
* @author Mauricio Walters <[email protected]>
29-
* @abstract
3029
*/
3130
abstract class AbstractAwsCommand extends ContainerAwareCommand
3231
{
32+
const COMMAND_SUCCESS = 0;
33+
const COMMAND_FAILURE = 1;
3334

3435
/**
3536
* Authenticate with AWS and instantiate client

Aws/Ec2Command.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ protected function getClient()
4040
{
4141
$credentials = $this->getCredentials();
4242

43-
$client = Ec2Client::factory(array(
44-
'key' => $credentials['aws_api_key'],
45-
'secret' => $credentials['aws_api_secret'],
46-
'region' => $credentials['aws_region'],
47-
));
48-
49-
return $client;
43+
return Ec2Client::factory(
44+
array(
45+
'key' => $credentials['aws_api_key'],
46+
'secret' => $credentials['aws_api_secret'],
47+
'region' => $credentials['aws_region'],
48+
)
49+
);
5050
}
5151
}

Command/Aws/Ec2/CopyImageCommand.php

+23-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ protected function configure()
5050
->addOption('Description', 'description', InputOption::VALUE_OPTIONAL, 'A description for the new AMI in the destination region')
5151
->addOption('ClientToken', 'clienttoken', InputOption::VALUE_OPTIONAL, 'Unique, case-sensitive identifier you provide to ensure the idempotency of the request')
5252
->addOption('DryRun', 'dryrun', InputOption::VALUE_NONE, null)
53-
->addOption('AmiName', 'aminame', InputOption::VALUE_NONE, 'Use AMI name instead if ID');
53+
->addOption('AmiName', 'aminame', InputOption::VALUE_NONE, 'Use AMI name instead of ID')
54+
;
5455
}
5556

5657
/**
@@ -64,12 +65,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
6465

6566
if ($options['AmiName']) {
6667
$name = $options['SourceImageId'];
67-
$image = $client->describeImages(["Filters" => [["Name" => "name", "Values" => [$name]]]]); # TODO if more than one instance is returned, warn the user
68-
$sourceImageId = $image['Images'][0]['ImageId'];
69-
$options['SourceImageId'] = $sourceImageId;
68+
$image = $client->describeImages(
69+
[
70+
"Filters" => [
71+
[
72+
"Name" => "name",
73+
"Values" => [$name]
74+
]
75+
]
76+
]
77+
);
78+
$imageCollection = $image['Images'];
79+
if (count($imageCollection) > 1) {
80+
$output->writeln('<error>Know that the AMI name provided matched more than one image. Please be more specific to avoid copying the wrong image.</error>');
81+
82+
return self::COMMAND_FAILURE;
83+
}
84+
85+
$options['SourceImageId'] = $imageCollection[0]['ImageId'];
7086
}
7187

7288
$result = $client->copyImage($options);
73-
$output->writeln($result->get('ImageId'));
89+
$output->writeln(sprintf('<info>Successfully copied image %s.</info>', $result->get('ImageId')));
90+
91+
return self::COMMAND_SUCCESS;
7492
}
7593
}

Command/Aws/Ec2/CreateImageCommand.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ protected function configure()
4747
->addArgument('Name', InputArgument::REQUIRED, 'A name for the new image.')
4848
->addArgument('InstanceId', InputArgument::REQUIRED, 'The ID of the instance')
4949
->addArgument('Description', InputArgument::OPTIONAL, 'A description for the new image.')
50-
->addOption('BlockDeviceMappings', 'mappings', InputOption::VALUE_OPTIONAL, 'Information about one or more block device mappings. Takes JSON')
50+
->addOption('BlockDeviceMappings', 'mappings', InputOption::VALUE_OPTIONAL, 'Information about one or more block device mappings. Takes JSON.')
5151
->addOption('NoReboot', 'noreboot', InputOption::VALUE_NONE, 'Amazon EC2 will not shut down the instance before creating the image. Filesystem integrity is not guaranteed.')
52-
->addOption('DryRun', 'dryrun', InputOption::VALUE_NONE, null);
52+
->addOption('DryRun', 'dryrun', InputOption::VALUE_NONE, null)
53+
;
5354
}
5455

5556
/**
@@ -62,8 +63,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
6263
$options['BlockDeviceMappings'] = json_decode($options['BlockDeviceMappings']);
6364

6465
$client = $this->getClient();
65-
6666
$result = $client->createImage($options);
67-
$output->writeln($result->get('ImageId'));
67+
$output->writeln('AMI Image created with id '.$result->get('ImageId'));
68+
69+
return self::COMMAND_SUCCESS;
6870
}
6971
}

Command/Aws/Ec2/DeregisterImageCommand.php

+22-4
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
6060

6161
if ($options['AmiName']) {
6262
$name = $options['ImageId'];
63-
$image = $client->describeImages(["Filters" => [["Name" => "name", "Values" => [$name]]]]); # TODO if more than one instance is returned, warn the user
64-
$imageId = $image['Images'][0]['ImageId'];
65-
$options['ImageId'] = $imageId;
63+
$image = $client->describeImages(
64+
[
65+
"Filters" => [
66+
[
67+
"Name" => "name", "Values" => [$name]
68+
]
69+
]
70+
]
71+
);
72+
$imageCollection = $image['Images'];
73+
if (count($imageCollection) > 1) {
74+
$output->writeln('<error>Know that the AMI name provided matched more than one image. Please be more specific to avoid deregistering the wrong image.</error>');
75+
76+
return self::COMMAND_FAILURE;
77+
}
78+
79+
$options['ImageId'] = $imageCollection[0]['ImageId'];
6680
}
6781

68-
$result = $client->deregisterImage($options);
82+
$client->deregisterImage($options);
83+
84+
$output->writeln(sprintf('<info>Successfully deregistered image %s.</info>', $options['ImageId']));
85+
86+
return self::COMMAND_SUCCESS;
6987
}
7088
}

DependencyInjection/Configuration.php

-29
This file was deleted.

DependencyInjection/UecodeAwsCliExtension.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Uecode\Bundle\AwsCliBundle\DependencyInjection;
44

5-
use Symfony\Component\DependencyInjection\ContainerBuilder;
65
use Symfony\Component\Config\FileLocator;
7-
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
87
use Symfony\Component\DependencyInjection\Loader;
8+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
99

1010
/**
1111
* This is the class that loads and manages your bundle configuration
@@ -19,10 +19,5 @@ class UecodeAwsCliExtension extends Extension
1919
*/
2020
public function load(array $configs, ContainerBuilder $container)
2121
{
22-
$configuration = new Configuration();
23-
$config = $this->processConfiguration($configuration, $configs);
24-
25-
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
26-
$loader->load('services.yml');
2722
}
2823
}

Resources/config/services.yml

-7
This file was deleted.

0 commit comments

Comments
 (0)