2019-09-22 22:30:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2020-10-31 18:14:28 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2019-09-22 22:30:53 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2022-05-29 20:19:04 +00:00
|
|
|
use Pterodactyl\Facades\Activity;
|
2022-05-29 22:20:54 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2022-06-26 20:21:07 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2019-09-22 22:30:53 +00:00
|
|
|
use Pterodactyl\Services\Eggs\EggConfigurationService;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
2020-10-31 18:14:28 +00:00
|
|
|
use Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection;
|
2019-12-22 21:28:51 +00:00
|
|
|
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
|
2019-09-22 22:30:53 +00:00
|
|
|
|
2019-12-22 21:28:51 +00:00
|
|
|
class ServerDetailsController extends Controller
|
2019-09-22 22:30:53 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* ServerConfigurationController constructor.
|
|
|
|
*/
|
2019-12-22 21:28:51 +00:00
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
protected ConnectionInterface $connection,
|
|
|
|
private ServerRepository $repository,
|
|
|
|
private ServerConfigurationStructureService $configurationStructureService,
|
|
|
|
private EggConfigurationService $eggConfigurationService
|
2019-12-22 21:28:51 +00:00
|
|
|
) {
|
2019-09-22 22:30:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-22 21:28:51 +00:00
|
|
|
* Returns details about the server that allows Wings to self-recover and ensure
|
|
|
|
* that the state of the server matches the Panel at all times.
|
|
|
|
*
|
2019-09-22 22:30:53 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __invoke(Request $request, string $uuid): JsonResponse
|
2019-09-22 22:30:53 +00:00
|
|
|
{
|
|
|
|
$server = $this->repository->getByUuid($uuid);
|
|
|
|
|
2020-10-31 18:14:28 +00:00
|
|
|
return new JsonResponse([
|
2019-12-22 21:28:51 +00:00
|
|
|
'settings' => $this->configurationStructureService->handle($server),
|
|
|
|
'process_configuration' => $this->eggConfigurationService->handle($server),
|
|
|
|
]);
|
2019-09-22 22:30:53 +00:00
|
|
|
}
|
2020-04-10 19:04:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists all servers with their configurations that are assigned to the requesting node.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function list(Request $request): ServerConfigurationCollection
|
2020-04-10 19:04:11 +00:00
|
|
|
{
|
2020-10-31 18:14:28 +00:00
|
|
|
/** @var \Pterodactyl\Models\Node $node */
|
2020-04-10 23:54:50 +00:00
|
|
|
$node = $request->attributes->get('node');
|
2020-04-10 19:04:11 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
// Avoid run-away N+1 SQL queries by preloading the relationships that are used
|
2020-10-31 18:14:28 +00:00
|
|
|
// within each of the services called below.
|
2020-10-31 18:28:31 +00:00
|
|
|
$servers = Server::query()->with('allocations', 'egg', 'mounts', 'variables', 'location')
|
2020-10-31 18:14:28 +00:00
|
|
|
->where('node_id', $node->id)
|
2020-11-01 22:27:14 +00:00
|
|
|
// If you don't cast this to a string you'll end up with a stringified per_page returned in
|
|
|
|
// the metadata, and then Wings will panic crash as a result.
|
2021-01-23 20:09:16 +00:00
|
|
|
->paginate((int) $request->input('per_page', 50));
|
2020-04-10 19:04:11 +00:00
|
|
|
|
2020-10-31 18:14:28 +00:00
|
|
|
return new ServerConfigurationCollection($servers);
|
2020-04-10 19:04:11 +00:00
|
|
|
}
|
2021-02-24 05:20:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the state of all servers on the node to be normal. This is triggered
|
|
|
|
* when Wings restarts and is useful for ensuring that any servers on the node
|
|
|
|
* do not get incorrectly stuck in installing/restoring from backup states since
|
|
|
|
* a Wings reboot would completely stop those processes.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function resetState(Request $request): JsonResponse
|
2021-02-24 05:20:02 +00:00
|
|
|
{
|
|
|
|
$node = $request->attributes->get('node');
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
// Get all the servers that are currently marked as restoring from a backup
|
2021-02-24 05:20:02 +00:00
|
|
|
// on this node that do not have a failed backup tracked in the audit logs table
|
|
|
|
// as well.
|
|
|
|
//
|
|
|
|
// For each of those servers we'll track a new audit log entry to mark them as
|
|
|
|
// failed and then update them all to be in a valid state.
|
|
|
|
$servers = Server::query()
|
2022-05-29 22:20:54 +00:00
|
|
|
->with([
|
|
|
|
'activity' => fn ($builder) => $builder
|
|
|
|
->where('activity_logs.event', 'server:backup.restore-started')
|
|
|
|
->latest('timestamp'),
|
|
|
|
])
|
|
|
|
->where('node_id', $node->id)
|
|
|
|
->where('status', Server::STATUS_RESTORING_BACKUP)
|
2021-02-24 05:20:02 +00:00
|
|
|
->get();
|
|
|
|
|
2022-05-29 22:20:54 +00:00
|
|
|
$this->connection->transaction(function () use ($node, $servers) {
|
|
|
|
/** @var \Pterodactyl\Models\Server $server */
|
|
|
|
foreach ($servers as $server) {
|
|
|
|
/** @var \Pterodactyl\Models\ActivityLog|null $activity */
|
|
|
|
$activity = $server->activity->first();
|
|
|
|
if (!is_null($activity)) {
|
|
|
|
if ($subject = $activity->subjects->where('subject_type', 'backup')->first()) {
|
|
|
|
// Just create a new audit entry for this event and update the server state
|
|
|
|
// so that power actions, file management, and backups can resume as normal.
|
|
|
|
Activity::event('server:backup.restore-failed')
|
|
|
|
->subject($server, $subject->subject)
|
|
|
|
->property('name', $subject->subject->name)
|
|
|
|
->log();
|
|
|
|
}
|
|
|
|
}
|
2022-05-29 20:19:04 +00:00
|
|
|
}
|
2021-02-24 05:20:02 +00:00
|
|
|
|
2022-05-29 22:20:54 +00:00
|
|
|
// Update any server marked as installing or restoring as being in a normal state
|
|
|
|
// at this point in the process.
|
|
|
|
Server::query()->where('node_id', $node->id)
|
|
|
|
->whereIn('status', [Server::STATUS_INSTALLING, Server::STATUS_RESTORING_BACKUP])
|
|
|
|
->update(['status' => null]);
|
|
|
|
});
|
2021-02-24 05:20:02 +00:00
|
|
|
|
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
|
|
|
}
|
2019-09-22 22:30:53 +00:00
|
|
|
}
|