2018-02-17 02:58:58 +01:00
|
|
|
<?php
|
2020-10-15 00:19:31 +02:00
|
|
|
|
2021-07-19 07:53:45 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-08-05 00:05:14 +02:00
|
|
|
namespace App\Controller\Api\Stations;
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2019-08-07 06:33:55 +02:00
|
|
|
use App\Entity;
|
2022-06-22 03:48:32 +02:00
|
|
|
use App\Exception\StationUnsupportedException;
|
2019-09-10 18:40:31 +02:00
|
|
|
use App\Exception\Supervisor\NotRunningException;
|
2019-08-09 17:00:21 +02:00
|
|
|
use App\Http\Response;
|
|
|
|
use App\Http\ServerRequest;
|
2022-05-29 02:30:12 +02:00
|
|
|
use App\Nginx\Nginx;
|
2021-12-29 22:19:34 +01:00
|
|
|
use App\OpenApi;
|
2022-06-22 03:48:32 +02:00
|
|
|
use App\Radio\Adapters;
|
2018-08-05 00:05:14 +02:00
|
|
|
use App\Radio\Configuration;
|
2022-06-21 17:45:42 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2021-12-29 06:36:30 +01:00
|
|
|
use OpenApi\Attributes as OA;
|
2019-08-07 06:33:55 +02:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2022-05-08 20:05:02 +02:00
|
|
|
use Throwable;
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2021-12-29 06:36:30 +01:00
|
|
|
#[
|
|
|
|
OA\Get(
|
|
|
|
path: '/station/{station_id}/status',
|
|
|
|
operationId: 'getServiceStatus',
|
|
|
|
description: 'Retrieve the current status of all serivces associated with the radio broadcast.',
|
2021-12-29 22:19:34 +01:00
|
|
|
security: OpenApi::API_KEY_SECURITY,
|
2021-12-29 06:36:30 +01:00
|
|
|
tags: ['Stations: Service Control'],
|
|
|
|
parameters: [
|
2022-01-11 05:31:24 +01:00
|
|
|
new OA\Parameter(ref: OpenApi::REF_STATION_ID_REQUIRED),
|
2021-12-29 06:36:30 +01:00
|
|
|
],
|
|
|
|
responses: [
|
|
|
|
new OA\Response(
|
|
|
|
response: 200,
|
|
|
|
description: 'Success',
|
|
|
|
content: new OA\JsonContent(
|
2021-12-29 22:19:34 +01:00
|
|
|
ref: '#/components/schemas/Api_StationServiceStatus'
|
2021-12-29 06:36:30 +01:00
|
|
|
)
|
|
|
|
),
|
2022-01-11 05:31:24 +01:00
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_ACCESS_DENIED, response: 403),
|
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_NOT_FOUND, response: 404),
|
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_GENERIC_ERROR, response: 500),
|
2021-12-29 06:36:30 +01:00
|
|
|
]
|
|
|
|
),
|
|
|
|
OA\Post(
|
|
|
|
path: '/station/{station_id}/restart',
|
|
|
|
operationId: 'restartServices',
|
|
|
|
description: 'Restart all services associated with the radio broadcast.',
|
2021-12-29 22:19:34 +01:00
|
|
|
security: OpenApi::API_KEY_SECURITY,
|
2021-12-29 06:36:30 +01:00
|
|
|
tags: ['Stations: Service Control'],
|
|
|
|
parameters: [
|
2022-01-11 05:31:24 +01:00
|
|
|
new OA\Parameter(ref: OpenApi::REF_STATION_ID_REQUIRED),
|
2021-12-29 06:36:30 +01:00
|
|
|
],
|
|
|
|
responses: [
|
2022-01-11 05:31:24 +01:00
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_SUCCESS, response: 200),
|
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_ACCESS_DENIED, response: 403),
|
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_NOT_FOUND, response: 404),
|
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_GENERIC_ERROR, response: 500),
|
2021-12-29 06:36:30 +01:00
|
|
|
]
|
|
|
|
),
|
|
|
|
OA\Post(
|
|
|
|
path: '/station/{station_id}/frontend/{action}',
|
|
|
|
operationId: 'doFrontendServiceAction',
|
|
|
|
description: 'Perform service control actions on the radio frontend (Icecast, SHOUTcast, etc.)',
|
2021-12-29 22:19:34 +01:00
|
|
|
security: OpenApi::API_KEY_SECURITY,
|
2021-12-29 06:36:30 +01:00
|
|
|
tags: ['Stations: Service Control'],
|
|
|
|
parameters: [
|
2022-01-11 05:31:24 +01:00
|
|
|
new OA\Parameter(ref: OpenApi::REF_STATION_ID_REQUIRED),
|
2021-12-29 06:36:30 +01:00
|
|
|
new OA\Parameter(
|
|
|
|
name: 'action',
|
|
|
|
description: 'The action to perform (start, stop, restart)',
|
|
|
|
in: 'path',
|
|
|
|
required: false,
|
|
|
|
schema: new OA\Schema(type: 'string', default: 'restart')
|
|
|
|
),
|
|
|
|
],
|
|
|
|
responses: [
|
2022-01-11 05:31:24 +01:00
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_SUCCESS, response: 200),
|
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_ACCESS_DENIED, response: 403),
|
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_NOT_FOUND, response: 404),
|
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_GENERIC_ERROR, response: 500),
|
2021-12-29 06:36:30 +01:00
|
|
|
]
|
|
|
|
),
|
|
|
|
OA\Post(
|
|
|
|
path: '/station/{station_id}/backend/{action}',
|
|
|
|
operationId: 'doBackendServiceAction',
|
|
|
|
description: 'Perform service control actions on the radio backend (Liquidsoap)',
|
2021-12-29 22:19:34 +01:00
|
|
|
security: OpenApi::API_KEY_SECURITY,
|
2021-12-29 06:36:30 +01:00
|
|
|
tags: ['Stations: Service Control'],
|
|
|
|
parameters: [
|
2022-01-11 05:31:24 +01:00
|
|
|
new OA\Parameter(ref: OpenApi::REF_STATION_ID_REQUIRED),
|
2021-12-29 06:36:30 +01:00
|
|
|
new OA\Parameter(
|
|
|
|
name: 'action',
|
|
|
|
description: 'The action to perform (for all: start, stop, restart, skip, disconnect)',
|
|
|
|
in: 'path',
|
|
|
|
required: false,
|
|
|
|
schema: new OA\Schema(type: 'string', default: 'restart')
|
|
|
|
),
|
|
|
|
],
|
|
|
|
responses: [
|
2022-01-11 05:31:24 +01:00
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_SUCCESS, response: 200),
|
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_ACCESS_DENIED, response: 403),
|
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_NOT_FOUND, response: 404),
|
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_GENERIC_ERROR, response: 500),
|
2021-12-29 06:36:30 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
2022-05-22 02:27:28 +02:00
|
|
|
final class ServicesController
|
2018-02-17 02:58:58 +01:00
|
|
|
{
|
2021-04-23 07:24:12 +02:00
|
|
|
public function __construct(
|
2022-06-21 17:45:42 +02:00
|
|
|
private readonly EntityManagerInterface $em,
|
2022-05-29 02:30:12 +02:00
|
|
|
private readonly Configuration $configuration,
|
|
|
|
private readonly Nginx $nginx,
|
2022-06-22 03:48:32 +02:00
|
|
|
private readonly Adapters $adapters,
|
2021-04-23 07:24:12 +02:00
|
|
|
) {
|
2018-02-17 02:58:58 +01:00
|
|
|
}
|
|
|
|
|
2022-05-22 09:39:00 +02:00
|
|
|
public function statusAction(
|
|
|
|
ServerRequest $request,
|
|
|
|
Response $response,
|
2022-05-24 07:50:43 +02:00
|
|
|
string $station_id
|
2022-05-22 09:39:00 +02:00
|
|
|
): ResponseInterface {
|
2019-08-09 17:00:21 +02:00
|
|
|
$station = $request->getStation();
|
2019-01-16 15:53:04 +01:00
|
|
|
|
2022-06-22 03:48:32 +02:00
|
|
|
$backend = $this->adapters->getBackendAdapter($station);
|
|
|
|
$frontend = $this->adapters->getFrontendAdapter($station);
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2021-01-19 18:52:45 +01:00
|
|
|
return $response->withJson(
|
|
|
|
new Entity\Api\StationServiceStatus(
|
2022-06-22 03:48:32 +02:00
|
|
|
null !== $backend && $backend->isRunning($station),
|
|
|
|
null !== $frontend && $frontend->isRunning($station),
|
2021-09-22 04:56:29 +02:00
|
|
|
$station->getHasStarted(),
|
|
|
|
$station->getNeedsRestart()
|
2021-01-19 18:52:45 +01:00
|
|
|
)
|
|
|
|
);
|
2019-01-16 15:53:04 +01:00
|
|
|
}
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2022-05-22 09:39:00 +02:00
|
|
|
public function reloadAction(
|
|
|
|
ServerRequest $request,
|
|
|
|
Response $response,
|
2022-05-24 07:50:43 +02:00
|
|
|
string $station_id
|
2022-05-22 09:39:00 +02:00
|
|
|
): ResponseInterface {
|
2022-02-25 01:00:49 +01:00
|
|
|
// Reloading attempts to update configuration without restarting broadcasting, if possible and supported.
|
|
|
|
$station = $request->getStation();
|
2022-03-16 05:11:51 +01:00
|
|
|
|
|
|
|
try {
|
2022-06-21 17:45:42 +02:00
|
|
|
$station->setHasStarted(true);
|
|
|
|
$this->em->persist($station);
|
|
|
|
$this->em->flush();
|
|
|
|
|
2022-03-16 05:11:51 +01:00
|
|
|
$this->configuration->writeConfiguration(
|
|
|
|
station: $station,
|
|
|
|
forceRestart: true
|
|
|
|
);
|
2022-05-29 02:30:12 +02:00
|
|
|
|
|
|
|
$this->nginx->writeConfiguration($station);
|
2022-05-08 20:05:02 +02:00
|
|
|
} catch (Throwable $e) {
|
2022-03-16 05:11:51 +01:00
|
|
|
return $response->withJson(
|
|
|
|
new Entity\Api\Error(
|
|
|
|
500,
|
|
|
|
$e->getMessage()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-02-25 01:00:49 +01:00
|
|
|
|
|
|
|
return $response->withJson(new Entity\Api\Status(true, __('Station reloaded.')));
|
|
|
|
}
|
|
|
|
|
2022-05-22 09:39:00 +02:00
|
|
|
public function restartAction(
|
|
|
|
ServerRequest $request,
|
|
|
|
Response $response,
|
2022-05-24 07:50:43 +02:00
|
|
|
string $station_id
|
2022-05-22 09:39:00 +02:00
|
|
|
): ResponseInterface {
|
2022-02-25 01:00:49 +01:00
|
|
|
// Restarting will always shut down and restart any services.
|
2019-08-09 17:00:21 +02:00
|
|
|
$station = $request->getStation();
|
2022-03-16 05:11:51 +01:00
|
|
|
|
|
|
|
try {
|
2022-06-21 17:45:42 +02:00
|
|
|
$station->setHasStarted(true);
|
|
|
|
$this->em->persist($station);
|
|
|
|
$this->em->flush();
|
|
|
|
|
2022-03-16 05:11:51 +01:00
|
|
|
$this->configuration->writeConfiguration(
|
|
|
|
station: $station,
|
|
|
|
forceRestart: true,
|
|
|
|
attemptReload: false
|
|
|
|
);
|
2022-05-29 02:30:12 +02:00
|
|
|
|
|
|
|
$this->nginx->writeConfiguration($station);
|
2022-05-08 20:05:02 +02:00
|
|
|
} catch (Throwable $e) {
|
2022-03-16 05:11:51 +01:00
|
|
|
return $response->withJson(
|
|
|
|
new Entity\Api\Error(
|
|
|
|
500,
|
|
|
|
$e->getMessage()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2019-08-22 00:33:47 +02:00
|
|
|
return $response->withJson(new Entity\Api\Status(true, __('Station restarted.')));
|
2018-02-17 02:58:58 +01:00
|
|
|
}
|
|
|
|
|
2019-09-04 20:00:51 +02:00
|
|
|
public function frontendAction(
|
|
|
|
ServerRequest $request,
|
|
|
|
Response $response,
|
2022-05-24 07:50:43 +02:00
|
|
|
string $station_id,
|
2021-12-12 16:18:07 +01:00
|
|
|
string $do = 'restart'
|
2019-09-04 20:00:51 +02:00
|
|
|
): ResponseInterface {
|
2019-08-09 17:00:21 +02:00
|
|
|
$station = $request->getStation();
|
2022-06-22 03:48:32 +02:00
|
|
|
$frontend = $this->adapters->getFrontendAdapter($station);
|
|
|
|
|
|
|
|
if (null === $frontend) {
|
|
|
|
throw new StationUnsupportedException();
|
|
|
|
}
|
2018-02-17 02:58:58 +01:00
|
|
|
|
|
|
|
switch ($do) {
|
2019-08-07 06:33:55 +02:00
|
|
|
case 'stop':
|
2018-09-22 13:52:43 +02:00
|
|
|
$frontend->stop($station);
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2021-11-16 14:13:43 +01:00
|
|
|
return $response->withJson(new Entity\Api\Status(true, __('Service stopped.')));
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2019-08-07 06:33:55 +02:00
|
|
|
case 'start':
|
2018-09-22 13:52:43 +02:00
|
|
|
$frontend->start($station);
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2021-11-16 14:13:43 +01:00
|
|
|
return $response->withJson(new Entity\Api\Status(true, __('Service started.')));
|
|
|
|
|
|
|
|
case 'reload':
|
|
|
|
$frontend->write($station);
|
|
|
|
$frontend->reload($station);
|
|
|
|
|
|
|
|
return $response->withJson(new Entity\Api\Status(true, __('Service reloaded.')));
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2019-08-07 06:33:55 +02:00
|
|
|
case 'restart':
|
2018-02-17 02:58:58 +01:00
|
|
|
default:
|
2019-09-04 20:00:51 +02:00
|
|
|
try {
|
2018-09-22 13:52:43 +02:00
|
|
|
$frontend->stop($station);
|
2021-06-08 08:40:49 +02:00
|
|
|
} catch (NotRunningException) {
|
2019-09-04 20:00:51 +02:00
|
|
|
}
|
2018-06-09 18:17:21 +02:00
|
|
|
|
2018-09-22 13:52:43 +02:00
|
|
|
$frontend->write($station);
|
|
|
|
$frontend->start($station);
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2021-11-16 14:13:43 +01:00
|
|
|
return $response->withJson(new Entity\Api\Status(true, __('Service restarted.')));
|
2018-02-17 02:58:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 20:00:51 +02:00
|
|
|
public function backendAction(
|
|
|
|
ServerRequest $request,
|
|
|
|
Response $response,
|
2022-05-24 07:50:43 +02:00
|
|
|
string $station_id,
|
2021-12-12 16:18:07 +01:00
|
|
|
string $do = 'restart'
|
2019-09-04 20:00:51 +02:00
|
|
|
): ResponseInterface {
|
2019-08-09 17:00:21 +02:00
|
|
|
$station = $request->getStation();
|
2022-06-22 03:48:32 +02:00
|
|
|
$backend = $this->adapters->getBackendAdapter($station);
|
|
|
|
|
|
|
|
if (null === $backend) {
|
|
|
|
throw new StationUnsupportedException();
|
|
|
|
}
|
2018-02-17 02:58:58 +01:00
|
|
|
|
|
|
|
switch ($do) {
|
2019-08-07 06:33:55 +02:00
|
|
|
case 'skip':
|
2022-06-22 03:48:32 +02:00
|
|
|
$backend->skip($station);
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2019-08-09 17:00:21 +02:00
|
|
|
return $response->withJson(new Entity\Api\Status(true, __('Song skipped.')));
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2019-08-07 06:33:55 +02:00
|
|
|
case 'disconnect':
|
2022-06-22 03:48:32 +02:00
|
|
|
$backend->disconnectStreamer($station);
|
2018-07-16 00:27:25 +02:00
|
|
|
|
2019-08-09 17:00:21 +02:00
|
|
|
return $response->withJson(new Entity\Api\Status(true, __('Streamer disconnected.')));
|
2018-07-16 00:27:25 +02:00
|
|
|
|
2019-08-07 06:33:55 +02:00
|
|
|
case 'stop':
|
2018-09-22 13:52:43 +02:00
|
|
|
$backend->stop($station);
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2021-11-16 14:13:43 +01:00
|
|
|
return $response->withJson(new Entity\Api\Status(true, __('Service stopped.')));
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2019-08-07 06:33:55 +02:00
|
|
|
case 'start':
|
2018-09-22 13:52:43 +02:00
|
|
|
$backend->start($station);
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2021-11-16 14:13:43 +01:00
|
|
|
return $response->withJson(new Entity\Api\Status(true, __('Service started.')));
|
|
|
|
|
|
|
|
case 'reload':
|
|
|
|
$backend->write($station);
|
|
|
|
$backend->reload($station);
|
|
|
|
|
|
|
|
return $response->withJson(new Entity\Api\Status(true, __('Service reloaded.')));
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2019-08-07 06:33:55 +02:00
|
|
|
case 'restart':
|
2018-02-17 02:58:58 +01:00
|
|
|
default:
|
2019-09-04 20:00:51 +02:00
|
|
|
try {
|
2018-09-22 13:52:43 +02:00
|
|
|
$backend->stop($station);
|
2021-06-08 08:40:49 +02:00
|
|
|
} catch (NotRunningException) {
|
2019-09-04 20:00:51 +02:00
|
|
|
}
|
2018-06-09 18:17:21 +02:00
|
|
|
|
2018-09-22 13:52:43 +02:00
|
|
|
$backend->write($station);
|
|
|
|
$backend->start($station);
|
2018-02-17 02:58:58 +01:00
|
|
|
|
2021-11-16 14:13:43 +01:00
|
|
|
return $response->withJson(new Entity\Api\Status(true, __('Service restarted.')));
|
2018-02-17 02:58:58 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-16 00:27:25 +02:00
|
|
|
}
|