sintonia/legacy/build/airtime-setup/load.php

190 lines
4.2 KiB
PHP
Raw Normal View History

2014-11-28 00:48:03 +01:00
<?php
2021-10-11 16:10:47 +02:00
function booleanReduce($a, $b)
{
return $a && $b;
}
2014-11-28 01:10:23 +01:00
/**
* Check to see if Airtime is properly configured.
*
2021-10-11 16:10:47 +02:00
* @return bool true if all Airtime dependencies and services are
* properly configured and running
2014-11-28 01:10:23 +01:00
*/
2021-10-11 16:10:47 +02:00
function checkConfiguration()
{
$r1 = array_reduce(checkPhpDependencies(), 'booleanReduce', true);
$r2 = array_reduce(checkExternalServices(), 'booleanReduce', true);
return $r1 && $r2;
2014-11-28 01:10:23 +01:00
}
2014-11-28 21:30:11 +01:00
/**
* Check for Airtime's PHP dependencies and return an associative
2021-10-11 16:10:47 +02:00
* array with the results.
2014-11-28 21:30:11 +01:00
*
* @return array associative array of dependency check results
*/
2021-10-11 16:10:47 +02:00
function checkPhpDependencies()
{
return [
'postgres' => checkDatabaseDependencies(),
];
2014-11-28 00:48:03 +01:00
}
2014-11-28 21:30:11 +01:00
/**
2021-10-11 16:10:47 +02:00
* Check that the PHP dependencies for the database exist.
2014-11-28 21:30:11 +01:00
*
2021-10-11 16:10:47 +02:00
* @return bool true if the database dependencies exist
2014-11-28 21:30:11 +01:00
*/
2021-10-11 16:10:47 +02:00
function checkDatabaseDependencies()
{
2014-12-01 21:49:53 +01:00
global $extensions;
2014-11-28 21:30:11 +01:00
// Check the PHP extension list for the Postgres db extensions
2021-10-11 16:10:47 +02:00
return in_array('pdo_pgsql', $extensions)
&& in_array('pgsql', $extensions);
2014-11-28 21:30:11 +01:00
}
2014-11-28 00:48:03 +01:00
/**
* Check that all external services are configured correctly and return an associative
2021-10-11 16:10:47 +02:00
* array with the results.
2020-05-19 16:16:31 +02:00
*
* @return array associative array of external service check results
*/
2021-10-11 16:10:47 +02:00
function checkExternalServices()
{
return [
'database' => checkDatabaseConfiguration(),
'analyzer' => checkAnalyzerService(),
'pypo' => checkPlayoutService(),
'liquidsoap' => checkLiquidsoapService(),
'rabbitmq' => checkRMQConnection(),
'celery' => checkCeleryService(),
'api' => checkApiService(),
];
}
2014-11-28 21:30:11 +01:00
/**
2021-10-11 16:10:47 +02:00
* Check the database configuration by fetching a connection from Propel.
2014-11-28 21:30:11 +01:00
*
2021-10-11 16:10:47 +02:00
* @return bool true if a connection is made to the database
2014-11-28 21:30:11 +01:00
*/
2021-10-11 16:10:47 +02:00
function checkDatabaseConfiguration()
{
configureDatabase();
2014-11-28 00:48:03 +01:00
try {
2014-11-28 21:30:11 +01:00
// Try to establish a database connection. If something goes
// wrong, the database is improperly configured
Propel::getConnection();
Propel::close();
2014-11-28 00:48:03 +01:00
} catch (Exception $e) {
return false;
}
return true;
}
2014-11-28 21:30:11 +01:00
/**
2021-10-11 16:10:47 +02:00
* Initialize Propel to configure the Airtime database.
2014-11-28 21:30:11 +01:00
*/
2021-10-11 16:10:47 +02:00
function configureDatabase()
{
Propel::init(PROPEL_CONFIG_FILEPATH);
}
2014-12-09 23:48:16 +01:00
/**
2021-10-11 16:10:47 +02:00
* Check that we can connect to RabbitMQ.
2020-05-19 16:16:31 +02:00
*
2014-12-19 17:58:47 +01:00
* @return true if the RabbitMQ connection can be established
2014-12-09 23:48:16 +01:00
*/
2021-10-11 16:10:47 +02:00
function checkRMQConnection()
{
$config = Config::getConfig();
$conn = new \PhpAmqpLib\Connection\AMQPStreamConnection(
$config['rabbitmq']['host'],
$config['rabbitmq']['port'],
$config['rabbitmq']['user'],
$config['rabbitmq']['password'],
$config['rabbitmq']['vhost']
2021-10-11 16:10:47 +02:00
);
2014-12-09 23:48:16 +01:00
return isset($conn);
}
2014-12-19 17:58:47 +01:00
/**
2021-10-11 16:10:47 +02:00
* Check if airtime-analyzer is currently running.
2020-05-19 16:16:31 +02:00
*
2021-10-11 16:10:47 +02:00
* @return bool true if airtime-analyzer is running
2014-12-19 17:58:47 +01:00
*/
2021-10-11 16:10:47 +02:00
function checkAnalyzerService()
{
exec('pgrep -f libretime-analyzer', $out, $status);
if (($out > 0) && $status == 0) {
return posix_kill(rtrim($out[0]), 0);
}
2021-10-11 16:10:47 +02:00
return $status == 0;
}
2014-12-19 17:58:47 +01:00
/**
2021-10-12 14:05:55 +02:00
* Check if libretime-playout is currently running.
2020-05-19 16:16:31 +02:00
*
2021-10-12 14:05:55 +02:00
* @return bool true if libretime-playout is running
2014-12-19 17:58:47 +01:00
*/
2021-10-11 16:10:47 +02:00
function checkPlayoutService()
{
2021-10-12 14:05:55 +02:00
exec('pgrep -f libretime-playout', $out, $status);
if ($out > 0) {
return posix_kill(rtrim($out[0]), 0);
}
2021-10-11 16:10:47 +02:00
return $status == 0;
}
2014-12-19 17:58:47 +01:00
/**
2021-10-12 14:05:55 +02:00
* Check if libretime-liquidsoap is currently running.
2020-05-19 16:16:31 +02:00
*
2021-10-12 14:05:55 +02:00
* @return bool true if libretime-liquidsoap is running
2014-12-19 17:58:47 +01:00
*/
2021-10-11 16:10:47 +02:00
function checkLiquidsoapService()
{
2021-10-12 14:05:55 +02:00
exec('pgrep -f libretime-liquidsoap', $out, $status);
if ($out > 0) {
return posix_kill(rtrim($out[0]), 0);
}
2021-10-11 16:10:47 +02:00
return $status == 0;
}
/**
2021-10-12 14:05:55 +02:00
* Check if libretime-celery is currently running.
2020-05-19 16:16:31 +02:00
*
2021-10-12 14:05:55 +02:00
* @return bool true if libretime-celery is running
*/
2021-10-11 16:10:47 +02:00
function checkCeleryService()
{
exec('pgrep -f -u celery libretime_worker', $out, $status);
if (array_key_exists(0, $out) && $status == 0) {
return 1;
}
2021-10-11 16:10:47 +02:00
return $status == 0;
}
2020-01-30 14:47:36 +01:00
/**
2021-10-11 16:10:47 +02:00
* Check if libretime-api is currently running.
2020-01-30 14:47:36 +01:00
*
2021-10-11 16:10:47 +02:00
* @return bool true if libretime-api is running
2020-01-30 14:47:36 +01:00
*/
2021-10-11 16:10:47 +02:00
function checkApiService()
{
exec('pgrep -f -u www-data uwsgi', $out, $status);
2020-01-30 14:47:36 +01:00
if (array_key_exists(0, $out) && $status == 0) {
return 1;
}
2021-10-11 16:10:47 +02:00
2020-01-30 14:47:36 +01:00
return $status == 0;
}