Unify env var names on Ansible installs with their Docker counterparts.

This commit is contained in:
Buster "Silver Eagle" Neece 2021-01-21 13:32:47 -06:00
parent 5285024018
commit 64423a7097
No known key found for this signature in database
GPG Key ID: 6D9E12FF03411F4E
7 changed files with 186 additions and 133 deletions

1
.gitignore vendored
View File

@ -16,7 +16,6 @@ tmp/cache/*---*
/env.ini
/app/env.ini
/app/.env
/env.ini
/azuracast.env
/util/fixtures/*
/util/fixtures/**/*

View File

@ -62,32 +62,22 @@ 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'],
'driver' => 'pdo_mysql',
'charset' => 'utf8mb4',
'defaultTableOptions' => [
$connectionOptions = array_merge(
$environment->getDatabaseSettings(),
[
'driver' => 'pdo_mysql',
'charset' => 'utf8mb4',
'collate' => 'utf8mb4_general_ci',
],
'driverOptions' => [
// PDO::MYSQL_ATTR_INIT_COMMAND = 1002;
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'];
}
'defaultTableOptions' => [
'charset' => 'utf8mb4',
'collate' => 'utf8mb4_general_ci',
],
'driverOptions' => [
// PDO::MYSQL_ATTR_INIT_COMMAND = 1002;
1002 => 'SET NAMES utf8mb4 COLLATE utf8mb4_general_ci',
],
'platform' => new Doctrine\DBAL\Platforms\MariaDb1027Platform(),
]
);
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;
},

View File

@ -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,
];
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);
foreach ($settingsToMigrate as $oldSetting => $newSetting) {
// In the future, if the env vars change, disregard this migration.
if ($oldSetting === $newSetting) {
continue;
}
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;

View File

@ -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);

View File

@ -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),
];
}
}

View File

@ -1,64 +1,67 @@
---
- name: (Prod) Generate MariaDB User Password
command: pwgen 8 -sn 1
register: prod_mysql_user_password
when: app_env == "production"
- name : (Prod) Generate MariaDB User Password
command : pwgen 8 -sn 1
register : prod_mysql_user_password
when : app_env == "production"
- name: Assign User Password
set_fact:
mysql_user_password: "{{ prod_mysql_user_password.stdout if app_env == 'production' else dev_mysql_user_password }}"
- name : Assign User Password
set_fact :
mysql_user_password : "{{ prod_mysql_user_password.stdout if app_env == 'production' else dev_mysql_user_password }}"
- name: Add configuration
template: src=my.cnf.j2 dest=/etc/mysql/conf.d/my.cnf owner=root group=root mode=0644
- name : Add configuration
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)
template: src=root_empty_my.cnf.j2 dest=/root/.my.cnf owner=root group=root mode=0600 force=no
- 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
- name: MariaDB Cleanup
command: 'mysql --defaults-extra-file=/root/.my.cnf -ne "{{ item }}"'
with_items:
- "DELETE FROM mysql.user WHERE User=''"
- "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"
- "DROP DATABASE test"
- "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'"
changed_when: False
ignore_errors: True
- name : MariaDB Cleanup
command : 'mysql --defaults-extra-file=/root/.my.cnf -ne "{{ item }}"'
with_items :
- "DELETE FROM mysql.user WHERE User=''"
- "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"
- "DROP DATABASE test"
- "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'"
changed_when : False
ignore_errors : True
# MySQL Secure Installation
- name: Set up AzuraCast database user
mysql_user:
name: azuracast
host: "localhost"
password: "{{ mysql_user_password }}"
priv: "azuracast.*:ALL,GRANT"
state: present
notify: send mysql notification
# MySQL Secure Installation
- name : Set up AzuraCast database user
mysql_user :
name : azuracast
host : "localhost"
password : "{{ mysql_user_password }}"
priv : "azuracast.*:ALL,GRANT"
state : present
notify : send mysql notification
- name: Add .my.cnf
template: src=user_my.cnf.j2 dest="{{ app_base }}/.my.cnf" owner=azuracast group=www-data mode=0644 force=yes
- name : Add .my.cnf
template : src=user_my.cnf.j2 dest="{{ app_base }}/.my.cnf" owner=azuracast group=www-data mode=0644 force=yes
- name: Reload privilege tables
command: 'mysql --defaults-extra-file=/root/.my.cnf -ne "{{ item }}"'
with_items:
- "FLUSH PRIVILEGES"
changed_when: False
- name : Reload privilege tables
command : 'mysql --defaults-extra-file=/root/.my.cnf -ne "{{ item }}"'
with_items :
- "FLUSH PRIVILEGES"
changed_when : False
# Create Database
- name: Create MySQL Database
mysql_db:
config_file: "/root/.my.cnf"
name: azuracast
state: present
collation: utf8mb4_unicode_ci
encoding: utf8mb4
register: azuracast_db_created
# Create Database
- name : Create MySQL Database
mysql_db :
config_file : "/root/.my.cnf"
name : azuracast
state : present
collation : utf8mb4_unicode_ci
encoding : utf8mb4
register : azuracast_db_created
- name: Set up environment file
ini_file:
dest: "{{ www_base }}/env.ini"
section: "configuration"
option: "{{ item.option }}"
value: "{{ item.value }}"
with_items:
- { option: 'db_username', value: 'azuracast' }
- { option: 'db_password', value: "{{ mysql_user_password }}" }
- name : Set up environment file
ini_file :
dest : "{{ www_base }}/env.ini"
section : "configuration"
option : "{{ item.option }}"
value : "{{ item.value }}"
with_items :
- { 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 }}" }

View File

@ -1,18 +1,24 @@
---
- name: Ensure update script continues to be executable
file: path="{{ item }}" state=touch mode="a+x"
with_items:
- "{{ www_base }}/update.sh"
- "{{ www_base }}/bin/console"
- name : Ensure update script continues to be executable
file : path="{{ item }}" state=touch mode="a+x"
with_items :
- "{{ www_base }}/update.sh"
- "{{ www_base }}/bin/console"
- name: Run AzuraCast Setup (Install Mode)
become: true
become_user: azuracast
shell: php {{ www_base }}/bin/console azuracast:setup
when: update_mode|bool == false
- name : Run AzuraCast Setup (Install Mode)
become : true
become_user : azuracast
shell : php {{ www_base }}/bin/console azuracast:setup
when : update_mode|bool == false
- 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
- 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
shell : php {{ www_base }}/bin/console azuracast:setup --update
when : update_mode|bool == true