ajoute le stock chaud, la correction automatique et les nouveaux essais de création de stack
Stock de stacks prêtes par lab dans Redis (prise atomique en Lua, ménage par comparaison avec Heat, disjoncteur), files gérées par Horizon, séances planifiables. Correcteur Go sur l'hyperviseur : socket Unix, commandes lues dans le lab.yaml uniquement, exécution par l'agent invité QEMU. Une création de stack ratée est supprimée et réessayée deux fois, puis la session passe en échec, visible du prof. Corrige aussi trois défauts des étapes précédentes : la console se coupait à la première frappe d'un navigateur (trames texte), l'élève ne pouvait pas rejoindre une séance, la déconnexion n'avait pas de jeton CSRF. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ca2f6f3b7a
commit
80b6c0722d
71 changed files with 2959 additions and 170 deletions
88
portail/app/Services/OpenStack/FabriqueStack.php
Normal file
88
portail/app/Services/OpenStack/FabriqueStack.php
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\OpenStack;
|
||||
|
||||
use App\Services\Labs\DefinitionLab;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Crée la stack Heat d'un lab et attend qu'elle soit prête — partagé par les
|
||||
* sessions d'élèves (CreerStackLab) et le stock chaud (CreerStackStock).
|
||||
*
|
||||
* Chaque essai porte son propre nom ({base}-e{n}) : une stack ratée met un
|
||||
* moment à disparaître, et Heat refuse deux stacks du même nom. Avant un
|
||||
* nouvel essai, nettoyerEssais() supprime les restes des précédents (y
|
||||
* compris ceux d'un processus tué en plein travail).
|
||||
*/
|
||||
class FabriqueStack
|
||||
{
|
||||
// Heat construit un lab (réseaux + quelques VM) en 20 à 40 s : au-delà
|
||||
// de 3 minutes, l'essai est considéré comme raté.
|
||||
private const ATTENTE_MAX_SECONDES = 180;
|
||||
|
||||
public function __construct(
|
||||
private readonly HeatClient $heat,
|
||||
private readonly GenerateurTemplateHeat $generateur,
|
||||
) {}
|
||||
|
||||
public function nettoyerEssais(string $base): void
|
||||
{
|
||||
foreach ($this->heat->listerStacks() as $stack) {
|
||||
if (str_starts_with($stack['nom'], "{$base}-e") && $stack['statut'] !== 'DELETE_IN_PROGRESS') {
|
||||
$this->heat->supprimerStack($stack['nom'], $stack['id']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param (callable(string, string): void)|null $apresCreation appelé dès que Heat a accepté la stack (nom, id)
|
||||
* @return array{nom: string, id: string, instances: array<string, string>}
|
||||
*
|
||||
* @throws RuntimeException si Heat refuse la stack ou si elle n'aboutit pas (elle est alors supprimée)
|
||||
*/
|
||||
public function fabriquer(DefinitionLab $lab, string $nom, ?callable $apresCreation = null): array
|
||||
{
|
||||
['template' => $template] = $this->generateur->genererDepuis($lab, $nom);
|
||||
|
||||
$stack = $this->heat->creerStack($nom, $template);
|
||||
|
||||
if ($apresCreation !== null) {
|
||||
$apresCreation($nom, $stack['id']);
|
||||
}
|
||||
|
||||
$etat = $this->attendre($nom, $stack['id']);
|
||||
|
||||
if ($etat['statut'] !== 'CREATE_COMPLETE') {
|
||||
$this->heat->supprimerStack($nom, $stack['id']);
|
||||
|
||||
throw new RuntimeException("La stack {$nom} n'a pas abouti : {$etat['statut']} ({$etat['raison']})");
|
||||
}
|
||||
|
||||
$instances = [];
|
||||
foreach ($lab->machines as $machine) {
|
||||
$instances[$machine['nom']] = $etat['sorties'][GenerateurTemplateHeat::cle($machine['nom']).'_id'];
|
||||
}
|
||||
|
||||
return ['nom' => $nom, 'id' => $stack['id'], 'instances' => $instances];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{statut: string, raison: ?string, sorties: array<string, mixed>}
|
||||
*/
|
||||
private function attendre(string $nom, string $id): array
|
||||
{
|
||||
$limite = time() + self::ATTENTE_MAX_SECONDES;
|
||||
|
||||
do {
|
||||
$etat = $this->heat->etatStack($nom, $id);
|
||||
|
||||
if (! str_ends_with($etat['statut'], '_IN_PROGRESS')) {
|
||||
return $etat;
|
||||
}
|
||||
|
||||
sleep(2);
|
||||
} while (time() < $limite);
|
||||
|
||||
return ['statut' => 'DELAI_DEPASSE', 'raison' => 'plus de '.self::ATTENTE_MAX_SECONDES.' s', 'sorties' => []];
|
||||
}
|
||||
}
|
||||
|
|
@ -55,6 +55,27 @@ class HeatClient
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Toutes les stacks du projet (quelques dizaines au plus : pas de pagination).
|
||||
*
|
||||
* @return array<int, array{nom: string, id: string, statut: string, cree_a: string}>
|
||||
*/
|
||||
public function listerStacks(): array
|
||||
{
|
||||
$reponse = $this->requete()->get($this->url('/stacks'));
|
||||
|
||||
if ($reponse->failed()) {
|
||||
throw new RuntimeException('Impossible de lister les stacks : '.$reponse->body());
|
||||
}
|
||||
|
||||
return array_map(fn (array $s) => [
|
||||
'nom' => $s['stack_name'],
|
||||
'id' => $s['id'],
|
||||
'statut' => $s['stack_status'],
|
||||
'cree_a' => $s['creation_time'],
|
||||
], $reponse->json('stacks') ?? []);
|
||||
}
|
||||
|
||||
public function supprimerStack(string $nom, string $id): void
|
||||
{
|
||||
$reponse = $this->requete()->delete($this->url("/stacks/{$nom}/{$id}"));
|
||||
|
|
|
|||
|
|
@ -17,9 +17,9 @@ class NovaClient
|
|||
public function consoleSerielle(string $instanceId): string
|
||||
{
|
||||
$reponse = Http::withHeaders([
|
||||
'X-Auth-Token' => $this->auth->jeton(),
|
||||
'OpenStack-API-Version' => 'compute 2.99',
|
||||
])
|
||||
'X-Auth-Token' => $this->auth->jeton(),
|
||||
'OpenStack-API-Version' => 'compute 2.99',
|
||||
])
|
||||
->post(config('openstack.nova_url')."/servers/{$instanceId}/remote-consoles", [
|
||||
'remote_console' => ['protocol' => 'serial', 'type' => 'serial'],
|
||||
]);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue