Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -343,12 +343,41 @@ class vCenter
|
||||
// ─── 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 {
|
||||
$user = $data['user'] ?? 'root';
|
||||
$ip = $data['ip'] ?? '103.42.58.37';
|
||||
$password = $data['password'] ?? 'TGS@1232';
|
||||
$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)) {
|
||||
@@ -362,13 +391,19 @@ class vCenter
|
||||
}
|
||||
}
|
||||
|
||||
public function change_password_linux(
|
||||
array $data = ['ip' => '10.10.10.173', 'user' => 'admin', 'password' => 'TGS@123', 'new_password' => ''],
|
||||
): bool {
|
||||
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.');
|
||||
@@ -383,18 +418,19 @@ class vCenter
|
||||
}
|
||||
}
|
||||
|
||||
public function change_password_windows(
|
||||
array $data = [
|
||||
'ip' => '103.42.58.37',
|
||||
'user' => 'administrator',
|
||||
'password' => 'TGS@123',
|
||||
'new_password' => '',
|
||||
],
|
||||
): bool {
|
||||
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.');
|
||||
|
||||
Reference in New Issue
Block a user