-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
check.php
195 lines (165 loc) · 4.29 KB
/
check.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
use PhpAmqpLib\Exception\AMQPRuntimeException;
function booleanReduce($a, $b)
{
return $a && $b;
}
/**
* Check to see if Airtime is properly configured.
*
* @return bool true if all Airtime dependencies and services are
* properly configured and running
*/
function checkConfiguration()
{
$r1 = array_reduce(checkPhpDependencies(), 'booleanReduce', true);
$r2 = array_reduce(checkExternalServices(), 'booleanReduce', true);
return $r1 && $r2;
}
/**
* Check for Airtime's PHP dependencies and return an associative
* array with the results.
*
* @return array associative array of dependency check results
*/
function checkPhpDependencies()
{
return [
'postgres' => checkDatabaseDependencies(),
];
}
/**
* Check that the PHP dependencies for the database exist.
*
* @return bool true if the database dependencies exist
*/
function checkDatabaseDependencies()
{
global $extensions;
// Check the PHP extension list for the Postgres db extensions
return in_array('pdo_pgsql', $extensions)
&& in_array('pgsql', $extensions);
}
function with_systemd()
{
return !empty(shell_exec('which systemctl'));
}
/**
* Check that all external services are configured correctly and return an associative
* array with the results.
*
* @return array associative array of external service check results
*/
function checkExternalServices()
{
$result = [
'database' => checkDatabaseConfiguration(),
'rabbitmq' => checkRMQConnection(),
];
if (with_systemd()) {
$result['analyzer'] = checkAnalyzerService();
$result['pypo'] = checkPlayoutService();
$result['liquidsoap'] = checkLiquidsoapService();
$result['celery'] = checkCeleryService();
$result['api'] = checkApiService();
}
return $result;
}
/**
* Check the database configuration by fetching a connection from Propel.
*
* @return bool true if a connection is made to the database
*/
function checkDatabaseConfiguration()
{
configureDatabase();
try {
// Try to establish a database connection. If something goes
// wrong, the database is improperly configured
Propel::getConnection();
Propel::close();
} catch (Exception $exc) {
Logging::error($exc->getMessage());
return false;
}
return true;
}
/**
* Initialize Propel to configure the Airtime database.
*/
function configureDatabase()
{
Propel::init(PROPEL_CONFIG_FILEPATH);
}
/**
* Check that we can connect to RabbitMQ.
*
* @return true if the RabbitMQ connection can be established
*/
function checkRMQConnection()
{
$config = Config::getConfig();
try {
$conn = new \PhpAmqpLib\Connection\AMQPStreamConnection(
$config['rabbitmq']['host'],
$config['rabbitmq']['port'],
$config['rabbitmq']['user'],
$config['rabbitmq']['password'],
$config['rabbitmq']['vhost']
);
return isset($conn);
} catch (AMQPRuntimeException $exc) {
Logging::error($exc->getMessage());
return false;
}
}
/**
* Check if airtime-analyzer is currently running.
*
* @return bool true if airtime-analyzer is running
*/
function checkAnalyzerService()
{
exec('systemctl is-active libretime-analyzer --quiet', $out, $status);
return $status == 0;
}
/**
* Check if libretime-playout is currently running.
*
* @return bool true if libretime-playout is running
*/
function checkPlayoutService()
{
exec('systemctl is-active libretime-playout --quiet', $out, $status);
return $status == 0;
}
/**
* Check if libretime-liquidsoap is currently running.
*
* @return bool true if libretime-liquidsoap is running
*/
function checkLiquidsoapService()
{
exec('systemctl is-active libretime-liquidsoap --quiet', $out, $status);
return $status == 0;
}
/**
* Check if libretime-worker is currently running.
*
* @return bool true if libretime-worker is running
*/
function checkCeleryService()
{
exec('systemctl is-active libretime-worker --quiet', $out, $status);
return $status == 0;
}
/**
* Check if libretime-api is currently running.
*
* @return bool true if libretime-api is running
*/
function checkApiService()
{
exec('systemctl status libretime-api --quiet', $out, $status);
return $status == 0;
}