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
|
/env.ini
|
||||||
/app/env.ini
|
/app/env.ini
|
||||||
/app/.env
|
/app/.env
|
||||||
/env.ini
|
|
||||||
/azuracast.env
|
/azuracast.env
|
||||||
/util/fixtures/*
|
/util/fixtures/*
|
||||||
/util/fixtures/**/*
|
/util/fixtures/**/*
|
||||||
|
|
|
@ -62,32 +62,22 @@ return [
|
||||||
App\Doctrine\Event\SetExplicitChangeTracking $eventChangeTracking,
|
App\Doctrine\Event\SetExplicitChangeTracking $eventChangeTracking,
|
||||||
App\EventDispatcher $dispatcher
|
App\EventDispatcher $dispatcher
|
||||||
) {
|
) {
|
||||||
$connectionOptions = [
|
$connectionOptions = array_merge(
|
||||||
'host' => $_ENV['MYSQL_HOST'] ?? 'mariadb',
|
$environment->getDatabaseSettings(),
|
||||||
'port' => $_ENV['MYSQL_PORT'] ?? 3306,
|
[
|
||||||
'dbname' => $_ENV['MYSQL_DATABASE'],
|
'driver' => 'pdo_mysql',
|
||||||
'user' => $_ENV['MYSQL_USER'],
|
|
||||||
'password' => $_ENV['MYSQL_PASSWORD'],
|
|
||||||
'driver' => 'pdo_mysql',
|
|
||||||
'charset' => 'utf8mb4',
|
|
||||||
'defaultTableOptions' => [
|
|
||||||
'charset' => 'utf8mb4',
|
'charset' => 'utf8mb4',
|
||||||
'collate' => 'utf8mb4_general_ci',
|
'defaultTableOptions' => [
|
||||||
],
|
'charset' => 'utf8mb4',
|
||||||
'driverOptions' => [
|
'collate' => 'utf8mb4_general_ci',
|
||||||
// PDO::MYSQL_ATTR_INIT_COMMAND = 1002;
|
],
|
||||||
1002 => 'SET NAMES utf8mb4 COLLATE utf8mb4_general_ci',
|
'driverOptions' => [
|
||||||
],
|
// PDO::MYSQL_ATTR_INIT_COMMAND = 1002;
|
||||||
'platform' => new Doctrine\DBAL\Platforms\MariaDb1027Platform(),
|
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 {
|
try {
|
||||||
// Fetch and store entity manager.
|
// Fetch and store entity manager.
|
||||||
|
@ -146,13 +136,11 @@ return [
|
||||||
|
|
||||||
// Redis cache
|
// Redis cache
|
||||||
Redis::class => function (Environment $environment) {
|
Redis::class => function (Environment $environment) {
|
||||||
$redisHost = $_ENV['REDIS_HOST'] ?? ($environment->isDocker() ? 'redis' : 'localhost');
|
$settings = $environment->getRedisSettings();
|
||||||
$redisPort = (int)($_ENV['REDIS_PORT'] ?? 6379);
|
|
||||||
$redisDb = (int)($_ENV['REDIS_DB'] ?? 1);
|
|
||||||
|
|
||||||
$redis = new Redis();
|
$redis = new Redis();
|
||||||
$redis->connect($redisHost, $redisPort, 15);
|
$redis->connect($settings['host'], $settings['port'], 15);
|
||||||
$redis->select($redisDb);
|
$redis->select($settings['db']);
|
||||||
|
|
||||||
return $redis;
|
return $redis;
|
||||||
},
|
},
|
||||||
|
|
|
@ -11,40 +11,64 @@ class MigrateConfigCommand extends CommandAbstract
|
||||||
SymfonyStyle $io,
|
SymfonyStyle $io,
|
||||||
Environment $environment
|
Environment $environment
|
||||||
): int {
|
): int {
|
||||||
$env_path = $environment->getBaseDirectory() . '/env.ini';
|
$envSettings = [];
|
||||||
$iniSettings = [];
|
|
||||||
|
|
||||||
if (file_exists($env_path)) {
|
$iniPath = $environment->getBaseDirectory() . '/env.ini';
|
||||||
$iniSettings = parse_ini_file($env_path);
|
if (file_exists($iniPath)) {
|
||||||
|
$envSettings = (array)parse_ini_file($iniPath);
|
||||||
|
}
|
||||||
|
|
||||||
if (!empty($iniSettings['db_password'])) {
|
// Migrate from existing legacy config files.
|
||||||
$io->writeln(__('Configuration already set up.'));
|
$legacyIniPath = $environment->getBaseDirectory() . '/app/env.ini';
|
||||||
return 0;
|
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')) {
|
// Migrate from older environment variable names to new ones.
|
||||||
$iniSettings['application_env'] = @file_get_contents($environment->getBaseDirectory() . '/app/.env');
|
$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,
|
||||||
|
];
|
||||||
|
|
||||||
if (empty($iniSettings['db_password'])) {
|
foreach ($settingsToMigrate as $oldSetting => $newSetting) {
|
||||||
$legacy_path = $environment->getBaseDirectory() . '/app/env.ini';
|
// In the future, if the env vars change, disregard this migration.
|
||||||
if (file_exists($legacy_path)) {
|
if ($oldSetting === $newSetting) {
|
||||||
$old_settings = parse_ini_file($legacy_path);
|
continue;
|
||||||
$iniSettings = array_merge($iniSettings, $old_settings);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file_exists($environment->getBaseDirectory() . '/app/config/db.conf.php')) {
|
if (!empty($envSettings[$oldSetting])) {
|
||||||
$db_conf = include($environment->getBaseDirectory() . '/app/config/db.conf.php');
|
$envSettings[$newSetting] ??= $envSettings[$oldSetting];
|
||||||
$iniSettings['db_password'] = $db_conf['password'];
|
unset($envSettings[$oldSetting]);
|
||||||
|
|
||||||
if ($db_conf['user'] === 'root') {
|
|
||||||
$iniSettings['db_username'] = 'root';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$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',
|
'; AzuraCast Environment Settings',
|
||||||
';',
|
';',
|
||||||
|
@ -52,12 +76,16 @@ class MigrateConfigCommand extends CommandAbstract
|
||||||
';',
|
';',
|
||||||
'[configuration]',
|
'[configuration]',
|
||||||
];
|
];
|
||||||
|
foreach ($envSettings as $settingKey => $settingVal) {
|
||||||
foreach ($iniSettings as $setting_key => $setting_val) {
|
$iniData[] = $settingKey . '="' . $settingVal . '"';
|
||||||
$ini_data[] = $setting_key . '="' . $setting_val . '"';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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.'));
|
$io->writeln(__('Configuration successfully written.'));
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -36,12 +36,6 @@ class SetupCommand extends CommandAbstract
|
||||||
|
|
||||||
if ($update) {
|
if ($update) {
|
||||||
$io->note(__('Running in update mode.'));
|
$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);
|
$em = $di->get(EntityManagerInterface::class);
|
||||||
|
|
|
@ -50,6 +50,17 @@ class Environment
|
||||||
|
|
||||||
public const LOG_LEVEL = 'LOG_LEVEL';
|
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
|
// Default settings
|
||||||
protected array $defaults = [
|
protected array $defaults = [
|
||||||
self::APP_NAME => 'AzuraCast',
|
self::APP_NAME => 'AzuraCast',
|
||||||
|
@ -264,4 +275,28 @@ class Environment
|
||||||
? LogLevel::NOTICE
|
? LogLevel::NOTICE
|
||||||
: LogLevel::DEBUG;
|
: 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),
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,64 +1,67 @@
|
||||||
---
|
---
|
||||||
- name: (Prod) Generate MariaDB User Password
|
- name : (Prod) Generate MariaDB User Password
|
||||||
command: pwgen 8 -sn 1
|
command : pwgen 8 -sn 1
|
||||||
register: prod_mysql_user_password
|
register : prod_mysql_user_password
|
||||||
when: app_env == "production"
|
when : app_env == "production"
|
||||||
|
|
||||||
- name: Assign User Password
|
- name : Assign User Password
|
||||||
set_fact:
|
set_fact :
|
||||||
mysql_user_password: "{{ prod_mysql_user_password.stdout if app_env == 'production' else dev_mysql_user_password }}"
|
mysql_user_password : "{{ prod_mysql_user_password.stdout if app_env == 'production' else dev_mysql_user_password }}"
|
||||||
|
|
||||||
- name: Add configuration
|
- name : Add configuration
|
||||||
template: src=my.cnf.j2 dest=/etc/mysql/conf.d/my.cnf owner=root group=root mode=0644
|
template : src=my.cnf.j2 dest=/etc/mysql/conf.d/my.cnf owner=root group=root mode=0644
|
||||||
|
|
||||||
- name: Add empty root .my.cnf (if none exists)
|
- name : Add empty root .my.cnf (if none exists)
|
||||||
template: src=root_empty_my.cnf.j2 dest=/root/.my.cnf owner=root group=root mode=0600 force=no
|
template : src=root_empty_my.cnf.j2 dest=/root/.my.cnf owner=root group=root mode=0600 force=no
|
||||||
|
|
||||||
- name: MariaDB Cleanup
|
- name : MariaDB Cleanup
|
||||||
command: 'mysql --defaults-extra-file=/root/.my.cnf -ne "{{ item }}"'
|
command : 'mysql --defaults-extra-file=/root/.my.cnf -ne "{{ item }}"'
|
||||||
with_items:
|
with_items :
|
||||||
- "DELETE FROM mysql.user WHERE User=''"
|
- "DELETE FROM mysql.user WHERE User=''"
|
||||||
- "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"
|
- "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"
|
||||||
- "DROP DATABASE test"
|
- "DROP DATABASE test"
|
||||||
- "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'"
|
- "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'"
|
||||||
changed_when: False
|
changed_when : False
|
||||||
ignore_errors: True
|
ignore_errors : True
|
||||||
|
|
||||||
# MySQL Secure Installation
|
# MySQL Secure Installation
|
||||||
- name: Set up AzuraCast database user
|
- name : Set up AzuraCast database user
|
||||||
mysql_user:
|
mysql_user :
|
||||||
name: azuracast
|
name : azuracast
|
||||||
host: "localhost"
|
host : "localhost"
|
||||||
password: "{{ mysql_user_password }}"
|
password : "{{ mysql_user_password }}"
|
||||||
priv: "azuracast.*:ALL,GRANT"
|
priv : "azuracast.*:ALL,GRANT"
|
||||||
state: present
|
state : present
|
||||||
notify: send mysql notification
|
notify : send mysql notification
|
||||||
|
|
||||||
- name: Add .my.cnf
|
- name : Add .my.cnf
|
||||||
template: src=user_my.cnf.j2 dest="{{ app_base }}/.my.cnf" owner=azuracast group=www-data mode=0644 force=yes
|
template : src=user_my.cnf.j2 dest="{{ app_base }}/.my.cnf" owner=azuracast group=www-data mode=0644 force=yes
|
||||||
|
|
||||||
- name: Reload privilege tables
|
- name : Reload privilege tables
|
||||||
command: 'mysql --defaults-extra-file=/root/.my.cnf -ne "{{ item }}"'
|
command : 'mysql --defaults-extra-file=/root/.my.cnf -ne "{{ item }}"'
|
||||||
with_items:
|
with_items :
|
||||||
- "FLUSH PRIVILEGES"
|
- "FLUSH PRIVILEGES"
|
||||||
changed_when: False
|
changed_when : False
|
||||||
|
|
||||||
# Create Database
|
# Create Database
|
||||||
- name: Create MySQL Database
|
- name : Create MySQL Database
|
||||||
mysql_db:
|
mysql_db :
|
||||||
config_file: "/root/.my.cnf"
|
config_file : "/root/.my.cnf"
|
||||||
name: azuracast
|
name : azuracast
|
||||||
state: present
|
state : present
|
||||||
collation: utf8mb4_unicode_ci
|
collation : utf8mb4_unicode_ci
|
||||||
encoding: utf8mb4
|
encoding : utf8mb4
|
||||||
register: azuracast_db_created
|
register : azuracast_db_created
|
||||||
|
|
||||||
- name: Set up environment file
|
- name : Set up environment file
|
||||||
ini_file:
|
ini_file :
|
||||||
dest: "{{ www_base }}/env.ini"
|
dest : "{{ www_base }}/env.ini"
|
||||||
section: "configuration"
|
section : "configuration"
|
||||||
option: "{{ item.option }}"
|
option : "{{ item.option }}"
|
||||||
value: "{{ item.value }}"
|
value : "{{ item.value }}"
|
||||||
with_items:
|
with_items :
|
||||||
- { option: 'db_username', value: 'azuracast' }
|
- { option : 'MYSQL_HOST', value : 'azuracast' }
|
||||||
- { option: 'db_password', value: "{{ mysql_user_password }}" }
|
- { option : 'MYSQL_PORT', value : '3306' }
|
||||||
|
- { option : 'MYSQL_USER', value : 'azuracast' }
|
||||||
|
- { option : 'MYSQL_DB', value : 'azuracast' }
|
||||||
|
- { option : 'MYSQL_PASSWORD', value : "{{ mysql_user_password }}" }
|
||||||
|
|
|
@ -1,18 +1,24 @@
|
||||||
---
|
---
|
||||||
- name: Ensure update script continues to be executable
|
- name : Ensure update script continues to be executable
|
||||||
file: path="{{ item }}" state=touch mode="a+x"
|
file : path="{{ item }}" state=touch mode="a+x"
|
||||||
with_items:
|
with_items :
|
||||||
- "{{ www_base }}/update.sh"
|
- "{{ www_base }}/update.sh"
|
||||||
- "{{ www_base }}/bin/console"
|
- "{{ www_base }}/bin/console"
|
||||||
|
|
||||||
- name: Run AzuraCast Setup (Install Mode)
|
- name : Run AzuraCast Setup (Install Mode)
|
||||||
become: true
|
become : true
|
||||||
become_user: azuracast
|
become_user : azuracast
|
||||||
shell: php {{ www_base }}/bin/console azuracast:setup
|
shell : php {{ www_base }}/bin/console azuracast:setup
|
||||||
when: update_mode|bool == false
|
when : update_mode|bool == false
|
||||||
|
|
||||||
- name: Run AzuraCast Setup (Update Mode)
|
- name : Migrate Legacy Configuration (Update Mode)
|
||||||
become: true
|
become : true
|
||||||
become_user: azuracast
|
become_user : azuracast
|
||||||
shell: php {{ www_base }}/bin/console azuracast:setup --update
|
shell : php {{ www_base }}/bin/console azuracast:config:migrate
|
||||||
when: update_mode|bool == true
|
when : update_mode|bool == true
|
||||||
|
|
||||||
|
- name : Run AzuraCast Setup (Update Mode)
|
||||||
|
become : true
|
||||||
|
become_user : azuracast
|
||||||
|
shell : php {{ www_base }}/bin/console azuracast:setup --update
|
||||||
|
when : update_mode|bool == true
|
||||||
|
|
Loading…
Reference in New Issue