64 lines
2.1 KiB
PHP
64 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\OpenStack;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use RuntimeException;
|
|
|
|
/**
|
|
* Juste ce qu'il faut pour demander à Nova l'adresse de la console série
|
|
* d'une VM (étape 9). La liaison Laravel ↔ OpenStack complète (Heat, labs)
|
|
* est l'étape 10 : voir HeatClient.
|
|
*/
|
|
class NovaClient
|
|
{
|
|
public function __construct(private readonly KeystoneAuth $auth) {}
|
|
|
|
public function consoleSerielle(string $instanceId): string
|
|
{
|
|
$reponse = Http::withHeaders([
|
|
'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'],
|
|
]);
|
|
|
|
if ($reponse->failed()) {
|
|
throw new RuntimeException("Nova a refusé la demande de console série pour {$instanceId} : ".$reponse->body());
|
|
}
|
|
|
|
return $reponse->json('remote_console.url');
|
|
}
|
|
|
|
/**
|
|
* Redémarrage forcé (bloc A, self-healing) : équivalent d'un appui sur
|
|
* le bouton reset. Le disque de la VM est conservé.
|
|
*/
|
|
public function redemarrerForce(string $instanceId): void
|
|
{
|
|
$this->action($instanceId, ['reboot' => ['type' => 'HARD']]);
|
|
}
|
|
|
|
/** Démarre une VM éteinte (l'élève a lancé « poweroff »). */
|
|
public function demarrer(string $instanceId): void
|
|
{
|
|
$this->action($instanceId, ['os-start' => null]);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $corps
|
|
*/
|
|
private function action(string $instanceId, array $corps): void
|
|
{
|
|
$reponse = Http::withHeaders([
|
|
'X-Auth-Token' => $this->auth->jeton(),
|
|
'OpenStack-API-Version' => 'compute 2.99',
|
|
])
|
|
->post(config('openstack.nova_url')."/servers/{$instanceId}/action", $corps);
|
|
|
|
if ($reponse->failed()) {
|
|
throw new RuntimeException("Nova a refusé l'action ".array_key_first($corps)." sur {$instanceId} : ".$reponse->body());
|
|
}
|
|
}
|
|
}
|