95 lines
3.3 KiB
PHP
95 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Services\vCenter;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class DeleteVMService
|
|
{
|
|
protected Client $client;
|
|
protected PowerShellService $powerShellService;
|
|
|
|
public function __construct(Client $client, PowerShellService $powerShellService)
|
|
{
|
|
$this->client = $client;
|
|
$this->powerShellService = $powerShellService;
|
|
}
|
|
|
|
protected function resolveVCenterFromProduct($product): ?\App\Models\vCenter
|
|
{
|
|
if ($product->vCenter) {
|
|
return $product->vCenter;
|
|
}
|
|
|
|
if ($product->ipaddress?->host?->datacenter?->vCenter) {
|
|
return $product->ipaddress->host->datacenter->vCenter;
|
|
}
|
|
|
|
$service = $product->services()->first();
|
|
if ($service) {
|
|
$service->load('vmware.vCenter');
|
|
if ($service->vmware?->vCenter) {
|
|
return $service->vmware->vCenter;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function deleteVM($product, string $confirmName): array
|
|
{
|
|
$vmName = (string) $product->name;
|
|
|
|
// ponytail: confirm guard — caller must pass exact vm name to proceed
|
|
if ($confirmName !== $vmName) {
|
|
return ['error' => true, 'message' => "Tên server xác nhận không khớp. Vui lòng nhập đúng: $vmName"];
|
|
}
|
|
|
|
$vcenter = $this->resolveVCenterFromProduct($product);
|
|
if (!$vcenter) {
|
|
return ['error' => true, 'message' => 'Không tìm thấy thông tin vCenter cho VM'];
|
|
}
|
|
|
|
$loginResult = $this->client->loginServer($vcenter);
|
|
if (!$loginResult) {
|
|
return ['error' => true, 'message' => 'Login_Again'];
|
|
}
|
|
|
|
$check = $this->client->checkApiExits();
|
|
if (!$check) {
|
|
return ['error' => true, 'message' => 'Login_Again'];
|
|
}
|
|
|
|
$host = $this->client->apiUri;
|
|
$user = $this->client->getUsername();
|
|
$password = $this->client->getPassword();
|
|
|
|
try {
|
|
Log::channel('Custom')->info("===================================================================");
|
|
Log::channel('Custom')->info("Delete VM - $vmName");
|
|
|
|
$command = "& 'C:\CLISave\DeleteVM\deleteVM.ps1' -VmName '$vmName' -VCenterHost '$host' -VCenterUser '$user' -VCenterPassword '$password'";
|
|
Log::channel('Custom')->info("Executing deleteVM command for: $vmName");
|
|
|
|
$cmdResult = $this->powerShellService->executePowerCLI($command);
|
|
Log::channel('Custom')->info("Command Result: " . $cmdResult);
|
|
|
|
if (strpos($cmdResult, 'Login_Fail') !== false) {
|
|
return ['error' => true, 'message' => 'SSH Login to PowerShell server failed'];
|
|
}
|
|
|
|
if (strpos($cmdResult, 'SUCCESS') === false) {
|
|
return ['error' => true, 'message' => 'PowerShell command failed: ' . $cmdResult];
|
|
}
|
|
|
|
Log::channel('Custom')->info("After run Command |" . now());
|
|
Log::channel('Custom')->info("===================================================================");
|
|
|
|
return ['error' => false];
|
|
} catch (\Exception $error) {
|
|
Log::channel('Custom')->error("Delete VM error: " . $error->getMessage());
|
|
return ['error' => true, 'message' => $error->getMessage()];
|
|
}
|
|
}
|
|
}
|