2022-08-15 11:42:28 +02:00
|
|
|
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
|
|
|
|
class ServicesController extends AbstractController
|
|
|
|
{
|
|
|
|
|
|
|
|
private $services_available = array("recording", "analyzis", "miner", "plotter");
|
|
|
|
private $allowed_actions = array("start", "stop");
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-18 16:32:29 +02:00
|
|
|
* @Route("/services/status", name="services_status")
|
2022-08-18 21:08:03 +02:00
|
|
|
* @Route("/{_locale<%app.supported_locales%>}/services/status", name="services_status_i18n")
|
2022-08-15 11:42:28 +02:00
|
|
|
*/
|
|
|
|
public function service_status() {
|
|
|
|
$status = array_map(function($service) {
|
|
|
|
return array(
|
|
|
|
"name" => $service,
|
|
|
|
"status" => $this->systemd_service_status($service)
|
|
|
|
);
|
|
|
|
}, $this->services_available);
|
|
|
|
return $this->render('services/status.html.twig', [
|
|
|
|
'status' => $status
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-18 16:32:29 +02:00
|
|
|
* @Route("/services/manage/{action}/{service}", name="services_manager")
|
|
|
|
* @Route("/{_locale<%app.supported_locales%>}/services/manage/{action}/{service}", name="service_manager_i18n")
|
2022-08-15 11:42:28 +02:00
|
|
|
*/
|
2022-08-16 05:21:53 +02:00
|
|
|
public function service_manage($action, $service="all")
|
2022-08-15 11:42:28 +02:00
|
|
|
{
|
|
|
|
$error = "";
|
|
|
|
if (in_array($action, $this->allowed_actions)) {
|
2022-08-16 05:21:53 +02:00
|
|
|
if ($service == "all") {
|
|
|
|
foreach ($this->services_available as $service) {
|
|
|
|
if(($output = $this->manage_systemd_service($action, $service)) != "true") {
|
|
|
|
$error .= "Error while managing $service service";
|
|
|
|
dump($output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (in_array($service, $this->services_available)) {
|
2022-08-15 11:42:28 +02:00
|
|
|
if(($output = $this->manage_systemd_service($action, $service)) != "true") {
|
2022-08-16 05:21:53 +02:00
|
|
|
$error .= "Error while managing $service service";
|
2022-08-15 11:42:28 +02:00
|
|
|
dump($output);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$error .= "Service not found";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$error .= "Action not allowed";
|
|
|
|
}
|
|
|
|
if ($error != "") {
|
|
|
|
return new Response($error, Response::HTTP_BAD_REQUEST);
|
|
|
|
} else {
|
|
|
|
return new Response("OK", Response::HTTP_OK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function manage_systemd_service($action, $service)
|
|
|
|
{
|
2022-08-16 05:21:53 +02:00
|
|
|
// TODO correct this command (failed with not root user)
|
|
|
|
$command = "./daemon/birdnet_manager.sh $action birdnet_$service";
|
|
|
|
$old_path = getcwd();
|
|
|
|
$workdir = $this->getParameter("kernel.project_dir");
|
|
|
|
chdir($workdir);
|
2022-08-15 11:42:28 +02:00
|
|
|
$output = shell_exec($command);
|
2022-08-16 05:21:53 +02:00
|
|
|
dump($output);
|
|
|
|
chdir($old_path);
|
2022-08-15 11:42:28 +02:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function systemd_service_status($service)
|
|
|
|
{
|
2022-08-16 05:21:53 +02:00
|
|
|
$status = array();
|
|
|
|
$command = "systemctl is-active birdnet_".$service.".service";
|
|
|
|
$output = shell_exec($command);
|
|
|
|
if (! is_null($output))
|
|
|
|
$status["status"] = $output;
|
|
|
|
else
|
|
|
|
$status["status"] = "unknown";
|
|
|
|
$command = "systemctl is-enabled birdnet_".$service.".service";
|
|
|
|
$output = shell_exec($command);
|
|
|
|
if (! is_null($output))
|
|
|
|
$status["enabled"] = $output;
|
|
|
|
else
|
|
|
|
$status["enabled"] = "unknown";
|
|
|
|
$status["eta"] = $this->systemd_timer_eta($service);
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function systemd_timer_eta($service)
|
|
|
|
{
|
|
|
|
$eta = "";
|
|
|
|
$command = "systemctl list-timers | grep $service.timer | cut -d' ' -f5";
|
|
|
|
$output = shell_exec($command);
|
|
|
|
// dump($output);
|
|
|
|
if (! is_null($output))
|
|
|
|
$eta = $output;
|
|
|
|
else
|
|
|
|
$eta = "na";
|
|
|
|
return $eta;
|
2022-08-15 11:42:28 +02:00
|
|
|
}
|
|
|
|
}
|