Skip to content

Commit

Permalink
Add health checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Nov 3, 2024
1 parent 46b2b68 commit bf0c372
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 49 deletions.
8 changes: 4 additions & 4 deletions src/Commands/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function configure(): void
->setName('deploy')
->setDescription('Deploy the application')
->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'The environment to deploy to', 'dev')
->addOption('directory', 'd', InputOption::VALUE_OPTIONAL, 'The directory to deploy', getcwd())
->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'The location of the configuration file to use')
->addOption('force', null, InputOption::VALUE_NONE, 'Force the deployment');
}

Expand All @@ -43,10 +43,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/** @var string $environment */
$environment = $input->getOption('env');

/** @var string $dir */
$dir = $input->getOption('directory');
/** @var string|null $configFileName */
$configFileName = $input->getOption('config');

$config = Config::loadConfig($brefCloud, $dir);
$config = Config::loadConfig($brefCloud, $configFileName);

$appName = $config['name'];

Expand Down
116 changes: 71 additions & 45 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,86 @@ class Config
* @return array{name: string, team: string, type: string}
* @throws Exception
*/
public static function loadConfig(BrefCloudClient $client, string $directory): array
public static function loadConfig(BrefCloudClient $client, ?string $fileName): array
{
if (is_file($directory . '/bref.php')) {
$config = require $directory . '/bref.php';

// @TODO: find a way to bring the Bref\Cloud\Laravel as a dependency to the CLI.
if (! $config instanceof \Bref\Cloud\Laravel) {
throw new Exception('The "bref.php" file must return an instance of \Bref\Cloud\Laravel');
if ($fileName) {
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
if ($fileExtension === 'yml' || $fileExtension === 'yaml') {
return self::loadServerlessConfig($fileName);
}
return self::loadBrefConfig($fileName, $client);
}

// @TODO I don't think this is the best place to put this code, but
// we need access to the entire Laravel object before it gets serialized
// so that we can process it properly.
// The important attributes here are:
// - $path
// - $exclude
// - S3 source code URL
// - @TODO: assets?
[$hash, $path] = self::zipProjectContents($directory, $config);

$s3Path = $client->uploadSourceCodeToS3($hash, $path, $config->team);

$php = $config->path($s3Path)->serialize();

return [
'name' => $config->name,
'team' => $config->team,
'type' => 'laravel',
'php' => $php,
];
if (is_file('bref.php')) {
return self::loadBrefConfig('bref.php', $client);
}

if (is_file('serverless.yml')) {
$serverlessConfig = self::readYamlFile('serverless.yml');
if (empty($serverlessConfig['service']) || ! is_string($serverlessConfig['service']) || str_contains($serverlessConfig['service'], '$')) {
throw new Exception('The "service" name in "serverless.yml" cannot contain variables, it is not supported by Bref Cloud');
}
$team = (string) ($serverlessConfig['bref']['team'] ?? $serverlessConfig['custom']['bref']['team'] ?? '');
if (empty($team)) {
throw new Exception('To deploy a Serverless Framework project with Bref Cloud you must set the team name in the "bref.team" field in "serverless.yml"');
}
if (str_contains($team, '$')) {
throw new Exception('The "service" name in "serverless.yml" cannot contain variables, it is not supported by Bref Cloud');
}
return [
'name' => $serverlessConfig['service'],
'team' => $team,
'type' => 'serverless-framework',
];
return self::loadServerlessConfig('serverless.yml');
}

throw new Exception('No "serverless.yml" file found in the current directory');
}

/**
* @return array{name: string, team: string, type: string}
*/
private static function loadServerlessConfig(string $fileName): array
{
$serverlessConfig = self::readYamlFile($fileName);
if (empty($serverlessConfig['service']) || ! is_string($serverlessConfig['service']) || str_contains($serverlessConfig['service'], '$')) {
throw new Exception('The "service" name in "serverless.yml" cannot contain variables, it is not supported by Bref Cloud');
}
$team = (string) ($serverlessConfig['bref']['team'] ?? $serverlessConfig['custom']['bref']['team'] ?? '');
if (empty($team)) {
throw new Exception('To deploy a Serverless Framework project with Bref Cloud you must set the team name in the "bref.team" field in "serverless.yml"');
}
if (str_contains($team, '$')) {
throw new Exception('The "service" name in "serverless.yml" cannot contain variables, it is not supported by Bref Cloud');
}
return [
'name' => $serverlessConfig['service'],
'team' => $team,
'type' => 'serverless-framework',
// Health checks are automatically enabled if the package is installed
'healthChecks' => file_exists('vendor/bref/laravel-health-check/composer.json'),
];
}

/**
* @return array{name: string, team: string, type: string}
*/
private static function loadBrefConfig(string $fileName, BrefCloudClient $client): array
{
$config = require $fileName;

// @TODO: find a way to bring the Bref\Cloud\Laravel as a dependency to the CLI.
if (! $config instanceof \Bref\Cloud\Laravel) {
throw new Exception('The "bref.php" file must return an instance of \Bref\Cloud\Laravel');
}

// @TODO I don't think this is the best place to put this code, but
// we need access to the entire Laravel object before it gets serialized
// so that we can process it properly.
// The important attributes here are:
// - $path
// - $exclude
// - S3 source code URL
// - @TODO: assets?
[$hash, $path] = self::zipProjectContents($config);

$s3Path = $client->uploadSourceCodeToS3($hash, $path, $config->team);

$php = $config->path($s3Path)->serialize();

return [
'name' => $config->name,
'team' => $config->team,
'type' => 'laravel',
'php' => $php,
];
}

/**
* @return array<array-key, mixed>
* @throws Exception
Expand All @@ -90,8 +116,9 @@ private static function readYamlFile(string $fileName): array
}
}

private static function zipProjectContents(string $directory, \Bref\Cloud\Laravel $config)
private static function zipProjectContents()
{
$directory = getcwd();
if (! is_dir($directory . '.bref/')) {
mkdir($directory . '.bref/');
}
Expand Down Expand Up @@ -139,5 +166,4 @@ private static function addFolderContentsToZipArchive(ZipArchive $zip, $rootDire
}
}
}

}

0 comments on commit bf0c372

Please sign in to comment.