generated from open-southeners/php-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make registry configurable & refactor layers fetching to be better te…
…sted
- Loading branch information
Showing
15 changed files
with
6,289 additions
and
89 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace OpenSoutheners\SidecarLocal\Exceptions; | ||
|
||
class SidecarIncompatibleFunctionArchitecture extends \Exception | ||
{ | ||
public const MESSAGE = 'Architecture "%s" not supported by function "%s". Supported "%s".'; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace OpenSoutheners\SidecarLocal\Exceptions; | ||
|
||
class SidecarIncompatibleFunctionRuntime extends \Exception | ||
{ | ||
public const MESSAGE = 'Runtime "%s" not supported by function "%s". Supported "%s".'; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
|
||
namespace OpenSoutheners\SidecarLocal; | ||
|
||
use Hammerstone\Sidecar\Clients\LambdaClient; | ||
use Hammerstone\Sidecar\LambdaFunction; | ||
use Illuminate\Filesystem\Filesystem; | ||
use Illuminate\Support\Str; | ||
use OpenSoutheners\SidecarLocal\Exceptions\SidecarIncompatibleFunctionArchitecture; | ||
use OpenSoutheners\SidecarLocal\Exceptions\SidecarIncompatibleFunctionRuntime; | ||
use Symfony\Component\Console\Helper\ProgressBar; | ||
use ZipArchive; | ||
|
||
class SidecarLayers | ||
{ | ||
public function __construct( | ||
protected LambdaClient $client, | ||
protected Filesystem $filesystem | ||
) { | ||
// | ||
} | ||
|
||
/** | ||
* Fetch function's required layers then return volume mapping otherwise false. | ||
*/ | ||
public function fetch(LambdaFunction $function, array $arns, ProgressBar $progressBar): string|false | ||
{ | ||
if (count($arns) === 0) { | ||
return false; | ||
} | ||
|
||
// We need to set environment any value rather than "local" | ||
// to be able to grab layers from AWS cloud. | ||
config(['sidecar.env' => 'deploying']); | ||
|
||
$zipArchive = new ZipArchive(); | ||
|
||
$basePath = config('sidecar-local.path') ?? $function->package()->getBasePath(); | ||
$functionLayerDirectory = "{$basePath}/../layers/{$function->name()}"; | ||
|
||
$this->filesystem->ensureDirectoryExists($functionLayerDirectory); | ||
|
||
$progressBar->start(); | ||
|
||
foreach ($arns as $arn) { | ||
$layerTmpFilePath = $this->getLayerTmpPath($arn); | ||
|
||
$layerData = $this->checkArchitecture($function, $this->fetchByArn($arn)); | ||
|
||
$layerLocation = data_get($layerData, 'Content.Location'); | ||
|
||
if (! $layerLocation || ! $this->filesystem->copy($layerLocation, $layerTmpFilePath)) { | ||
throw new \Exception("Cannot download layer for function '{$function->name()}'."); | ||
} | ||
|
||
$zipArchive->open($layerTmpFilePath); | ||
$zipArchive->extractTo($functionLayerDirectory); | ||
$zipArchive->close(); | ||
|
||
$progressBar->advance(); | ||
} | ||
|
||
$progressBar->clear(); | ||
|
||
return Str::of($functionLayerDirectory) | ||
->replaceFirst($basePath, '.') | ||
->append(':/opt:ro') | ||
->value(); | ||
} | ||
|
||
/** | ||
* Get temporary path for layer artifacts (files). | ||
*/ | ||
protected function getLayerTmpPath(string $arn): string | ||
{ | ||
$layerDirectoryName = Str::of($arn)->afterLast(':layer:')->replaceLast(':', '/'); | ||
$layerZipFileName = $layerDirectoryName->afterLast('/')->append('.zip')->value(); | ||
$layerDirectoryName = $layerDirectoryName->value(); | ||
|
||
$layerTmpDirectoryPath = sys_get_temp_dir()."/SidecarLocal/layers/{$layerDirectoryName}"; | ||
|
||
return "{$layerTmpDirectoryPath}/{$layerZipFileName}"; | ||
} | ||
|
||
/** | ||
* Fetch layer by ARN string. | ||
*/ | ||
protected function fetchByArn(string $arn): array | ||
{ | ||
return $this->client->getLayerVersionByArn(['Arn' => $arn])->toArray(); | ||
} | ||
|
||
/** | ||
* Check function runtime is within layer compatible runtime list. | ||
*/ | ||
protected function checkRuntime(LambdaFunction $function, array $layer): array | ||
{ | ||
if (! isset($layer['CompatibleRuntimes'])) { | ||
return $layer; | ||
} | ||
|
||
if (! in_array($function->runtime(), $layer['CompatibleRuntimes'])) { | ||
throw new SidecarIncompatibleFunctionRuntime( | ||
sprintf( | ||
SidecarIncompatibleFunctionRuntime::MESSAGE, | ||
$function->architecture(), | ||
$function->name(), | ||
implode(', ', $layer['CompatibleRuntimes']) | ||
) | ||
); | ||
} | ||
|
||
return $layer; | ||
} | ||
|
||
/** | ||
* Check function architecture is within supported layer architecture list. | ||
*/ | ||
protected function checkArchitecture(LambdaFunction $function, array $layer): array | ||
{ | ||
if (! isset($layer['CompatibleArchitectures'])) { | ||
return $layer; | ||
} | ||
|
||
if (! in_array($function->architecture(), $layer['CompatibleArchitectures'])) { | ||
throw new SidecarIncompatibleFunctionArchitecture( | ||
sprintf( | ||
SidecarIncompatibleFunctionArchitecture::MESSAGE, | ||
$function->architecture(), | ||
$function->name(), | ||
implode(', ', $layer['CompatibleArchitectures']) | ||
) | ||
); | ||
} | ||
|
||
return $layer; | ||
} | ||
} |
Oops, something went wrong.