✨ feat(ansible): Ajoute la gestion du mode maintenance via commande et listener.
This commit is contained in:
@@ -65,7 +65,16 @@
|
||||
state: started
|
||||
enabled: yes
|
||||
when: ansible_os_family == "Debian"
|
||||
|
||||
- name: Maintenance mode artemis on
|
||||
ansible.builtin.command: php bin/console mainframe:maintenance artemis true
|
||||
become: false
|
||||
args:
|
||||
chdir: "{{ path }}"
|
||||
- name: Maintenance mode api on
|
||||
ansible.builtin.command: php bin/console mainframe:maintenance api true
|
||||
become: false
|
||||
args:
|
||||
chdir: "{{ path }}"
|
||||
- name: Créer le fichier .env.local avec les secrets de production
|
||||
ansible.builtin.copy:
|
||||
content: |
|
||||
@@ -292,3 +301,13 @@
|
||||
- "{{ path }}/public/media"
|
||||
- "{{ path }}/public/storage" # For uploads
|
||||
- "{{ path }}/public/tmp-sign" # For uploads
|
||||
- name: Maintenance mode artemis off
|
||||
ansible.builtin.command: php bin/console mainframe:maintenance artemis false
|
||||
become: false
|
||||
args:
|
||||
chdir: "{{ path }}"
|
||||
- name: Maintenance mode api off
|
||||
ansible.builtin.command: php bin/console mainframe:maintenance api false
|
||||
become: false
|
||||
args:
|
||||
chdir: "{{ path }}"
|
||||
|
||||
54
src/Command/MaintenanceControl.php
Normal file
54
src/Command/MaintenanceControl.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use AllowDynamicProperties;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
|
||||
#[AllowDynamicProperties] #[AsCommand(name: 'mainframe:maintenance')]
|
||||
class MaintenanceControl extends Command
|
||||
{
|
||||
public function __construct(private readonly KernelInterface $kernel,?string $name = null)
|
||||
{
|
||||
parent::__construct($name);
|
||||
$this->file = $this->kernel->getProjectDir()."/var/maintenance.json";
|
||||
if(!is_file($this->file)){
|
||||
file_put_contents($this->file, json_encode([]));
|
||||
}
|
||||
$encode = json_decode(file_get_contents($this->file),true);
|
||||
if(!isset($encode['artemis'])){
|
||||
$encode['artemis'] = false;
|
||||
}
|
||||
if(!isset($encode['customer_space'])){
|
||||
$encode['customer_space'] = false;
|
||||
}
|
||||
if(!isset($encode['api'])){
|
||||
$encode['api'] = false;
|
||||
}
|
||||
$this->encode = $encode;
|
||||
file_put_contents($this->file, json_encode($encode));
|
||||
}
|
||||
protected function configure()
|
||||
{
|
||||
$this->addArgument('mode');
|
||||
$this->addArgument('isEnabled');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$io->title("Maintenance Control Command");
|
||||
$arhs = $input->getArguments();
|
||||
|
||||
$this->encode[$arhs['mode']] = ($arhs['isEnabled'] == "true");
|
||||
file_put_contents($this->file, json_encode( $this->encode));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
58
src/EventListener/MaintenanceSubscriber.php
Normal file
58
src/EventListener/MaintenanceSubscriber.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\EventListener;
|
||||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Twig\Environment;
|
||||
|
||||
#[AsEventListener(event: KernelEvents::REQUEST, method: 'onKernelRequest', priority: 10)]
|
||||
class MaintenanceSubscriber
|
||||
{
|
||||
private mixed $encode;
|
||||
private string $file;
|
||||
|
||||
public function __construct(private readonly Environment $environment,private readonly KernelInterface $kernel)
|
||||
{
|
||||
$this->file = $this->kernel->getProjectDir()."/var/maintenance.json";
|
||||
if(!is_file($this->file)){
|
||||
file_put_contents($this->file, json_encode([]));
|
||||
}
|
||||
$encode = json_decode(file_get_contents($this->file),true);
|
||||
if(!isset($encode['artemis'])){
|
||||
$encode['artemis'] = false;
|
||||
}
|
||||
if(!isset($encode['customer_space'])){
|
||||
$encode['customer_space'] = false;
|
||||
}
|
||||
if(!isset($encode['api'])){
|
||||
$encode['api'] = false;
|
||||
}
|
||||
$this->encode = $encode;
|
||||
file_put_contents($this->file, json_encode($encode));
|
||||
}
|
||||
|
||||
public function onKernelRequest(RequestEvent $event)
|
||||
{
|
||||
$route = $event->getRequest()->get('_route');
|
||||
if($route == "app_login" || str_contains($event->getRequest()->getPathInfo(),"/artemis")) {
|
||||
if($this->encode['artemis']) {
|
||||
$response = new Response($this->environment->render('maintenance/artemis.twig'));
|
||||
$event->setResponse($response);
|
||||
$event->stopPropagation();
|
||||
}
|
||||
}
|
||||
if(str_contains($event->getRequest()->getPathInfo(),"/api")) {
|
||||
if($this->encode['artemis']) {
|
||||
$response = new JsonResponse([
|
||||
'message' => 'maintenance mode enabled',
|
||||
]);
|
||||
$event->setResponse($response);
|
||||
$event->stopPropagation();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
35
templates/maintenance/artemis.twig
Normal file
35
templates/maintenance/artemis.twig
Normal file
@@ -0,0 +1,35 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr" class="dark">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Maintenance en cours</title>
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
<script>
|
||||
// Ceci est une astuce simple pour simuler le dark mode pour cet exemple.
|
||||
// En production, vous utiliseriez un processus de construction pour inclure votre CSS.
|
||||
tailwind.config = {
|
||||
darkMode: 'class',
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body class="bg-gray-100 dark:bg-gray-900 transition-colors duration-500 min-h-screen flex items-center justify-center p-4">
|
||||
|
||||
<div class="max-w-xl w-full text-center bg-white dark:bg-gray-800 shadow-xl rounded-2xl p-8 md:p-12 border-t-4 border-indigo-500 dark:border-indigo-400">
|
||||
|
||||
<svg class="mx-auto h-16 w-16 text-indigo-600 dark:text-indigo-400 mb-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.82 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.82 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.82-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.82-3.31 2.37-2.37.525.32 1.15.54 1.83.656.76.126 1.5.034 2.1-.247zM15 12a3 3 0 11-6 0 3 3 0 016 0z" />
|
||||
</svg>
|
||||
|
||||
<h1 class="text-4xl font-extrabold text-gray-900 dark:text-white mb-4">
|
||||
Site en Maintenance
|
||||
</h1>
|
||||
|
||||
<p class="text-lg text-gray-600 dark:text-gray-400 mb-8">
|
||||
Nous effectuons actuellement une mise à jour planifiée pour améliorer nos services. Nous serons de retour très bientôt !
|
||||
</p>
|
||||
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user