libretime/legacy/application/check.php

185 lines
4.0 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
function with_systemd()
{
return !empty(shell_exec('which systemctl'));
}
/**
* 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()
{
$result = [
2021-10-11 16:10:47 +02:00
'database' => checkDatabaseConfiguration(),
'rabbitmq' => checkRMQConnection(),
];
if (with_systemd()) {
$result['analyzer'] = checkAnalyzerService();
$result['pypo'] = checkPlayoutService();
$result['liquidsoap'] = checkLiquidsoapService();
$result['celery'] = checkCeleryService();
$result['api'] = checkApiService();
}
return $result;
}
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('systemctl is-active libretime-analyzer --quiet', $out, $status);
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()
{
exec('systemctl is-active libretime-playout --quiet', $out, $status);
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()
{
exec('systemctl is-active libretime-liquidsoap --quiet', $out, $status);
2021-10-11 16:10:47 +02:00
return $status == 0;
}
/**
* Check if libretime-worker is currently running.
2020-05-19 16:16:31 +02:00
*
* @return bool true if libretime-worker is running
*/
2021-10-11 16:10:47 +02:00
function checkCeleryService()
{
exec('systemctl is-active libretime-worker --quiet', $out, $status);
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('systemctl status libretime-api --quiet', $out, $status);
2021-10-11 16:10:47 +02:00
2020-01-30 14:47:36 +01:00
return $status == 0;
}