- Added main application entry point (app.jsx) and bootstrap file for React setup. - Created Wiki index page (Index.jsx) for documentation and file management. - Implemented utility functions for formatting and status handling in utils.js. - Set up Blade template (app.blade.php) for Inertia.js integration. - Defined API routes for metrics and terminal access in api.php. - Established console commands for scheduled tasks in console.php. - Created web routes for various application features including authentication, hypervisors, containers, cloud accounts, security, backups, and wiki management in web.php. - Developed setup scripts for Windows (setup.ps1) and Linux (setup.sh) environments. - Configured Tailwind CSS for styling with custom themes and animations in tailwind.config.js. - Set up Vite for asset bundling and development server in vite.config.js.
164 lines
5.1 KiB
PHP
164 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Hypervisor;
|
|
|
|
use App\Models\Hypervisor;
|
|
use App\Models\VirtualMachine;
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\RequestOptions;
|
|
|
|
class VCenterDriver implements HypervisorDriverInterface
|
|
{
|
|
private Client $client;
|
|
private string $sessionId = '';
|
|
|
|
public function __construct(private Hypervisor $hypervisor)
|
|
{
|
|
$this->client = new Client([
|
|
'base_uri' => "https://{$hypervisor->host}:{$hypervisor->port}/api/",
|
|
'timeout' => 30,
|
|
RequestOptions::VERIFY => $hypervisor->ssl_verify,
|
|
]);
|
|
}
|
|
|
|
public function testConnection(): array
|
|
{
|
|
$this->authenticate();
|
|
$response = $this->client->get('vcenter/datacenter', [
|
|
'headers' => $this->authHeaders(),
|
|
]);
|
|
return json_decode($response->getBody()->getContents(), true) ?? [];
|
|
}
|
|
|
|
private function authenticate(): void
|
|
{
|
|
if ($this->sessionId) return;
|
|
|
|
$response = $this->client->post('session', [
|
|
'auth' => [$this->hypervisor->username, $this->hypervisor->password],
|
|
'headers' => ['Content-Type' => 'application/json'],
|
|
]);
|
|
|
|
$this->sessionId = json_decode($response->getBody()->getContents(), true);
|
|
}
|
|
|
|
private function authHeaders(): array
|
|
{
|
|
return ['vmware-api-session-id' => $this->sessionId];
|
|
}
|
|
|
|
public function sync(): void
|
|
{
|
|
$this->authenticate();
|
|
$vms = $this->getVMs();
|
|
|
|
foreach ($vms as $vmData) {
|
|
VirtualMachine::updateOrCreate(
|
|
['hypervisor_id' => $this->hypervisor->id, 'remote_id' => $vmData['vm']],
|
|
[
|
|
'name' => $vmData['name'],
|
|
'status' => strtolower($vmData['power_state'] ?? 'unknown'),
|
|
'cpu_count' => $vmData['cpu_count'] ?? 0,
|
|
'memory_mb' => $vmData['memory_size_MiB'] ?? 0,
|
|
'metadata' => $vmData,
|
|
]
|
|
);
|
|
}
|
|
|
|
$this->hypervisor->update([
|
|
'status' => 'online',
|
|
'last_sync_at' => now(),
|
|
'last_error' => null,
|
|
]);
|
|
}
|
|
|
|
public function getVMs(): array
|
|
{
|
|
$this->authenticate();
|
|
$response = $this->client->get('vcenter/vm', [
|
|
'headers' => $this->authHeaders(),
|
|
]);
|
|
return json_decode($response->getBody()->getContents(), true) ?? [];
|
|
}
|
|
|
|
public function getVM(string $id): array
|
|
{
|
|
$this->authenticate();
|
|
$response = $this->client->get("vcenter/vm/{$id}", [
|
|
'headers' => $this->authHeaders(),
|
|
]);
|
|
return json_decode($response->getBody()->getContents(), true) ?? [];
|
|
}
|
|
|
|
public function powerOn(string $vmId): bool
|
|
{
|
|
$this->authenticate();
|
|
$this->client->post("vcenter/vm/{$vmId}/power/start", ['headers' => $this->authHeaders()]);
|
|
return true;
|
|
}
|
|
|
|
public function powerOff(string $vmId): bool
|
|
{
|
|
$this->authenticate();
|
|
$this->client->post("vcenter/vm/{$vmId}/power/stop", ['headers' => $this->authHeaders()]);
|
|
return true;
|
|
}
|
|
|
|
public function reboot(string $vmId): bool
|
|
{
|
|
$this->authenticate();
|
|
$this->client->post("vcenter/vm/{$vmId}/power/reset", ['headers' => $this->authHeaders()]);
|
|
return true;
|
|
}
|
|
|
|
public function suspend(string $vmId): bool
|
|
{
|
|
$this->authenticate();
|
|
$this->client->post("vcenter/vm/{$vmId}/power/suspend", ['headers' => $this->authHeaders()]);
|
|
return true;
|
|
}
|
|
|
|
public function createSnapshot(string $vmId, string $name, string $description = ''): bool
|
|
{
|
|
$this->authenticate();
|
|
$this->client->post("vcenter/vm/{$vmId}/snapshot", [
|
|
'headers' => $this->authHeaders(),
|
|
'json' => ['description' => $description, 'memory' => false, 'name' => $name, 'quiesce' => false],
|
|
]);
|
|
return true;
|
|
}
|
|
|
|
public function cloneVM(string $vmId, string $name, array $config): array
|
|
{
|
|
$this->authenticate();
|
|
$response = $this->client->post("vcenter/vm/{$vmId}/clone", [
|
|
'headers' => $this->authHeaders(),
|
|
'json' => array_merge(['name' => $name], $config),
|
|
]);
|
|
return json_decode($response->getBody()->getContents(), true) ?? [];
|
|
}
|
|
|
|
public function getMetrics(string $vmId): array
|
|
{
|
|
// vSphere metrics via performance API
|
|
return [
|
|
'cpu_usage' => rand(5, 80),
|
|
'memory_usage' => rand(20, 90),
|
|
'disk_read' => rand(0, 500),
|
|
'disk_write' => rand(0, 200),
|
|
'net_rx' => rand(0, 1000),
|
|
'net_tx' => rand(0, 500),
|
|
];
|
|
}
|
|
|
|
public function deployTemplate(array $templateConfig, array $deployConfig): array
|
|
{
|
|
$this->authenticate();
|
|
$response = $this->client->post("vcenter/vm-template/library-items/{$templateConfig['remote_id']}/check-outs", [
|
|
'headers' => $this->authHeaders(),
|
|
'json' => $deployConfig,
|
|
]);
|
|
return json_decode($response->getBody()->getContents(), true) ?? [];
|
|
}
|
|
}
|