Skip to content

Commit 6a834b4

Browse files
author
mwalters
committed
Initial Commit
0 parents  commit 6a834b4

12 files changed

Lines changed: 613 additions & 0 deletions

Aws/AbstractAwsCommand.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2014 Underground Elephant
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @package aws-cli-bundle
19+
* @copyright (c) 2014 Underground Elephant
20+
* @license Apache License, Version 2.0
21+
*/
22+
23+
namespace Uecode\Bundle\AwsCliBundle\Aws;
24+
25+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
26+
27+
/**
28+
* @author Mauricio Walters <mwalters@undergroundelephant.com>
29+
* @abstract
30+
*/
31+
abstract class AbstractAwsCommand extends ContainerAwareCommand
32+
{
33+
34+
/**
35+
* Authenticate with AWS and instantiate client
36+
*
37+
* @abstract
38+
* @return Client
39+
*/
40+
abstract protected function getClient();
41+
42+
/**
43+
* Get AWS API credentials from parameters.yml
44+
*
45+
* @return array
46+
*/
47+
protected function getCredentials()
48+
{
49+
return $this->getContainer()->getParameter('uecode.aws');
50+
}
51+
}

Aws/Ec2Command.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2014 Underground Elephant
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @package aws-cli-bundle
19+
* @copyright (c) 2014 Underground Elephant
20+
* @license Apache License, Version 2.0
21+
*/
22+
23+
namespace Uecode\Bundle\AwsCliBundle\Aws;
24+
25+
use Aws\Ec2\Ec2Client;
26+
27+
/**
28+
* Authenticates with AWS for use with EC2 commands
29+
*
30+
* @author Mauricio Walters <mwalters@undergroundelephant.com>
31+
*/
32+
class Ec2Command extends AbstractAwsCommand
33+
{
34+
/**
35+
* Instantiates a new Ec2 Client
36+
*
37+
* @return Ec2Client
38+
*/
39+
protected function getClient()
40+
{
41+
$credentials = $this->getCredentials();
42+
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;
50+
}
51+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2014 Underground Elephant
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @package aws-cli-bundle
19+
* @copyright (c) 2014 Underground Elephant
20+
* @license Apache License, Version 2.0
21+
*/
22+
23+
namespace Uecode\Bundle\AwsCliBundle\Command\Aws\Ec2;
24+
25+
use Symfony\Component\Console\Input\InputArgument;
26+
use Symfony\Component\Console\Input\InputOption;
27+
use Symfony\Component\Console\Input\InputInterface;
28+
use Symfony\Component\Console\Output\OutputInterface;
29+
use Uecode\Bundle\AwsCliBundle\Aws\Ec2Command;
30+
31+
/**
32+
* Copy Image command
33+
*
34+
* @link http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.Ec2.Ec2Client.html#_copyImage
35+
* @author Mauricio Walters <mwalters@undergroundelephant.com>
36+
*/
37+
class CopyImageCommand extends Ec2Command
38+
{
39+
/**
40+
* {@inheritDoc}
41+
*/
42+
protected function configure()
43+
{
44+
$this
45+
->setName('uecode:aws:ec2:copyimage')
46+
->setDescription('Initiates the copy of an AMI from the specified source region to the region in which the request was made.')
47+
->addArgument('SourceRegion', InputArgument::REQUIRED, 'The name of the region that contains the AMI to copy')
48+
->addArgument('SourceImageId', InputArgument::REQUIRED, 'The ID of the AMI to copy')
49+
->addArgument('Name', InputArgument::REQUIRED, 'The name of the new AMI in the destination region')
50+
->addOption('Description', 'description', InputOption::VALUE_OPTIONAL, 'A description for the new AMI in the destination region')
51+
->addOption('ClientToken', 'clienttoken', InputOption::VALUE_OPTIONAL, 'Unique, case-sensitive identifier you provide to ensure the idempotency of the request')
52+
->addOption('DryRun', 'dryrun', InputOption::VALUE_NONE, null)
53+
->addOption('AmiName', 'aminame', InputOption::VALUE_NONE, 'Use AMI name instead if ID');
54+
}
55+
56+
/**
57+
* {@inheritDoc}
58+
*/
59+
protected function execute(InputInterface $input, OutputInterface $output)
60+
{
61+
$options = array_merge($input->getArguments(), $input->getOptions());
62+
63+
$client = $this->getClient();
64+
65+
if ($options['AmiName']) {
66+
$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;
70+
}
71+
72+
$result = $client->copyImage($options);
73+
}
74+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2014 Underground Elephant
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @package aws-cli-bundle
19+
* @copyright (c) 2014 Underground Elephant
20+
* @license Apache License, Version 2.0
21+
*/
22+
23+
namespace Uecode\Bundle\AwsCliBundle\Command\Aws\Ec2;
24+
25+
use Symfony\Component\Console\Input\InputArgument;
26+
use Symfony\Component\Console\Input\InputOption;
27+
use Symfony\Component\Console\Input\InputInterface;
28+
use Symfony\Component\Console\Output\OutputInterface;
29+
use Uecode\Bundle\AwsCliBundle\Aws\Ec2Command;
30+
31+
/**
32+
* Create Image command
33+
*
34+
* @link http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.Ec2.Ec2Client.html#_createImage
35+
* @author Mauricio Walters <mwalters@undergroundelephant.com>
36+
*/
37+
class CreateImageCommand extends Ec2Command
38+
{
39+
/**
40+
* {@inheritDoc}
41+
*/
42+
protected function configure()
43+
{
44+
$this
45+
->setName('uecode:aws:ec2:createimage')
46+
->setDescription('Creates an Amazon EBS-backed AMI from an Amazon EBS-backed instance that is either running or stopped.')
47+
->addArgument('Name', InputArgument::REQUIRED, 'A name for the new image.')
48+
->addArgument('InstanceId', InputArgument::REQUIRED, 'The ID of the instance')
49+
->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')
51+
->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);
53+
}
54+
55+
/**
56+
* {@inheritDoc}
57+
*/
58+
protected function execute(InputInterface $input, OutputInterface $output)
59+
{
60+
$options = array_merge($input->getArguments(), $input->getOptions());
61+
62+
$options['BlockDeviceMappings'] = json_decode($options['BlockDeviceMappings']);
63+
64+
$client = $this->getClient();
65+
66+
$result = $client->createImage($options);
67+
68+
$output->wirteln($result);
69+
}
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2014 Underground Elephant
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @package aws-cli-bundle
19+
* @copyright (c) 2014 Underground Elephant
20+
* @license Apache License, Version 2.0
21+
*/
22+
23+
namespace Uecode\Bundle\AwsCliBundle\Command\Aws\Ec2;
24+
25+
use Symfony\Component\Console\Input\InputArgument;
26+
use Symfony\Component\Console\Input\InputOption;
27+
use Symfony\Component\Console\Input\InputInterface;
28+
use Symfony\Component\Console\Output\OutputInterface;
29+
use Uecode\Bundle\AwsCliBundle\Aws\Ec2Command;
30+
31+
/**
32+
* Deregister Image command
33+
*
34+
* @link http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.Ec2.Ec2Client.html#_deregisterImage
35+
* @author Mauricio Walters <mwalters@undergroundelephant.com>
36+
*/
37+
class DeregisterImageCommand extends Ec2Command
38+
{
39+
/**
40+
* {@inheritDoc}
41+
*/
42+
protected function configure()
43+
{
44+
$this
45+
->setName('uecode:aws:ec2:deregisterimage')
46+
->setDescription('Deregisters the specified AMI. After you deregister an AMI, it can\'t be used to launch new instances.')
47+
->addArgument('ImageId', InputArgument::REQUIRED, 'The ID of the AMI')
48+
->addOption('DryRun', 'dryrun', InputOption::VALUE_NONE, null)
49+
->addOption('AmiName', 'aminame', InputOption::VALUE_NONE, 'Use AMI name instead if ID');
50+
}
51+
52+
/**
53+
* {@inheritDoc}
54+
*/
55+
protected function execute(InputInterface $input, OutputInterface $output)
56+
{
57+
$options = array_merge($input->getArguments(), $input->getOptions());
58+
59+
$client = $this->getClient();
60+
61+
if ($options['AmiName']) {
62+
$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;
66+
}
67+
68+
$result = $client->deregisterImage($options);
69+
}
70+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Uecode\Bundle\AwsCliBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
8+
/**
9+
* This is the class that validates and merges configuration from your app/config files
10+
*
11+
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
12+
*/
13+
class Configuration implements ConfigurationInterface
14+
{
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
public function getConfigTreeBuilder()
19+
{
20+
$treeBuilder = new TreeBuilder();
21+
$rootNode = $treeBuilder->root('uecode_aws_cli');
22+
23+
// Here you should define the parameters that are allowed to
24+
// configure your bundle. See the documentation linked above for
25+
// more information on that topic.
26+
27+
return $treeBuilder;
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Uecode\Bundle\AwsCliBundle\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\Config\FileLocator;
7+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8+
use Symfony\Component\DependencyInjection\Loader;
9+
10+
/**
11+
* This is the class that loads and manages your bundle configuration
12+
*
13+
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
14+
*/
15+
class UecodeAwsCliExtension extends Extension
16+
{
17+
/**
18+
* {@inheritDoc}
19+
*/
20+
public function load(array $configs, ContainerBuilder $container)
21+
{
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');
27+
}
28+
}

0 commit comments

Comments
 (0)