Unify env var names on Ansible installs with their Docker counterparts.
This commit is contained in:
parent
5285024018
commit
64423a7097
|
@ -16,7 +16,6 @@ tmp/cache/*---*
|
|||
/env.ini
|
||||
/app/env.ini
|
||||
/app/.env
|
||||
/env.ini
|
||||
/azuracast.env
|
||||
/util/fixtures/*
|
||||
/util/fixtures/**/*
|
||||
|
|
|
@ -62,12 +62,9 @@ return [
|
|||
App\Doctrine\Event\SetExplicitChangeTracking $eventChangeTracking,
|
||||
App\EventDispatcher $dispatcher
|
||||
) {
|
||||
$connectionOptions = [
|
||||
'host' => $_ENV['MYSQL_HOST'] ?? 'mariadb',
|
||||
'port' => $_ENV['MYSQL_PORT'] ?? 3306,
|
||||
'dbname' => $_ENV['MYSQL_DATABASE'],
|
||||
'user' => $_ENV['MYSQL_USER'],
|
||||
'password' => $_ENV['MYSQL_PASSWORD'],
|
||||
$connectionOptions = array_merge(
|
||||
$environment->getDatabaseSettings(),
|
||||
[
|
||||
'driver' => 'pdo_mysql',
|
||||
'charset' => 'utf8mb4',
|
||||
'defaultTableOptions' => [
|
||||
|
@ -79,15 +76,8 @@ return [
|
|||
1002 => 'SET NAMES utf8mb4 COLLATE utf8mb4_general_ci',
|
||||
],
|
||||
'platform' => new Doctrine\DBAL\Platforms\MariaDb1027Platform(),
|
||||
];
|
||||
|
||||
if (!$environment->isDocker()) {
|
||||
$connectionOptions['host'] = $_ENV['db_host'] ?? 'localhost';
|
||||
$connectionOptions['port'] = $_ENV['db_port'] ?? '3306';
|
||||
$connectionOptions['dbname'] = $_ENV['db_name'] ?? 'azuracast';
|
||||
$connectionOptions['user'] = $_ENV['db_username'] ?? 'azuracast';
|
||||
$connectionOptions['password'] = $_ENV['db_password'];
|
||||
}
|
||||
]
|
||||
);
|
||||
|
||||
try {
|
||||
// Fetch and store entity manager.
|
||||
|
@ -146,13 +136,11 @@ return [
|
|||
|
||||
// Redis cache
|
||||
Redis::class => function (Environment $environment) {
|
||||
$redisHost = $_ENV['REDIS_HOST'] ?? ($environment->isDocker() ? 'redis' : 'localhost');
|
||||
$redisPort = (int)($_ENV['REDIS_PORT'] ?? 6379);
|
||||
$redisDb = (int)($_ENV['REDIS_DB'] ?? 1);
|
||||
$settings = $environment->getRedisSettings();
|
||||
|
||||
$redis = new Redis();
|
||||
$redis->connect($redisHost, $redisPort, 15);
|
||||
$redis->select($redisDb);
|
||||
$redis->connect($settings['host'], $settings['port'], 15);
|
||||
$redis->select($settings['db']);
|
||||
|
||||
return $redis;
|
||||
},
|
||||
|
|
|
@ -11,40 +11,64 @@ class MigrateConfigCommand extends CommandAbstract
|
|||
SymfonyStyle $io,
|
||||
Environment $environment
|
||||
): int {
|
||||
$env_path = $environment->getBaseDirectory() . '/env.ini';
|
||||
$iniSettings = [];
|
||||
$envSettings = [];
|
||||
|
||||
if (file_exists($env_path)) {
|
||||
$iniSettings = parse_ini_file($env_path);
|
||||
$iniPath = $environment->getBaseDirectory() . '/env.ini';
|
||||
if (file_exists($iniPath)) {
|
||||
$envSettings = (array)parse_ini_file($iniPath);
|
||||
}
|
||||
|
||||
if (!empty($iniSettings['db_password'])) {
|
||||
$io->writeln(__('Configuration already set up.'));
|
||||
return 0;
|
||||
// Migrate from existing legacy config files.
|
||||
$legacyIniPath = $environment->getBaseDirectory() . '/app/env.ini';
|
||||
if (file_exists($legacyIniPath)) {
|
||||
$iniSettings = parse_ini_file($legacyIniPath);
|
||||
$envSettings = array_merge($envSettings, (array)$iniSettings);
|
||||
}
|
||||
|
||||
$legacyAppEnvFile = $environment->getBaseDirectory() . '/app/.env';
|
||||
if (file_exists($legacyAppEnvFile)) {
|
||||
$envSettings[Environment::APP_ENV] ??= file_get_contents($legacyAppEnvFile);
|
||||
}
|
||||
|
||||
$legacyDbConfFile = $environment->getBaseDirectory() . '/app/config/db.conf.php';
|
||||
if (file_exists($legacyDbConfFile)) {
|
||||
$dbConf = include($legacyDbConfFile);
|
||||
|
||||
$envSettings[Environment::DB_PASSWORD] ??= $dbConf['password'];
|
||||
if (isset($dbConf['user']) && 'root' === $dbConf['user']) {
|
||||
$envSettings[Environment::DB_USER] = 'root';
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($iniSettings['application_env']) && file_exists($environment->getBaseDirectory() . '/app/.env')) {
|
||||
$iniSettings['application_env'] = @file_get_contents($environment->getBaseDirectory() . '/app/.env');
|
||||
// Migrate from older environment variable names to new ones.
|
||||
$settingsToMigrate = [
|
||||
'application_env' => Environment::APP_ENV,
|
||||
'db_host' => Environment::DB_HOST,
|
||||
'db_port' => Environment::DB_PORT,
|
||||
'db_name' => Environment::DB_NAME,
|
||||
'db_username' => Environment::DB_USER,
|
||||
'db_password' => Environment::DB_PASSWORD,
|
||||
];
|
||||
|
||||
foreach ($settingsToMigrate as $oldSetting => $newSetting) {
|
||||
// In the future, if the env vars change, disregard this migration.
|
||||
if ($oldSetting === $newSetting) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (empty($iniSettings['db_password'])) {
|
||||
$legacy_path = $environment->getBaseDirectory() . '/app/env.ini';
|
||||
if (file_exists($legacy_path)) {
|
||||
$old_settings = parse_ini_file($legacy_path);
|
||||
$iniSettings = array_merge($iniSettings, $old_settings);
|
||||
}
|
||||
|
||||
if (file_exists($environment->getBaseDirectory() . '/app/config/db.conf.php')) {
|
||||
$db_conf = include($environment->getBaseDirectory() . '/app/config/db.conf.php');
|
||||
$iniSettings['db_password'] = $db_conf['password'];
|
||||
|
||||
if ($db_conf['user'] === 'root') {
|
||||
$iniSettings['db_username'] = 'root';
|
||||
}
|
||||
if (!empty($envSettings[$oldSetting])) {
|
||||
$envSettings[$newSetting] ??= $envSettings[$oldSetting];
|
||||
unset($envSettings[$oldSetting]);
|
||||
}
|
||||
}
|
||||
|
||||
$ini_data = [
|
||||
// Set sensible defaults for variables that may not be set.
|
||||
$envSettings[Environment::DB_HOST] ??= 'localhost';
|
||||
$envSettings[Environment::DB_PORT] ??= '3306';
|
||||
$envSettings[Environment::DB_NAME] ??= 'azuracast';
|
||||
$envSettings[Environment::DB_USER] ??= 'azuracast';
|
||||
|
||||
$iniData = [
|
||||
';',
|
||||
'; AzuraCast Environment Settings',
|
||||
';',
|
||||
|
@ -52,12 +76,16 @@ class MigrateConfigCommand extends CommandAbstract
|
|||
';',
|
||||
'[configuration]',
|
||||
];
|
||||
|
||||
foreach ($iniSettings as $setting_key => $setting_val) {
|
||||
$ini_data[] = $setting_key . '="' . $setting_val . '"';
|
||||
foreach ($envSettings as $settingKey => $settingVal) {
|
||||
$iniData[] = $settingKey . '="' . $settingVal . '"';
|
||||
}
|
||||
|
||||
file_put_contents($env_path, implode("\n", $ini_data));
|
||||
file_put_contents($iniPath, implode("\n", $iniData));
|
||||
|
||||
// Remove legacy files.
|
||||
@unlink($legacyIniPath);
|
||||
@unlink($legacyAppEnvFile);
|
||||
@unlink($legacyDbConfFile);
|
||||
|
||||
$io->writeln(__('Configuration successfully written.'));
|
||||
return 0;
|
||||
|
|
|
@ -36,12 +36,6 @@ class SetupCommand extends CommandAbstract
|
|||
|
||||
if ($update) {
|
||||
$io->note(__('Running in update mode.'));
|
||||
|
||||
if (!$environment->isDocker()) {
|
||||
$io->section(__('Migrating Legacy Configuration'));
|
||||
$this->runCommand($output, 'azuracast:config:migrate');
|
||||
$io->newLine();
|
||||
}
|
||||
}
|
||||
|
||||
$em = $di->get(EntityManagerInterface::class);
|
||||
|
|
|
@ -50,6 +50,17 @@ class Environment
|
|||
|
||||
public const LOG_LEVEL = 'LOG_LEVEL';
|
||||
|
||||
// Database and Cache Configuration Variables
|
||||
public const DB_HOST = 'MYSQL_HOST';
|
||||
public const DB_PORT = 'MYSQL_PORT';
|
||||
public const DB_NAME = 'MYSQL_DATABASE';
|
||||
public const DB_USER = 'MYSQL_USER';
|
||||
public const DB_PASSWORD = 'MYSQL_PASSWORD';
|
||||
|
||||
public const REDIS_HOST = 'REDIS_HOST';
|
||||
public const REDIS_PORT = 'REDIS_PORT';
|
||||
public const REDIS_DB = 'REDIS_DB';
|
||||
|
||||
// Default settings
|
||||
protected array $defaults = [
|
||||
self::APP_NAME => 'AzuraCast',
|
||||
|
@ -264,4 +275,28 @@ class Environment
|
|||
? LogLevel::NOTICE
|
||||
: LogLevel::DEBUG;
|
||||
}
|
||||
|
||||
public function getDatabaseSettings(): array
|
||||
{
|
||||
if (!isset($this->data[self::DB_NAME], $this->data[self::DB_USER], $this->data[self::DB_PASSWORD])) {
|
||||
throw new \InvalidArgumentException('Database connection parameters not provided.');
|
||||
}
|
||||
|
||||
return [
|
||||
'host' => $this->data[self::DB_HOST] ?? ($this->isDocker() ? 'mariadb' : 'localhost'),
|
||||
'port' => (int)($this->data[self::DB_PORT] ?? 3306),
|
||||
'dbname' => $this->data[self::DB_NAME],
|
||||
'user' => $this->data[self::DB_USER],
|
||||
'password' => $this->data[self::DB_PASSWORD],
|
||||
];
|
||||
}
|
||||
|
||||
public function getRedisSettings(): array
|
||||
{
|
||||
return [
|
||||
'host' => $this->data[self::REDIS_HOST] ?? ($this->isDocker() ? 'redis' : 'localhost'),
|
||||
'port' => (int)($this->data[self::REDIS_PORT] ?? 6379),
|
||||
'db' => (int)($this->data[self::REDIS_DB] ?? 1),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,5 +60,8 @@
|
|||
option : "{{ item.option }}"
|
||||
value : "{{ item.value }}"
|
||||
with_items :
|
||||
- { option: 'db_username', value: 'azuracast' }
|
||||
- { option: 'db_password', value: "{{ mysql_user_password }}" }
|
||||
- { option : 'MYSQL_HOST', value : 'azuracast' }
|
||||
- { option : 'MYSQL_PORT', value : '3306' }
|
||||
- { option : 'MYSQL_USER', value : 'azuracast' }
|
||||
- { option : 'MYSQL_DB', value : 'azuracast' }
|
||||
- { option : 'MYSQL_PASSWORD', value : "{{ mysql_user_password }}" }
|
||||
|
|
|
@ -11,6 +11,12 @@
|
|||
shell : php {{ www_base }}/bin/console azuracast:setup
|
||||
when : update_mode|bool == false
|
||||
|
||||
- name : Migrate Legacy Configuration (Update Mode)
|
||||
become : true
|
||||
become_user : azuracast
|
||||
shell : php {{ www_base }}/bin/console azuracast:config:migrate
|
||||
when : update_mode|bool == true
|
||||
|
||||
- name : Run AzuraCast Setup (Update Mode)
|
||||
become : true
|
||||
become_user : azuracast
|
||||
|
|
Loading…
Reference in New Issue