update rm deadcode

This commit is contained in:
baodev0
2026-09-27 11:42:40 +07:00
parent 82a71244ca
commit 3af59bd9c5
129 changed files with 5194 additions and 2698 deletions
-179
View File
@@ -2,7 +2,6 @@
namespace App\Http\Services;
use Illuminate\Support\Facades\Log;
use App\Http\Services\vCenter\Client;
use App\Http\Services\vCenter\PowerManagementService;
use App\Http\Services\vCenter\VirtualMachineService;
@@ -12,8 +11,6 @@ use App\Http\Services\vCenter\TemplateService;
use App\Http\Services\vCenter\VMConfigurationService;
use App\Http\Services\vCenter\PowerShellService;
use App\Http\Services\vCenter\Concerns\ManagesSsh;
use Exception;
use phpseclib3\Net\SSH2;
class vCenter
{
@@ -26,7 +23,6 @@ class vCenter
private int $diskCapacity = 1073741824;
private int $memory = 1024;
private ?string $os = null;
private SSH2|null $process = null;
private Client $client;
private PowerManagementService $power;
@@ -339,179 +335,4 @@ class vCenter
{
return $this->shell->getWindowsRdpPort($vmwareId, $guestUser, $guestPassword);
}
// ─── Legacy VPS Direct SSH ────────────────────────────────────────────────
// These methods use direct SSH to guest VM, not via jumphost.
/** Look up host/user/password for a legacy target from infra_management, decrypting the stored password. */
private function resolveLegacyInfra(string $type): ?array
{
$infra = \App\Models\InfraManagement::where('type', $type)
->where('status', true)
->first();
if (!$infra) {
Log::error("Legacy VPS lookup failed: no active infra_management row for type={$type}.");
return null;
}
return [
'ip' => $infra->host,
'user' => $infra->username,
'password' => \App\Http\Facade\SignRSA::DecryptFunc($infra->password),
];
}
protected function loginVPS(array|null $data = []): ?SSH2
{
try {
$ip = $data['ip'] ?? null;
$user = $data['user'] ?? null;
$password = $data['password'] ?? null;
if ($ip === null || $user === null || $password === null) {
$infra = $this->resolveLegacyInfra('legacy_vps');
if (!$infra) {
throw new Exception('No legacy VPS credentials available (data not provided and no active infra_management row).');
}
$ip ??= $infra['ip'];
$user ??= $infra['user'];
$password ??= $infra['password'];
}
$this->process = new SSH2($ip, 22, 10);
if (!$this->process->login($user, $password)) {
throw new Exception("Cannot login to VPS with IP: $ip, User: $user");
}
return $this->process;
} catch (Exception $exception) {
Log::error('Login VPS failed: ' . $exception->getMessage());
$this->process = null;
return null;
}
}
public function change_password_linux(array $data = ['new_password' => '']): bool
{
try {
if (empty($data['new_password'])) {
throw new Exception('New password cannot be empty.');
}
if (!isset($data['ip'], $data['user'], $data['password'])) {
$infra = $this->resolveLegacyInfra('jumphost');
if (!$infra) {
throw new Exception('No jumphost credentials available in infra_management.');
}
$data += $infra;
}
$this->loginVPS($data);
if (!$this->process) {
throw new Exception('Cannot login to VPS.');
}
$username = $data['user'] ?? 'root';
$newPassword = $data['new_password'];
$this->process->exec('echo ' . escapeshellarg("$username:$newPassword") . ' | chpasswd');
return true;
} catch (Exception $exception) {
Log::error('Failed to change password: ' . $exception->getMessage());
return false;
}
}
public function change_password_windows(array $data = ['new_password' => '']): bool
{
try {
if (empty($data['new_password'])) {
throw new Exception('New password cannot be empty.');
}
if (!isset($data['ip'], $data['user'], $data['password'])) {
$infra = $this->resolveLegacyInfra('legacy_vps');
if (!$infra) {
throw new Exception('No legacy VPS credentials available in infra_management.');
}
$data += $infra;
}
$this->loginVPS($data);
if (!$this->process) {
throw new Exception('Cannot login to VPS.');
}
$username = $data['user'] ?? 'administrator';
$newPassword = $data['new_password'];
$response = $this->process->exec("net user \"$username\" \"$newPassword\"");
if (stripos($response, 'successfully') === false) {
throw new Exception("Failed to change password. Response: $response");
}
return true;
} catch (Exception $exception) {
Log::error('Failed to change password: ' . $exception->getMessage());
return false;
}
}
public function extend_disk_centos_7(): bool
{
try {
$this->loginVPS();
if (!$this->process) {
throw new Exception('Cannot login to VPS');
}
$checkDisk = $this->process->exec("lsblk | grep -w 'sda2'");
if (empty(trim($checkDisk))) {
throw new Exception('Disk /dev/sda2 not found');
}
$this->process->exec('sudo tee /sys/class/block/sda/device/rescan');
$this->process->exec('sudo pvcreate /dev/sda2');
$this->process->exec('sudo vgextend /dev/centos /dev/sda2');
$this->process->exec('sudo lvextend /dev/centos/root /dev/sda2');
$this->process->exec('sudo xfs_growfs /dev/centos/root');
return true;
} catch (Exception $exception) {
Log::error('Failed to extend disk: ' . $exception->getMessage());
return false;
}
}
public function extend_disk_centos_8(): bool
{
try {
$this->loginVPS();
if (!$this->process) {
throw new Exception('Cannot login to VPS');
}
$checkDisk = $this->process->exec("lsblk | grep -w 'sda3'");
if (empty(trim($checkDisk))) {
throw new Exception('Disk /dev/sda3 not found');
}
$this->process->exec('sudo pvcreate /dev/sda3');
$this->process->exec('sudo vgextend /dev/cl /dev/sda3');
$this->process->exec('sudo lvextend /dev/cl/root /dev/sd3');
$this->process->exec('sudo xfs_growfs /dev/cl/root');
return true;
} catch (Exception $exception) {
Log::error('Failed to extend disk: ' . $exception->getMessage());
return false;
}
}
public function extend_disk_ubuntu(): bool
{
try {
$this->loginVPS();
if (!$this->process) {
throw new Exception('Cannot login to VPS');
}
$checkDisk = $this->process->exec("lsblk | grep -w 'sda3'");
if (empty(trim($checkDisk))) {
throw new Exception('Disk /dev/sda3 not found');
}
$this->process->exec('sudo tee /sys/class/block/sda/device/rescan');
$this->process->exec('sudo growpart /dev/sda 3');
$this->process->exec('sudo pvresize /dev/sda3');
$this->process->exec('sudo lvresize -l+100%FREE --resizefs /dev/mapper/ubuntu--vg-ubuntu--lv');
return true;
} catch (Exception $exception) {
Log::error('Failed to extend disk: ' . $exception->getMessage());
return false;
}
}
}