ajoute la liaison Laravel-OpenStack et le modèle Heat du lab 08
Un gabarit Heat généré depuis chaque lab.yaml, une stack par session de lab (création à l'admission, suppression à l'expiration via des jobs Redis et un worker systemd dédié). Passage du compte de service à une authentification par mot de passe : Heat refuse de créer un trust à partir d'un jeton d'application credential (voir ADR 0002). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
8742bde807
commit
67a754f733
34 changed files with 1095 additions and 91 deletions
81
portail/app/Jobs/CreerStackLab.php
Normal file
81
portail/app/Jobs/CreerStackLab.php
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\LabSession;
|
||||
use App\Services\Labs\DefinitionLab;
|
||||
use App\Services\OpenStack\GenerateurTemplateHeat;
|
||||
use App\Services\OpenStack\HeatClient;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Lance la stack Heat d'un lab pour un élève admis (étape 10). Dispatché par
|
||||
* SeanceController::admettre — « admettre » un élève, c'est lancer son lab.
|
||||
*/
|
||||
class CreerStackLab implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public int $tries = 1;
|
||||
|
||||
public function __construct(private readonly LabSession $labSession) {}
|
||||
|
||||
public function handle(GenerateurTemplateHeat $generateur, HeatClient $heat): void
|
||||
{
|
||||
$lab = DefinitionLab::depuis($this->labSession->lab->identifiant);
|
||||
$nomStack = "lab-session-{$this->labSession->id}";
|
||||
|
||||
['template' => $template] = $generateur->genererDepuis($lab, $nomStack);
|
||||
|
||||
$stack = $heat->creerStack($nomStack, $template);
|
||||
|
||||
$this->labSession->forceFill([
|
||||
'heat_stack_id' => $stack['id'],
|
||||
'heat_stack_name' => $nomStack,
|
||||
])->save();
|
||||
|
||||
$etat = $this->attendreCompletion($heat, $nomStack, $stack['id']);
|
||||
|
||||
if ($etat['statut'] !== 'CREATE_COMPLETE') {
|
||||
Log::error("CreerStackLab : la stack {$nomStack} n'a pas abouti", $etat);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($lab->machines as $machine) {
|
||||
$cle = GenerateurTemplateHeat::cle($machine['nom']);
|
||||
$this->labSession->machines()->create([
|
||||
'nom' => $machine['nom'],
|
||||
'instance_id' => $etat['sorties']["{$cle}_id"],
|
||||
]);
|
||||
}
|
||||
|
||||
$this->labSession->forceFill(['etat' => LabSession::ACTIF])->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Heat crée une stack de cette taille (réseaux + quelques VM) en
|
||||
* général en moins d'une minute : on attend jusqu'à 2 minutes.
|
||||
*
|
||||
* @return array{statut: string, raison: ?string, sorties: array<string, mixed>}
|
||||
*/
|
||||
private function attendreCompletion(HeatClient $heat, string $nom, string $id): array
|
||||
{
|
||||
for ($tentative = 0; $tentative < 60; $tentative++) {
|
||||
$etat = $heat->etatStack($nom, $id);
|
||||
|
||||
if (! str_ends_with($etat['statut'], '_IN_PROGRESS')) {
|
||||
return $etat;
|
||||
}
|
||||
|
||||
sleep(2);
|
||||
}
|
||||
|
||||
return $etat;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue