update fix deadcode, security
This commit is contained in:
@@ -12,7 +12,6 @@ use App\Models\vCenter as VCenterModel;
|
||||
use App\Models\VirtualMachines;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* VmRebuildSwapService
|
||||
@@ -121,40 +120,4 @@ final class VmRebuildSwapService
|
||||
'new_vm_id' => $ctx->newVmId,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the old VM from vCenter. Best-effort: the DB is already swapped,
|
||||
* so a leftover ghost VM is recoverable manually and must not throw past
|
||||
* the queue.
|
||||
*/
|
||||
public function deleteOldVm(RebuildContext $ctx): void
|
||||
{
|
||||
if ($ctx->oldVmId === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$vcenter = VCenterModel::find($ctx->vcenterId);
|
||||
if (!$vcenter || !$this->vCenter->loginServer($vcenter)) {
|
||||
Log::error('VmRebuildSwapService: cannot login to delete old VM', [
|
||||
'old_vm_id' => $ctx->oldVmId,
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Force power-off before delete to avoid "VM is running" rejection.
|
||||
$this->vCenter->PowerOffVirtualMachine($ctx->oldVmId);
|
||||
$res = $this->vCenter->DeleteVirtualMachine($ctx->oldVmId);
|
||||
|
||||
Log::info('VmRebuildSwapService: old VM deleted', [
|
||||
'old_vm_id' => $ctx->oldVmId,
|
||||
'result' => $res,
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
Log::error('VmRebuildSwapService: deleteOldVm threw', [
|
||||
'old_vm_id' => $ctx->oldVmId,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\VirtualMachine;
|
||||
|
||||
use App\Http\Services\vCenter;
|
||||
use App\Support\TlsHttpClientFactory;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class VmPowerService
|
||||
{
|
||||
public function start(vCenter $vCenter, string $vmId): array|string
|
||||
{
|
||||
try {
|
||||
if (!$vCenter->checkApiExits()) {
|
||||
return 'Login_Again';
|
||||
}
|
||||
|
||||
$response = TlsHttpClientFactory::vCenter()
|
||||
->withHeaders(['vmware-api-session-id' => $vCenter->token])
|
||||
->post("https://{$vCenter->apiUri}/rest/vcenter/vm/{$vmId}/power/start");
|
||||
|
||||
if ($response->status() === 200) {
|
||||
return ['error' => false, 'data' => 'success'];
|
||||
}
|
||||
|
||||
return ['error' => true, 'data' => $response->body()];
|
||||
} catch (Throwable $e) {
|
||||
Log::error('VmPowerService: start VM failed: ' . $e->getMessage());
|
||||
return ['error' => true, 'data' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,9 +79,4 @@ class VmProviderSettingsService
|
||||
{
|
||||
return (string) $this->get('default_resource_pool', self::DEFAULTS['default_resource_pool']);
|
||||
}
|
||||
|
||||
public function defaultDatastore(): string
|
||||
{
|
||||
return (string) $this->get('default_datastore', self::DEFAULTS['default_datastore']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user