Skip to content

Commit

Permalink
Layer is published for first time
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm committed Dec 20, 2019
1 parent 8c8a35f commit 6838a5c
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 30 deletions.
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SHELL := /bin/bash
php_versions = 72 73 74
php_versions = 73

docker-images:
PWD=pwd
Expand All @@ -15,7 +16,7 @@ docker-images:
done

# The PHP runtimes
export: docker-images
layers: docker-images
PWD=pwd
rm -rf export/tmp || true
mkdir export/tmp
Expand All @@ -26,11 +27,12 @@ export: docker-images
echo "### Exporting $${dir} PHP$${php_version}"; \
echo "###"; \
cd ${PWD} ; cd export/tmp ; \
docker run --entrypoint "tar" bref/$${dir}-php-$${php_version} -ch -C /opt . | tar -x;zip -X --quiet --recurse-paths ../`echo "$${dir}-php-$${php_version}" | sed -e "s/layers\//layer-/g"`.zip . ; \
docker run --entrypoint "tar" bref/$${dir}-php-$${php_version} -ch -C /opt . | tar -x ; \
zip --quiet --recurse-paths ../`echo "$${dir}-php-$${php_version}" | sed -e "s/layers\//layer-/g"`.zip . ; \
echo ""; \
done \
done
rm -rf export/tmp

publish: export
publish: layers
php src/publish.php
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ functions:
```
;php/conf.d/php.ini
extension=amqp
extension=/opt/bref-aqmp/amqp.so
```
2 changes: 1 addition & 1 deletion checksums.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[]
{"amqp-php-73":"e465b47dcaf40a1adb05ae03b790d86e"}
21 changes: 20 additions & 1 deletion layers.json
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
[]
{
"amqp-php-73": {
"ca-central-1": 1,
"eu-central-1": 1,
"eu-north-1": 1,
"eu-west-1": 1,
"eu-west-2": 1,
"eu-west-3": 1,
"sa-east-1": 1,
"us-east-1": 1,
"us-east-2": 1,
"us-west-1": 1,
"us-west-2": 1,
"ap-south-1": 1,
"ap-northeast-1": 1,
"ap-northeast-2": 1,
"ap-southeast-1": 1,
"ap-southeast-2": 1
}
}
4 changes: 4 additions & 0 deletions layers/amqp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RUN set -xe; \
curl -Ls https://github.com/alanxz/rabbitmq-c/archive/v0.9.0.tar.gz \
| tar xzC ${LIBRABBITMQ_BUILD_DIR}

RUN echo ${LIBRABBITMQ_BUILD_DIR}
# Move into the unpackaged code directory
WORKDIR ${LIBRABBITMQ_BUILD_DIR}/rabbitmq-c-0.9.0/

Expand All @@ -19,3 +20,6 @@ RUN set -xe; \

RUN pecl install amqp

# Build the final image from the lambci image that is close to the production environment
FROM lambci/lambda:provided
COPY --from=0 /opt/bref/lib/php/extensions/no-debug-zts-20180731/amqp.so /opt/bref-aqmp/amqp.so
10 changes: 9 additions & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ protected function createContainer()
{
$builder = new ContainerBuilder();
$awsId = getenv('AWS_ID');
$awsProfile = getenv('AWS_PROFILE');
if (empty($awsId)){
$awsId = 'foobar';
$awsId = 'xxxxxxxxx';
}
if (empty($awsProfile)){
$awsProfile = null;
}

$projectDir = dirname(__DIR__);
Expand All @@ -28,6 +32,7 @@ protected function createContainer()
$builder->addDefinitions([
'project_dir' => $projectDir,
'aws_id' => $awsId,
'aws_profile' => $awsProfile,
'layer_names' => $localLayers,
LayerProvider::class => function (ContainerInterface $c) {
return new LayerProvider($c->get('layer_names'), $c->get('aws_id'));
Expand All @@ -38,6 +43,9 @@ protected function createContainer()
PublishCommand::class => function (ContainerInterface $c) {
return new PublishCommand($c->get(LayerPublisher::class), $c->get(RegionProvider::class), $c->get('project_dir'));
},
LayerPublisher::class => function (ContainerInterface $c) {
return new LayerPublisher($c->get('aws_profile'));
},
]);

return $builder->build();
Expand Down
62 changes: 45 additions & 17 deletions src/Aws/LayerPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,31 @@
class LayerPublisher
{
/**
* @param array $layers
* @var string|null
*/
private $awsProfile;

/**
*
* @param string|null $awsProfile
*/
public function __construct(?string $awsProfile)
{
$this->awsProfile = $awsProfile;
}


/**
* @param array<string, string> $layers Layer name and layer zip file path.
* @param array $regions
*/
public function publishLayers(array $layers, array $regions): void
{
/** @var Process[] $publishingProcesses */
$publishingProcesses = [];
foreach ($regions as $region) {
foreach ($layers as $layer) {
$publishingProcesses[$region.$layer] = $this->publishSingleLayer($region, $layer);
foreach ($layers as $layerName => $layerFilePath) {
$publishingProcesses[$region . $layerName] = $this->publishSingleLayer($region, $layerName, $layerFilePath);
}
}
$this->finishProcesses($publishingProcesses);
Expand All @@ -28,47 +43,53 @@ public function publishLayers(array $layers, array $regions): void
/** @var Process[] $permissionProcesses */
$permissionProcesses = [];
foreach ($regions as $region) {
foreach ($layers as $layer) {
$publishLayer = $publishingProcesses[$region . $layer];
foreach ($layers as $layerName => $layerFilePath) {
$publishLayer = $publishingProcesses[$region . $layerName];
$layerVersion = trim($publishLayer->getOutput());

$permissionProcesses[] = $this->addPublicLayerPermissions($region, $layer, $layerVersion);
$permissionProcesses[] = $this->addPublicLayerPermissions($region, $layerName, $layerVersion);
}
}
$this->finishProcesses($permissionProcesses);
}

/**
* @param string $region The AWS region to publish the layer to
* @param string $layer The file path to the layer relative ???
* @param string $layerName
* @param string $file The absolute file path to the layer
* @return Process
*/
private function publishSingleLayer(string $region, string $layer): Process
private function publishSingleLayer(string $region, string $layerName, string $file): Process
{
$file = __DIR__ . "/export/$layer.zip";

$process = new Process([
$args = [
'aws',
'lambda',
'publish-layer-version',
'--region',
$region,
'--layer-name',
$layer,
$layerName,
'--description',
$layer,
$layerName,
'--license-info',
'MIT',
'--zip-file',
'fileb://' . $file,
'fileb://'.$file,
'--compatible-runtimes',
'provided',
// Output the version so that we can fetch it and use it
'--output',
'text',
'--query',
'Version',
]);
];

if ($this->awsProfile !== null) {
$args[] = '--profile';
$args[] = $this->awsProfile;
}

$process = new Process($args);
$process->setTimeout(null);

return $process;
Expand Down Expand Up @@ -108,7 +129,7 @@ private function finishProcesses(array $processes): void
*/
private function addPublicLayerPermissions(string $region, string $layer, string $layerVersion): Process
{
$process = new Process([
$args = [
'aws',
'lambda',
'add-layer-version-permission',
Expand All @@ -124,7 +145,14 @@ private function addPublicLayerPermissions(string $region, string $layer, string
'lambda:GetLayerVersion',
'--principal',
'*',
]);
];

if ($this->awsProfile !== null) {
$args[] = '--profile';
$args[] = $this->awsProfile;
}

$process = new Process($args);
$process->setTimeout(null);

return $process;
Expand Down
1 change: 1 addition & 0 deletions src/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class HelpCommand
public function __invoke(OutputInterface $output)
{
$output->writeln('With this small application you may publish new layers and list existing ones in layer.json');
$output->writeln('You may specify the following environment variables: AWS_ID, AWS_PROFILE');

return 0;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public function __invoke(OutputInterface $output)
$export = [];
foreach ($this->regionProvider->getAll() as $region) {
$layers = $this->provider->listLayers($region);

//$export[$layerName][$region] = $layers[$layerName];
foreach ($layers as $layerName => $version) {
$export[$layerName][$region] = $version;
}

$output->writeln($region);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Command/PublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public function __invoke(OutputInterface $output)

$layers = [];
$finder = new Finder();
$finder->in(__DIR__.'/../export')
$finder->in($this->projectDir.'/export')
->name('layer-*');
foreach ($finder->files() as $file) {
/** @var \SplFileInfo $file */
$layerFile = $file->getFilename();
$layerFile = $file->getRealPath();
$layerName = substr($file->getFilenameWithoutExtension(), 6);
$md5 = md5_file($file->getRealPath());
$md5 = md5_file($layerFile);
$discoveredChecksums[$layerName] = $md5;
if (false === strstr($checksums, $md5)) {
// This layer is new.
Expand All @@ -57,7 +57,7 @@ public function __invoke(OutputInterface $output)
$output->writeln(sprintf('Found %d new layers', count($layers)));

try {
$this->publisher->publishLayers(array_values($layers), $this->regionProvider->getAll());
$this->publisher->publishLayers($layers, $this->regionProvider->getAll());
}catch(\Exception $e) {
// TODO write output.
exit(1);
Expand Down

0 comments on commit 6838a5c

Please sign in to comment.