59 lines
2.1 KiB
PHP
59 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\OpenStack;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
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 : on ne construit pas de client générique ici.
|
|
*/
|
|
class NovaClient
|
|
{
|
|
public function consoleSerielle(string $instanceId): string
|
|
{
|
|
$reponse = Http::withHeaders([
|
|
'X-Auth-Token' => $this->jetonKeystone(),
|
|
'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');
|
|
}
|
|
|
|
/**
|
|
* Jeton Keystone mis en cache (un jeton v3 dure 1h côté Keystone, on le
|
|
* garde un peu moins longtemps par prudence).
|
|
*/
|
|
private function jetonKeystone(): string
|
|
{
|
|
return Cache::remember('openstack.jeton_keystone', now()->addMinutes(50), function () {
|
|
$reponse = Http::post(config('openstack.auth_url').'/v3/auth/tokens', [
|
|
'auth' => [
|
|
'identity' => [
|
|
'methods' => ['application_credential'],
|
|
'application_credential' => [
|
|
'id' => config('openstack.application_credential_id'),
|
|
'secret' => config('openstack.application_credential_secret'),
|
|
],
|
|
],
|
|
],
|
|
]);
|
|
|
|
if ($reponse->failed()) {
|
|
throw new RuntimeException('Authentification OpenStack refusée : '.$reponse->body());
|
|
}
|
|
|
|
return $reponse->header('X-Subject-Token');
|
|
});
|
|
}
|
|
}
|