added support for container secrets

This commit is contained in:
pushrbx 2023-06-05 20:19:04 +01:00
parent 40812e814d
commit 317906cdf9

View File

@ -19,6 +19,8 @@ $safe_defaults = [
"DB_PASSWORD" => ""
];
$current_env = $_ENV;
if (!file_exists(".env")) {
copy(".env.dist", ".env");
$writer = new \MirazMac\DotEnv\Writer(__DIR__ . '/' . '.env');
@ -29,10 +31,30 @@ if (!file_exists(".env")) {
$writer->write();
}
// We'd like to support Container secrets. So we'll check if any of the env vars has a __FILE suffix
// then we'll try to load the file and set the env var to the contents of the file.
// https://docs.docker.com/engine/swarm/secrets/
$envWriter = new \MirazMac\DotEnv\Writer(__DIR__ . '/' . '.env');
$itemsWritten = 0;
foreach (array_keys($current_env) as $env_key) {
if (!str_contains($env_key, "__FILE")) {
continue;
}
if (!file_exists($current_env[$env_key])) {
echo "Couldn't load secret: " . $_ENV[$env_key] . PHP_EOL;
continue;
}
$originalKey = str_replace("__FILE", "", $env_key);
$envWriter->set($originalKey, file_get_contents($current_env[$env_key]));
$itemsWritten++;
}
if ($itemsWritten > 0) {
$envWriter->write();
}
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
$current_env = $_ENV;
if ($current_env["SCOUT_DRIVER"] === "typesense" && empty($current_env["TYPESENSE_API_KEY"])) {
echo "Please set the TYPESENSE_API_KEY environment variable when setting SCOUT_DRIVER to typesense.";